Upvote notification frequency selection (#6087)

Closes #5963

- Notify on every upvote
- Notify on every tenth upvote
- Notify logarithmically (on 10, 100, 1000...)
- Disable upvote notifications
This commit is contained in:
Peter Jaszkowiak
2017-11-21 12:14:14 -07:00
committed by Julian Lam
parent 3fd25257e0
commit 74ceb78800
6 changed files with 71 additions and 6 deletions

View File

@@ -13,7 +13,7 @@ var notifications = require('../notifications');
var plugins = require('../plugins');
var utils = require('../utils');
var SocketHelpers = {};
var SocketHelpers = module.exports;
SocketHelpers.notifyOnlineUsers = function (uid, result) {
winston.warn('[deprecated] SocketHelpers.notifyOnlineUsers, consider using socketHelpers.notifyNew(uid, \'newPost\', result);');
@@ -171,6 +171,51 @@ SocketHelpers.sendNotificationToTopicOwner = function (tid, fromuid, command, no
});
};
SocketHelpers.upvote = function (data, notification) {
if (!data || !data.post || !data.post.uid || !data.post.votes || !data.post.pid || !data.fromuid) {
return;
}
var votes = data.post.votes;
var touid = data.post.uid;
var fromuid = data.fromuid;
var pid = data.post.pid;
var shouldNotify = {
all: function () {
return votes > 0;
},
everyTen: function () {
return votes > 0 && votes % 10 === 0;
},
logarithmic: function () {
return votes > 1 && Math.log10(votes) % 1 === 0;
},
disabled: function () {
return false;
},
};
async.waterfall([
function (next) {
user.getSettings(touid, next);
},
function (settings, next) {
var should = shouldNotify[settings.upvoteNotifFreq] || shouldNotify.all;
if (should()) {
SocketHelpers.sendNotificationToPostOwner(pid, fromuid, 'upvote', notification);
}
next();
},
], function (err) {
if (err) {
winston.error(err);
}
});
};
SocketHelpers.rescindUpvoteNotification = function (pid, fromuid) {
var uid;
async.waterfall([
@@ -199,5 +244,3 @@ SocketHelpers.emitToTopicAndCategory = function (event, data) {
websockets.in('topic_' + data.tid).emit(event, data);
websockets.in('category_' + data.cid).emit(event, data);
};
module.exports = SocketHelpers;