Skip to content

Commit

Permalink
refactor: run all flag creation calls in a single batch
Browse files Browse the repository at this point in the history
  • Loading branch information
julianlam committed Jul 17, 2020
1 parent b249996 commit a099892
Showing 1 changed file with 33 additions and 22 deletions.
55 changes: 33 additions & 22 deletions src/flags.js
Original file line number Diff line number Diff line change
Expand Up @@ -305,47 +305,58 @@ Flags.create = async function (type, id, uid, reason, timestamp) {
// If the flag already exists, just add the report
if (targetFlagged) {
const flagId = await Flags.getFlagIdByTarget(type, id);
await Flags.addReport(flagId, uid, reason, timestamp);
await Promise.all([
Flags.addReport(flagId, uid, reason, timestamp),
Flags.update(flagId, uid, { state: 'open' }),
]);

return await Flags.get(flagId);
}

const flagId = await db.incrObjectField('global', 'nextFlagId');

await db.setObject('flag:' + flagId, {
flagId: flagId,
type: type,
targetId: id,
datetime: timestamp,
});
await Flags.addReport(flagId, uid, reason, timestamp);
await db.sortedSetAdd('flags:datetime', timestamp, flagId); // by time, the default
await db.sortedSetAdd('flags:byType:' + type, timestamp, flagId); // by flag type
await db.sortedSetAdd('flags:hash', flagId, [type, id, uid].join(':')); // save zset for duplicate checking
await db.sortedSetIncrBy('flags:byTarget', 1, [type, id].join(':')); // by flag target (score is count)
await analytics.increment('flags'); // some fancy analytics
const batched = [];

batched.push(
db.setObject.bind(db, 'flag:' + flagId, {
flagId: flagId,
type: type,
targetId: id,
datetime: timestamp,
}),
Flags.addReport.bind(Flags, flagId, uid, reason, timestamp),
db.sortedSetAdd.bind(db, 'flags:datetime', timestamp, flagId), // by time, the default
db.sortedSetAdd.bind(db, 'flags:byType:' + type, timestamp, flagId), // by flag type
db.sortedSetAdd.bind(db, 'flags:hash', flagId, [type, id, uid].join(':')), // save zset for duplicate checking
db.sortedSetIncrBy.bind(db, 'flags:byTarget', 1, [type, id].join(':')), // by flag target (score is count)
analytics.increment.bind(analytics, 'flags') // some fancy analytics
);

if (targetUid) {
await db.sortedSetAdd('flags:byTargetUid:' + targetUid, timestamp, flagId); // by target uid
batched.push(db.sortedSetAdd.bind(db, 'flags:byTargetUid:' + targetUid, timestamp, flagId)); // by target uid
}

if (targetCid) {
await db.sortedSetAdd('flags:byCid:' + targetCid, timestamp, flagId); // by target cid
batched.push(db.sortedSetAdd.bind(db, 'flags:byCid:' + targetCid, timestamp, flagId)); // by target cid
}

if (type === 'post') {
await db.sortedSetAdd('flags:byPid:' + id, timestamp, flagId); // by target pid
batched.push(
db.sortedSetAdd.bind(db, 'flags:byPid:' + id, timestamp, flagId), // by target pid
posts.setPostField.bind(posts, id, 'flagId', flagId)
);

if (targetUid) {
await user.incrementUserFlagsBy(targetUid, 1);
batched.push(user.incrementUserFlagsBy.bind(user, targetUid, 1));
}

await posts.setPostField(id, 'flagId', flagId);
} else if (type === 'user') {
await user.setUserField(id, 'flagId', flagId);
batched.push(user.setUserField.bind(user, id, 'flagId', flagId));
}

// Run all the database calls in one single batched call...
await Promise.all(batched.map(async method => await method()));

if (doHistoryAppend) {
await Flags.update(flagId, uid, { state: 'open' });
Flags.update(flagId, uid, { state: 'open' });
}

return await Flags.get(flagId);
Expand Down

0 comments on commit a099892

Please sign in to comment.