You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
nodebb/src/topics/follow.js

192 lines
5.3 KiB
JavaScript

'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) {
10 years ago
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() {};
10 years ago
if (!parseInt(uid, 10)) {
return callback();
}
10 years ago
async.waterfall([
function (next) {
Topics.exists(tid, next);
},
function (exists, next) {
if (!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 }),
10 years ago
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 }),
10 years ago
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';
10 years ago
});
db.isMemberOfSets(keys, uid, callback);
};
Topics.getFollowers = function(tid, callback) {
db.getSetMembers('tid:' + tid + ':followers', callback);
};
10 years ago
Topics.notifyFollowers = function(postData, exceptUid, callback) {
callback = callback || function() {};
var followers;
var title;
var titleEscaped;
10 years ago
async.waterfall([
function (next) {
10 years ago
Topics.getFollowers(postData.topic.tid, next);
},
function (followers, next) {
10 years ago
if (!Array.isArray(followers) || !followers.length) {
return callback();
}
var index = followers.indexOf(exceptUid.toString());
if (index !== -1) {
followers.splice(index, 1);
}
if (!followers.length) {
return callback();
}
10 years ago
privileges.topics.filterUids('read', postData.topic.tid, followers, next);
10 years ago
},
function (_followers, next) {
10 years ago
followers = _followers;
if (!followers.length) {
return callback();
}
title = postData.topic.title;
10 years ago
if (title) {
title = S(title).decodeHTMLEntities().s;
titleEscaped = title.replace(/%/g, '%').replace(/,/g, ',');
11 years ago
}
10 years ago
notifications.create({
bodyShort: '[[notifications:user_posted_to, ' + postData.user.username + ', ' + titleEscaped + ']]',
10 years ago
bodyLong: postData.content,
pid: postData.pid,
path: '/post/' + postData.pid,
nid: 'new_post:tid:' + postData.topic.tid + ':pid:' + postData.pid + ':uid:' + exceptUid,
10 years ago
tid: postData.topic.tid,
9 years ago
from: exceptUid,
mergeId: 'notifications:user_posted_to|' + postData.topic.tid,
topicTitle: title
}, next);
10 years ago
},
function (notification, next) {
if (notification) {
notifications.push(notification, followers);
}
if (parseInt(meta.config.disableEmailSubscriptions, 10) === 1) {
return next();
}
10 years ago
async.eachLimit(followers, 3, function(toUid, next) {
async.parallel({
10 years ago
userData: async.apply(user.getUserFields, toUid, ['username', 'userslug']),
10 years ago
userSettings: async.apply(user.getSettings, toUid)
}, function(err, data) {
if (err) {
return next(err);
}
if (data.userSettings.sendPostNotifications) {
10 years ago
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://'),
10 years ago
site_title: meta.config.title || 'NodeBB',
username: data.userData.username,
userslug: data.userData.userslug,
10 years ago
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();
10 years ago
}
});
});
10 years ago
next();
}
], callback);
};
};