mirror of
https://gitcode.com/gh_mirrors/hy/Hydro.git
synced 2026-10-17 09:36:08 +08:00
core: discussion: sort by creation time
This commit is contained in:
1 parent
00ac1c957a
commit
0cbb51cc19
3 files changed
+22
-48
No files matched your search
@@ -1,5 +1,4 @@
|
||||
import { omit } from 'lodash';
|
||||
import moment from 'moment-timezone';
|
||||
import { FilterQuery, ObjectID } from 'mongodb';
|
||||
import { Context } from '../context';
|
||||
import { DiscussionNodeNotFoundError, DocumentNotFoundError } from '../error';
|
||||
@@ -13,7 +12,6 @@ import { buildProjection } from '../utils';
|
||||
import * as contest from './contest';
|
||||
import * as document from './document';
|
||||
import problem from './problem';
|
||||
import ScheduleModel from './schedule';
|
||||
import * as training from './training';
|
||||
|
||||
export interface DiscussionDoc extends Document { }
|
||||
@@ -119,7 +117,7 @@ export function count(domainId: string, query: FilterQuery<DiscussionDoc>) {
|
||||
|
||||
export function getMulti(domainId: string, query: FilterQuery<DiscussionDoc> = {}, projection = PROJECTION_LIST) {
|
||||
return document.getMulti(domainId, document.TYPE_DISCUSSION, query)
|
||||
.sort({ pin: -1, sort: -1 })
|
||||
.sort({ pin: -1, docId: -1 })
|
||||
.project(buildProjection(projection));
|
||||
}
|
||||
|
||||
@@ -314,50 +312,21 @@ export async function getListVnodes(domainId: string, ddocs: any, getHidden = fa
|
||||
return res;
|
||||
}
|
||||
|
||||
bus.on('problem/delete', async (domainId, docId) => {
|
||||
const dids = await document.getMulti(
|
||||
domainId, document.TYPE_DISCUSSION,
|
||||
{ parentType: document.TYPE_PROBLEM, parentId: docId },
|
||||
).project({ docId: 1 }).map((ddoc) => ddoc.docId).toArray();
|
||||
const drids = await document.getMulti(
|
||||
domainId, document.TYPE_DISCUSSION_REPLY,
|
||||
{ parentType: document.TYPE_DISCUSSION, parentId: { $in: dids } },
|
||||
).project({ docId: 1 }).map((drdoc) => drdoc.docId).toArray();
|
||||
return await Promise.all([
|
||||
document.deleteMultiStatus(domainId, document.TYPE_DISCUSSION, { docId: { $in: dids } }),
|
||||
document.deleteMulti(domainId, document.TYPE_DISCUSSION, { docId: { $in: dids } }),
|
||||
document.deleteMulti(domainId, document.TYPE_DISCUSSION_REPLY, { docId: { $in: drids } }),
|
||||
]);
|
||||
});
|
||||
|
||||
const t = Math.exp(-0.15);
|
||||
|
||||
async function updateSort() {
|
||||
const cursor = document.coll.find({ docType: document.TYPE_DISCUSSION });
|
||||
// eslint-disable-next-line no-constant-condition
|
||||
while (true) {
|
||||
// eslint-disable-next-line no-await-in-loop
|
||||
const data = await cursor.next();
|
||||
if (!data) return;
|
||||
// eslint-disable-next-line no-await-in-loop
|
||||
const rCount = await getMultiReply(data.domainId, data.docId).count();
|
||||
const sort = ((data.sort || 100) + Math.max(rCount - (data.lastRCount || 0), 0) * 10) * t;
|
||||
// eslint-disable-next-line no-await-in-loop
|
||||
await document.coll.updateOne({ _id: data._id }, { $set: { sort, lastRCount: rCount } });
|
||||
}
|
||||
}
|
||||
|
||||
export function apply(ctx: Context) {
|
||||
ctx.using(['worker'], async ({ worker }) => {
|
||||
worker.addHandler('discussion.sort', updateSort);
|
||||
if (!await ScheduleModel.count({ type: 'schedule', subType: 'discussion.sort' })) {
|
||||
await ScheduleModel.add({
|
||||
type: 'schedule',
|
||||
subType: 'discussion.sort',
|
||||
executeAfter: moment().minute(0).second(0).millisecond(0).toDate(),
|
||||
interval: [1, 'hour'],
|
||||
});
|
||||
}
|
||||
ctx.on('problem/delete', async (domainId, docId) => {
|
||||
const dids = await document.getMulti(
|
||||
domainId, document.TYPE_DISCUSSION,
|
||||
{ parentType: document.TYPE_PROBLEM, parentId: docId },
|
||||
).project({ docId: 1 }).map((ddoc) => ddoc.docId).toArray();
|
||||
const drids = await document.getMulti(
|
||||
domainId, document.TYPE_DISCUSSION_REPLY,
|
||||
{ parentType: document.TYPE_DISCUSSION, parentId: { $in: dids } },
|
||||
).project({ docId: 1 }).map((drdoc) => drdoc.docId).toArray();
|
||||
return await Promise.all([
|
||||
document.deleteMultiStatus(domainId, document.TYPE_DISCUSSION, { docId: { $in: dids } }),
|
||||
document.deleteMulti(domainId, document.TYPE_DISCUSSION, { docId: { $in: dids } }),
|
||||
document.deleteMulti(domainId, document.TYPE_DISCUSSION_REPLY, { docId: { $in: drids } }),
|
||||
]);
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
@@ -433,8 +433,8 @@ export async function apply(ctx: Context) {
|
||||
// For problem solution
|
||||
{ key: { domainId: 1, docType: 1, parentType: 1, parentId: 1, vote: -1, docId: -1 }, name: 'solution', sparse: true },
|
||||
// For discussion
|
||||
{ key: { domainId: 1, docType: 1, pin: -1, sort: -1, docId: -1 }, name: 'discussionSort', sparse: true },
|
||||
{ key: { domainId: 1, docType: 1, parentType: 1, parentId: 1, pin: -1, sort: -1, docId: -1 }, name: 'discussionNodeSort', sparse: true },
|
||||
{ key: { domainId: 1, docType: 1, pin: -1, docId: -1 }, name: 'discussionSort', sparse: true },
|
||||
{ key: { domainId: 1, docType: 1, parentType: 1, parentId: 1, pin: -1, docId: -1 }, name: 'discussionNodeSort', sparse: true },
|
||||
// Hidden doc
|
||||
{ key: { domainId: 1, docType: 1, hidden: 1, docId: -1 }, name: 'hiddenDoc', sparse: true },
|
||||
// For contest
|
||||
|
||||
@@ -560,6 +560,11 @@ const scripts: UpgradeScript[] = [
|
||||
await db.collection('oplog').updateMany({}, { $unset: { 'args.verifyPassword': '' } });
|
||||
return true;
|
||||
},
|
||||
async function _73_74() {
|
||||
await db.collection('document').updateMany({ docType: document.TYPE_DISCUSSION }, { $unset: { sort: '' } });
|
||||
await ScheduleModel.deleteMany({ subType: 'discussion.sort' });
|
||||
return true;
|
||||
},
|
||||
];
|
||||
|
||||
export default scripts;
|
||||
Reference in new issue
Block a user