closes #5096
parent
ca6101223a
commit
6f86621e30
@ -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;
|
||||
});
|
@ -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);
|
||||
}
|
||||
};
|
||||
};
|
@ -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();
|
||||
});
|
||||
}
|
@ -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);
|
||||
};
|
||||
};
|
@ -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…
Reference in New Issue