mirror of
https://gitcode.com/gh_mirrors/hy/Hydro.git
synced 2026-10-19 17:38:19 +08:00
core: fix problem solution vote race condition (#861)
This commit is contained in:
1 parent
598972b1c9
commit
8903486c6c
6 files changed
+54
-51
No files matched your search
@@ -70,12 +70,18 @@ class SolutionModel {
|
||||
}
|
||||
|
||||
static async vote(domainId: string, psid: ObjectId, uid: number, value: number) {
|
||||
let pssdoc = await document.getStatus(domainId, document.TYPE_PROBLEM_SOLUTION, psid, uid);
|
||||
await document.setStatus(domainId, document.TYPE_PROBLEM_SOLUTION, psid, uid, { vote: value });
|
||||
if (pssdoc) value += -pssdoc.vote;
|
||||
const psdoc = await document.inc(domainId, document.TYPE_PROBLEM_SOLUTION, psid, 'vote', value);
|
||||
pssdoc = await document.getStatus(domainId, document.TYPE_PROBLEM_SOLUTION, psid, uid);
|
||||
return [psdoc, pssdoc];
|
||||
const before = await document.setStatus(
|
||||
domainId, document.TYPE_PROBLEM_SOLUTION, psid, uid,
|
||||
{ vote: value }, 'before',
|
||||
);
|
||||
let inc = value;
|
||||
if (before?.vote) inc -= before.vote;
|
||||
return [
|
||||
inc
|
||||
? await document.inc(domainId, document.TYPE_PROBLEM_SOLUTION, psid, 'vote', inc)
|
||||
: await document.get(domainId, document.TYPE_PROBLEM_SOLUTION, psid),
|
||||
await document.getStatus(domainId, document.TYPE_PROBLEM_SOLUTION, psid, uid),
|
||||
];
|
||||
}
|
||||
|
||||
static async getListStatus(domainId: string, psids: ObjectId[], uid: number) {
|
||||
|
||||
Reference in new issue
Block a user