-
Notifications
You must be signed in to change notification settings - Fork 74
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactored code in the add_target_uid_to_flags code
- Loading branch information
1 parent
206ca56
commit 8b19e96
Showing
2 changed files
with
50 additions
and
37 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
'use strict'; | ||
|
||
const db = require('../../database'); | ||
const batch = require('../../batch'); | ||
const posts = require('../../posts'); | ||
|
||
async function handlePostFlag(flagObj) { | ||
const targetUid = await posts.getPostField(flagObj.targetId, 'uid'); | ||
if (targetUid) { | ||
await db.setObjectField(`flag:${flagObj.flagId}`, 'targetUid', targetUid); | ||
} | ||
} | ||
|
||
async function handleUserFlag(flagObj) { | ||
await db.setObjectField(`flag:${flagObj.flagId}`, 'targetUid', flagObj.targetId); | ||
} | ||
|
||
async function processFlagObject(flagObj) { | ||
if (!flagObj || !flagObj.targetId) { | ||
return; | ||
} | ||
|
||
console.log('Processing Flag Object by Ismael:', flagObj); | ||
|
||
if (flagObj.type === 'post') { | ||
await handlePostFlag(flagObj); | ||
} else if (flagObj.type === 'user') { | ||
await handleUserFlag(flagObj); | ||
} | ||
} | ||
|
||
module.exports = { | ||
name: 'Add target uid to flag objects', | ||
timestamp: Date.UTC(2020, 7, 22), | ||
method: async function () { | ||
const { progress } = this; | ||
|
||
await batch.processSortedSet('flags:datetime', async (flagIds) => { | ||
progress.incr(flagIds.length); | ||
const flagData = await db.getObjects(flagIds.map(id => `flag:${id}`)); | ||
for (const flagObj of flagData) { | ||
/* eslint-disable no-await-in-loop */ | ||
await processFlagObject(flagObj); | ||
} | ||
}, { | ||
progress: progress, | ||
batch: 500, | ||
}); | ||
}, | ||
}; |