2022-09-17 18:27:08 +00:00
|
|
|
import { Inject, Injectable } from '@nestjs/common';
|
|
|
|
import Redis from 'ioredis';
|
|
|
|
import type { Antenna } from '@/models/entities/Antenna.js';
|
|
|
|
import type { Note } from '@/models/entities/Note.js';
|
|
|
|
import type { User } from '@/models/entities/User.js';
|
2022-12-18 10:50:02 +00:00
|
|
|
import { NoteEntityService } from '@/core/entities/NoteEntityService.js';
|
|
|
|
import { AntennaEntityService } from '@/core/entities/AntennaEntityService.js';
|
2022-09-17 18:27:08 +00:00
|
|
|
import { IdService } from '@/core/IdService.js';
|
|
|
|
import { isUserRelated } from '@/misc/is-user-related.js';
|
|
|
|
import { GlobalEventService } from '@/core/GlobalEventService.js';
|
2022-12-18 10:50:02 +00:00
|
|
|
import { PushNotificationService } from '@/core/PushNotificationService.js';
|
2022-09-17 18:27:08 +00:00
|
|
|
import * as Acct from '@/misc/acct.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 { DI } from '@/di-symbols.js';
|
2023-04-03 03:11:16 +00:00
|
|
|
import type { MutingsRepository, NotesRepository, AntennasRepository, UserListJoiningsRepository } from '@/models/index.js';
|
2022-12-04 01:16:03 +00:00
|
|
|
import { UtilityService } from '@/core/UtilityService.js';
|
2022-12-04 06:03:09 +00:00
|
|
|
import { bindThis } from '@/decorators.js';
|
2023-01-13 23:27:23 +00:00
|
|
|
import { StreamMessages } from '@/server/api/stream/types.js';
|
2023-01-09 05:12:42 +00:00
|
|
|
import type { OnApplicationShutdown } from '@nestjs/common';
|
2022-09-17 18:27:08 +00:00
|
|
|
|
|
|
|
@Injectable()
|
|
|
|
export class AntennaService implements OnApplicationShutdown {
|
2022-09-18 18:11:50 +00:00
|
|
|
private antennasFetched: boolean;
|
|
|
|
private antennas: Antenna[];
|
2022-09-17 18:27:08 +00:00
|
|
|
|
|
|
|
constructor(
|
2023-04-03 03:11:16 +00:00
|
|
|
@Inject(DI.redis)
|
|
|
|
private redisClient: Redis.Redis,
|
|
|
|
|
2023-04-07 02:20:14 +00:00
|
|
|
@Inject(DI.redisForPubsub)
|
|
|
|
private redisForPubsub: Redis.Redis,
|
2022-09-17 18:27:08 +00:00
|
|
|
|
|
|
|
@Inject(DI.mutingsRepository)
|
|
|
|
private mutingsRepository: MutingsRepository,
|
|
|
|
|
|
|
|
@Inject(DI.notesRepository)
|
|
|
|
private notesRepository: NotesRepository,
|
|
|
|
|
|
|
|
@Inject(DI.antennasRepository)
|
|
|
|
private antennasRepository: AntennasRepository,
|
|
|
|
|
|
|
|
@Inject(DI.userListJoiningsRepository)
|
|
|
|
private userListJoiningsRepository: UserListJoiningsRepository,
|
|
|
|
|
|
|
|
private utilityService: UtilityService,
|
|
|
|
private idService: IdService,
|
2023-02-04 01:02:03 +00:00
|
|
|
private globalEventService: GlobalEventService,
|
2022-12-18 10:50:02 +00:00
|
|
|
private pushNotificationService: PushNotificationService,
|
|
|
|
private noteEntityService: NoteEntityService,
|
|
|
|
private antennaEntityService: AntennaEntityService,
|
2022-09-17 18:27:08 +00:00
|
|
|
) {
|
2022-09-18 18:11:50 +00:00
|
|
|
this.antennasFetched = false;
|
|
|
|
this.antennas = [];
|
2022-09-17 18:27:08 +00:00
|
|
|
|
2023-04-07 02:20:14 +00:00
|
|
|
this.redisForPubsub.on('message', this.onRedisMessage);
|
2022-09-17 18:27:08 +00:00
|
|
|
}
|
|
|
|
|
2022-12-04 06:03:09 +00:00
|
|
|
@bindThis
|
2022-09-17 18:27:08 +00:00
|
|
|
public onApplicationShutdown(signal?: string | undefined) {
|
2023-04-07 02:20:14 +00:00
|
|
|
this.redisForPubsub.off('message', this.onRedisMessage);
|
2022-09-17 18:27:08 +00:00
|
|
|
}
|
|
|
|
|
2022-12-04 06:03:09 +00:00
|
|
|
@bindThis
|
2022-09-23 21:45:44 +00:00
|
|
|
private async onRedisMessage(_: string, data: string): Promise<void> {
|
2022-09-17 18:27:08 +00:00
|
|
|
const obj = JSON.parse(data);
|
|
|
|
|
|
|
|
if (obj.channel === 'internal') {
|
2023-01-13 23:27:23 +00:00
|
|
|
const { type, body } = obj.message as StreamMessages['internal']['payload'];
|
2022-09-17 18:27:08 +00:00
|
|
|
switch (type) {
|
|
|
|
case 'antennaCreated':
|
2023-01-25 02:18:16 +00:00
|
|
|
this.antennas.push({
|
|
|
|
...body,
|
|
|
|
createdAt: new Date(body.createdAt),
|
2023-03-20 11:12:38 +00:00
|
|
|
lastUsedAt: new Date(body.lastUsedAt),
|
2023-01-25 02:18:16 +00:00
|
|
|
});
|
2022-09-17 18:27:08 +00:00
|
|
|
break;
|
|
|
|
case 'antennaUpdated':
|
2023-01-25 02:18:16 +00:00
|
|
|
this.antennas[this.antennas.findIndex(a => a.id === body.id)] = {
|
|
|
|
...body,
|
|
|
|
createdAt: new Date(body.createdAt),
|
2023-03-20 11:12:38 +00:00
|
|
|
lastUsedAt: new Date(body.lastUsedAt),
|
2023-01-25 02:18:16 +00:00
|
|
|
};
|
2022-09-17 18:27:08 +00:00
|
|
|
break;
|
|
|
|
case 'antennaDeleted':
|
2022-09-18 18:11:50 +00:00
|
|
|
this.antennas = this.antennas.filter(a => a.id !== body.id);
|
2022-09-17 18:27:08 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-04 06:03:09 +00:00
|
|
|
@bindThis
|
2022-09-17 18:27:08 +00:00
|
|
|
public async addNoteToAntenna(antenna: Antenna, note: Note, noteUser: { id: User['id']; }): Promise<void> {
|
2023-04-03 03:11:16 +00:00
|
|
|
this.redisClient.xadd(
|
|
|
|
`antennaTimeline:${antenna.id}`,
|
|
|
|
'MAXLEN', '~', '200',
|
|
|
|
`${this.idService.parse(note.id).date.getTime()}-*`,
|
|
|
|
'note', note.id);
|
|
|
|
|
2023-02-04 01:02:03 +00:00
|
|
|
this.globalEventService.publishAntennaStream(antenna.id, 'note', note);
|
2022-09-17 18:27:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// NOTE: フォローしているユーザーのノート、リストのユーザーのノート、グループのユーザーのノート指定はパフォーマンス上の理由で無効になっている
|
|
|
|
|
2022-12-04 06:03:09 +00:00
|
|
|
@bindThis
|
2023-01-09 05:12:42 +00:00
|
|
|
public async checkHitAntenna(antenna: Antenna, note: (Note | Packed<'Note'>), noteUser: { id: User['id']; username: string; host: string | null; }): Promise<boolean> {
|
2022-09-17 18:27:08 +00:00
|
|
|
if (note.visibility === 'specified') return false;
|
2023-01-09 05:12:42 +00:00
|
|
|
if (note.visibility === 'followers') return false;
|
2022-09-17 18:27:08 +00:00
|
|
|
|
|
|
|
if (!antenna.withReplies && note.replyId != null) return false;
|
|
|
|
|
|
|
|
if (antenna.src === 'home') {
|
2023-01-09 05:12:42 +00:00
|
|
|
// TODO
|
2022-09-17 18:27:08 +00:00
|
|
|
} else if (antenna.src === 'list') {
|
|
|
|
const listUsers = (await this.userListJoiningsRepository.findBy({
|
|
|
|
userListId: antenna.userListId!,
|
|
|
|
})).map(x => x.userId);
|
|
|
|
|
|
|
|
if (!listUsers.includes(note.userId)) return false;
|
|
|
|
} else if (antenna.src === 'users') {
|
|
|
|
const accts = antenna.users.map(x => {
|
|
|
|
const { username, host } = Acct.parse(x);
|
|
|
|
return this.utilityService.getFullApAccount(username, host).toLowerCase();
|
|
|
|
});
|
|
|
|
if (!accts.includes(this.utilityService.getFullApAccount(noteUser.username, noteUser.host).toLowerCase())) return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
const keywords = antenna.keywords
|
|
|
|
// Clean up
|
|
|
|
.map(xs => xs.filter(x => x !== ''))
|
|
|
|
.filter(xs => xs.length > 0);
|
|
|
|
|
|
|
|
if (keywords.length > 0) {
|
2023-02-28 11:20:23 +00:00
|
|
|
if (note.text == null && note.cw == null) return false;
|
|
|
|
|
|
|
|
const _text = (note.text ?? '') + '\n' + (note.cw ?? '');
|
2022-09-17 18:27:08 +00:00
|
|
|
|
|
|
|
const matched = keywords.some(and =>
|
|
|
|
and.every(keyword =>
|
|
|
|
antenna.caseSensitive
|
2023-02-28 11:20:23 +00:00
|
|
|
? _text.includes(keyword)
|
|
|
|
: _text.toLowerCase().includes(keyword.toLowerCase()),
|
2022-09-17 18:27:08 +00:00
|
|
|
));
|
|
|
|
|
|
|
|
if (!matched) return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
const excludeKeywords = antenna.excludeKeywords
|
|
|
|
// Clean up
|
|
|
|
.map(xs => xs.filter(x => x !== ''))
|
|
|
|
.filter(xs => xs.length > 0);
|
|
|
|
|
|
|
|
if (excludeKeywords.length > 0) {
|
2023-02-28 11:20:23 +00:00
|
|
|
if (note.text == null && note.cw == null) return false;
|
|
|
|
|
|
|
|
const _text = (note.text ?? '') + '\n' + (note.cw ?? '');
|
|
|
|
|
2022-09-17 18:27:08 +00:00
|
|
|
const matched = excludeKeywords.some(and =>
|
|
|
|
and.every(keyword =>
|
|
|
|
antenna.caseSensitive
|
2023-02-28 11:20:23 +00:00
|
|
|
? _text.includes(keyword)
|
|
|
|
: _text.toLowerCase().includes(keyword.toLowerCase()),
|
2022-09-17 18:27:08 +00:00
|
|
|
));
|
|
|
|
|
|
|
|
if (matched) return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (antenna.withFile) {
|
|
|
|
if (note.fileIds && note.fileIds.length === 0) return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: eval expression
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2022-12-04 06:03:09 +00:00
|
|
|
@bindThis
|
2022-09-17 18:27:08 +00:00
|
|
|
public async getAntennas() {
|
2022-09-18 18:11:50 +00:00
|
|
|
if (!this.antennasFetched) {
|
2023-03-20 11:12:38 +00:00
|
|
|
this.antennas = await this.antennasRepository.findBy({
|
|
|
|
isActive: true,
|
|
|
|
});
|
2022-09-18 18:11:50 +00:00
|
|
|
this.antennasFetched = true;
|
2022-09-17 18:27:08 +00:00
|
|
|
}
|
|
|
|
|
2022-09-18 18:11:50 +00:00
|
|
|
return this.antennas;
|
2022-09-17 18:27:08 +00:00
|
|
|
}
|
|
|
|
}
|