Skip to content

Commit

Permalink
fix: #7519
Browse files Browse the repository at this point in the history
  • Loading branch information
barisusakli committed Apr 5, 2019
1 parent fb58e23 commit ed91d3f
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 15 deletions.
16 changes: 9 additions & 7 deletions src/socket.io/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -337,7 +337,11 @@ SocketUser.setModerationNote = function (socket, data, callback) {
if (!socket.uid || !data || !data.uid || !data.note) {
return callback(new Error('[[error:invalid-data]]'));
}

const noteData = {
uid: socket.uid,
note: data.note,
timestamp: Date.now(),
};
async.waterfall([
function (next) {
privileges.users.canEdit(socket.uid, data.uid, next);
Expand All @@ -354,12 +358,10 @@ SocketUser.setModerationNote = function (socket, data, callback) {
return next(new Error('[[error:no-privileges]]'));
}

var note = {
uid: socket.uid,
note: data.note,
timestamp: Date.now(),
};
db.sortedSetAdd('uid:' + data.uid + ':moderation:notes', note.timestamp, JSON.stringify(note), next);
db.sortedSetAdd('uid:' + data.uid + ':moderation:notes', noteData.timestamp, noteData.timestamp, next);
},
function (next) {
db.setObject('uid:' + data.uid + ':moderation:note:' + noteData.timestamp, noteData, next);
},
], callback);
};
Expand Down
54 changes: 54 additions & 0 deletions src/upgrades/1.12.1/moderation_notes_refactor.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
'use strict';

var async = require('async');
var db = require('../../database');
var batch = require('../../batch');


module.exports = {
name: 'Update moderation notes to hashes',
timestamp: Date.UTC(2018, 3, 5),
method: function (callback) {
var progress = this.progress;

batch.processSortedSet('users:joindate', function (ids, next) {
async.each(ids, function (uid, next) {
progress.incr();
db.getSortedSetRevRange('uid:' + uid + ':moderation:notes', 0, -1, function (err, notes) {
if (err || !notes.length) {
return next(err);
}

async.eachSeries(notes, function (note, next) {
var noteData;
async.waterfall([
function (next) {
try {
noteData = JSON.parse(note);
setImmediate(next);
} catch (err) {
next(err);
}
},
function (next) {
db.sortedSetRemove('uid:' + uid + ':moderation:notes', note, next);
},
function (next) {
db.setObject('uid:' + uid + ':moderation:note:' + noteData.timestamp, {
uid: noteData.uid,
timestamp: noteData.timestamp,
note: noteData.note,
}, next);
},
function (next) {
db.sortedSetAdd('uid:' + uid + ':moderation:notes', noteData.timestamp, noteData.timestamp, next);
},
], next);
}, next);
});
}, next);
}, {
progress: this.progress,
}, callback);
},
};
17 changes: 9 additions & 8 deletions src/user/info.js
Original file line number Diff line number Diff line change
Expand Up @@ -158,18 +158,19 @@ module.exports = function (User) {
function (next) {
db.getSortedSetRevRange('uid:' + uid + ':moderation:notes', start, stop, next);
},
function (noteIds, next) {
const keys = noteIds.map(id => 'uid:' + uid + ':moderation:note:' + id);
db.getObjects(keys, next);
},
function (notes, next) {
var uids = [];
noteData = notes.map(function (note) {
try {
var data = JSON.parse(note);
uids.push(data.uid);
data.timestampISO = utils.toISOString(data.timestamp);
data.note = validator.escape(String(data.note));
return data;
} catch (err) {
return next(err);
if (note) {
uids.push(note.uid);
note.timestampISO = utils.toISOString(note.timestamp);
note.note = validator.escape(String(note.note));
}
return note;
});

User.getUsersFields(uids, ['uid', 'username', 'userslug', 'picture'], next);
Expand Down

0 comments on commit ed91d3f

Please sign in to comment.