Skip to content

Commit

Permalink
스팸필터를 위한 특수 stream
Browse files Browse the repository at this point in the history
  • Loading branch information
yunochi committed Nov 4, 2024
1 parent 73d7b37 commit 29a155e
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 0 deletions.
2 changes: 2 additions & 0 deletions packages/backend/src/server/ServerModule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import { OAuth2ProviderService } from './oauth/OAuth2ProviderService.js';

import { MainChannelService } from './api/stream/channels/main.js';
import { AdminChannelService } from './api/stream/channels/admin.js';
import { AdminAllNoteChannelService } from './api/stream/channels/admin-allnote.js';
import { AntennaChannelService } from './api/stream/channels/antenna.js';
import { ChannelChannelService } from './api/stream/channels/channel.js';
import { DriveChannelService } from './api/stream/channels/drive.js';
Expand Down Expand Up @@ -78,6 +79,7 @@ import { SigninWithPasskeyApiService } from './api/SigninWithPasskeyApiService.j
StreamingApiServerService,
MainChannelService,
AdminChannelService,
AdminAllNoteChannelService,
AntennaChannelService,
ChannelChannelService,
DriveChannelService,
Expand Down
3 changes: 3 additions & 0 deletions packages/backend/src/server/api/stream/ChannelsService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { GlobalTimelineChannelService } from './channels/global-timeline.js';
import { MainChannelService } from './channels/main.js';
import { ChannelChannelService } from './channels/channel.js';
import { AdminChannelService } from './channels/admin.js';
import { AdminAllNoteChannelService } from './channels/admin-allnote.js';
import { ServerStatsChannelService } from './channels/server-stats.js';
import { QueueStatsChannelService } from './channels/queue-stats.js';
import { UserListChannelService } from './channels/user-list.js';
Expand Down Expand Up @@ -40,6 +41,7 @@ export class ChannelsService {
private serverStatsChannelService: ServerStatsChannelService,
private queueStatsChannelService: QueueStatsChannelService,
private adminChannelService: AdminChannelService,
private adminAllNoteChannelService: AdminAllNoteChannelService,
private reversiChannelService: ReversiChannelService,
private reversiGameChannelService: ReversiGameChannelService,
) {
Expand All @@ -62,6 +64,7 @@ export class ChannelsService {
case 'serverStats': return this.serverStatsChannelService;
case 'queueStats': return this.queueStatsChannelService;
case 'admin': return this.adminChannelService;
case '__adminAllNote': return this.adminAllNoteChannelService;
case 'reversi': return this.reversiChannelService;
case 'reversiGame': return this.reversiGameChannelService;

Expand Down
92 changes: 92 additions & 0 deletions packages/backend/src/server/api/stream/channels/admin-allnote.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
/*
* SPDX-FileCopyrightText: syuilo and misskey-project
* SPDX-License-Identifier: AGPL-3.0-only
*/

import { Injectable } from '@nestjs/common';
import type { Packed } from '@/misc/json-schema.js';
import { MetaService } from '@/core/MetaService.js';
import { NoteEntityService } from '@/core/entities/NoteEntityService.js';
import { bindThis } from '@/decorators.js';
import { RoleService } from '@/core/RoleService.js';
import { isRenotePacked, isQuotePacked } from '@/misc/is-renote.js';
import type { JsonObject } from '@/misc/json-value.js';
import Channel, { type MiChannelService } from '../channel.js';

class AdminAllNoteChannel extends Channel {
public readonly chName = '__adminAllNote';
public static shouldShare = true;
public static requireCredential = true as const;
public static kind = 'read:admin:stream';
private withRenotes: boolean;
private withFiles: boolean;

constructor(
private metaService: MetaService,
private roleService: RoleService,
private noteEntityService: NoteEntityService,

id: string,
connection: Channel['connection'],
) {
super(id, connection);
//this.onNote = this.onNote.bind(this);
}

@bindThis
public async init(params: JsonObject) {
const policies = await this.roleService.getUserPolicies(this.user ? this.user.id : null);
const isAdmin = await this.roleService.isAdministrator(this.user ?? null);
if (!policies.gtlAvailable || !isAdmin) return;

this.withRenotes = !!(params.withRenotes ?? true);
this.withFiles = !!(params.withFiles ?? false);

// Subscribe events
this.subscriber.on('notesStream', this.onNote);
}

@bindThis
private async onNote(note: Packed<'Note'>) {
if (this.withFiles && (note.fileIds == null || note.fileIds.length === 0)) return;

if (note.channelId != null) return;

if (isRenotePacked(note) && !isQuotePacked(note) && !this.withRenotes) return;

this.connection.cacheNote(note);

this.send('note', note);
}

@bindThis
public dispose() {
// Unsubscribe events
this.subscriber.off('notesStream', this.onNote);
}
}

@Injectable()
export class AdminAllNoteChannelService implements MiChannelService<true> {
public readonly shouldShare = AdminAllNoteChannel.shouldShare;
public readonly requireCredential = AdminAllNoteChannel.requireCredential;
public readonly kind = AdminAllNoteChannel.kind;

constructor(
private metaService: MetaService,
private roleService: RoleService,
private noteEntityService: NoteEntityService,
) {
}

@bindThis
public create(id: string, connection: Channel['connection']): AdminAllNoteChannel {
return new AdminAllNoteChannel(
this.metaService,
this.roleService,
this.noteEntityService,
id,
connection,
);
}
}

0 comments on commit 29a155e

Please sign in to comment.