2023-02-16 14:09:41 +00:00
|
|
|
import { Injectable } from '@nestjs/common';
|
2022-02-27 02:07:39 +00:00
|
|
|
import { normalizeForSearch } from '@/misc/normalize-for-search.js';
|
2022-06-27 12:48:10 +00:00
|
|
|
import { isUserRelated } from '@/misc/is-user-related.js';
|
2023-03-10 05:22:37 +00:00
|
|
|
import type { Packed } from '@/misc/json-schema.js';
|
2022-09-17 18:27:08 +00:00
|
|
|
import { NoteEntityService } from '@/core/entities/NoteEntityService.js';
|
2022-12-04 06:03:09 +00:00
|
|
|
import { bindThis } from '@/decorators.js';
|
2022-09-17 18:27:08 +00:00
|
|
|
import Channel from '../channel.js';
|
2018-10-07 02:06:17 +00:00
|
|
|
|
2022-09-17 18:27:08 +00:00
|
|
|
class HashtagChannel extends Channel {
|
2018-10-11 14:01:57 +00:00
|
|
|
public readonly chName = 'hashtag';
|
2018-10-11 14:07:20 +00:00
|
|
|
public static shouldShare = false;
|
2018-11-10 17:22:34 +00:00
|
|
|
public static requireCredential = false;
|
2019-04-07 12:50:36 +00:00
|
|
|
private q: string[][];
|
2018-10-11 14:01:57 +00:00
|
|
|
|
2022-09-17 18:27:08 +00:00
|
|
|
constructor(
|
|
|
|
private noteEntityService: NoteEntityService,
|
|
|
|
|
|
|
|
id: string,
|
|
|
|
connection: Channel['connection'],
|
|
|
|
) {
|
2022-02-27 02:07:39 +00:00
|
|
|
super(id, connection);
|
2022-12-04 06:03:09 +00:00
|
|
|
//this.onNote = this.onNote.bind(this);
|
2022-02-27 02:07:39 +00:00
|
|
|
}
|
|
|
|
|
2022-12-04 06:03:09 +00:00
|
|
|
@bindThis
|
2018-10-07 02:06:17 +00:00
|
|
|
public async init(params: any) {
|
2019-04-07 12:50:36 +00:00
|
|
|
this.q = params.q;
|
2018-10-07 02:06:17 +00:00
|
|
|
|
2019-04-07 12:50:36 +00:00
|
|
|
if (this.q == null) return;
|
2018-10-20 00:31:52 +00:00
|
|
|
|
2018-10-07 02:06:17 +00:00
|
|
|
// Subscribe stream
|
2019-04-07 12:50:36 +00:00
|
|
|
this.subscriber.on('notesStream', this.onNote);
|
|
|
|
}
|
|
|
|
|
2022-12-04 06:03:09 +00:00
|
|
|
@bindThis
|
2021-09-22 13:35:55 +00:00
|
|
|
private async onNote(note: Packed<'Note'>) {
|
2019-04-23 13:35:26 +00:00
|
|
|
const noteTags = note.tags ? note.tags.map((t: string) => t.toLowerCase()) : [];
|
2021-02-07 01:43:34 +00:00
|
|
|
const matched = this.q.some(tags => tags.every(tag => noteTags.includes(normalizeForSearch(tag))));
|
2019-04-07 12:50:36 +00:00
|
|
|
if (!matched) return;
|
|
|
|
|
|
|
|
// Renoteなら再pack
|
|
|
|
if (note.renoteId != null) {
|
2022-09-17 18:27:08 +00:00
|
|
|
note.renote = await this.noteEntityService.pack(note.renoteId, this.user, {
|
2021-12-09 14:58:30 +00:00
|
|
|
detail: true,
|
2019-04-07 12:50:36 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
// 流れてきたNoteがミュートしているユーザーが関わるものだったら無視する
|
2023-04-05 01:21:10 +00:00
|
|
|
if (isUserRelated(note, this.userIdsWhoMeMuting)) return;
|
2021-08-17 12:48:59 +00:00
|
|
|
// 流れてきたNoteがブロックされているユーザーが関わるものだったら無視する
|
2023-04-05 01:21:10 +00:00
|
|
|
if (isUserRelated(note, this.userIdsWhoBlockingMe)) return;
|
2023-07-07 22:08:16 +00:00
|
|
|
|
2023-04-05 01:21:10 +00:00
|
|
|
if (note.renote && !note.text && isUserRelated(note, this.userIdsWhoMeMutingRenotes)) return;
|
2019-04-07 12:50:36 +00:00
|
|
|
|
2021-03-21 08:38:09 +00:00
|
|
|
this.connection.cacheNote(note);
|
|
|
|
|
2019-04-07 12:50:36 +00:00
|
|
|
this.send('note', note);
|
|
|
|
}
|
|
|
|
|
2022-12-04 06:03:09 +00:00
|
|
|
@bindThis
|
2019-04-07 12:50:36 +00:00
|
|
|
public dispose() {
|
|
|
|
// Unsubscribe events
|
|
|
|
this.subscriber.off('notesStream', this.onNote);
|
2018-10-07 02:06:17 +00:00
|
|
|
}
|
|
|
|
}
|
2022-09-17 18:27:08 +00:00
|
|
|
|
|
|
|
@Injectable()
|
|
|
|
export class HashtagChannelService {
|
|
|
|
public readonly shouldShare = HashtagChannel.shouldShare;
|
|
|
|
public readonly requireCredential = HashtagChannel.requireCredential;
|
|
|
|
|
|
|
|
constructor(
|
|
|
|
private noteEntityService: NoteEntityService,
|
|
|
|
) {
|
|
|
|
}
|
|
|
|
|
2022-12-04 06:03:09 +00:00
|
|
|
@bindThis
|
2022-09-17 18:27:08 +00:00
|
|
|
public create(id: string, connection: Channel['connection']): HashtagChannel {
|
|
|
|
return new HashtagChannel(
|
|
|
|
this.noteEntityService,
|
|
|
|
id,
|
|
|
|
connection,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|