mirror of
https://iceshrimp.dev/limepotato/jormungandr-bite.git
synced 2024-11-22 01:47:39 -07:00
[backend] Reset poll votes when choices change on note edit
This commit is contained in:
parent
8b78709378
commit
35c75bbebf
2 changed files with 73 additions and 50 deletions
|
@ -25,6 +25,7 @@ import {
|
||||||
Notes,
|
Notes,
|
||||||
NoteEdits,
|
NoteEdits,
|
||||||
DriveFiles,
|
DriveFiles,
|
||||||
|
PollVotes,
|
||||||
} from "@/models/index.js";
|
} from "@/models/index.js";
|
||||||
import type { IMentionedRemoteUsers, Note } from "@/models/entities/note.js";
|
import type { IMentionedRemoteUsers, Note } from "@/models/entities/note.js";
|
||||||
import type { IObject, IPost } from "../type.js";
|
import type { IObject, IPost } from "../type.js";
|
||||||
|
@ -710,30 +711,40 @@ export async function updateNote(value: string | IObject, resolver?: Resolver) {
|
||||||
userHost: actor.host,
|
userHost: actor.host,
|
||||||
});
|
});
|
||||||
updating = true;
|
updating = true;
|
||||||
} else if (
|
|
||||||
dbPoll.multiple !== poll.multiple ||
|
|
||||||
dbPoll.expiresAt !== poll.expiresAt ||
|
|
||||||
dbPoll.noteVisibility !== note.visibility ||
|
|
||||||
JSON.stringify(dbPoll.choices) !== JSON.stringify(poll.choices)
|
|
||||||
) {
|
|
||||||
await Polls.update(
|
|
||||||
{ noteId: note.id },
|
|
||||||
{
|
|
||||||
choices: poll?.choices,
|
|
||||||
multiple: poll?.multiple,
|
|
||||||
votes: poll?.votes,
|
|
||||||
expiresAt: poll?.expiresAt,
|
|
||||||
noteVisibility:
|
|
||||||
note.visibility === "hidden" ? "home" : note.visibility,
|
|
||||||
},
|
|
||||||
);
|
|
||||||
updating = true;
|
|
||||||
} else {
|
} else {
|
||||||
for (let i = 0; i < poll.choices.length; i++) {
|
const choicesChanged = JSON.stringify(dbPoll.choices) !== JSON.stringify(poll.choices);
|
||||||
if (dbPoll.votes[i] !== poll.votes?.[i]) {
|
|
||||||
await Polls.update({ noteId: note.id }, { votes: poll?.votes });
|
if (
|
||||||
updating = true;
|
dbPoll.multiple !== poll.multiple ||
|
||||||
break;
|
dbPoll.expiresAt !== poll.expiresAt ||
|
||||||
|
dbPoll.noteVisibility !== note.visibility ||
|
||||||
|
choicesChanged
|
||||||
|
) {
|
||||||
|
await Polls.update(
|
||||||
|
{ noteId: note.id },
|
||||||
|
{
|
||||||
|
choices: poll?.choices,
|
||||||
|
multiple: poll?.multiple,
|
||||||
|
votes: poll?.votes,
|
||||||
|
expiresAt: poll?.expiresAt,
|
||||||
|
noteVisibility:
|
||||||
|
note.visibility === "hidden" ? "home" : note.visibility,
|
||||||
|
},
|
||||||
|
);
|
||||||
|
|
||||||
|
// Reset votes
|
||||||
|
if (choicesChanged) {
|
||||||
|
await PollVotes.delete({ noteId: dbPoll.noteId });
|
||||||
|
}
|
||||||
|
|
||||||
|
updating = true;
|
||||||
|
} else {
|
||||||
|
for (let i = 0; i < poll.choices.length; i++) {
|
||||||
|
if (dbPoll.votes[i] !== poll.votes?.[i]) {
|
||||||
|
await Polls.update({ noteId: note.id }, { votes: poll?.votes });
|
||||||
|
updating = true;
|
||||||
|
break;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -10,10 +10,12 @@ import { extractHashtags } from "@/misc/extract-hashtags.js";
|
||||||
import type { IMentionedRemoteUsers } from "@/models/entities/note.js";
|
import type { IMentionedRemoteUsers } from "@/models/entities/note.js";
|
||||||
import { Note } from "@/models/entities/note.js";
|
import { Note } from "@/models/entities/note.js";
|
||||||
import {
|
import {
|
||||||
Users,
|
Users,
|
||||||
Notes,
|
Notes,
|
||||||
UserProfiles,
|
UserProfiles,
|
||||||
Polls, NoteEdits,
|
Polls,
|
||||||
|
NoteEdits,
|
||||||
|
PollVotes,
|
||||||
} from "@/models/index.js";
|
} from "@/models/index.js";
|
||||||
import type { DriveFile } from "@/models/entities/drive-file.js";
|
import type { DriveFile } from "@/models/entities/drive-file.js";
|
||||||
import { In } from "typeorm";
|
import { In } from "typeorm";
|
||||||
|
@ -121,30 +123,40 @@ export default async function (
|
||||||
userHost: user.host,
|
userHost: user.host,
|
||||||
});
|
});
|
||||||
publishing = true;
|
publishing = true;
|
||||||
} else if (
|
|
||||||
dbPoll.multiple !== data.poll.multiple ||
|
|
||||||
dbPoll.expiresAt !== data.poll.expiresAt ||
|
|
||||||
dbPoll.noteVisibility !== note.visibility ||
|
|
||||||
JSON.stringify(dbPoll.choices) !== JSON.stringify(data.poll.choices)
|
|
||||||
) {
|
|
||||||
await Polls.update(
|
|
||||||
{ noteId: note.id },
|
|
||||||
{
|
|
||||||
choices: data.poll?.choices,
|
|
||||||
multiple: data.poll?.multiple,
|
|
||||||
votes: data.poll?.votes,
|
|
||||||
expiresAt: data.poll?.expiresAt,
|
|
||||||
noteVisibility:
|
|
||||||
note.visibility === "hidden" ? "home" : note.visibility,
|
|
||||||
},
|
|
||||||
);
|
|
||||||
publishing = true;
|
|
||||||
} else {
|
} else {
|
||||||
for (let i = 0; i < data.poll.choices.length; i++) {
|
const choicesChanged = JSON.stringify(dbPoll.choices) !== JSON.stringify(data.poll.choices);
|
||||||
if (dbPoll.votes[i] !== data.poll.votes?.[i]) {
|
|
||||||
await Polls.update({ noteId: note.id }, { votes: data.poll?.votes });
|
if (
|
||||||
publishing = true;
|
dbPoll.multiple !== data.poll.multiple ||
|
||||||
break;
|
dbPoll.expiresAt !== data.poll.expiresAt ||
|
||||||
|
dbPoll.noteVisibility !== note.visibility ||
|
||||||
|
choicesChanged
|
||||||
|
) {
|
||||||
|
await Polls.update(
|
||||||
|
{ noteId: note.id },
|
||||||
|
{
|
||||||
|
choices: data.poll?.choices,
|
||||||
|
multiple: data.poll?.multiple,
|
||||||
|
votes: choicesChanged ? new Array(data.poll.choices.length).fill(0) : data.poll?.votes,
|
||||||
|
expiresAt: data.poll?.expiresAt,
|
||||||
|
noteVisibility:
|
||||||
|
note.visibility === "hidden" ? "home" : note.visibility,
|
||||||
|
},
|
||||||
|
);
|
||||||
|
|
||||||
|
// Reset votes
|
||||||
|
if (JSON.stringify(dbPoll.choices) !== JSON.stringify(data.poll.choices)) {
|
||||||
|
await PollVotes.delete({ noteId: dbPoll.noteId });
|
||||||
|
}
|
||||||
|
|
||||||
|
publishing = true;
|
||||||
|
} else {
|
||||||
|
for (let i = 0; i < data.poll.choices.length; i++) {
|
||||||
|
if (dbPoll.votes[i] !== data.poll.votes?.[i]) {
|
||||||
|
await Polls.update({ noteId: note.id }, { votes: data.poll?.votes });
|
||||||
|
publishing = true;
|
||||||
|
break;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue