1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 | 1× 1× 1× 1× 1× 1× 1× 1× 1× 1× 1× 1× 1× 1× 9× 9× 9× 9× 9× 9× 9× 1× 1× 1× 13× 1× 13× 13× 13× 13× 13× 13× 13× 13× 13× 13× 13× 13× | 'use strict'; var async = require('async'); var nconf = require('nconf'); var S = require('string'); var winston = require('winston'); var db = require('../database'); var user = require('../user'); var notifications = require('../notifications'); var privileges = require('../privileges'); var meta = require('../meta'); var emailer = require('../emailer'); var plugins = require('../plugins'); module.exports = function(Topics) { Topics.toggleFollow = function(tid, uid, callback) { callback = callback || function() {}; var isFollowing; async.waterfall([ function (next) { Topics.exists(tid, next); }, function (exists, next) { if (!exists) { return next(new Error('[[error:no-topic]]')); } Topics.isFollowing([tid], uid, next); }, function (_isFollowing, next) { isFollowing = _isFollowing[0]; if (isFollowing) { Topics.unfollow(tid, uid, next); } else { Topics.follow(tid, uid, next); } }, function(next) { next(null, !isFollowing); } ], callback); }; Topics.follow = function(tid, uid, callback) { callback = callback || function() {}; Iif (!parseInt(uid, 10)) { return callback(); } async.waterfall([ function (next) { Topics.exists(tid, next); }, function (exists, next) { Iif (!exists) { return next(new Error('[[error:no-topic]]')); } db.setAdd('tid:' + tid + ':followers', uid, next); }, async.apply(plugins.fireHook, 'action:topic.follow', { uid: uid, tid: tid }), function(next) { db.sortedSetAdd('uid:' + uid + ':followed_tids', Date.now(), tid, next); } ], callback); }; Topics.unfollow = function(tid, uid, callback) { callback = callback || function() {}; async.waterfall([ function (next) { Topics.exists(tid, next); }, function (exists, next) { if (!exists) { return next(new Error('[[error:no-topic]]')); } db.setRemove('tid:' + tid + ':followers', uid, next); }, async.apply(plugins.fireHook, 'action:topic.unfollow', { uid: uid, tid: tid }), function(next) { db.sortedSetRemove('uid:' + uid + ':followed_tids', tid, next); } ], callback); }; Topics.isFollowing = function(tids, uid, callback) { if (!Array.isArray(tids)) { return callback(); } if (!parseInt(uid, 10)) { return callback(null, tids.map(function() { return false; })); } var keys = tids.map(function(tid) { return 'tid:' + tid + ':followers'; }); db.isMemberOfSets(keys, uid, callback); }; Topics.getFollowers = function(tid, callback) { db.getSetMembers('tid:' + tid + ':followers', callback); }; Topics.notifyFollowers = function(postData, exceptUid, callback) { callback = callback || function() {}; var followers; var title; var titleEscaped; async.waterfall([ function (next) { Topics.getFollowers(postData.topic.tid, next); }, function (followers, next) { Iif (!Array.isArray(followers) || !followers.length) { return callback(); } var index = followers.indexOf(exceptUid.toString()); Eif (index !== -1) { followers.splice(index, 1); } Eif (!followers.length) { return callback(); } privileges.topics.filterUids('read', postData.topic.tid, followers, next); }, function (_followers, next) { followers = _followers; if (!followers.length) { return callback(); } title = postData.topic.title; if (title) { title = S(title).decodeHTMLEntities().s; titleEscaped = title.replace(/%/g, '%').replace(/,/g, ','); } notifications.create({ bodyShort: '[[notifications:user_posted_to, ' + postData.user.username + ', ' + titleEscaped + ']]', bodyLong: postData.content, pid: postData.pid, nid: 'new_post:tid:' + postData.topic.tid + ':pid:' + postData.pid + ':uid:' + exceptUid, tid: postData.topic.tid, from: exceptUid, mergeId: 'notifications:user_posted_to|' + postData.topic.tid, topicTitle: title }, next); }, function (notification, next) { if (notification) { notifications.push(notification, followers); } if (parseInt(meta.config.disableEmailSubscriptions, 10) === 1) { return next(); } async.eachLimit(followers, 3, function(toUid, next) { async.parallel({ userData: async.apply(user.getUserFields, toUid, ['username', 'userslug']), userSettings: async.apply(user.getSettings, toUid) }, function(err, data) { if (err) { return next(err); } if (data.userSettings.sendPostNotifications) { emailer.send('notif_post', toUid, { pid: postData.pid, subject: '[' + (meta.config.title || 'NodeBB') + '] ' + title, intro: '[[notifications:user_posted_to, ' + postData.user.username + ', ' + titleEscaped + ']]', postBody: postData.content.replace(/"\/\//g, '"http://'), site_title: meta.config.title || 'NodeBB', username: data.userData.username, userslug: data.userData.userslug, url: nconf.get('url') + '/topic/' + postData.topic.tid, base_url: nconf.get('url') }, next); } else { winston.debug('[topics.notifyFollowers] uid ' + toUid + ' does not have post notifications enabled, skipping.'); next(); } }); }); next(); } ], callback); }; }; |