mirror of
https://github.com/NodeBB/NodeBB.git
synced 2025-10-26 16:46:12 +01:00
feat: #7743, user/digest, user/email, user/follow
This commit is contained in:
@@ -14,112 +14,71 @@ var utils = require('../utils');
|
||||
|
||||
var Digest = module.exports;
|
||||
|
||||
Digest.execute = function (payload, callback) {
|
||||
callback = callback || function () {};
|
||||
|
||||
var digestsDisabled = meta.config.disableEmailSubscriptions === 1;
|
||||
Digest.execute = async function (payload) {
|
||||
const digestsDisabled = meta.config.disableEmailSubscriptions === 1;
|
||||
if (digestsDisabled) {
|
||||
winston.info('[user/jobs] Did not send digests (' + payload.interval + ') because subscription system is disabled.');
|
||||
return callback();
|
||||
return;
|
||||
}
|
||||
|
||||
async.waterfall([
|
||||
function (next) {
|
||||
if (payload.subscribers) {
|
||||
setImmediate(next, undefined, payload.subscribers);
|
||||
} else {
|
||||
Digest.getSubscribers(payload.interval, next);
|
||||
let subscribers = payload.subscribers;
|
||||
if (!subscribers) {
|
||||
subscribers = await Digest.getSubscribers(payload.interval);
|
||||
}
|
||||
},
|
||||
function (subscribers, next) {
|
||||
if (!subscribers.length) {
|
||||
return callback();
|
||||
return;
|
||||
}
|
||||
|
||||
var data = {
|
||||
try {
|
||||
const count = await Digest.send({
|
||||
interval: payload.interval,
|
||||
subscribers: subscribers,
|
||||
};
|
||||
|
||||
Digest.send(data, next);
|
||||
},
|
||||
], function (err, count) {
|
||||
if (err) {
|
||||
winston.error('[user/jobs] Could not send digests (' + payload.interval + ')', err);
|
||||
} else {
|
||||
winston.info('[user/jobs] Digest (' + payload.interval + ') scheduling completed. ' + count + ' email(s) sent.');
|
||||
}
|
||||
|
||||
callback(err);
|
||||
});
|
||||
winston.info('[user/jobs] Digest (' + payload.interval + ') scheduling completed. ' + count + ' email(s) sent.');
|
||||
} catch (err) {
|
||||
winston.error('[user/jobs] Could not send digests (' + payload.interval + ')', err);
|
||||
throw err;
|
||||
}
|
||||
};
|
||||
|
||||
Digest.getSubscribers = function (interval, callback) {
|
||||
async.waterfall([
|
||||
function (next) {
|
||||
var subs = [];
|
||||
Digest.getSubscribers = async function (interval) {
|
||||
var subscribers = [];
|
||||
|
||||
batch.processSortedSet('users:joindate', function (uids, next) {
|
||||
async.waterfall([
|
||||
function (next) {
|
||||
user.getMultipleUserSettings(uids, next);
|
||||
},
|
||||
function (settings, next) {
|
||||
const subUids = [];
|
||||
await batch.processSortedSet('users:joindate', async function (uids) {
|
||||
const settings = await user.getMultipleUserSettings(uids);
|
||||
let subUids = [];
|
||||
settings.forEach(function (hash) {
|
||||
if (hash.dailyDigestFreq === interval) {
|
||||
subUids.push(hash.uid);
|
||||
}
|
||||
});
|
||||
user.bans.filterBanned(subUids, next);
|
||||
},
|
||||
function (uids, next) {
|
||||
subs = subs.concat(uids);
|
||||
next();
|
||||
},
|
||||
], next);
|
||||
}, { interval: 1000 }, function (err) {
|
||||
next(err, subs);
|
||||
});
|
||||
},
|
||||
function (subscribers, next) {
|
||||
plugins.fireHook('filter:digest.subscribers', {
|
||||
subUids = await user.bans.filterBanned(subUids);
|
||||
subscribers = subscribers.concat(subUids);
|
||||
}, { interval: 1000 });
|
||||
|
||||
const results = await plugins.fireHook('filter:digest.subscribers', {
|
||||
interval: interval,
|
||||
subscribers: subscribers,
|
||||
}, next);
|
||||
},
|
||||
function (results, next) {
|
||||
next(null, results.subscribers);
|
||||
},
|
||||
], callback);
|
||||
});
|
||||
return results.subscribers;
|
||||
};
|
||||
|
||||
Digest.send = function (data, callback) {
|
||||
Digest.send = async function (data) {
|
||||
var emailsSent = 0;
|
||||
if (!data || !data.subscribers || !data.subscribers.length) {
|
||||
return callback(null, emailsSent);
|
||||
return emailsSent;
|
||||
}
|
||||
var now = new Date();
|
||||
const now = new Date();
|
||||
|
||||
async.waterfall([
|
||||
function (next) {
|
||||
user.getUsersFields(data.subscribers, ['uid', 'username', 'userslug', 'lastonline'], next);
|
||||
},
|
||||
function (users, next) {
|
||||
async.eachLimit(users, 100, function (userObj, next) {
|
||||
async.waterfall([
|
||||
function (next) {
|
||||
async.parallel({
|
||||
notifications: async.apply(user.notifications.getDailyUnread, userObj.uid),
|
||||
topics: async.apply(getTermTopics, data.interval, userObj.uid, 0, 9),
|
||||
}, next);
|
||||
},
|
||||
function (data, next) {
|
||||
var notifications = data.notifications.filter(Boolean);
|
||||
const users = await user.getUsersFields(data.subscribers, ['uid', 'username', 'userslug', 'lastonline']);
|
||||
|
||||
async.eachLimit(users, 100, async function (userObj) {
|
||||
let [notifications, topics] = await Promise.all([
|
||||
user.notifications.getDailyUnread(userObj.uid),
|
||||
getTermTopics(data.interval, userObj.uid, 0, 9),
|
||||
]);
|
||||
notifications = notifications.filter(Boolean);
|
||||
// If there are no notifications and no new topics, don't bother sending a digest
|
||||
if (!notifications.length && !data.topics.length) {
|
||||
return next();
|
||||
if (!notifications.length && !topics.length) {
|
||||
return;
|
||||
}
|
||||
|
||||
notifications.forEach(function (notification) {
|
||||
@@ -129,12 +88,11 @@ Digest.send = function (data, callback) {
|
||||
});
|
||||
|
||||
// Fix relative paths in topic data
|
||||
data.topics = data.topics.map(function (topicObj) {
|
||||
var user = topicObj.hasOwnProperty('teaser') && topicObj.teaser !== undefined ? topicObj.teaser.user : topicObj.user;
|
||||
topics = topics.map(function (topicObj) {
|
||||
const user = topicObj.hasOwnProperty('teaser') && topicObj.teaser !== undefined ? topicObj.teaser.user : topicObj.user;
|
||||
if (user && user.picture && utils.isRelativeUrl(user.picture)) {
|
||||
user.picture = nconf.get('base_url') + user.picture;
|
||||
}
|
||||
|
||||
return topicObj;
|
||||
});
|
||||
emailsSent += 1;
|
||||
@@ -151,16 +109,11 @@ Digest.send = function (data, callback) {
|
||||
winston.error('[user/jobs] Could not send digest email', err);
|
||||
}
|
||||
});
|
||||
next();
|
||||
},
|
||||
], next);
|
||||
}, next);
|
||||
},
|
||||
], function (err) {
|
||||
callback(err, emailsSent);
|
||||
});
|
||||
return emailsSent;
|
||||
};
|
||||
|
||||
function getTermTopics(term, uid, start, stop, callback) {
|
||||
async function getTermTopics(term, uid, start, stop) {
|
||||
const options = {
|
||||
uid: uid,
|
||||
start: start,
|
||||
@@ -169,27 +122,14 @@ Digest.send = function (data, callback) {
|
||||
sort: 'posts',
|
||||
teaserPost: 'last-post',
|
||||
};
|
||||
|
||||
async.waterfall([
|
||||
function (next) {
|
||||
topics.getSortedTopics(options, next);
|
||||
},
|
||||
function (data, next) {
|
||||
let data = await topics.getSortedTopics(options);
|
||||
if (!data.topics.length) {
|
||||
topics.getLatestTopics(options, next);
|
||||
} else {
|
||||
next(null, data);
|
||||
data = await topics.getLatestTopics(options);
|
||||
}
|
||||
},
|
||||
(data, next) => {
|
||||
data.topics.forEach(function (topicObj) {
|
||||
if (topicObj && topicObj.teaser && topicObj.teaser.content && topicObj.teaser.content.length > 255) {
|
||||
topicObj.teaser.content = topicObj.teaser.content.slice(0, 255) + '...';
|
||||
}
|
||||
});
|
||||
|
||||
next(null, data.topics);
|
||||
},
|
||||
], callback);
|
||||
return data.topics;
|
||||
}
|
||||
};
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
|
||||
'use strict';
|
||||
|
||||
var async = require('async');
|
||||
var nconf = require('nconf');
|
||||
|
||||
var user = require('../user');
|
||||
@@ -13,30 +12,24 @@ var emailer = require('../emailer');
|
||||
|
||||
var UserEmail = module.exports;
|
||||
|
||||
UserEmail.exists = function (email, callback) {
|
||||
user.getUidByEmail(email.toLowerCase(), function (err, exists) {
|
||||
callback(err, !!exists);
|
||||
});
|
||||
UserEmail.exists = async function (email) {
|
||||
const uid = await user.getUidByEmail(email.toLowerCase());
|
||||
return !!uid;
|
||||
};
|
||||
|
||||
UserEmail.available = function (email, callback) {
|
||||
db.isSortedSetMember('email:uid', email.toLowerCase(), function (err, exists) {
|
||||
callback(err, !exists);
|
||||
});
|
||||
UserEmail.available = async function (email) {
|
||||
const exists = await db.isSortedSetMember('email:uid', email.toLowerCase());
|
||||
return !exists;
|
||||
};
|
||||
|
||||
UserEmail.sendValidationEmail = function (uid, options, callback) {
|
||||
UserEmail.sendValidationEmail = async function (uid, options) {
|
||||
/*
|
||||
* Options:
|
||||
* - email, overrides email retrieval
|
||||
* - force, sends email even if it is too soon to send another
|
||||
*/
|
||||
|
||||
// Handling for 2 arguments
|
||||
if (arguments.length === 2 && typeof options === 'function') {
|
||||
callback = options;
|
||||
options = {};
|
||||
}
|
||||
options = options || {};
|
||||
|
||||
// Fallback behaviour (email passed in as second argument)
|
||||
if (typeof options === 'string') {
|
||||
@@ -45,117 +38,66 @@ UserEmail.sendValidationEmail = function (uid, options, callback) {
|
||||
};
|
||||
}
|
||||
|
||||
callback = callback || function () {};
|
||||
var confirm_code = utils.generateUUID();
|
||||
var confirm_link = nconf.get('url') + '/confirm/' + confirm_code;
|
||||
let confirm_code = utils.generateUUID();
|
||||
const confirm_link = nconf.get('url') + '/confirm/' + confirm_code;
|
||||
|
||||
var emailInterval = meta.config.emailConfirmInterval;
|
||||
const emailInterval = meta.config.emailConfirmInterval;
|
||||
|
||||
async.waterfall([
|
||||
function (next) {
|
||||
// If no email passed in (default), retrieve email from uid
|
||||
if (options.email && options.email.length) {
|
||||
return setImmediate(next, null, options.email);
|
||||
if (!options.email || !options.email.length) {
|
||||
options.email = await user.getUserField(uid, 'email');
|
||||
}
|
||||
|
||||
user.getUserField(uid, 'email', next);
|
||||
},
|
||||
function (email, next) {
|
||||
options.email = email;
|
||||
if (!options.email) {
|
||||
return callback();
|
||||
return;
|
||||
}
|
||||
|
||||
if (options.force) {
|
||||
return setImmediate(next, null, false);
|
||||
let sent = false;
|
||||
if (!options.force) {
|
||||
sent = await db.get('uid:' + uid + ':confirm:email:sent');
|
||||
}
|
||||
|
||||
db.get('uid:' + uid + ':confirm:email:sent', next);
|
||||
},
|
||||
function (sent, next) {
|
||||
if (sent) {
|
||||
return next(new Error('[[error:confirm-email-already-sent, ' + emailInterval + ']]'));
|
||||
throw new Error('[[error:confirm-email-already-sent, ' + emailInterval + ']]');
|
||||
}
|
||||
db.set('uid:' + uid + ':confirm:email:sent', 1, next);
|
||||
},
|
||||
function (next) {
|
||||
db.pexpireAt('uid:' + uid + ':confirm:email:sent', Date.now() + (emailInterval * 60 * 1000), next);
|
||||
},
|
||||
function (next) {
|
||||
plugins.fireHook('filter:user.verify.code', confirm_code, next);
|
||||
},
|
||||
function (_confirm_code, next) {
|
||||
confirm_code = _confirm_code;
|
||||
db.setObject('confirm:' + confirm_code, {
|
||||
await db.set('uid:' + uid + ':confirm:email:sent', 1);
|
||||
await db.pexpireAt('uid:' + uid + ':confirm:email:sent', Date.now() + (emailInterval * 60 * 1000));
|
||||
confirm_code = await plugins.fireHook('filter:user.verify.code', confirm_code);
|
||||
|
||||
await db.setObject('confirm:' + confirm_code, {
|
||||
email: options.email.toLowerCase(),
|
||||
uid: uid,
|
||||
}, next);
|
||||
},
|
||||
function (next) {
|
||||
db.expireAt('confirm:' + confirm_code, Math.floor((Date.now() / 1000) + (60 * 60 * 24)), next);
|
||||
},
|
||||
function (next) {
|
||||
user.getUserField(uid, 'username', next);
|
||||
},
|
||||
function (username, next) {
|
||||
var title = meta.config.title || meta.config.browserTitle || 'NodeBB';
|
||||
var template = options.template || 'welcome';
|
||||
});
|
||||
await db.expireAt('confirm:' + confirm_code, Math.floor((Date.now() / 1000) + (60 * 60 * 24)));
|
||||
const username = await user.getUserField(uid, 'username');
|
||||
|
||||
var data = {
|
||||
const data = {
|
||||
username: username,
|
||||
confirm_link: confirm_link,
|
||||
confirm_code: confirm_code,
|
||||
|
||||
subject: options.subject || '[[email:welcome-to, ' + title + ']]',
|
||||
template: template,
|
||||
subject: options.subject || '[[email:welcome-to, ' + (meta.config.title || meta.config.browserTitle || 'NodeBB') + ']]',
|
||||
template: options.template || 'welcome',
|
||||
uid: uid,
|
||||
};
|
||||
|
||||
if (plugins.hasListeners('action:user.verify')) {
|
||||
plugins.fireHook('action:user.verify', { uid: uid, data: data });
|
||||
next();
|
||||
} else {
|
||||
emailer.send(template, uid, data, next);
|
||||
await emailer.send(data.template, uid, data);
|
||||
}
|
||||
},
|
||||
function (next) {
|
||||
next(null, confirm_code);
|
||||
},
|
||||
], callback);
|
||||
return confirm_code;
|
||||
};
|
||||
|
||||
UserEmail.confirm = function (code, callback) {
|
||||
var confirmObj;
|
||||
async.waterfall([
|
||||
function (next) {
|
||||
db.getObject('confirm:' + code, next);
|
||||
},
|
||||
function (_confirmObj, next) {
|
||||
confirmObj = _confirmObj;
|
||||
UserEmail.confirm = async function (code) {
|
||||
const confirmObj = await db.getObject('confirm:' + code);
|
||||
if (!confirmObj || !confirmObj.uid || !confirmObj.email) {
|
||||
return next(new Error('[[error:invalid-data]]'));
|
||||
throw new Error('[[error:invalid-data]]');
|
||||
}
|
||||
|
||||
user.getUserField(confirmObj.uid, 'email', next);
|
||||
},
|
||||
function (currentEmail, next) {
|
||||
const currentEmail = await user.getUserField(confirmObj.uid, 'email');
|
||||
if (!currentEmail || currentEmail.toLowerCase() !== confirmObj.email) {
|
||||
return next(new Error('[[error:invalid-email]]'));
|
||||
throw new Error('[[error:invalid-email]]');
|
||||
}
|
||||
|
||||
async.series([
|
||||
async.apply(user.setUserField, confirmObj.uid, 'email:confirmed', 1),
|
||||
async.apply(db.delete, 'confirm:' + code),
|
||||
async.apply(db.delete, 'uid:' + confirmObj.uid + ':confirm:email:sent'),
|
||||
function (next) {
|
||||
db.sortedSetRemove('users:notvalidated', confirmObj.uid, next);
|
||||
},
|
||||
function (next) {
|
||||
plugins.fireHook('action:user.email.confirmed', { uid: confirmObj.uid, email: confirmObj.email }, next);
|
||||
},
|
||||
], next);
|
||||
},
|
||||
], function (err) {
|
||||
callback(err);
|
||||
});
|
||||
await user.setUserField(confirmObj.uid, 'email:confirmed', 1);
|
||||
await db.delete('confirm:' + code);
|
||||
await db.delete('uid:' + confirmObj.uid + ':confirm:email:sent');
|
||||
await db.sortedSetRemove('users:notvalidated', confirmObj.uid);
|
||||
await plugins.fireHook('action:user.email.confirmed', { uid: confirmObj.uid, email: confirmObj.email });
|
||||
};
|
||||
|
||||
@@ -1,103 +1,85 @@
|
||||
|
||||
'use strict';
|
||||
|
||||
var async = require('async');
|
||||
var plugins = require('../plugins');
|
||||
var db = require('../database');
|
||||
|
||||
module.exports = function (User) {
|
||||
User.follow = function (uid, followuid, callback) {
|
||||
toggleFollow('follow', uid, followuid, callback);
|
||||
User.follow = async function (uid, followuid) {
|
||||
await toggleFollow('follow', uid, followuid);
|
||||
};
|
||||
|
||||
User.unfollow = function (uid, unfollowuid, callback) {
|
||||
toggleFollow('unfollow', uid, unfollowuid, callback);
|
||||
User.unfollow = async function (uid, unfollowuid) {
|
||||
await toggleFollow('unfollow', uid, unfollowuid);
|
||||
};
|
||||
|
||||
function toggleFollow(type, uid, theiruid, callback) {
|
||||
async function toggleFollow(type, uid, theiruid) {
|
||||
if (parseInt(uid, 10) <= 0 || parseInt(theiruid, 10) <= 0) {
|
||||
return callback(new Error('[[error:invalid-uid]]'));
|
||||
throw new Error('[[error:invalid-uid]]');
|
||||
}
|
||||
|
||||
if (parseInt(uid, 10) === parseInt(theiruid, 10)) {
|
||||
return callback(new Error('[[error:you-cant-follow-yourself]]'));
|
||||
throw new Error('[[error:you-cant-follow-yourself]]');
|
||||
}
|
||||
|
||||
async.waterfall([
|
||||
function (next) {
|
||||
User.exists(theiruid, next);
|
||||
},
|
||||
function (exists, next) {
|
||||
const exists = await User.exists(theiruid);
|
||||
if (!exists) {
|
||||
return next(new Error('[[error:no-user]]'));
|
||||
throw new Error('[[error:no-user]]');
|
||||
}
|
||||
User.isFollowing(uid, theiruid, next);
|
||||
},
|
||||
function (isFollowing, next) {
|
||||
const isFollowing = await User.isFollowing(uid, theiruid);
|
||||
if (type === 'follow') {
|
||||
if (isFollowing) {
|
||||
return next(new Error('[[error:already-following]]'));
|
||||
throw new Error('[[error:already-following]]');
|
||||
}
|
||||
var now = Date.now();
|
||||
async.parallel([
|
||||
async.apply(db.sortedSetAddBulk, [
|
||||
const now = Date.now();
|
||||
await Promise.all([
|
||||
db.sortedSetAddBulk([
|
||||
['following:' + uid, now, theiruid],
|
||||
['followers:' + theiruid, now, uid],
|
||||
]),
|
||||
async.apply(User.incrementUserFieldBy, uid, 'followingCount', 1),
|
||||
async.apply(User.incrementUserFieldBy, theiruid, 'followerCount', 1),
|
||||
], next);
|
||||
User.incrementUserFieldBy(uid, 'followingCount', 1),
|
||||
User.incrementUserFieldBy(theiruid, 'followerCount', 1),
|
||||
]);
|
||||
} else {
|
||||
if (!isFollowing) {
|
||||
return next(new Error('[[error:not-following]]'));
|
||||
throw new Error('[[error:not-following]]');
|
||||
}
|
||||
async.parallel([
|
||||
async.apply(db.sortedSetRemove, 'following:' + uid, theiruid),
|
||||
async.apply(db.sortedSetRemove, 'followers:' + theiruid, uid),
|
||||
async.apply(User.decrementUserFieldBy, uid, 'followingCount', 1),
|
||||
async.apply(User.decrementUserFieldBy, theiruid, 'followerCount', 1),
|
||||
], next);
|
||||
await Promise.all([
|
||||
db.sortedSetRemoveBulk([
|
||||
['following:' + uid, theiruid],
|
||||
['followers:' + theiruid, uid],
|
||||
]),
|
||||
User.decrementUserFieldBy(uid, 'followingCount', 1),
|
||||
User.decrementUserFieldBy(theiruid, 'followerCount', 1),
|
||||
]);
|
||||
}
|
||||
},
|
||||
], function (err) {
|
||||
callback(err);
|
||||
});
|
||||
}
|
||||
|
||||
User.getFollowing = function (uid, start, stop, callback) {
|
||||
getFollow(uid, 'following', start, stop, callback);
|
||||
User.getFollowing = async function (uid, start, stop) {
|
||||
return await getFollow(uid, 'following', start, stop);
|
||||
};
|
||||
|
||||
User.getFollowers = function (uid, start, stop, callback) {
|
||||
getFollow(uid, 'followers', start, stop, callback);
|
||||
User.getFollowers = async function (uid, start, stop) {
|
||||
return await getFollow(uid, 'followers', start, stop);
|
||||
};
|
||||
|
||||
function getFollow(uid, type, start, stop, callback) {
|
||||
async function getFollow(uid, type, start, stop) {
|
||||
if (parseInt(uid, 10) <= 0) {
|
||||
return setImmediate(callback, null, []);
|
||||
return [];
|
||||
}
|
||||
async.waterfall([
|
||||
function (next) {
|
||||
db.getSortedSetRevRange(type + ':' + uid, start, stop, next);
|
||||
},
|
||||
function (uids, next) {
|
||||
plugins.fireHook('filter:user.' + type, {
|
||||
const uids = await db.getSortedSetRevRange(type + ':' + uid, start, stop);
|
||||
const data = await plugins.fireHook('filter:user.' + type, {
|
||||
uids: uids,
|
||||
uid: uid,
|
||||
start: start,
|
||||
stop: stop,
|
||||
}, next);
|
||||
},
|
||||
function (data, next) {
|
||||
User.getUsers(data.uids, uid, next);
|
||||
},
|
||||
], callback);
|
||||
});
|
||||
return await User.getUsers(data.uids, uid);
|
||||
}
|
||||
|
||||
User.isFollowing = function (uid, theirid, callback) {
|
||||
User.isFollowing = async function (uid, theirid) {
|
||||
if (parseInt(uid, 10) <= 0 || parseInt(theirid, 10) <= 0) {
|
||||
return setImmediate(callback, null, false);
|
||||
return false;
|
||||
}
|
||||
db.isSortedSetMember('following:' + uid, theirid, callback);
|
||||
return await db.isSortedSetMember('following:' + uid, theirid);
|
||||
};
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user