From 1e3cdc99ac03a32f649c6a45af2f8d54dbfbdf83 Mon Sep 17 00:00:00 2001 From: Julian Lam Date: Tue, 28 Aug 2018 14:29:47 -0400 Subject: [PATCH] fixes #6702 --- src/upgrades/1.10.2/username_email_history.js | 62 +++++++++++++++++++ src/user/create.js | 4 ++ 2 files changed, 66 insertions(+) create mode 100644 src/upgrades/1.10.2/username_email_history.js diff --git a/src/upgrades/1.10.2/username_email_history.js b/src/upgrades/1.10.2/username_email_history.js new file mode 100644 index 0000000000..a3f6fcc347 --- /dev/null +++ b/src/upgrades/1.10.2/username_email_history.js @@ -0,0 +1,62 @@ +'use strict'; + +var db = require('../../database'); + +var async = require('async'); +var batch = require('../../batch'); +var user = require('../../user'); + +module.exports = { + name: 'Record first entry in username/email history', + timestamp: Date.UTC(2018, 7, 28), + method: function (callback) { + batch.processSortedSet('users:joindate', function (ids, next) { + async.each(ids, function (uid, next) { + async.parallel([ + function (next) { + // Username + async.waterfall([ + async.apply(db.sortedSetCard, 'user:' + uid + ':usernames'), + (count, next) => { + if (count > 0) { + // User has changed their username before, no record of original username, skip. + return setImmediate(next, null, null); + } + + user.getUserFields(uid, ['username', 'joindate'], next); + }, + (userdata, next) => { + if (!userdata) { + return setImmediate(next); + } + + db.sortedSetAdd('user:' + uid + ':usernames', userdata.joindate, [userdata.username, userdata.joindate].join(':'), next); + }, + ], next); + }, + function (next) { + // Email + async.waterfall([ + async.apply(db.sortedSetCard, 'user:' + uid + ':emails'), + (count, next) => { + if (count > 0) { + // User has changed their email before, no record of original email, skip. + return setImmediate(next, null, null); + } + + user.getUserFields(uid, ['email', 'joindate'], next); + }, + (userdata, next) => { + if (!userdata) { + return setImmediate(next); + } + + db.sortedSetAdd('user:' + uid + ':emails', userdata.joindate, [userdata.email, userdata.joindate].join(':'), next); + }, + ], next); + }, + ], next); + }, next); + }, callback); + }, +}; diff --git a/src/user/create.js b/src/user/create.js index b0eba41c6b..a82c050409 100644 --- a/src/user/create.js +++ b/src/user/create.js @@ -93,6 +93,9 @@ module.exports = function (User) { function (next) { db.sortedSetsAdd(['users:postcount', 'users:reputation'], 0, userData.uid, next); }, + function (next) { + db.sortedSetAdd('user:' + userData.uid + ':usernames', timestamp, userData.username, next); + }, function (next) { groups.join('registered-users', userData.uid, next); }, @@ -104,6 +107,7 @@ module.exports = function (User) { async.parallel([ async.apply(db.sortedSetAdd, 'email:uid', userData.uid, userData.email.toLowerCase()), async.apply(db.sortedSetAdd, 'email:sorted', 0, userData.email.toLowerCase() + ':' + userData.uid), + async.apply(db.sortedSetAdd, 'user:' + userData.uid + ':emails', timestamp, userData.email), ], next); if (parseInt(userData.uid, 10) !== 1 && parseInt(meta.config.requireEmailConfirmation, 10) === 1) {