mizzkey/packages/backend/src/server/api/stream/channels/channel.ts

86 lines
2.4 KiB
TypeScript
Raw Normal View History

/*
* SPDX-FileCopyrightText: syuilo and other misskey contributors
* SPDX-License-Identifier: AGPL-3.0-only
*/
import { Injectable } from '@nestjs/common';
import { isUserRelated } from '@/misc/is-user-related.js';
2023-03-10 06:22:37 +01:00
import type { Packed } from '@/misc/json-schema.js';
2022-09-17 20:27:08 +02:00
import { NoteEntityService } from '@/core/entities/NoteEntityService.js';
import { bindThis } from '@/decorators.js';
2022-09-17 20:27:08 +02:00
import Channel from '../channel.js';
2022-09-17 20:27:08 +02:00
class ChannelChannel extends Channel {
public readonly chName = 'channel';
public static shouldShare = false;
public static requireCredential = false;
private channelId: string;
2022-09-17 20:27:08 +02:00
constructor(
private noteEntityService: NoteEntityService,
id: string,
connection: Channel['connection'],
) {
super(id, connection);
//this.onNote = this.onNote.bind(this);
}
@bindThis
public async init(params: any) {
this.channelId = params.channelId as string;
// Subscribe stream
this.subscriber.on('notesStream', this.onNote);
}
@bindThis
private async onNote(note: Packed<'Note'>) {
if (note.channelId !== this.channelId) return;
// 流れてきたNoteがミュートしているユーザーが関わるものだったら無視する
2023-04-05 03:21:10 +02:00
if (isUserRelated(note, this.userIdsWhoMeMuting)) return;
// 流れてきたNoteがブロックされているユーザーが関わるものだったら無視する
2023-04-05 03:21:10 +02:00
if (isUserRelated(note, this.userIdsWhoBlockingMe)) return;
2023-04-05 03:21:10 +02:00
if (note.renote && !note.text && isUserRelated(note, this.userIdsWhoMeMutingRenotes)) return;
if (this.user && note.renoteId && !note.text) {
if (note.renote && Object.keys(note.renote.reactions).length > 0) {
const myRenoteReaction = await this.noteEntityService.populateMyReaction(note.renoteId, this.user.id);
note.renote!.myReaction = myRenoteReaction;
}
}
2021-03-21 09:38:09 +01:00
this.connection.cacheNote(note);
this.send('note', note);
}
@bindThis
public dispose() {
// Unsubscribe events
this.subscriber.off('notesStream', this.onNote);
}
}
2022-09-17 20:27:08 +02:00
@Injectable()
export class ChannelChannelService {
public readonly shouldShare = ChannelChannel.shouldShare;
public readonly requireCredential = ChannelChannel.requireCredential;
constructor(
private noteEntityService: NoteEntityService,
) {
}
@bindThis
2022-09-17 20:27:08 +02:00
public create(id: string, connection: Channel['connection']): ChannelChannel {
return new ChannelChannel(
this.noteEntityService,
id,
connection,
);
}
}