upsert reactions to avoid error+retry overhead

This commit is contained in:
Hazelnoot 2025-05-30 15:37:27 -04:00
parent 9637e1b82b
commit 693cdde327

View file

@ -176,28 +176,11 @@ export class ReactionService {
reaction,
};
try {
await this.noteReactionsRepository.insert(record);
} catch (e) {
if (isDuplicateKeyValueError(e)) {
const exists = await this.noteReactionsRepository.findOneByOrFail({
noteId: note.id,
userId: user.id,
await this.noteReactionsRepository.upsert(record, {
skipUpdateIfNoValuesChanged: true,
conflictPaths: ['noteId', 'userId'],
});
if (exists.reaction !== reaction) {
// 別のリアクションがすでにされていたら置き換える
await this.delete(user, note);
await this.noteReactionsRepository.insert(record);
} else {
// 同じリアクションがすでにされていたらエラー
throw new IdentifiableError('51c42bb4-931a-456b-bff7-e5a8a70dd298');
}
} else {
throw e;
}
}
// Increment reactions count
if (this.meta.enableReactionsBuffering) {
await this.reactionsBufferingService.create(note.id, user.id, reaction, note.reactionAndUserPairCache);