forked from UCF-CEN-5016/NodeBB-UCF
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
886c682
commit 6e9e4ee
Showing
2 changed files
with
203 additions
and
75 deletions.
There are no files selected for viewing
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 |
---|---|---|
@@ -1,82 +1,89 @@ | ||
'use strict'; | ||
|
||
const plugins = require('../plugins'); | ||
const posts = require('../posts'); | ||
|
||
"use strict"; | ||
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { | ||
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } | ||
return new (P || (P = Promise))(function (resolve, reject) { | ||
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } | ||
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } | ||
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } | ||
step((generator = generator.apply(thisArg, _arguments || [])).next()); | ||
}); | ||
}; | ||
var __importDefault = (this && this.__importDefault) || function (mod) { | ||
return (mod && mod.__esModule) ? mod : { "default": mod }; | ||
}; | ||
const plugins_1 = __importDefault(require("../plugins")); | ||
const posts_1 = __importDefault(require("../posts")); | ||
module.exports = function (Topics) { | ||
Topics.merge = async function (tids, uid, options) { | ||
options = options || {}; | ||
|
||
const topicsData = await Topics.getTopicsFields(tids, ['scheduled']); | ||
if (topicsData.some(t => t.scheduled)) { | ||
throw new Error('[[error:cant-merge-scheduled]]'); | ||
} | ||
|
||
const oldestTid = findOldestTopic(tids); | ||
let mergeIntoTid = oldestTid; | ||
if (options.mainTid) { | ||
mergeIntoTid = options.mainTid; | ||
} else if (options.newTopicTitle) { | ||
mergeIntoTid = await createNewTopic(options.newTopicTitle, oldestTid); | ||
} | ||
|
||
const otherTids = tids.sort((a, b) => a - b) | ||
.filter(tid => tid && parseInt(tid, 10) !== parseInt(mergeIntoTid, 10)); | ||
|
||
for (const tid of otherTids) { | ||
/* eslint-disable no-await-in-loop */ | ||
const pids = await Topics.getPids(tid); | ||
for (const pid of pids) { | ||
await Topics.movePostToTopic(uid, pid, mergeIntoTid); | ||
Topics.merge = function (tids, uid, options) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
function createNewTopic(title, oldestTid) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
const topicData = yield Topics.getTopicFields(oldestTid, ['uid', 'cid']); | ||
const params = { | ||
uid: topicData.uid, | ||
cid: topicData.cid, | ||
title: title, | ||
}; | ||
const result = yield plugins_1.default.hooks.fire('filter:topic.mergeCreateNewTopic', { | ||
oldestTid: oldestTid, | ||
params: params, | ||
}); | ||
const tid = yield Topics.create(result.params); | ||
return tid; | ||
}); | ||
} | ||
function updateViewCount(mergeIntoTid, tids) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
const topicData = yield Topics.getTopicsFields(tids, ['viewcount']); | ||
const totalViewCount = topicData.reduce((count, topic) => count + topic.viewcount, 0); | ||
yield Topics.setTopicField(mergeIntoTid, 'viewcount', totalViewCount); | ||
}); | ||
} | ||
function findOldestTopic(tids) { | ||
return Math.min.apply(null, tids); | ||
} | ||
options = options || {}; | ||
const topicsData = yield Topics.getTopicsFields(tids, ['scheduled']); | ||
if (topicsData.some(t => t.scheduled)) { | ||
throw new Error('[[error:cant-merge-scheduled]]'); | ||
} | ||
|
||
await Topics.setTopicField(tid, 'mainPid', 0); | ||
await Topics.delete(tid, uid); | ||
await Topics.setTopicFields(tid, { | ||
const oldestTid = findOldestTopic(tids); | ||
let mergeIntoTid = oldestTid; | ||
if (options.mainTid) { | ||
mergeIntoTid = options.mainTid; | ||
} | ||
else if (options.newTopicTitle) { | ||
mergeIntoTid = yield createNewTopic(options.newTopicTitle, oldestTid); | ||
} | ||
const otherTids = tids.sort((a, b) => a - b) | ||
.filter(tid => tid && tid !== mergeIntoTid); | ||
for (const tid of otherTids) { | ||
/* eslint-disable no-await-in-loop */ | ||
const pids = yield Topics.getPids(tid); | ||
for (const pid of pids) { | ||
yield Topics.movePostToTopic(uid, pid, mergeIntoTid); | ||
} | ||
yield Topics.setTopicField(tid, 'mainPid', 0); | ||
yield Topics.delete(tid, uid); | ||
yield Topics.setTopicFields(tid, { | ||
mergeIntoTid: mergeIntoTid, | ||
mergerUid: uid, | ||
mergedTimestamp: Date.now(), | ||
}); | ||
} | ||
yield Promise.all([ | ||
// The next line calls a function in a module that has not been updated to TS yet | ||
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-call | ||
posts_1.default.updateQueuedPostsTopic(mergeIntoTid, otherTids), | ||
updateViewCount(mergeIntoTid, tids), | ||
]); | ||
yield plugins_1.default.hooks.fire('action:topic.merge', { | ||
uid: uid, | ||
tids: tids, | ||
mergeIntoTid: mergeIntoTid, | ||
mergerUid: uid, | ||
mergedTimestamp: Date.now(), | ||
otherTids: otherTids, | ||
}); | ||
} | ||
|
||
await Promise.all([ | ||
posts.updateQueuedPostsTopic(mergeIntoTid, otherTids), | ||
updateViewCount(mergeIntoTid, tids), | ||
]); | ||
|
||
plugins.hooks.fire('action:topic.merge', { | ||
uid: uid, | ||
tids: tids, | ||
mergeIntoTid: mergeIntoTid, | ||
otherTids: otherTids, | ||
return mergeIntoTid; | ||
}); | ||
return mergeIntoTid; | ||
}; | ||
|
||
async function createNewTopic(title, oldestTid) { | ||
const topicData = await Topics.getTopicFields(oldestTid, ['uid', 'cid']); | ||
const params = { | ||
uid: topicData.uid, | ||
cid: topicData.cid, | ||
title: title, | ||
}; | ||
const result = await plugins.hooks.fire('filter:topic.mergeCreateNewTopic', { | ||
oldestTid: oldestTid, | ||
params: params, | ||
}); | ||
const tid = await Topics.create(result.params); | ||
return tid; | ||
} | ||
|
||
async function updateViewCount(mergeIntoTid, tids) { | ||
const topicData = await Topics.getTopicsFields(tids, ['viewcount']); | ||
const totalViewCount = topicData.reduce( | ||
(count, topic) => count + parseInt(topic.viewcount, 10), 0 | ||
); | ||
await Topics.setTopicField(mergeIntoTid, 'viewcount', totalViewCount); | ||
} | ||
|
||
function findOldestTopic(tids) { | ||
return Math.min.apply(null, tids); | ||
} | ||
}; |
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,121 @@ | ||
import plugins from '../plugins'; | ||
import posts from '../posts'; | ||
|
||
type hooksFire = { | ||
params : {uid : number, cid : number, title : string} | ||
} | ||
|
||
interface myOptionsInterface { | ||
mainTid? : number; | ||
newTopicTitle? : string; | ||
} | ||
|
||
interface myTopicDataInterface { | ||
scheduled : string; | ||
uid : number; | ||
cid : number; | ||
viewcount : number; | ||
} | ||
|
||
interface myTopicFieldsInterface { | ||
mergeIntoTid : number; | ||
mergerUid : number; | ||
mergedTimestamp : number; | ||
} | ||
|
||
interface myTopicsInterface { | ||
merge : (tids : number[], uid : number, options : myOptionsInterface) => Promise<number>; | ||
delete : (tid: number, uid : number) => Promise<void>; | ||
create : (obj : {uid : number, cid : number, title : string}) => Promise<number> | ||
movePostToTopic : (uid : number, pid : number, mergeIntoTid : number) => Promise<void>; | ||
|
||
getPids : (tid : number) => Promise<number[]>; | ||
getTopicFields : (tid : number, something : string[]) => Promise<myTopicDataInterface>; | ||
getTopicsFields : (tids : number[], something : string[]) => Promise<myTopicDataInterface[]>; | ||
|
||
setTopicField : (tid : number, x : string, y : number) => Promise<void>; | ||
setTopicFields : (tid : number, field : myTopicFieldsInterface) => Promise<void>; | ||
} | ||
|
||
|
||
export = function (Topics : myTopicsInterface) { | ||
Topics.merge = async function (tids:number[], uid:number, options : myOptionsInterface) { | ||
async function createNewTopic(title : string, oldestTid : number) { | ||
const topicData = await Topics.getTopicFields(oldestTid, ['uid', 'cid']); | ||
const params = { | ||
uid: topicData.uid, | ||
cid: topicData.cid, | ||
title: title, | ||
}; | ||
const result : hooksFire = await plugins.hooks.fire('filter:topic.mergeCreateNewTopic', { | ||
oldestTid: oldestTid, | ||
params: params, | ||
}) as hooksFire; | ||
|
||
const tid = await Topics.create(result.params); | ||
return tid; | ||
} | ||
|
||
async function updateViewCount(mergeIntoTid : number, tids : number[]) { | ||
const topicData = await Topics.getTopicsFields(tids, ['viewcount']); | ||
const totalViewCount = topicData.reduce( | ||
(count, topic) => count + topic.viewcount, 0 | ||
); | ||
await Topics.setTopicField(mergeIntoTid, 'viewcount', totalViewCount); | ||
} | ||
|
||
function findOldestTopic(tids : number[]) : number { | ||
return Math.min.apply(null, tids) as number; | ||
} | ||
|
||
options = options || {}; | ||
|
||
const topicsData = await Topics.getTopicsFields(tids, ['scheduled']); | ||
if (topicsData.some(t => t.scheduled)) { | ||
throw new Error('[[error:cant-merge-scheduled]]'); | ||
} | ||
|
||
const oldestTid = findOldestTopic(tids); | ||
let mergeIntoTid = oldestTid; | ||
if (options.mainTid) { | ||
mergeIntoTid = options.mainTid; | ||
} else if (options.newTopicTitle) { | ||
mergeIntoTid = await createNewTopic(options.newTopicTitle, oldestTid); | ||
} | ||
|
||
const otherTids = tids.sort((a, b) => a - b) | ||
.filter(tid => tid && tid !== mergeIntoTid); | ||
|
||
for (const tid of otherTids) { | ||
/* eslint-disable no-await-in-loop */ | ||
const pids = await Topics.getPids(tid); | ||
for (const pid of pids) { | ||
await Topics.movePostToTopic(uid, pid, mergeIntoTid); | ||
} | ||
|
||
await Topics.setTopicField(tid, 'mainPid', 0); | ||
await Topics.delete(tid, uid); | ||
await Topics.setTopicFields(tid, { | ||
mergeIntoTid: mergeIntoTid, | ||
mergerUid: uid, | ||
mergedTimestamp: Date.now(), | ||
}); | ||
} | ||
|
||
await Promise.all([ | ||
// The next line calls a function in a module that has not been updated to TS yet | ||
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-call | ||
posts.updateQueuedPostsTopic(mergeIntoTid, otherTids), | ||
updateViewCount(mergeIntoTid, tids), | ||
]); | ||
|
||
|
||
await plugins.hooks.fire('action:topic.merge', { | ||
uid: uid, | ||
tids: tids, | ||
mergeIntoTid: mergeIntoTid, | ||
otherTids: otherTids, | ||
}); | ||
return mergeIntoTid; | ||
}; | ||
}; |