mirror of
https://github.com/NodeBB/NodeBB.git
synced 2026-02-02 04:40:01 +01:00
moved posts and recent to new files
This commit is contained in:
@@ -1,3 +1,5 @@
|
||||
'use strict';
|
||||
|
||||
var path = require('path'),
|
||||
async = require('async'),
|
||||
sm = require('sitemap'),
|
||||
@@ -28,6 +30,10 @@ var path = require('path'),
|
||||
function(next) {
|
||||
var categoryUrls = [];
|
||||
categories.getAllCategories(0, function(err, data) {
|
||||
if (err) {
|
||||
return next(err);
|
||||
}
|
||||
|
||||
data.categories.forEach(function(category) {
|
||||
if (!category.disabled) {
|
||||
categoryUrls.push({
|
||||
@@ -43,16 +49,17 @@ var path = require('path'),
|
||||
},
|
||||
function(next) {
|
||||
var topicUrls = [];
|
||||
topics.getAllTopics(0, -1, function(err, topics) {
|
||||
topics.forEach(function(topic) {
|
||||
topics.getTopicsFromSet(0, 'topics:recent', 0, -1, function(err, data) {
|
||||
if (err) {
|
||||
return next(err);
|
||||
}
|
||||
|
||||
if (parseInt(topic.deleted, 10) !== 1) {
|
||||
topicUrls.push({
|
||||
url: path.join('/topic', topic.slug),
|
||||
changefreq: 'daily',
|
||||
priority: '0.6'
|
||||
});
|
||||
}
|
||||
data.topics.forEach(function(topic) {
|
||||
topicUrls.push({
|
||||
url: path.join('/topic', topic.slug),
|
||||
changefreq: 'daily',
|
||||
priority: '0.6'
|
||||
});
|
||||
});
|
||||
|
||||
next(null, topicUrls);
|
||||
@@ -63,13 +70,13 @@ var path = require('path'),
|
||||
returnUrls = returnUrls.concat(data[0]).concat(data[1]);
|
||||
}
|
||||
|
||||
callback(null, returnUrls);
|
||||
callback(err, returnUrls);
|
||||
});
|
||||
},
|
||||
render: function(callback) {
|
||||
async.parallel([sitemap.getStaticUrls, sitemap.getDynamicUrls], function(err, urls) {
|
||||
var urls = urls[0].concat(urls[1]),
|
||||
map = sm.createSitemap({
|
||||
urls = urls[0].concat(urls[1]);
|
||||
var map = sm.createSitemap({
|
||||
hostname: nconf.get('url'),
|
||||
cacheTime: 600000,
|
||||
urls: urls
|
||||
@@ -79,6 +86,6 @@ var path = require('path'),
|
||||
});
|
||||
});
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
module.exports = sitemap;
|
||||
173
src/topics.js
173
src/topics.js
@@ -2,11 +2,7 @@
|
||||
|
||||
var async = require('async'),
|
||||
gravatar = require('gravatar'),
|
||||
path = require('path'),
|
||||
nconf = require('nconf'),
|
||||
validator = require('validator'),
|
||||
S = require('string'),
|
||||
winston = require('winston'),
|
||||
|
||||
db = require('./database'),
|
||||
posts = require('./posts'),
|
||||
@@ -15,20 +11,15 @@ var async = require('async'),
|
||||
user = require('./user'),
|
||||
categories = require('./categories'),
|
||||
categoryTools = require('./categoryTools'),
|
||||
posts = require('./posts'),
|
||||
threadTools = require('./threadTools'),
|
||||
postTools = require('./postTools'),
|
||||
notifications = require('./notifications'),
|
||||
favourites = require('./favourites'),
|
||||
meta = require('./meta'),
|
||||
Plugins = require('./plugins'),
|
||||
emitter = require('./emitter');
|
||||
threadTools = require('./threadTools');
|
||||
|
||||
(function(Topics) {
|
||||
|
||||
require('./topics/create')(Topics);
|
||||
require('./topics/unread')(Topics);
|
||||
require('./topics/recent')(Topics);
|
||||
require('./topics/fork')(Topics);
|
||||
require('./topics/posts')(Topics);
|
||||
|
||||
|
||||
Topics.getTopicData = function(tid, callback) {
|
||||
@@ -90,62 +81,6 @@ var async = require('async'),
|
||||
});
|
||||
};
|
||||
|
||||
Topics.getTopicPosts = function(tid, start, end, uid, reverse, callback) {
|
||||
posts.getPostsByTid(tid, start, end, reverse, function(err, postData) {
|
||||
if(err) {
|
||||
return callback(err);
|
||||
}
|
||||
|
||||
if (Array.isArray(postData) && !postData.length) {
|
||||
return callback(null, []);
|
||||
}
|
||||
|
||||
for(var i=0; i<postData.length; ++i) {
|
||||
postData[i].index = start + i;
|
||||
}
|
||||
|
||||
var pids = postData.map(function(post) {
|
||||
return post.pid;
|
||||
});
|
||||
|
||||
async.parallel({
|
||||
favourites : function(next) {
|
||||
favourites.getFavouritesByPostIDs(pids, uid, next);
|
||||
},
|
||||
voteData : function(next) {
|
||||
favourites.getVoteStatusByPostIDs(pids, uid, next);
|
||||
},
|
||||
userData : function(next) {
|
||||
async.each(postData, posts.addUserInfoToPost, next);
|
||||
},
|
||||
privileges : function(next) {
|
||||
async.map(pids, function (pid, next) {
|
||||
postTools.privileges(pid, uid, next);
|
||||
}, next);
|
||||
}
|
||||
}, function(err, results) {
|
||||
if(err) {
|
||||
return callback(err);
|
||||
}
|
||||
|
||||
for (var i = 0; i < postData.length; ++i) {
|
||||
postData[i].favourited = results.favourites[i];
|
||||
postData[i].upvoted = results.voteData[i].upvoted;
|
||||
postData[i].downvoted = results.voteData[i].downvoted;
|
||||
postData[i].votes = postData[i].votes || 0;
|
||||
postData[i].display_moderator_tools = parseInt(uid, 10) !== 0 && results.privileges[i].editable;
|
||||
postData[i].display_move_tools = results.privileges[i].move;
|
||||
|
||||
if(parseInt(postData[i].deleted, 10) === 1 && !results.privileges[i].view_deleted) {
|
||||
postData[i].content = 'This post is deleted!';
|
||||
}
|
||||
}
|
||||
|
||||
callback(null, postData);
|
||||
});
|
||||
});
|
||||
};
|
||||
|
||||
Topics.getPageCount = function(tid, uid, callback) {
|
||||
db.sortedSetCard('tid:' + tid + ':posts', function(err, postCount) {
|
||||
if(err) {
|
||||
@@ -194,7 +129,7 @@ var async = require('async'),
|
||||
});
|
||||
};
|
||||
|
||||
function getTopics(set, uid, tids, callback) {
|
||||
Topics.getTopics = function(set, uid, tids, callback) {
|
||||
var returnTopics = {
|
||||
topics: [],
|
||||
nextStart: 0
|
||||
@@ -229,32 +164,6 @@ var async = require('async'),
|
||||
});
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
Topics.getLatestTids = function(start, end, term, callback) {
|
||||
var terms = {
|
||||
day: 86400000,
|
||||
week: 604800000,
|
||||
month: 2592000000
|
||||
};
|
||||
|
||||
var since = terms.day;
|
||||
if(terms[term]) {
|
||||
since = terms[term];
|
||||
}
|
||||
|
||||
var count = parseInt(end, 10) === -1 ? end : end - start + 1;
|
||||
|
||||
db.getSortedSetRevRangeByScore(['topics:recent', '+inf', Date.now() - since, 'LIMIT', start, count], callback);
|
||||
};
|
||||
|
||||
Topics.getLatestTopics = function(uid, start, end, term, callback) {
|
||||
Topics.getLatestTids(start, end, term, function(err, tids) {
|
||||
if(err) {
|
||||
return callback(err);
|
||||
}
|
||||
getTopics('topics:recent', uid, tids, callback);
|
||||
});
|
||||
};
|
||||
|
||||
Topics.getTopicsFromSet = function(uid, set, start, end, callback) {
|
||||
@@ -263,7 +172,7 @@ var async = require('async'),
|
||||
return callback(err);
|
||||
}
|
||||
|
||||
getTopics(set, uid, tids, callback);
|
||||
Topics.getTopics(set, uid, tids, callback);
|
||||
});
|
||||
};
|
||||
|
||||
@@ -385,11 +294,10 @@ var async = require('async'),
|
||||
Topics.getPageCount(tid, uid, next);
|
||||
},
|
||||
threadTools : function(next) {
|
||||
Plugins.fireHook('filter:topic.thread_tools', [], next);
|
||||
plugins.fireHook('filter:topic.thread_tools', [], next);
|
||||
}
|
||||
}, function(err, results) {
|
||||
if (err) {
|
||||
winston.error('[Topics.getTopicWithPosts] Could not retrieve topic data: ', err.message);
|
||||
return callback(err);
|
||||
}
|
||||
|
||||
@@ -406,34 +314,6 @@ var async = require('async'),
|
||||
});
|
||||
};
|
||||
|
||||
Topics.getAllTopics = function(start, end, callback) {
|
||||
db.getSortedSetRevRange('topics:tid', start, end, function(err, tids) {
|
||||
if(err) {
|
||||
return callback(err);
|
||||
}
|
||||
|
||||
async.map(tids, function(tid, next) {
|
||||
Topics.getTopicDataWithUser(tid, next);
|
||||
}, callback);
|
||||
});
|
||||
};
|
||||
|
||||
Topics.getTitleByPid = function(pid, callback) {
|
||||
Topics.getTopicFieldByPid('title', pid, callback);
|
||||
};
|
||||
|
||||
Topics.getTopicFieldByPid = function(field, pid, callback) {
|
||||
posts.getPostField(pid, 'tid', function(err, tid) {
|
||||
Topics.getTopicField(tid, field, callback);
|
||||
});
|
||||
};
|
||||
|
||||
Topics.getTopicDataByPid = function(pid, callback) {
|
||||
posts.getPostField(pid, 'tid', function(err, tid) {
|
||||
Topics.getTopicData(tid, callback);
|
||||
});
|
||||
};
|
||||
|
||||
Topics.getTeasers = function(tids, callback) {
|
||||
|
||||
if(!Array.isArray(tids)) {
|
||||
@@ -489,27 +369,6 @@ var async = require('async'),
|
||||
db.setObjectField('topic:' + tid, field, value, callback);
|
||||
};
|
||||
|
||||
Topics.increasePostCount = function(tid, callback) {
|
||||
incrementFieldAndUpdateSortedSet(tid, 'postcount', 1, 'topics:posts', callback);
|
||||
};
|
||||
|
||||
Topics.decreasePostCount = function(tid, callback) {
|
||||
incrementFieldAndUpdateSortedSet(tid, 'postcount', -1, 'topics:posts', callback);
|
||||
};
|
||||
|
||||
Topics.increaseViewCount = function(tid, callback) {
|
||||
incrementFieldAndUpdateSortedSet(tid, 'viewcount', 1, 'topics:views', callback);
|
||||
};
|
||||
|
||||
function incrementFieldAndUpdateSortedSet(tid, field, by, set, callback) {
|
||||
db.incrObjectFieldBy('topic:' + tid, field, by, function(err, value) {
|
||||
if(err) {
|
||||
return callback(err);
|
||||
}
|
||||
db.sortedSetAdd(set, value, tid, callback);
|
||||
});
|
||||
}
|
||||
|
||||
Topics.isLocked = function(tid, callback) {
|
||||
Topics.getTopicField(tid, 'locked', function(err, locked) {
|
||||
if(err) {
|
||||
@@ -524,26 +383,6 @@ var async = require('async'),
|
||||
Topics.setTopicField(tid, 'lastposttime', timestamp);
|
||||
};
|
||||
|
||||
Topics.onNewPostMade = function(postData) {
|
||||
Topics.increasePostCount(postData.tid);
|
||||
Topics.updateTimestamp(postData.tid, postData.timestamp);
|
||||
Topics.addPostToTopic(postData.tid, postData.pid, postData.timestamp);
|
||||
};
|
||||
|
||||
emitter.on('event:newpost', Topics.onNewPostMade);
|
||||
|
||||
Topics.addPostToTopic = function(tid, pid, timestamp, callback) {
|
||||
db.sortedSetAdd('tid:' + tid + ':posts', timestamp, pid, callback);
|
||||
};
|
||||
|
||||
Topics.removePostFromTopic = function(tid, pid, callback) {
|
||||
db.sortedSetRemove('tid:' + tid + ':posts', pid, callback);
|
||||
};
|
||||
|
||||
Topics.getPids = function(tid, callback) {
|
||||
db.getSortedSetRange('tid:' + tid + ':posts', 0, -1, callback);
|
||||
};
|
||||
|
||||
Topics.getUids = function(tid, callback) {
|
||||
var uids = {};
|
||||
Topics.getPids(tid, function(err, pids) {
|
||||
|
||||
131
src/topics/posts.js
Normal file
131
src/topics/posts.js
Normal file
@@ -0,0 +1,131 @@
|
||||
|
||||
|
||||
'use strict';
|
||||
|
||||
var async = require('async'),
|
||||
|
||||
db = require('./../database'),
|
||||
emitter = require('./../emitter'),
|
||||
favourites = require('./../favourites'),
|
||||
posts = require('./../posts'),
|
||||
postTools = require('./../postTools');
|
||||
|
||||
|
||||
|
||||
module.exports = function(Topics) {
|
||||
|
||||
Topics.onNewPostMade = function(postData) {
|
||||
Topics.increasePostCount(postData.tid);
|
||||
Topics.updateTimestamp(postData.tid, postData.timestamp);
|
||||
Topics.addPostToTopic(postData.tid, postData.pid, postData.timestamp);
|
||||
};
|
||||
|
||||
emitter.on('event:newpost', Topics.onNewPostMade);
|
||||
|
||||
Topics.getTopicPosts = function(tid, start, end, uid, reverse, callback) {
|
||||
posts.getPostsByTid(tid, start, end, reverse, function(err, postData) {
|
||||
if(err) {
|
||||
return callback(err);
|
||||
}
|
||||
|
||||
if (Array.isArray(postData) && !postData.length) {
|
||||
return callback(null, []);
|
||||
}
|
||||
|
||||
for(var i=0; i<postData.length; ++i) {
|
||||
postData[i].index = start + i;
|
||||
}
|
||||
|
||||
var pids = postData.map(function(post) {
|
||||
return post.pid;
|
||||
});
|
||||
|
||||
async.parallel({
|
||||
favourites : function(next) {
|
||||
favourites.getFavouritesByPostIDs(pids, uid, next);
|
||||
},
|
||||
voteData : function(next) {
|
||||
favourites.getVoteStatusByPostIDs(pids, uid, next);
|
||||
},
|
||||
userData : function(next) {
|
||||
async.each(postData, posts.addUserInfoToPost, next);
|
||||
},
|
||||
privileges : function(next) {
|
||||
async.map(pids, function (pid, next) {
|
||||
postTools.privileges(pid, uid, next);
|
||||
}, next);
|
||||
}
|
||||
}, function(err, results) {
|
||||
if(err) {
|
||||
return callback(err);
|
||||
}
|
||||
|
||||
for (var i = 0; i < postData.length; ++i) {
|
||||
postData[i].favourited = results.favourites[i];
|
||||
postData[i].upvoted = results.voteData[i].upvoted;
|
||||
postData[i].downvoted = results.voteData[i].downvoted;
|
||||
postData[i].votes = postData[i].votes || 0;
|
||||
postData[i].display_moderator_tools = parseInt(uid, 10) !== 0 && results.privileges[i].editable;
|
||||
postData[i].display_move_tools = results.privileges[i].move;
|
||||
|
||||
if(parseInt(postData[i].deleted, 10) === 1 && !results.privileges[i].view_deleted) {
|
||||
postData[i].content = 'This post is deleted!';
|
||||
}
|
||||
}
|
||||
|
||||
callback(null, postData);
|
||||
});
|
||||
});
|
||||
};
|
||||
|
||||
Topics.addPostToTopic = function(tid, pid, timestamp, callback) {
|
||||
db.sortedSetAdd('tid:' + tid + ':posts', timestamp, pid, callback);
|
||||
};
|
||||
|
||||
Topics.removePostFromTopic = function(tid, pid, callback) {
|
||||
db.sortedSetRemove('tid:' + tid + ':posts', pid, callback);
|
||||
};
|
||||
|
||||
Topics.getPids = function(tid, callback) {
|
||||
db.getSortedSetRange('tid:' + tid + ':posts', 0, -1, callback);
|
||||
};
|
||||
|
||||
Topics.increasePostCount = function(tid, callback) {
|
||||
incrementFieldAndUpdateSortedSet(tid, 'postcount', 1, 'topics:posts', callback);
|
||||
};
|
||||
|
||||
Topics.decreasePostCount = function(tid, callback) {
|
||||
incrementFieldAndUpdateSortedSet(tid, 'postcount', -1, 'topics:posts', callback);
|
||||
};
|
||||
|
||||
Topics.increaseViewCount = function(tid, callback) {
|
||||
incrementFieldAndUpdateSortedSet(tid, 'viewcount', 1, 'topics:views', callback);
|
||||
};
|
||||
|
||||
function incrementFieldAndUpdateSortedSet(tid, field, by, set, callback) {
|
||||
db.incrObjectFieldBy('topic:' + tid, field, by, function(err, value) {
|
||||
if(err) {
|
||||
return callback(err);
|
||||
}
|
||||
db.sortedSetAdd(set, value, tid, callback);
|
||||
});
|
||||
}
|
||||
|
||||
Topics.getTitleByPid = function(pid, callback) {
|
||||
Topics.getTopicFieldByPid('title', pid, callback);
|
||||
};
|
||||
|
||||
Topics.getTopicFieldByPid = function(field, pid, callback) {
|
||||
posts.getPostField(pid, 'tid', function(err, tid) {
|
||||
Topics.getTopicField(tid, field, callback);
|
||||
});
|
||||
};
|
||||
|
||||
Topics.getTopicDataByPid = function(pid, callback) {
|
||||
posts.getPostField(pid, 'tid', function(err, tid) {
|
||||
Topics.getTopicData(tid, callback);
|
||||
});
|
||||
};
|
||||
|
||||
|
||||
};
|
||||
36
src/topics/recent.js
Normal file
36
src/topics/recent.js
Normal file
@@ -0,0 +1,36 @@
|
||||
|
||||
|
||||
'use strict';
|
||||
|
||||
var db = require('./../database');
|
||||
|
||||
module.exports = function(Topics) {
|
||||
|
||||
Topics.getLatestTopics = function(uid, start, end, term, callback) {
|
||||
Topics.getLatestTids(start, end, term, function(err, tids) {
|
||||
if(err) {
|
||||
return callback(err);
|
||||
}
|
||||
|
||||
Topics.getTopics('topics:recent', uid, tids, callback);
|
||||
});
|
||||
};
|
||||
|
||||
Topics.getLatestTids = function(start, end, term, callback) {
|
||||
var terms = {
|
||||
day: 86400000,
|
||||
week: 604800000,
|
||||
month: 2592000000
|
||||
};
|
||||
|
||||
var since = terms.day;
|
||||
if(terms[term]) {
|
||||
since = terms[term];
|
||||
}
|
||||
|
||||
var count = parseInt(end, 10) === -1 ? end : end - start + 1;
|
||||
|
||||
db.getSortedSetRevRangeByScore(['topics:recent', '+inf', Date.now() - since, 'LIMIT', start, count], callback);
|
||||
};
|
||||
|
||||
};
|
||||
Reference in New Issue
Block a user