v1.18.x
barisusakli 8 years ago
parent ca6101223a
commit 6f86621e30

@ -88,8 +88,8 @@
"file-too-big": "Maximum allowed file size is %1 kB - please upload a smaller file",
"guest-upload-disabled": "Guest uploading has been disabled",
"already-favourited": "You have already bookmarked this post",
"already-unfavourited": "You have already unbookmarked this post",
"already-bookmarked": "You have already bookmarked this post",
"already-unbookmarked": "You have already unbookmarked this post",
"cant-ban-other-admins": "You can't ban other admins!",
"cant-remove-last-admin": "You are the only administrator. Add another user as an administrator before removing yourself as admin",

@ -41,7 +41,7 @@
"account/posts": "Posts made by %1",
"account/topics": "Topics created by %1",
"account/groups": "%1's Groups",
"account/favourites": "%1's Bookmarked Posts",
"account/bookmarks": "%1's Bookmarked Posts",
"account/settings": "User Settings",
"account/watched": "Topics watched by %1",
"account/upvoted": "Posts upvoted by %1",

@ -104,9 +104,9 @@
"confirm_move": "Move",
"confirm_fork": "Fork",
"favourite": "Bookmark",
"favourites": "Bookmarks",
"favourites.has_no_favourites": "You haven't bookmarked any posts yet.",
"bookmark": "Bookmark",
"bookmarks": "Bookmarks",
"bookmarks.has_no_bookmarks": "You haven't bookmarked any posts yet.",
"loading_more_posts": "Loading More Posts",
"move_topic": "Move Topic",

@ -25,7 +25,7 @@
"profile": "Profile",
"profile_views": "Profile views",
"reputation": "Reputation",
"favourites":"Bookmarks",
"bookmarks":"Bookmarks",
"watched": "Watched",
"followers": "Followers",
"following": "Following",

@ -0,0 +1,17 @@
'use strict';
/* globals define */
define('forum/account/bookmarks', ['forum/account/header', 'forum/account/posts'], function(header, posts) {
var Bookmarks = {};
Bookmarks.init = function() {
header.init();
$('[component="post/content"] img:not(.not-responsive)').addClass('img-responsive');
posts.handleInfiniteScroll('posts.loadMoreBookmarks', 'account/bookmarks');
};
return Bookmarks;
});

@ -1,17 +0,0 @@
'use strict';
/* globals define, app, utils */
define('forum/account/favourites', ['forum/account/header', 'forum/account/posts'], function(header, posts) {
var Favourites = {};
Favourites.init = function() {
header.init();
$('[component="post/content"] img:not(.not-responsive)').addClass('img-responsive');
posts.handleInfiniteScroll('posts.loadMoreFavourites', 'account/favourites');
};
return Favourites;
});

@ -16,7 +16,7 @@ define('forum/topic/events', [
var events = {
'event:user_status_change': onUserStatusChange,
'event:voted': updatePostVotesAndUserReputation,
'event:favourited': updateFavouriteCount,
'event:bookmarked': updateBookmarkCount,
'event:topic_deleted': threadTools.setDeleteState,
'event:topic_restored': threadTools.setDeleteState,
@ -36,8 +36,8 @@ define('forum/topic/events', [
'event:post_deleted': togglePostDeleteState,
'event:post_restored': togglePostDeleteState,
'posts.favourite': togglePostFavourite,
'posts.unfavourite': togglePostFavourite,
'posts.bookmark': togglePostBookmark,
'posts.unbookmark': togglePostBookmark,
'posts.upvote': togglePostVote,
'posts.downvote': togglePostVote,
@ -69,15 +69,15 @@ define('forum/topic/events', [
}
function updatePostVotesAndUserReputation(data) {
var votes = components.get('post/vote-count', data.post.pid),
reputationElements = $('.reputation[data-uid="' + data.post.uid + '"]');
var votes = components.get('post/vote-count', data.post.pid);
var reputationElements = $('.reputation[data-uid="' + data.post.uid + '"]');
votes.html(data.post.votes).attr('data-votes', data.post.votes);
reputationElements.html(data.user.reputation).attr('data-reputation', data.user.reputation);
}
function updateFavouriteCount(data) {
$('[data-pid="' + data.post.pid + '"] .favouriteCount').html(data.post.reputation).attr('data-favourites', data.post.reputation);
function updateBookmarkCount(data) {
$('[data-pid="' + data.post.pid + '"] .bookmarkCount').html(data.post.bookmarks).attr('data-bookmarks', data.post.bookmarks);
}
function onTopicPurged() {
@ -198,17 +198,17 @@ define('forum/topic/events', [
}
}
function togglePostFavourite(data) {
var favBtn = $('[data-pid="' + data.post.pid + '"] [component="post/favourite"]');
function togglePostBookmark(data) {
var el = $('[data-pid="' + data.post.pid + '"] [component="post/bookmark"]');
if (!favBtn.length) {
if (!el.length) {
return;
}
favBtn.attr('data-favourited', data.isFavourited);
el.attr('data-bookmarked', data.isBookmarked);
favBtn.find('[component="post/favourite/on"]').toggleClass('hidden', !data.isFavourited);
favBtn.find('[component="post/favourite/off"]').toggleClass('hidden', data.isFavourited);
el.find('[component="post/bookmark/on"]').toggleClass('hidden', !data.isBookmarked);
el.find('[component="post/bookmark/off"]').toggleClass('hidden', data.isBookmarked);
}
function togglePostVote(data) {

@ -52,7 +52,7 @@ define('forum/topic/postTools', ['share', 'navigator', 'components', 'translator
PostTools.toggle = function(pid, isDeleted) {
var postEl = components.get('post', 'pid', pid);
postEl.find('[component="post/quote"], [component="post/favourite"], [component="post/reply"], [component="post/flag"], [component="user/chat"]')
postEl.find('[component="post/quote"], [component="post/bookmark"], [component="post/reply"], [component="post/flag"], [component="user/chat"]')
.toggleClass('hidden', isDeleted);
postEl.find('[component="post/delete"]').toggleClass('hidden', isDeleted);
@ -149,8 +149,8 @@ define('forum/topic/postTools', ['share', 'navigator', 'components', 'translator
});
});
postContainer.on('click', '[component="post/favourite"]', function() {
favouritePost($(this), getData($(this), 'data-pid'));
postContainer.on('click', '[component="post/bookmark"]', function() {
bookmarkPost($(this), getData($(this), 'data-pid'));
});
postContainer.on('click', '[component="post/upvote"]', function() {
@ -330,8 +330,8 @@ define('forum/topic/postTools', ['share', 'navigator', 'components', 'translator
return selectionText;
}
function favouritePost(button, pid) {
var method = button.attr('data-favourited') === 'false' ? 'posts.favourite' : 'posts.unfavourite';
function bookmarkPost(button, pid) {
var method = button.attr('data-bookmarked') === 'false' ? 'posts.bookmark' : 'posts.unbookmark';
socket.emit(method, {
pid: pid,

@ -28,8 +28,8 @@ define('components', function() {
'post/vote-count': function(pid) {
return components.core.post('pid', pid).find('[component="post/vote-count"]');
},
'post/favourite-count': function(pid) {
return components.core.post('pid', pid).find('[component="post/favourite-count"]');
'post/bookmark-count': function(pid) {
return components.core.post('pid', pid).find('[component="post/bookmark-count"]');
},
'user/postcount': function(uid) {

@ -13,14 +13,14 @@ var accountHelpers = require('./helpers');
var postsController = {};
postsController.getFavourites = function(req, res, next) {
postsController.getBookmarks = function(req, res, next) {
var data = {
template: 'account/favourites',
set: 'favourites',
template: 'account/bookmarks',
set: 'bookmarks',
type: 'posts',
noItemsFoundKey: '[[topic:favourites.has_no_favourites]]',
noItemsFoundKey: '[[topic:bookmarks.has_no_bookmarks]]',
method: posts.getPostSummariesFromSet,
crumb: '[[user:favourites]]'
crumb: '[[user:bookmarks]]'
};
getFromUserSet(data, req, res, next);
};

@ -23,6 +23,8 @@ var plugins = require('./plugins');
require('./posts/recent')(Posts);
require('./posts/flags')(Posts);
require('./posts/tools')(Posts);
require('./posts/votes')(Posts);
require('./posts/bookmarks')(Posts);
Posts.exists = function(pid, callback) {
db.isSortedSetMember('posts:pid', pid, callback);

@ -0,0 +1,107 @@
'use strict';
var async = require('async');
var db = require('../database');
var plugins = require('../plugins');
module.exports = function(Posts) {
Posts.bookmark = function (pid, uid, callback) {
toggleBookmark('bookmark', pid, uid, callback);
};
Posts.unbookmark = function(pid, uid, callback) {
toggleBookmark('unbookmark', pid, uid, callback);
};
function toggleBookmark(type, pid, uid, callback) {
if (!parseInt(uid, 10)) {
return callback(new Error('[[error:not-logged-in]]'));
}
var isBookmarking = type === 'bookmark';
async.parallel({
owner: function(next) {
Posts.getPostField(pid, 'uid', next);
},
postData: function(next) {
Posts.getPostFields(pid, ['pid', 'uid'], next);
},
hasBookmarked: function(next) {
Posts.hasBookmarked(pid, uid, next);
}
}, function(err, results) {
if (err) {
return callback(err);
}
if (isBookmarking && results.hasBookmarked) {
return callback(new Error('[[error:already-bookmarked]]'));
}
if (!isBookmarking && !results.hasBookmarked) {
return callback(new Error('[[error:already-unbookmarked]]'));
}
async.waterfall([
function(next) {
if (isBookmarking) {
db.sortedSetAdd('uid:' + uid + ':bookmarks', Date.now(), pid, next);
} else {
db.sortedSetRemove('uid:' + uid + ':bookmarks', pid, next);
}
},
function(next) {
db[isBookmarking ? 'setAdd' : 'setRemove']('pid:' + pid + ':users_bookmarked', uid, next);
},
function(next) {
db.setCount('pid:' + pid + ':users_bookmarked', next);
},
function(count, next) {
results.postData.bookmarks = count;
Posts.setPostField(pid, 'bookmarks', count, next);
}
], function(err) {
if (err) {
return callback(err);
}
var current = results.hasBookmarked ? 'bookmarked' : 'unbookmarked';
plugins.fireHook('action:post.' + type, {
pid: pid,
uid: uid,
owner: results.owner,
current: current
});
callback(null, {
post: results.postData,
isBookmarked: isBookmarking
});
});
});
}
Posts.hasBookmarked = function(pid, uid, callback) {
if (!parseInt(uid, 10)) {
if (Array.isArray(pid)) {
callback(null, pid.map(function() { return false; }));
} else {
callback(null, false);
}
return;
}
if (Array.isArray(pid)) {
var sets = pid.map(function(pid) {
return 'pid:' + pid + ':users_bookmarked';
});
db.isMemberOfSets(sets, uid, callback);
} else {
db.isSetMember('pid:' + pid + ':users_bookmarked', uid, callback);
}
};
};

@ -38,7 +38,6 @@ module.exports = function(Posts) {
'tid': tid,
'content': content,
'timestamp': timestamp,
'reputation': 0,
'editor': '',
'edited': 0,
'deleted': 0

@ -132,7 +132,7 @@ module.exports = function(Posts) {
deletePostFromCategoryRecentPosts(pid, next);
},
function (next) {
deletePostFromUsersFavourites(pid, next);
deletePostFromUsersBookmarks(pid, next);
},
function (next) {
deletePostFromUsersVotes(pid, next);
@ -219,14 +219,14 @@ module.exports = function(Posts) {
});
}
function deletePostFromUsersFavourites(pid, callback) {
db.getSetMembers('pid:' + pid + ':users_favourited', function(err, uids) {
function deletePostFromUsersBookmarks(pid, callback) {
db.getSetMembers('pid:' + pid + ':users_bookmarked', function(err, uids) {
if (err) {
return callback(err);
}
var sets = uids.map(function(uid) {
return 'uid:' + uid + ':favourites';
return 'uid:' + uid + ':bookmarks';
});
db.sortedSetsRemove(sets, pid, function(err) {
@ -234,7 +234,7 @@ module.exports = function(Posts) {
return callback(err);
}
db.delete('pid:' + pid + ':users_favourited', callback);
db.delete('pid:' + pid + ':users_bookmarked', callback);
});
});
}

@ -1,104 +1,17 @@
"use strict";
'use strict';
var async = require('async');
var db = require('./database');
var posts = require('./posts');
var user = require('./user');
var plugins = require('./plugins');
var meta = require('./meta');
(function (Favourites) {
var meta = require('../meta');
var db = require('../database');
var user = require('../user');
var plugins = require('../plugins');
var votesInProgress = {};
function vote(type, unvote, pid, uid, callback) {
uid = parseInt(uid, 10);
if (uid === 0) {
return callback(new Error('[[error:not-logged-in]]'));
}
posts.getPostFields(pid, ['pid', 'uid', 'tid'], function (err, postData) {
if (err) {
return callback(err);
}
var now = Date.now();
if (type === 'upvote' && !unvote) {
db.sortedSetAdd('uid:' + uid + ':upvote', now, pid);
} else {
db.sortedSetRemove('uid:' + uid + ':upvote', pid);
}
if (type === 'upvote' || unvote) {
db.sortedSetRemove('uid:' + uid + ':downvote', pid);
} else {
db.sortedSetAdd('uid:' + uid + ':downvote', now, pid);
}
user[type === 'upvote' ? 'incrementUserFieldBy' : 'decrementUserFieldBy'](postData.uid, 'reputation', 1, function (err, newreputation) {
if (err) {
return callback(err);
}
if (parseInt(postData.uid, 10)) {
db.sortedSetAdd('users:reputation', newreputation, postData.uid);
}
adjustPostVotes(postData, uid, type, unvote, function(err) {
callback(err, {
user: {
reputation: newreputation
},
post: postData,
upvote: type === 'upvote' && !unvote,
downvote: type === 'downvote' && !unvote
});
});
});
});
}
function adjustPostVotes(postData, uid, type, unvote, callback) {
var notType = (type === 'upvote' ? 'downvote' : 'upvote');
async.series([
function(next) {
if (unvote) {
db.setRemove('pid:' + postData.pid + ':' + type, uid, next);
} else {
db.setAdd('pid:' + postData.pid + ':' + type, uid, next);
}
},
function(next) {
db.setRemove('pid:' + postData.pid + ':' + notType, uid, next);
}
], function(err) {
if (err) {
return callback(err);
}
module.exports = function(Posts) {
async.parallel({
upvotes: function(next) {
db.setCount('pid:' + postData.pid + ':upvote', next);
},
downvotes: function(next) {
db.setCount('pid:' + postData.pid + ':downvote', next);
}
}, function(err, results) {
if (err) {
return callback(err);
}
postData.upvotes = parseInt(results.upvotes, 10);
postData.downvotes = parseInt(results.downvotes, 10);
postData.votes = postData.upvotes - postData.downvotes;
posts.updatePostVoteCount(postData, callback);
});
});
}
var votesInProgress = {};
Favourites.upvote = function(pid, uid, callback) {
Posts.upvote = function(pid, uid, callback) {
if (parseInt(meta.config['reputation:disabled'], 10) === 1) {
return callback(new Error('[[error:reputation-system-disabled]]'));
}
@ -115,7 +28,7 @@ var meta = require('./meta');
});
};
Favourites.downvote = function(pid, uid, callback) {
Posts.downvote = function(pid, uid, callback) {
if (parseInt(meta.config['reputation:disabled'], 10) === 1) {
return callback(new Error('[[error:reputation-system-disabled]]'));
}
@ -136,7 +49,7 @@ var meta = require('./meta');
});
};
Favourites.unvote = function(pid, uid, callback) {
Posts.unvote = function(pid, uid, callback) {
if (voteInProgress(pid, uid)) {
return callback(new Error('[[error:already-voting-for-this-post]]'));
}
@ -149,6 +62,50 @@ var meta = require('./meta');
});
};
Posts.hasVoted = function(pid, uid, callback) {
if (!parseInt(uid, 10)) {
return callback(null, {upvoted: false, downvoted: false});
}
db.isMemberOfSets(['pid:' + pid + ':upvote', 'pid:' + pid + ':downvote'], uid, function(err, hasVoted) {
if (err) {
return callback(err);
}
callback (null, {upvoted: hasVoted[0], downvoted: hasVoted[1]});
});
};
Posts.getVoteStatusByPostIDs = function(pids, uid, callback) {
if (!parseInt(uid, 10)) {
var data = pids.map(function() { return false; });
return callback(null, {upvotes: data, downvotes: data});
}
var upvoteSets = [];
var downvoteSets = [];
for (var i=0; i<pids.length; ++i) {
upvoteSets.push('pid:' + pids[i] + ':upvote');
downvoteSets.push('pid:' + pids[i] + ':downvote');
}
async.parallel({
upvotes: function(next) {
db.isMemberOfSets(upvoteSets, uid, next);
},
downvotes: function(next) {
db.isMemberOfSets(downvoteSets, uid, next);
}
}, callback);
};
Posts.getUpvotedUidsByPids = function(pids, callback) {
var sets = pids.map(function(pid) {
return 'pid:' + pid + ':upvote';
});
db.getSetsMembers(sets, callback);
};
function voteInProgress(pid, uid) {
return Array.isArray(votesInProgress[uid]) && votesInProgress[uid].indexOf(parseInt(pid, 10)) !== -1;
}
@ -180,10 +137,10 @@ var meta = require('./meta');
function unvote(pid, uid, command, callback) {
async.parallel({
owner: function(next) {
posts.getPostField(pid, 'uid', next);
Posts.getPostField(pid, 'uid', next);
},
voteStatus: function(next) {
Favourites.hasVoted(pid, uid, next);
Posts.hasVoted(pid, uid, next);
},
reputation: function(next) {
user.getUserField(uid, 'reputation', next);
@ -229,146 +186,90 @@ var meta = require('./meta');
});
}
Favourites.hasVoted = function(pid, uid, callback) {
if (!parseInt(uid, 10)) {
return callback(null, {upvoted: false, downvoted: false});
function vote(type, unvote, pid, uid, callback) {
uid = parseInt(uid, 10);
if (uid === 0) {
return callback(new Error('[[error:not-logged-in]]'));
}
db.isMemberOfSets(['pid:' + pid + ':upvote', 'pid:' + pid + ':downvote'], uid, function(err, hasVoted) {
Posts.getPostFields(pid, ['pid', 'uid', 'tid'], function (err, postData) {
if (err) {
return callback(err);
}
callback (null, {upvoted: hasVoted[0], downvoted: hasVoted[1]});
});
};
Favourites.getVoteStatusByPostIDs = function(pids, uid, callback) {
if (!parseInt(uid, 10)) {
var data = pids.map(function() {return false;});
return callback(null, {upvotes: data, downvotes: data});
}
var upvoteSets = [],
downvoteSets = [];
var now = Date.now();
for (var i=0; i<pids.length; ++i) {
upvoteSets.push('pid:' + pids[i] + ':upvote');
downvoteSets.push('pid:' + pids[i] + ':downvote');
}
if (type === 'upvote' && !unvote) {
db.sortedSetAdd('uid:' + uid + ':upvote', now, pid);
} else {
db.sortedSetRemove('uid:' + uid + ':upvote', pid);
}
async.parallel({
upvotes: function(next) {
db.isMemberOfSets(upvoteSets, uid, next);
},
downvotes: function(next) {
db.isMemberOfSets(downvoteSets, uid, next);
if (type === 'upvote' || unvote) {
db.sortedSetRemove('uid:' + uid + ':downvote', pid);
} else {
db.sortedSetAdd('uid:' + uid + ':downvote', now, pid);
}
}, callback);
};
Favourites.favourite = function (pid, uid, callback) {
toggleFavourite('favourite', pid, uid, callback);
};
user[type === 'upvote' ? 'incrementUserFieldBy' : 'decrementUserFieldBy'](postData.uid, 'reputation', 1, function (err, newreputation) {
if (err) {
return callback(err);
}
Favourites.unfavourite = function(pid, uid, callback) {
toggleFavourite('unfavourite', pid, uid, callback);
};
if (parseInt(postData.uid, 10)) {
db.sortedSetAdd('users:reputation', newreputation, postData.uid);
}
function toggleFavourite(type, pid, uid, callback) {
if (!parseInt(uid, 10)) {
return callback(new Error('[[error:not-logged-in]]'));
}
var isFavouriting = type === 'favourite';
adjustPostVotes(postData, uid, type, unvote, function(err) {
callback(err, {
user: {
reputation: newreputation
},
post: postData,
upvote: type === 'upvote' && !unvote,
downvote: type === 'downvote' && !unvote
});
});
});
});
}
async.parallel({
owner: function(next) {
posts.getPostField(pid, 'uid', next);
},
postData: function(next) {
posts.getPostFields(pid, ['pid', 'uid'], next);
function adjustPostVotes(postData, uid, type, unvote, callback) {
var notType = (type === 'upvote' ? 'downvote' : 'upvote');
async.series([
function(next) {
if (unvote) {
db.setRemove('pid:' + postData.pid + ':' + type, uid, next);
} else {
db.setAdd('pid:' + postData.pid + ':' + type, uid, next);
}
},
hasFavourited: function(next) {
Favourites.hasFavourited(pid, uid, next);
function(next) {
db.setRemove('pid:' + postData.pid + ':' + notType, uid, next);
}
}, function(err, results) {
], function(err) {
if (err) {
return callback(err);
}
if (isFavouriting && results.hasFavourited) {
return callback(new Error('[[error:already-favourited]]'));
}
if (!isFavouriting && !results.hasFavourited) {
return callback(new Error('[[error:already-unfavourited]]'));
}
async.waterfall([
function(next) {
if (isFavouriting) {
db.sortedSetAdd('uid:' + uid + ':favourites', Date.now(), pid, next);
} else {
db.sortedSetRemove('uid:' + uid + ':favourites', pid, next);
}
},
function(next) {
db[isFavouriting ? 'setAdd' : 'setRemove']('pid:' + pid + ':users_favourited', uid, next);
},
function(next) {
db.setCount('pid:' + pid + ':users_favourited', next);
async.parallel({
upvotes: function(next) {
db.setCount('pid:' + postData.pid + ':upvote', next);
},
function(count, next) {
results.postData.reputation = count;
posts.setPostField(pid, 'reputation', count, next);
downvotes: function(next) {
db.setCount('pid:' + postData.pid + ':downvote', next);
}
], function(err) {
}, function(err, results) {
if (err) {
return callback(err);
}
var current = results.hasFavourited ? 'favourited' : 'unfavourited';
plugins.fireHook('action:post.' + type, {
pid: pid,
uid: uid,
owner: results.owner,
current: current
});
callback(null, {
post: results.postData,
isFavourited: isFavouriting
});
postData.upvotes = parseInt(results.upvotes, 10);
postData.downvotes = parseInt(results.downvotes, 10);
postData.votes = postData.upvotes - postData.downvotes;
Posts.updatePostVoteCount(postData, callback);
});
});
}
Favourites.hasFavourited = function(pid, uid, callback) {
if (!parseInt(uid, 10)) {
return callback(null, false);
}
db.isSetMember('pid:' + pid + ':users_favourited', uid, callback);
};
Favourites.getFavouritesByPostIDs = function(pids, uid, callback) {
if (!parseInt(uid, 10)) {
return callback(null, pids.map(function() {return false;}));
}
var sets = [];
for (var i=0; i<pids.length; ++i) {
sets.push('pid:' + pids[i] + ':users_favourited');
}
db.isMemberOfSets(sets, uid, callback);
};
Favourites.getUpvotedUidsByPids = function(pids, callback) {
var sets = pids.map(function(pid) {
return 'pid:' + pid + ':upvote';
});
db.getSetsMembers(sets, callback);
};
}(exports));
};

@ -17,7 +17,7 @@ module.exports = function (app, middleware, controllers) {
setupPageRoute(app, '/user/:userslug/best', middleware, middlewares, controllers.accounts.posts.getBestPosts);
setupPageRoute(app, '/user/:userslug/groups', middleware, middlewares, controllers.accounts.groups.get);
setupPageRoute(app, '/user/:userslug/favourites', middleware, accountMiddlewares, controllers.accounts.posts.getFavourites);
setupPageRoute(app, '/user/:userslug/bookmarks', middleware, accountMiddlewares, controllers.accounts.posts.getBookmarks);
setupPageRoute(app, '/user/:userslug/watched', middleware, accountMiddlewares, controllers.accounts.posts.getWatchedTopics);
setupPageRoute(app, '/user/:userslug/upvoted', middleware, accountMiddlewares, controllers.accounts.posts.getUpVotedPosts);
setupPageRoute(app, '/user/:userslug/downvoted', middleware, accountMiddlewares, controllers.accounts.posts.getDownVotedPosts);

@ -8,7 +8,6 @@ var meta = require('../meta');
var topics = require('../topics');
var user = require('../user');
var websockets = require('./index');
var socketTopics = require('./topics');
var socketHelpers = require('./helpers');
var utils = require('../../public/src/utils');
@ -16,10 +15,10 @@ var apiController = require('../controllers/api');
var SocketPosts = {};
require('./posts/edit')(SocketPosts);
require('./posts/move')(SocketPosts);
require('./posts/favourites')(SocketPosts);
require('./posts/votes')(SocketPosts);
require('./posts/bookmarks')(SocketPosts);
require('./posts/tools')(SocketPosts);
require('./posts/flag')(SocketPosts);
@ -77,8 +76,8 @@ SocketPosts.getPost = function(socket, pid, callback) {
apiController.getPostData(pid, socket.uid, callback);
};
SocketPosts.loadMoreFavourites = function(socket, data, callback) {
loadMorePosts('uid:' + data.uid + ':favourites', socket.uid, data, callback);
SocketPosts.loadMoreBookmarks = function(socket, data, callback) {
loadMorePosts('uid:' + data.uid + ':bookmarks', socket.uid, data, callback);
};
SocketPosts.loadMoreUserPosts = function(socket, data, callback) {

@ -0,0 +1,16 @@
'use strict';
var helpers = require('./helpers');
module.exports = function(SocketPosts) {
SocketPosts.bookmark = function(socket, data, callback) {
helpers.postCommand(socket, 'bookmark', 'bookmarked', '', data, callback);
};
SocketPosts.unbookmark = function(socket, data, callback) {
helpers.postCommand(socket, 'unbookmark', 'bookmarked', '', data, callback);
};
};

@ -1,162 +0,0 @@
'use strict';
var async = require('async');
var db = require('../../database');
var user = require('../../user');
var posts = require('../../posts');
var favourites = require('../../favourites');
var plugins = require('../../plugins');
var websockets = require('../index');
var privileges = require('../../privileges');
var socketHelpers = require('../helpers');
module.exports = function(SocketPosts) {
SocketPosts.getVoters = function(socket, data, callback) {
if (!data || !data.pid || !data.cid) {
return callback(new Error('[[error:invalid-data]]'));
}
async.waterfall([
function (next) {
privileges.categories.isAdminOrMod(data.cid, socket.uid, next);
},
function (isAdminOrMod, next) {
if (!isAdminOrMod) {
return next(new Error('[[error:no-privileges]]'));
}
async.parallel({
upvoteUids: function(next) {
db.getSetMembers('pid:' + data.pid + ':upvote', next);
},
downvoteUids: function(next) {
db.getSetMembers('pid:' + data.pid + ':downvote', next);
}
}, next);
},
function (results, next) {
async.parallel({
upvoters: function(next) {
user.getUsersFields(results.upvoteUids, ['username', 'userslug', 'picture'], next);
},
upvoteCount: function(next) {
next(null, results.upvoteUids.length);
},
downvoters: function(next) {
user.getUsersFields(results.downvoteUids, ['username', 'userslug', 'picture'], next);
},
downvoteCount: function(next) {
next(null, results.downvoteUids.length);
}
}, next);
}
], callback);
};
SocketPosts.getUpvoters = function(socket, pids, callback) {
if (!Array.isArray(pids)) {
return callback(new Error('[[error:invalid-data]]'));
}
favourites.getUpvotedUidsByPids(pids, function(err, data) {
if (err || !Array.isArray(data) || !data.length) {
return callback(err, []);
}
async.map(data, function(uids, next) {
var otherCount = 0;
if (uids.length > 6) {
otherCount = uids.length - 5;
uids = uids.slice(0, 5);
}
user.getUsernamesByUids(uids, function(err, usernames) {
next(err, {
otherCount: otherCount,
usernames: usernames
});
});
}, callback);
});
};
SocketPosts.upvote = function(socket, data, callback) {
favouriteCommand(socket, 'upvote', 'voted', 'notifications:upvoted_your_post_in', data, callback);
};
SocketPosts.downvote = function(socket, data, callback) {
favouriteCommand(socket, 'downvote', 'voted', '', data, callback);
};
SocketPosts.unvote = function(socket, data, callback) {
favouriteCommand(socket, 'unvote', 'voted', '', data, callback);
};
SocketPosts.favourite = function(socket, data, callback) {
favouriteCommand(socket, 'favourite', 'favourited', '', data, callback);
};
SocketPosts.unfavourite = function(socket, data, callback) {
favouriteCommand(socket, 'unfavourite', 'favourited', '', data, callback);
};
function favouriteCommand(socket, command, eventName, notification, data, callback) {
if (!socket.uid) {
return callback(new Error('[[error:not-logged-in]]'))
}
if (!data || !data.pid || !data.room_id) {
return callback(new Error('[[error:invalid-data]]'));
}
async.parallel({
exists: function(next) {
posts.exists(data.pid, next);
},
deleted: function(next) {
posts.getPostField(data.pid, 'deleted', next);
}
}, function(err, results) {
if (err || !results.exists) {
return callback(err || new Error('[[error:invalid-pid]]'));
}
if (parseInt(results.deleted, 10) === 1) {
return callback(new Error('[[error:post-deleted]]'));
}
/*
hooks:
filter:post.upvote
filter:post.downvote
filter:post.unvote
filter:post.favourite
filter:post.unfavourite
*/
plugins.fireHook('filter:post.' + command, {data: data, uid: socket.uid}, function(err, filteredData) {
if (err) {
return callback(err);
}
executeFavouriteCommand(socket, command, eventName, notification, filteredData.data, callback);
});
});
}
function executeFavouriteCommand(socket, command, eventName, notification, data, callback) {
favourites[command](data.pid, socket.uid, function(err, result) {
if (err) {
return callback(err);
}
if (result && eventName) {
socket.emit('posts.' + command, result);
websockets.in(data.room_id).emit('event:' + eventName, result);
}
if (result && notification) {
socketHelpers.sendNotificationToPostOwner(data.pid, socket.uid, command, notification);
} else if (result && command === 'unvote') {
socketHelpers.rescindUpvoteNotification(data.pid, socket.uid);
}
callback();
});
}
};

@ -0,0 +1,71 @@
'use strict';
var async = require('async');
var posts = require('../../posts');
var plugins = require('../../plugins');
var websockets = require('../index');
var socketHelpers = require('../helpers');
var helpers = module.exports;
helpers.postCommand = function(socket, command, eventName, notification, data, callback) {
if (!socket.uid) {
return callback(new Error('[[error:not-logged-in]]'));
}
if (!data || !data.pid || !data.room_id) {
return callback(new Error('[[error:invalid-data]]'));
}
async.parallel({
exists: function(next) {
posts.exists(data.pid, next);
},
deleted: function(next) {
posts.getPostField(data.pid, 'deleted', next);
}
}, function(err, results) {
if (err || !results.exists) {
return callback(err || new Error('[[error:invalid-pid]]'));
}
if (parseInt(results.deleted, 10) === 1) {
return callback(new Error('[[error:post-deleted]]'));
}
/*
hooks:
filter:post.upvote
filter:post.downvote
filter:post.unvote
filter:post.bookmark
filter:post.unbookmark
*/
plugins.fireHook('filter:post.' + command, {data: data, uid: socket.uid}, function(err, filteredData) {
if (err) {
return callback(err);
}
executeCommand(socket, command, eventName, notification, filteredData.data, callback);
});
});
};
function executeCommand(socket, command, eventName, notification, data, callback) {
posts[command](data.pid, socket.uid, function(err, result) {
if (err) {
return callback(err);
}
if (result && eventName) {
socket.emit('posts.' + command, result);
websockets.in(data.room_id).emit('event:' + eventName, result);
}
if (result && notification) {
socketHelpers.sendNotificationToPostOwner(data.pid, socket.uid, command, notification);
} else if (result && command === 'unvote') {
socketHelpers.rescindUpvoteNotification(data.pid, socket.uid);
}
callback();
});
}

@ -12,7 +12,6 @@ var socketTopics = require('../topics');
var privileges = require('../../privileges');
var plugins = require('../../plugins');
var social = require('../../social');
var favourites = require('../../favourites');
module.exports = function(SocketPosts) {
@ -23,7 +22,7 @@ module.exports = function(SocketPosts) {
async.parallel({
posts: function(next) {
posts.getPostFields(data.pid, ['deleted', 'reputation', 'uid'], next);
posts.getPostFields(data.pid, ['deleted', 'bookmarks', 'uid'], next);
},
isAdminOrMod: function(next) {
privileges.categories.isAdminOrMod(data.cid, socket.uid, next);
@ -34,8 +33,8 @@ module.exports = function(SocketPosts) {
canDelete: function(next) {
privileges.posts.canDelete(data.pid, socket.uid, next);
},
favourited: function(next) {
favourites.getFavouritesByPostIDs([data.pid], socket.uid, next);
bookmarked: function(next) {
posts.hasBookmarked(data.pid, socket.uid, next);
},
tools: function(next) {
plugins.fireHook('filter:post.tools', {pid: data.pid, uid: socket.uid, tools: []}, next);
@ -50,7 +49,7 @@ module.exports = function(SocketPosts) {
results.posts.tools = results.tools.tools;
results.posts.deleted = parseInt(results.posts.deleted, 10) === 1;
results.posts.favourited = results.favourited[0];
results.posts.bookmarked = results.bookmarked;
results.posts.selfPost = socket.uid && socket.uid === parseInt(results.posts.uid, 10);
results.posts.display_edit_tools = results.canEdit.flag;
results.posts.display_delete_tools = results.canDelete.flag;

@ -0,0 +1,92 @@
'use strict';
var async = require('async');
var db = require('../../database');
var user = require('../../user');
var posts = require('../../posts');
var privileges = require('../../privileges');
var helpers = require('./helpers');
module.exports = function(SocketPosts) {
SocketPosts.getVoters = function(socket, data, callback) {
if (!data || !data.pid || !data.cid) {
return callback(new Error('[[error:invalid-data]]'));
}
async.waterfall([
function (next) {
privileges.categories.isAdminOrMod(data.cid, socket.uid, next);
},
function (isAdminOrMod, next) {
if (!isAdminOrMod) {
return next(new Error('[[error:no-privileges]]'));
}
async.parallel({
upvoteUids: function(next) {
db.getSetMembers('pid:' + data.pid + ':upvote', next);
},
downvoteUids: function(next) {
db.getSetMembers('pid:' + data.pid + ':downvote', next);
}
}, next);
},
function (results, next) {
async.parallel({
upvoters: function(next) {
user.getUsersFields(results.upvoteUids, ['username', 'userslug', 'picture'], next);
},
upvoteCount: function(next) {
next(null, results.upvoteUids.length);
},
downvoters: function(next) {
user.getUsersFields(results.downvoteUids, ['username', 'userslug', 'picture'], next);
},
downvoteCount: function(next) {
next(null, results.downvoteUids.length);
}
}, next);
}
], callback);
};
SocketPosts.getUpvoters = function(socket, pids, callback) {
if (!Array.isArray(pids)) {
return callback(new Error('[[error:invalid-data]]'));
}
posts.getUpvotedUidsByPids(pids, function(err, data) {
if (err || !Array.isArray(data) || !data.length) {
return callback(err, []);
}
async.map(data, function(uids, next) {
var otherCount = 0;
if (uids.length > 6) {
otherCount = uids.length - 5;
uids = uids.slice(0, 5);
}
user.getUsernamesByUids(uids, function(err, usernames) {
next(err, {
otherCount: otherCount,
usernames: usernames
});
});
}, callback);
});
};
SocketPosts.upvote = function(socket, data, callback) {
helpers.postCommand(socket, 'upvote', 'voted', 'notifications:upvoted_your_post_in', data, callback);
};
SocketPosts.downvote = function(socket, data, callback) {
helpers.postCommand(socket, 'downvote', 'voted', '', data, callback);
};
SocketPosts.unvote = function(socket, data, callback) {
helpers.postCommand(socket, 'unvote', 'voted', '', data, callback);
};
};

@ -310,8 +310,8 @@ module.exports = function(Topics) {
postData.user.username = validator.escape(String(data.handle));
}
postData.favourited = false;
postData.votes = 0;
postData.bookmarked = false;
postData.display_edit_tools = true;
postData.display_delete_tools = true;
postData.display_moderator_tools = true;

@ -7,7 +7,6 @@ var validator = require('validator');
var db = require('../database');
var user = require('../user');
var favourites = require('../favourites');
var posts = require('../posts');
var meta = require('../meta');
@ -60,11 +59,11 @@ module.exports = function(Topics) {
}
async.parallel({
favourites: function(next) {
favourites.getFavouritesByPostIDs(pids, uid, next);
bookmarks: function(next) {
posts.hasBookmarked(pids, uid, next);
},
voteData: function(next) {
favourites.getVoteStatusByPostIDs(pids, uid, next);
posts.getVoteStatusByPostIDs(pids, uid, next);
},
userData: function(next) {
var uids = [];
@ -120,7 +119,7 @@ module.exports = function(Topics) {
postObj.deleted = parseInt(postObj.deleted, 10) === 1;
postObj.user = parseInt(postObj.uid, 10) ? results.userData[postObj.uid] : _.clone(results.userData[postObj.uid]);
postObj.editor = postObj.editor ? results.editors[postObj.editor] : null;
postObj.favourited = results.favourites[i];
postObj.bookmarked = results.bookmarks[i];
postObj.upvoted = results.voteData.upvotes[i];
postObj.downvoted = results.voteData.downvotes[i];
postObj.votes = postObj.votes || 0;

@ -10,7 +10,7 @@ var db = require('./database'),
schemaDate, thisSchemaDate,
// IMPORTANT: REMEMBER TO UPDATE VALUE OF latestSchema
latestSchema = Date.UTC(2016, 8, 22);
latestSchema = Date.UTC(2016, 9, 8);
Upgrade.check = function(callback) {
db.get('schemaDate', function(err, value) {
@ -850,6 +850,63 @@ Upgrade.upgrade = function(callback) {
winston.info('[2016/09/22] Setting category recent tids - skipped!');
next();
}
},
function(next) {
function upgradePosts(next) {
var batch = require('./batch');
batch.processSortedSet('posts:pid', function(ids, next) {
async.each(ids, function(id, next) {
console.log('processing pid ' + id);
async.waterfall([
function(next) {
db.rename('pid:' + id + ':users_favourited', 'pid:' + id + ':users_bookmarked', next);
},
function(next) {
db.getObjectField('post:' + id, 'reputation', next);
},
function(reputation, next) {
if (parseInt(reputation, 10)) {
db.setObjectField('post:' + id, 'bookmarks', reputation, next);
} else {
next();
}
},
function(next) {
db.deleteObjectField('post:' + id, 'reputation', next);
}
], next);
}, next);
}, {}, next);
}
function upgradeUsers(next) {
var batch = require('./batch');
batch.processSortedSet('users:joindate', function(ids, next) {
async.each(ids, function(id, next) {
console.log('processing uid ' + id);
db.rename('uid:' + id + ':favourites', 'uid:' + id + ':bookmarks', next);
}, next);
}, {}, next);
}
thisSchemaDate = Date.UTC(2016, 9, 8);
if (schemaDate < thisSchemaDate || 1) {
updatesMade = true;
winston.info('[2016/10/8] favourite -> bookmark refactor');
async.series([upgradePosts, upgradeUsers], function(err) {
if (err) {
return next(err);
}
winston.info('[2016/08/05] favourite- bookmark refactor done!');
Upgrade.update(thisSchemaDate, next);
});
} else {
winston.info('[2016/10/8] favourite -> bookmark refactor - skipped!');
next();
}
}
// Add new schema updates here
// IMPORTANT: REMEMBER TO UPDATE VALUE OF latestSchema IN LINE 24!!!

@ -1,13 +1,13 @@
'use strict';
var async = require('async'),
db = require('../database'),
posts = require('../posts'),
topics = require('../topics'),
favourites = require('../favourites'),
groups = require('../groups'),
plugins = require('../plugins'),
batch = require('../batch');
var async = require('async');
var db = require('../database');
var posts = require('../posts');
var topics = require('../topics');
var groups = require('../groups');
var plugins = require('../plugins');
var batch = require('../batch');
module.exports = function(User) {
@ -107,7 +107,7 @@ module.exports = function(User) {
var keys = [
'uid:' + uid + ':notifications:read',
'uid:' + uid + ':notifications:unread',
'uid:' + uid + ':favourites',
'uid:' + uid + ':bookmarks',
'uid:' + uid + ':followed_tids',
'uid:' + uid + ':ignored_tids',
'user:' + uid + ':settings',
@ -151,7 +151,7 @@ module.exports = function(User) {
});
async.eachSeries(pids, function(pid, next) {
favourites.unvote(pid, uid, next);
posts.unvote(pid, uid, next);
}, next);
}
], function(err) {

@ -0,0 +1,136 @@
'use strict';
/*global require, before, after*/
var assert = require('assert');
var async = require('async');
var db = require('./mocks/databasemock');
var topics = require('../src/topics');
var posts = require('../src/posts');
var categories = require('../src/categories');
var user = require('../src/user');
describe('Post\'s', function() {
var voterUid;
var voteeUid;
var postData;
before(function(done) {
async.parallel({
voterUid: function(next) {
user.create({username: 'upvoter'}, next);
},
voteeUid: function(next) {
user.create({username: 'upvotee'}, next);
},
category: function(next) {
categories.create({
name: 'Test Category',
description: 'Test category created by testing script'
}, next);
}
}, function(err, results) {
if (err) {
return done(err);
}
voterUid = results.voterUid;
voteeUid = results.voteeUid;
topics.post({
uid: results.voteeUid,
cid: results.category.cid,
title: 'Test Topic Title',
content: 'The content of test topic'
}, function(err, data) {
if (err) {
return done(err);
}
postData = data.postData;
done();
});
});
});
describe('voting', function() {
it('should upvote a post', function(done) {
posts.upvote(postData.pid, voterUid, function(err, result) {
assert.ifError(err);
assert.equal(result.post.upvotes, 1);
assert.equal(result.post.downvotes, 0);
assert.equal(result.post.votes, 1);
assert.equal(result.user.reputation, 1);
posts.hasVoted(postData.pid, voterUid, function(err, data) {
assert.ifError(err);
assert.equal(data.upvoted, true);
assert.equal(data.downvoted, false);
done();
});
});
});
it('should unvote a post', function(done) {
posts.unvote(postData.pid, voterUid, function(err, result) {
assert.ifError(err);
assert.equal(result.post.upvotes, 0);
assert.equal(result.post.downvotes, 0);
assert.equal(result.post.votes, 0);
assert.equal(result.user.reputation, 0);
posts.hasVoted(postData.pid, voterUid, function(err, data) {
assert.ifError(err);
assert.equal(data.upvoted, false);
assert.equal(data.downvoted, false);
done();
});
});
});
it('should downvote a post', function(done) {
posts.downvote(postData.pid, voterUid, function(err, result) {
assert.ifError(err);
assert.equal(result.post.upvotes, 0);
assert.equal(result.post.downvotes, 1);
assert.equal(result.post.votes, -1);
assert.equal(result.user.reputation, -1);
posts.hasVoted(postData.pid, voterUid, function(err, data) {
assert.ifError(err);
assert.equal(data.upvoted, false);
assert.equal(data.downvoted, true);
done();
});
});
});
});
describe('bookmarking', function() {
it('should bookmark a post', function(done) {
posts.bookmark(postData.pid, voterUid, function(err, data) {
assert.ifError(err);
assert.equal(data.isBookmarked, true);
posts.hasBookmarked(postData.pid, voterUid, function(err, hasBookmarked) {
assert.ifError(err);
assert.equal(hasBookmarked, true);
done();
});
});
});
it('should unbookmark a post', function(done) {
posts.unbookmark(postData.pid, voterUid, function(err, data) {
assert.ifError(err);
assert.equal(data.isBookmarked, false);
posts.hasBookmarked([postData.pid], voterUid, function(err, hasBookmarked) {
assert.ifError(err);
assert.equal(hasBookmarked[0], false);
done();
});
});
});
});
after(function(done) {
db.flushdb(done);
});
});
Loading…
Cancel
Save