refactor(backend): integrate CreateNotificationService to NotificationService

This commit is contained in:
syuilo
2023-03-16 14:24:11 +09:00
parent 0944c1cd6f
commit 89e2c302dd
10 changed files with 205 additions and 234 deletions

View File

@@ -9,7 +9,7 @@ import { IdService } from '@/core/IdService.js';
import type { NoteReaction } from '@/models/entities/NoteReaction.js';
import { isDuplicateKeyValueError } from '@/misc/is-duplicate-key-value-error.js';
import { GlobalEventService } from '@/core/GlobalEventService.js';
import { CreateNotificationService } from '@/core/CreateNotificationService.js';
import { NotificationService } from '@/core/NotificationService.js';
import PerUserReactionsChart from '@/core/chart/charts/per-user-reactions.js';
import { emojiRegex } from '@/misc/emoji-regex.js';
import { ApDeliverManagerService } from '@/core/activitypub/ApDeliverManagerService.js';
@@ -79,7 +79,7 @@ export class ReactionService {
private globalEventService: GlobalEventService,
private apRendererService: ApRendererService,
private apDeliverManagerService: ApDeliverManagerService,
private createNotificationService: CreateNotificationService,
private notificationService: NotificationService,
private perUserReactionsChart: PerUserReactionsChart,
) {
}
@@ -93,19 +93,19 @@ export class ReactionService {
throw new IdentifiableError('e70412a4-7197-4726-8e74-f3e0deb92aa7');
}
}
// check visibility
if (!await this.noteEntityService.isVisibleForMe(note, user.id)) {
throw new IdentifiableError('68e9d2d1-48bf-42c2-b90a-b20e09fd3d48', 'Note not accessible for you.');
}
if (note.reactionAcceptance === 'likeOnly' || ((note.reactionAcceptance === 'likeOnlyForRemote') && (user.host != null))) {
reaction = '❤️';
} else {
// TODO: cache
reaction = await this.toDbReaction(reaction, user.host);
}
const record: NoteReaction = {
id: this.idService.genId(),
createdAt: new Date(),
@@ -113,7 +113,7 @@ export class ReactionService {
userId: user.id,
reaction,
};
// Create reaction
try {
await this.noteReactionsRepository.insert(record);
@@ -123,7 +123,7 @@ export class ReactionService {
noteId: note.id,
userId: user.id,
});
if (exists.reaction !== reaction) {
// 別のリアクションがすでにされていたら置き換える
await this.delete(user, note);
@@ -136,7 +136,7 @@ export class ReactionService {
throw e;
}
}
// Increment reactions count
const sql = `jsonb_set("reactions", '{${reaction}}', (COALESCE("reactions"->>'${reaction}', '0')::int + 1)::text::jsonb)`;
await this.notesRepository.createQueryBuilder().update()
@@ -146,12 +146,12 @@ export class ReactionService {
})
.where('id = :id', { id: note.id })
.execute();
this.perUserReactionsChart.update(user, note);
// カスタム絵文字リアクションだったら絵文字情報も送る
const decodedReaction = this.decodeReaction(reaction);
const emoji = await this.emojisRepository.findOne({
where: {
name: decodedReaction.name,
@@ -159,7 +159,7 @@ export class ReactionService {
},
select: ['name', 'host', 'originalUrl', 'publicUrl'],
});
this.globalEventService.publishNoteStream(note.id, 'reacted', {
reaction: decodedReaction.reaction,
emoji: emoji != null ? {
@@ -169,16 +169,16 @@ export class ReactionService {
} : null,
userId: user.id,
});
// リアクションされたユーザーがローカルユーザーなら通知を作成
if (note.userHost === null) {
this.createNotificationService.createNotification(note.userId, 'reaction', {
this.notificationService.createNotification(note.userId, 'reaction', {
notifierId: user.id,
noteId: note.id,
reaction: reaction,
});
}
//#region 配信
if (this.userEntityService.isLocalUser(user) && !note.localOnly) {
const content = this.apRendererService.addContext(await this.apRendererService.renderLike(record, note));
@@ -187,7 +187,7 @@ export class ReactionService {
const reactee = await this.usersRepository.findOneBy({ id: note.userId });
dm.addDirectRecipe(reactee as RemoteUser);
}
if (['public', 'home', 'followers'].includes(note.visibility)) {
dm.addFollowersRecipe();
} else if (note.visibility === 'specified') {
@@ -196,7 +196,7 @@ export class ReactionService {
dm.addDirectRecipe(u as RemoteUser);
}
}
dm.execute();
}
//#endregion
@@ -209,18 +209,18 @@ export class ReactionService {
noteId: note.id,
userId: user.id,
});
if (exist == null) {
throw new IdentifiableError('60527ec9-b4cb-4a88-a6bd-32d3ad26817d', 'not reacted');
}
// Delete reaction
const result = await this.noteReactionsRepository.delete(exist.id);
if (result.affected !== 1) {
throw new IdentifiableError('60527ec9-b4cb-4a88-a6bd-32d3ad26817d', 'not reacted');
}
// Decrement reactions count
const sql = `jsonb_set("reactions", '{${exist.reaction}}', (COALESCE("reactions"->>'${exist.reaction}', '0')::int - 1)::text::jsonb)`;
await this.notesRepository.createQueryBuilder().update()
@@ -229,14 +229,14 @@ export class ReactionService {
})
.where('id = :id', { id: note.id })
.execute();
if (!user.isBot) this.notesRepository.decrement({ id: note.id }, 'score', 1);
this.globalEventService.publishNoteStream(note.id, 'unreacted', {
reaction: this.decodeReaction(exist.reaction).reaction,
userId: user.id,
});
//#region 配信
if (this.userEntityService.isLocalUser(user) && !note.localOnly) {
const content = this.apRendererService.addContext(this.apRendererService.renderUndo(await this.apRendererService.renderLike(exist, note), user));
@@ -250,7 +250,7 @@ export class ReactionService {
}
//#endregion
}
@bindThis
public async getFallbackReaction(): Promise<string> {
const meta = await this.metaService.fetch();
@@ -300,7 +300,7 @@ export class ReactionService {
// Unicode絵文字
const match = emojiRegex.exec(reaction);
if (match) {
// 合字を含む1つの絵文字
// 合字を含む1つの絵文字
const unicode = match[0];
// 異体字セレクタ除去