From 3fe1f7e70ee8ab516fb353fcfed8ac04319eee40 Mon Sep 17 00:00:00 2001 From: Michcio Date: Sun, 25 Sep 2022 18:42:35 +0200 Subject: [PATCH] Deal with withPackedNote(onNote) types in stream channels --- packages/backend/src/server/api/stream/channel.ts | 2 +- packages/backend/src/server/api/stream/channels/channel.ts | 6 ++++-- .../src/server/api/stream/channels/global-timeline.ts | 6 ++++-- packages/backend/src/server/api/stream/channels/hashtag.ts | 6 ++++-- .../backend/src/server/api/stream/channels/home-timeline.ts | 6 ++++-- .../src/server/api/stream/channels/hybrid-timeline.ts | 6 ++++-- .../src/server/api/stream/channels/local-timeline.ts | 6 ++++-- .../backend/src/server/api/stream/channels/user-list.ts | 6 ++++-- 8 files changed, 29 insertions(+), 15 deletions(-) diff --git a/packages/backend/src/server/api/stream/channel.ts b/packages/backend/src/server/api/stream/channel.ts index ba88685a1..e14aa77c1 100644 --- a/packages/backend/src/server/api/stream/channel.ts +++ b/packages/backend/src/server/api/stream/channel.ts @@ -62,7 +62,7 @@ export default abstract class Channel { }); } - protected withPackedNote(callback: (note: Packed<'Note'>) => void): (Note) => void { + protected withPackedNote(callback: (note: Packed<'Note'>) => Promise): (note: Note) => Promise { return async (note: Note) => { try { // because `note` was previously JSON.stringify'ed, the fields that diff --git a/packages/backend/src/server/api/stream/channels/channel.ts b/packages/backend/src/server/api/stream/channels/channel.ts index 727bfbd1e..f16a9f670 100644 --- a/packages/backend/src/server/api/stream/channels/channel.ts +++ b/packages/backend/src/server/api/stream/channels/channel.ts @@ -2,6 +2,7 @@ import { Users } from '@/models/index.js'; import { isUserRelated } from '@/misc/is-user-related.js'; import { User } from '@/models/entities/user.js'; import { Packed } from '@/misc/schema.js'; +import { Note } from '@/models/entities/note.js'; import { StreamMessages } from '../types.js'; import Channel from '../channel.js'; @@ -12,10 +13,11 @@ export default class extends Channel { private channelId: string; private typers: Record = {}; private emitTypersIntervalId: ReturnType; + private onNote: (note: Note) => Promise; constructor(id: string, connection: Channel['connection']) { super(id, connection); - this.onNote = this.withPackedNote(this.onNote.bind(this)); + this.onNote = this.withPackedNote(this.onPackedNote.bind(this)); } public async init(params: any) { @@ -27,7 +29,7 @@ export default class extends Channel { this.emitTypersIntervalId = setInterval(this.emitTypers, 5000); } - private async onNote(note: Packed<'Note'>) { + private async onPackedNote(note: Packed<'Note'>): Promise { if (note.channelId !== this.channelId) return; // 流れてきた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 1087d4b91..6b6f69dfe 100644 --- a/packages/backend/src/server/api/stream/channels/global-timeline.ts +++ b/packages/backend/src/server/api/stream/channels/global-timeline.ts @@ -3,16 +3,18 @@ import { checkWordMute } from '@/misc/check-word-mute.js'; import { isInstanceMuted } from '@/misc/is-instance-muted.js'; import { isUserRelated } from '@/misc/is-user-related.js'; import { Packed } from '@/misc/schema.js'; +import { Note } from '@/models/entities/note.js'; import Channel from '../channel.js'; export default class extends Channel { public readonly chName = 'globalTimeline'; public static shouldShare = true; public static requireCredential = false; + private onNote: (note: Note) => Promise; constructor(id: string, connection: Channel['connection']) { super(id, connection); - this.onNote = this.withPackedNote(this.onNote.bind(this)); + this.onNote = this.withPackedNote(this.onPackedNote.bind(this)); } public async init(params: any) { @@ -25,7 +27,7 @@ export default class extends Channel { this.subscriber.on('notesStream', this.onNote); } - private async onNote(note: Packed<'Note'>) { + private async onPackedNote(note: Packed<'Note'>): Promise { if (note.visibility !== 'public') return; if (note.channelId != null) return; diff --git a/packages/backend/src/server/api/stream/channels/hashtag.ts b/packages/backend/src/server/api/stream/channels/hashtag.ts index 9ec7e1962..3a4c4ad22 100644 --- a/packages/backend/src/server/api/stream/channels/hashtag.ts +++ b/packages/backend/src/server/api/stream/channels/hashtag.ts @@ -1,6 +1,7 @@ import { normalizeForSearch } from '@/misc/normalize-for-search.js'; import { isUserRelated } from '@/misc/is-user-related.js'; import { Packed } from '@/misc/schema.js'; +import { Note } from '@/models/entities/note.js'; import Channel from '../channel.js'; export default class extends Channel { @@ -8,10 +9,11 @@ export default class extends Channel { public static shouldShare = false; public static requireCredential = false; private q: string[][]; + private onNote: (note: Note) => Promise; constructor(id: string, connection: Channel['connection']) { super(id, connection); - this.onNote = this.withPackedNote(this.onNote.bind(this)); + this.onNote = this.withPackedNote(this.onPackedNote.bind(this)); } public async init(params: any) { @@ -23,7 +25,7 @@ export default class extends Channel { this.subscriber.on('notesStream', this.onNote); } - private async onNote(note: Packed<'Note'>) { + private async onPackedNote(note: Packed<'Note'>): Promise { const noteTags = note.tags ? note.tags.map((t: string) => t.toLowerCase()) : []; const matched = this.q.some(tags => tags.every(tag => noteTags.includes(normalizeForSearch(tag)))); if (!matched) return; 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 a7965491a..e4a5bfd43 100644 --- a/packages/backend/src/server/api/stream/channels/home-timeline.ts +++ b/packages/backend/src/server/api/stream/channels/home-timeline.ts @@ -2,16 +2,18 @@ import { checkWordMute } from '@/misc/check-word-mute.js'; import { isUserRelated } from '@/misc/is-user-related.js'; import { isInstanceMuted } from '@/misc/is-instance-muted.js'; import { Packed } from '@/misc/schema.js'; +import { Note } from '@/models/entities/note.js'; import Channel from '../channel.js'; export default class extends Channel { public readonly chName = 'homeTimeline'; public static shouldShare = true; public static requireCredential = true; + private onNote: (note: Note) => Promise; constructor(id: string, connection: Channel['connection']) { super(id, connection); - this.onNote = this.withPackedNote(this.onNote.bind(this)); + this.onNote = this.withPackedNote(this.onPackedNote.bind(this)); } public async init(params: any) { @@ -19,7 +21,7 @@ export default class extends Channel { this.subscriber.on('notesStream', this.onNote); } - private async onNote(note: Packed<'Note'>) { + private async onPackedNote(note: Packed<'Note'>): Promise { if (note.channelId) { if (!this.followingChannels.has(note.channelId)) return; } else { 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 d17a24c70..989e70590 100644 --- a/packages/backend/src/server/api/stream/channels/hybrid-timeline.ts +++ b/packages/backend/src/server/api/stream/channels/hybrid-timeline.ts @@ -3,16 +3,18 @@ import { checkWordMute } from '@/misc/check-word-mute.js'; import { isUserRelated } from '@/misc/is-user-related.js'; import { isInstanceMuted } from '@/misc/is-instance-muted.js'; import { Packed } from '@/misc/schema.js'; +import { Note } from '@/models/entities/note.js'; import Channel from '../channel.js'; export default class extends Channel { public readonly chName = 'hybridTimeline'; public static shouldShare = true; public static requireCredential = true; + private onNote: (note: Note) => Promise; constructor(id: string, connection: Channel['connection']) { super(id, connection); - this.onNote = this.withPackedNote(this.onNote.bind(this)); + this.onNote = this.withPackedNote(this.onPackedNote.bind(this)); } public async init(params: any) { @@ -23,7 +25,7 @@ export default class extends Channel { this.subscriber.on('notesStream', this.onNote); } - private async onNote(note: Packed<'Note'>) { + private async onPackedNote(note: Packed<'Note'>): Promise { // チャンネルの投稿ではなく、自分自身の投稿 または // チャンネルの投稿ではなく、その投稿のユーザーをフォローしている または // チャンネルの投稿ではなく、全体公開のローカルの投稿 または 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 987ed2a32..3aa76e389 100644 --- a/packages/backend/src/server/api/stream/channels/local-timeline.ts +++ b/packages/backend/src/server/api/stream/channels/local-timeline.ts @@ -2,16 +2,18 @@ import { fetchMeta } from '@/misc/fetch-meta.js'; import { checkWordMute } from '@/misc/check-word-mute.js'; import { isUserRelated } from '@/misc/is-user-related.js'; import { Packed } from '@/misc/schema.js'; +import { Note } from '@/models/entities/note.js'; import Channel from '../channel.js'; export default class extends Channel { public readonly chName = 'localTimeline'; public static shouldShare = true; public static requireCredential = false; + onNote: (note: Note) => Promise; constructor(id: string, connection: Channel['connection']) { super(id, connection); - this.onNote = this.withPackedNote(this.onNote.bind(this)); + this.onNote = this.withPackedNote(this.onPackedNote.bind(this)); } public async init(params: any) { @@ -24,7 +26,7 @@ export default class extends Channel { this.subscriber.on('notesStream', this.onNote); } - private async onNote(note: Packed<'Note'>) { + private async onPackedNote(note: Packed<'Note'>): Promise { if (note.user.host !== null) return; if (note.visibility !== 'public') return; if (note.channelId != null && !this.followingChannels.has(note.channelId)) return; 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 16690a368..39ee53c2b 100644 --- a/packages/backend/src/server/api/stream/channels/user-list.ts +++ b/packages/backend/src/server/api/stream/channels/user-list.ts @@ -2,6 +2,7 @@ import { UserListJoinings, UserLists } from '@/models/index.js'; import { User } from '@/models/entities/user.js'; import { isUserRelated } from '@/misc/is-user-related.js'; import { Packed } from '@/misc/schema.js'; +import { Note } from '@/models/entities/note.js'; import Channel from '../channel.js'; export default class extends Channel { @@ -11,11 +12,12 @@ export default class extends Channel { private listId: string; public listUsers: User['id'][] = []; private listUsersClock: NodeJS.Timer; + private onNote: (note: Note) => Promise; constructor(id: string, connection: Channel['connection']) { super(id, connection); this.updateListUsers = this.updateListUsers.bind(this); - this.onNote = this.withPackedNote(this.onNote.bind(this)); + this.onNote = this.withPackedNote(this.onPackedNote.bind(this)); } public async init(params: any) { @@ -48,7 +50,7 @@ export default class extends Channel { this.listUsers = users.map(x => x.userId); } - private async onNote(note: Packed<'Note'>) { + private async onPackedNote(note: Packed<'Note'>): Promise { if (!this.listUsers.includes(note.userId)) return; // 流れてきたNoteがミュートしているユーザーが関わるものだったら無視する