summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorsyuilo <Syuilotan@yahoo.co.jp>2022-03-26 17:19:27 +0900
committersyuilo <Syuilotan@yahoo.co.jp>2022-03-26 17:19:27 +0900
commitbc2c40a6caaf35afb119cf91cf1adfb03c5fa7eb (patch)
tree44f0e49766946130be7fe2362f9a65781a8b2e32
parentchore(deps): bump minimist from 1.2.5 to 1.2.6 in /packages/backend (#8447) (diff)
downloadmisskey-bc2c40a6caaf35afb119cf91cf1adfb03c5fa7eb.tar.gz
misskey-bc2c40a6caaf35afb119cf91cf1adfb03c5fa7eb.tar.bz2
misskey-bc2c40a6caaf35afb119cf91cf1adfb03c5fa7eb.zip
refactor and performance improvements
-rw-r--r--packages/backend/src/models/repositories/note.ts126
1 files changed, 65 insertions, 61 deletions
diff --git a/packages/backend/src/models/repositories/note.ts b/packages/backend/src/models/repositories/note.ts
index 56ef13066f..cf5fcb1787 100644
--- a/packages/backend/src/models/repositories/note.ts
+++ b/packages/backend/src/models/repositories/note.ts
@@ -71,6 +71,69 @@ async function hideNote(packedNote: Packed<'Note'>, meId: User['id'] | null) {
}
}
+async function populatePoll(note: Note, meId: User['id'] | null) {
+ const poll = await Polls.findOneByOrFail({ noteId: note.id });
+ const choices = poll.choices.map(c => ({
+ text: c,
+ votes: poll.votes[poll.choices.indexOf(c)],
+ isVoted: false,
+ }));
+
+ if (meId) {
+ if (poll.multiple) {
+ const votes = await PollVotes.findBy({
+ userId: meId,
+ noteId: note.id,
+ });
+
+ const myChoices = votes.map(v => v.choice);
+ for (const myChoice of myChoices) {
+ choices[myChoice].isVoted = true;
+ }
+ } else {
+ const vote = await PollVotes.findOneBy({
+ userId: meId,
+ noteId: note.id,
+ });
+
+ if (vote) {
+ choices[vote.choice].isVoted = true;
+ }
+ }
+ }
+
+ return {
+ multiple: poll.multiple,
+ expiresAt: poll.expiresAt,
+ choices,
+ };
+}
+
+async function populateMyReaction(note: Note, meId: User['id'], _hint_?: {
+ myReactions: Map<Note['id'], NoteReaction | null>;
+}) {
+ if (_hint_?.myReactions) {
+ const reaction = _hint_.myReactions.get(note.id);
+ if (reaction) {
+ return convertLegacyReaction(reaction.reaction);
+ } else if (reaction === null) {
+ return undefined;
+ }
+ // 実装上抜けがあるだけかもしれないので、「ヒントに含まれてなかったら(=undefinedなら)return」のようにはしない
+ }
+
+ const reaction = await NoteReactions.findOneBy({
+ userId: meId,
+ noteId: note.id,
+ });
+
+ if (reaction) {
+ return convertLegacyReaction(reaction.reaction);
+ }
+
+ return undefined;
+}
+
export const NoteRepository = db.getRepository(Note).extend({
async isVisibleForMe(note: Note, meId: User['id'] | null): Promise<boolean> {
// visibility が specified かつ自分が指定されていなかったら非表示
@@ -141,65 +204,6 @@ export const NoteRepository = db.getRepository(Note).extend({
const note = typeof src === 'object' ? src : await this.findOneByOrFail({ id: src });
const host = note.userHost;
- async function populatePoll() {
- const poll = await Polls.findOneByOrFail({ noteId: note.id });
- const choices = poll.choices.map(c => ({
- text: c,
- votes: poll.votes[poll.choices.indexOf(c)],
- isVoted: false,
- }));
-
- if (poll.multiple) {
- const votes = await PollVotes.findBy({
- userId: meId!,
- noteId: note.id,
- });
-
- const myChoices = votes.map(v => v.choice);
- for (const myChoice of myChoices) {
- choices[myChoice].isVoted = true;
- }
- } else {
- const vote = await PollVotes.findOneBy({
- userId: meId!,
- noteId: note.id,
- });
-
- if (vote) {
- choices[vote.choice].isVoted = true;
- }
- }
-
- return {
- multiple: poll.multiple,
- expiresAt: poll.expiresAt,
- choices,
- };
- }
-
- async function populateMyReaction() {
- if (options?._hint_?.myReactions) {
- const reaction = options._hint_.myReactions.get(note.id);
- if (reaction) {
- return convertLegacyReaction(reaction.reaction);
- } else if (reaction === null) {
- return undefined;
- }
- // 実装上抜けがあるだけかもしれないので、「ヒントに含まれてなかったら(=undefinedなら)return」のようにはしない
- }
-
- const reaction = await NoteReactions.findOneBy({
- userId: meId!,
- noteId: note.id,
- });
-
- if (reaction) {
- return convertLegacyReaction(reaction.reaction);
- }
-
- return undefined;
- }
-
let text = note.text;
if (note.name && (note.url ?? note.uri)) {
@@ -255,10 +259,10 @@ export const NoteRepository = db.getRepository(Note).extend({
_hint_: options?._hint_,
}) : undefined,
- poll: note.hasPoll ? populatePoll() : undefined,
+ poll: note.hasPoll ? populatePoll(note, meId) : undefined,
...(meId ? {
- myReaction: populateMyReaction(),
+ myReaction: populateMyReaction(note, meId, options?._hint_),
} : {}),
} : {}),
});