From bc3ae901cc25fed47b85a9c3b88b13aa4ac0442d Mon Sep 17 00:00:00 2001 From: Johann150 Date: Mon, 27 Jun 2022 14:48:10 +0200 Subject: [PATCH] refactor: remove duplicate code (#8895) --- .../backend/src/misc/is-blocker-user-related.ts | 15 --------------- .../backend/src/misc/is-muted-user-related.ts | 15 --------------- packages/backend/src/misc/is-user-related.ts | 15 +++++++++++++++ .../src/server/api/stream/channels/antenna.ts | 7 +++---- .../src/server/api/stream/channels/channel.ts | 7 +++---- .../server/api/stream/channels/global-timeline.ts | 7 +++---- .../src/server/api/stream/channels/hashtag.ts | 7 +++---- .../server/api/stream/channels/home-timeline.ts | 7 +++---- .../server/api/stream/channels/hybrid-timeline.ts | 7 +++---- .../server/api/stream/channels/local-timeline.ts | 7 +++---- .../src/server/api/stream/channels/user-list.ts | 7 +++---- .../backend/src/services/add-note-to-antenna.ts | 4 ++-- 12 files changed, 41 insertions(+), 64 deletions(-) delete mode 100644 packages/backend/src/misc/is-blocker-user-related.ts delete mode 100644 packages/backend/src/misc/is-muted-user-related.ts create mode 100644 packages/backend/src/misc/is-user-related.ts diff --git a/packages/backend/src/misc/is-blocker-user-related.ts b/packages/backend/src/misc/is-blocker-user-related.ts deleted file mode 100644 index 8c0ebfad9..000000000 --- a/packages/backend/src/misc/is-blocker-user-related.ts +++ /dev/null @@ -1,15 +0,0 @@ -export function isBlockerUserRelated(note: any, blockerUserIds: Set): boolean { - if (blockerUserIds.has(note.userId)) { - return true; - } - - if (note.reply != null && blockerUserIds.has(note.reply.userId)) { - return true; - } - - if (note.renote != null && blockerUserIds.has(note.renote.userId)) { - return true; - } - - return false; -} diff --git a/packages/backend/src/misc/is-muted-user-related.ts b/packages/backend/src/misc/is-muted-user-related.ts deleted file mode 100644 index 2caa743f9..000000000 --- a/packages/backend/src/misc/is-muted-user-related.ts +++ /dev/null @@ -1,15 +0,0 @@ -export function isMutedUserRelated(note: any, mutedUserIds: Set): boolean { - if (mutedUserIds.has(note.userId)) { - return true; - } - - if (note.reply != null && mutedUserIds.has(note.reply.userId)) { - return true; - } - - if (note.renote != null && mutedUserIds.has(note.renote.userId)) { - return true; - } - - return false; -} diff --git a/packages/backend/src/misc/is-user-related.ts b/packages/backend/src/misc/is-user-related.ts new file mode 100644 index 000000000..e6bbdb5d3 --- /dev/null +++ b/packages/backend/src/misc/is-user-related.ts @@ -0,0 +1,15 @@ +export function isUserRelated(note: any, userIds: Set): boolean { + if (userIds.has(note.userId)) { + return true; + } + + if (note.reply != null && userIds.has(note.reply.userId)) { + return true; + } + + if (note.renote != null && userIds.has(note.renote.userId)) { + return true; + } + + return false; +} diff --git a/packages/backend/src/server/api/stream/channels/antenna.ts b/packages/backend/src/server/api/stream/channels/antenna.ts index afd14946e..d28320d92 100644 --- a/packages/backend/src/server/api/stream/channels/antenna.ts +++ b/packages/backend/src/server/api/stream/channels/antenna.ts @@ -1,7 +1,6 @@ import Channel from '../channel.js'; import { Notes } from '@/models/index.js'; -import { isMutedUserRelated } from '@/misc/is-muted-user-related.js'; -import { isBlockerUserRelated } from '@/misc/is-blocker-user-related.js'; +import { isUserRelated } from '@/misc/is-user-related.js'; import { StreamMessages } from '../types.js'; export default class extends Channel { @@ -27,9 +26,9 @@ export default class extends Channel { const note = await Notes.pack(data.body.id, this.user, { detail: true }); // 流れてきたNoteがミュートしているユーザーが関わるものだったら無視する - if (isMutedUserRelated(note, this.muting)) return; + if (isUserRelated(note, this.muting)) return; // 流れてきたNoteがブロックされているユーザーが関わるものだったら無視する - if (isBlockerUserRelated(note, this.blocking)) return; + if (isUserRelated(note, this.blocking)) return; this.connection.cacheNote(note); diff --git a/packages/backend/src/server/api/stream/channels/channel.ts b/packages/backend/src/server/api/stream/channels/channel.ts index 16ad80939..5148cfd05 100644 --- a/packages/backend/src/server/api/stream/channels/channel.ts +++ b/packages/backend/src/server/api/stream/channels/channel.ts @@ -1,7 +1,6 @@ import Channel from '../channel.js'; import { Notes, Users } from '@/models/index.js'; -import { isMutedUserRelated } from '@/misc/is-muted-user-related.js'; -import { isBlockerUserRelated } from '@/misc/is-blocker-user-related.js'; +import { isUserRelated } from '@/misc/is-user-related.js'; import { User } from '@/models/entities/user.js'; import { StreamMessages } from '../types.js'; import { Packed } from '@/misc/schema.js'; @@ -45,9 +44,9 @@ export default class extends Channel { } // 流れてきたNoteがミュートしているユーザーが関わるものだったら無視する - if (isMutedUserRelated(note, this.muting)) return; + if (isUserRelated(note, this.muting)) return; // 流れてきたNoteがブロックされているユーザーが関わるものだったら無視する - if (isBlockerUserRelated(note, this.blocking)) return; + if (isUserRelated(note, this.blocking)) return; this.connection.cacheNote(note); diff --git a/packages/backend/src/server/api/stream/channels/global-timeline.ts b/packages/backend/src/server/api/stream/channels/global-timeline.ts index 1c7e038ab..5b4ae850e 100644 --- a/packages/backend/src/server/api/stream/channels/global-timeline.ts +++ b/packages/backend/src/server/api/stream/channels/global-timeline.ts @@ -1,10 +1,9 @@ -import { isMutedUserRelated } from '@/misc/is-muted-user-related.js'; import Channel from '../channel.js'; import { fetchMeta } from '@/misc/fetch-meta.js'; import { Notes } from '@/models/index.js'; import { checkWordMute } from '@/misc/check-word-mute.js'; -import { isBlockerUserRelated } from '@/misc/is-blocker-user-related.js'; import { isInstanceMuted } from '@/misc/is-instance-muted.js'; +import { isUserRelated } from '@/misc/is-user-related.js'; import { Packed } from '@/misc/schema.js'; export default class extends Channel { @@ -55,9 +54,9 @@ export default class extends Channel { if (isInstanceMuted(note, new Set(this.userProfile?.mutedInstances ?? []))) return; // 流れてきたNoteがミュートしているユーザーが関わるものだったら無視する - if (isMutedUserRelated(note, this.muting)) return; + if (isUserRelated(note, this.muting)) return; // 流れてきたNoteがブロックされているユーザーが関わるものだったら無視する - if (isBlockerUserRelated(note, this.blocking)) return; + if (isUserRelated(note, this.blocking)) return; // 流れてきたNoteがミュートすべきNoteだったら無視する // TODO: 将来的には、単にMutedNoteテーブルにレコードがあるかどうかで判定したい(以下の理由により難しそうではある) diff --git a/packages/backend/src/server/api/stream/channels/hashtag.ts b/packages/backend/src/server/api/stream/channels/hashtag.ts index 1b7a58022..741db447e 100644 --- a/packages/backend/src/server/api/stream/channels/hashtag.ts +++ b/packages/backend/src/server/api/stream/channels/hashtag.ts @@ -1,8 +1,7 @@ -import { isMutedUserRelated } from '@/misc/is-muted-user-related.js'; import Channel from '../channel.js'; import { Notes } from '@/models/index.js'; import { normalizeForSearch } from '@/misc/normalize-for-search.js'; -import { isBlockerUserRelated } from '@/misc/is-blocker-user-related.js'; +import { isUserRelated } from '@/misc/is-user-related.js'; import { Packed } from '@/misc/schema.js'; export default class extends Channel { @@ -38,9 +37,9 @@ export default class extends Channel { } // 流れてきたNoteがミュートしているユーザーが関わるものだったら無視する - if (isMutedUserRelated(note, this.muting)) return; + if (isUserRelated(note, this.muting)) return; // 流れてきたNoteがブロックされているユーザーが関わるものだったら無視する - if (isBlockerUserRelated(note, this.blocking)) return; + if (isUserRelated(note, this.blocking)) return; this.connection.cacheNote(note); diff --git a/packages/backend/src/server/api/stream/channels/home-timeline.ts b/packages/backend/src/server/api/stream/channels/home-timeline.ts index 3a8e55202..075a242ef 100644 --- a/packages/backend/src/server/api/stream/channels/home-timeline.ts +++ b/packages/backend/src/server/api/stream/channels/home-timeline.ts @@ -1,8 +1,7 @@ -import { isMutedUserRelated } from '@/misc/is-muted-user-related.js'; import Channel from '../channel.js'; import { Notes } from '@/models/index.js'; import { checkWordMute } from '@/misc/check-word-mute.js'; -import { isBlockerUserRelated } from '@/misc/is-blocker-user-related.js'; +import { isUserRelated } from '@/misc/is-user-related.js'; import { isInstanceMuted } from '@/misc/is-instance-muted.js'; import { Packed } from '@/misc/schema.js'; @@ -63,9 +62,9 @@ export default class extends Channel { } // 流れてきたNoteがミュートしているユーザーが関わるものだったら無視する - if (isMutedUserRelated(note, this.muting)) return; + if (isUserRelated(note, this.muting)) return; // 流れてきたNoteがブロックされているユーザーが関わるものだったら無視する - if (isBlockerUserRelated(note, this.blocking)) return; + if (isUserRelated(note, this.blocking)) return; // 流れてきたNoteがミュートすべきNoteだったら無視する // TODO: 将来的には、単にMutedNoteテーブルにレコードがあるかどうかで判定したい(以下の理由により難しそうではある) diff --git a/packages/backend/src/server/api/stream/channels/hybrid-timeline.ts b/packages/backend/src/server/api/stream/channels/hybrid-timeline.ts index f3ceeffa1..f5dedf77c 100644 --- a/packages/backend/src/server/api/stream/channels/hybrid-timeline.ts +++ b/packages/backend/src/server/api/stream/channels/hybrid-timeline.ts @@ -1,9 +1,8 @@ -import { isMutedUserRelated } from '@/misc/is-muted-user-related.js'; import Channel from '../channel.js'; import { fetchMeta } from '@/misc/fetch-meta.js'; import { Notes } from '@/models/index.js'; import { checkWordMute } from '@/misc/check-word-mute.js'; -import { isBlockerUserRelated } from '@/misc/is-blocker-user-related.js'; +import { isUserRelated } from '@/misc/is-user-related.js'; import { isInstanceMuted } from '@/misc/is-instance-muted.js'; import { Packed } from '@/misc/schema.js'; @@ -71,9 +70,9 @@ export default class extends Channel { } // 流れてきたNoteがミュートしているユーザーが関わるものだったら無視する - if (isMutedUserRelated(note, this.muting)) return; + if (isUserRelated(note, this.muting)) return; // 流れてきたNoteがブロックされているユーザーが関わるものだったら無視する - if (isBlockerUserRelated(note, this.blocking)) return; + if (isUserRelated(note, this.blocking)) return; // 流れてきたNoteがミュートすべきNoteだったら無視する // TODO: 将来的には、単にMutedNoteテーブルにレコードがあるかどうかで判定したい(以下の理由により難しそうではある) diff --git a/packages/backend/src/server/api/stream/channels/local-timeline.ts b/packages/backend/src/server/api/stream/channels/local-timeline.ts index 4e198482a..8bb927987 100644 --- a/packages/backend/src/server/api/stream/channels/local-timeline.ts +++ b/packages/backend/src/server/api/stream/channels/local-timeline.ts @@ -1,9 +1,8 @@ -import { isMutedUserRelated } from '@/misc/is-muted-user-related.js'; import Channel from '../channel.js'; import { fetchMeta } from '@/misc/fetch-meta.js'; import { Notes } from '@/models/index.js'; import { checkWordMute } from '@/misc/check-word-mute.js'; -import { isBlockerUserRelated } from '@/misc/is-blocker-user-related.js'; +import { isUserRelated } from '@/misc/is-user-related.js'; import { Packed } from '@/misc/schema.js'; export default class extends Channel { @@ -52,9 +51,9 @@ export default class extends Channel { } // 流れてきたNoteがミュートしているユーザーが関わるものだったら無視する - if (isMutedUserRelated(note, this.muting)) return; + if (iUserRelated(note, this.muting)) return; // 流れてきたNoteがブロックされているユーザーが関わるものだったら無視する - if (isBlockerUserRelated(note, this.blocking)) return; + if (isUserRelated(note, this.blocking)) return; // 流れてきたNoteがミュートすべきNoteだったら無視する // TODO: 将来的には、単にMutedNoteテーブルにレコードがあるかどうかで判定したい(以下の理由により難しそうではある) diff --git a/packages/backend/src/server/api/stream/channels/user-list.ts b/packages/backend/src/server/api/stream/channels/user-list.ts index d8034e83f..97ad2983c 100644 --- a/packages/backend/src/server/api/stream/channels/user-list.ts +++ b/packages/backend/src/server/api/stream/channels/user-list.ts @@ -1,8 +1,7 @@ import Channel from '../channel.js'; import { Notes, UserListJoinings, UserLists } from '@/models/index.js'; -import { isMutedUserRelated } from '@/misc/is-muted-user-related.js'; import { User } from '@/models/entities/user.js'; -import { isBlockerUserRelated } from '@/misc/is-blocker-user-related.js'; +import { isUserRelated } from '@/misc/is-user-related.js'; import { Packed } from '@/misc/schema.js'; export default class extends Channel { @@ -76,9 +75,9 @@ export default class extends Channel { } // 流れてきたNoteがミュートしているユーザーが関わるものだったら無視する - if (isMutedUserRelated(note, this.muting)) return; + if (isUserRelated(note, this.muting)) return; // 流れてきたNoteがブロックされているユーザーが関わるものだったら無視する - if (isBlockerUserRelated(note, this.blocking)) return; + if (isUserRelated(note, this.blocking)) return; this.send('note', note); } diff --git a/packages/backend/src/services/add-note-to-antenna.ts b/packages/backend/src/services/add-note-to-antenna.ts index f86f394f8..1f344222e 100644 --- a/packages/backend/src/services/add-note-to-antenna.ts +++ b/packages/backend/src/services/add-note-to-antenna.ts @@ -2,7 +2,7 @@ import { Antenna } from '@/models/entities/antenna.js'; import { Note } from '@/models/entities/note.js'; import { AntennaNotes, Mutings, Notes } from '@/models/index.js'; import { genId } from '@/misc/gen-id.js'; -import { isMutedUserRelated } from '@/misc/is-muted-user-related.js'; +import { isUserRelated } from '@/misc/is-user-related.js'; import { publishAntennaStream, publishMainStream } from '@/services/stream.js'; import { User } from '@/models/entities/user.js'; @@ -39,7 +39,7 @@ export async function addNoteToAntenna(antenna: Antenna, note: Note, noteUser: { _note.renote = await Notes.findOneByOrFail({ id: note.renoteId }); } - if (isMutedUserRelated(_note, new Set(mutings.map(x => x.muteeId)))) { + if (isUserRelated(_note, new Set(mutings.map(x => x.muteeId)))) { return; }