@ -11,44 +11,40 @@ const batch = require('../batch');
module . exports = function ( Topics ) {
Topics . delete = async function ( tid , uid ) {
await removeTopicPidsFromCid ( tid ) ;
const cid = await Topics . getTopicField ( tid , 'cid' ) ;
await removeTopicPidsFromCid ( tid , cid ) ;
await Topics . setTopicFields ( tid , {
deleted : 1 ,
deleterUid : uid ,
deletedTimestamp : Date . now ( ) ,
} ) ;
await categories . updateRecentTidForCid ( cid ) ;
} ;
async function removeTopicPidsFromCid ( tid ) {
const [ cid , pids ] = await Promise . all ( [
Topics . getTopicField ( tid , 'cid' ) ,
Topics . getPids ( tid ) ,
] ) ;
async function removeTopicPidsFromCid ( tid , cid ) {
const pids = await Topics . getPids ( tid ) ;
await db . sortedSetRemove ( ` cid: ${ cid } :pids ` , pids ) ;
await categories . updateRecentTidForCid ( cid ) ;
}
async function addTopicPidsToCid ( tid ) {
const [ cid , pids ] = await Promise . all ( [
Topics . getTopicField ( tid , 'cid' ) ,
Topics . getPids ( tid ) ,
] ) ;
async function addTopicPidsToCid ( tid , cid ) {
const pids = await Topics . getPids ( tid ) ;
let postData = await posts . getPostsFields ( pids , [ 'pid' , 'timestamp' , 'deleted' ] ) ;
postData = postData . filter ( post => post && ! post . deleted ) ;
const pidsToAdd = postData . map ( post => post . pid ) ;
const scores = postData . map ( post => post . timestamp ) ;
await db . sortedSetAdd ( ` cid: ${ cid } :pids ` , scores , pidsToAdd ) ;
await categories . updateRecentTidForCid ( cid ) ;
}
Topics . restore = async function ( tid ) {
const cid = await Topics . getTopicField ( tid , 'cid' ) ;
await Promise . all ( [
Topics . deleteTopicFields ( tid , [
'deleterUid' , 'deletedTimestamp' ,
] ) ,
addTopicPidsToCid ( tid ),
addTopicPidsToCid ( tid , cid ),
] ) ;
await Topics . setTopicField ( tid , 'deleted' , 0 ) ;
await categories . updateRecentTidForCid ( cid ) ;
} ;
Topics . purgePostsAndTopic = async function ( tid , uid ) {