mirror of
https://codeberg.org/yeentown/barkey.git
synced 2025-07-07 20:44:34 +00:00
merge: Reduce overhead and DB error spam when a user changes their reaction (!1082)
View MR for information: https://activitypub.software/TransFem-org/Sharkey/-/merge_requests/1082 Approved-by: dakkar <dakkar@thenautilus.net> Approved-by: Marie <github@yuugi.dev>
This commit is contained in:
commit
d6156c5913
1 changed files with 25 additions and 19 deletions
|
@ -10,7 +10,7 @@ import { IdentifiableError } from '@/misc/identifiable-error.js';
|
||||||
import type { MiRemoteUser, MiUser } from '@/models/User.js';
|
import type { MiRemoteUser, MiUser } from '@/models/User.js';
|
||||||
import type { MiNote } from '@/models/Note.js';
|
import type { MiNote } from '@/models/Note.js';
|
||||||
import { IdService } from '@/core/IdService.js';
|
import { IdService } from '@/core/IdService.js';
|
||||||
import type { MiNoteReaction } from '@/models/NoteReaction.js';
|
import { MiNoteReaction } from '@/models/NoteReaction.js';
|
||||||
import { isDuplicateKeyValueError } from '@/misc/is-duplicate-key-value-error.js';
|
import { isDuplicateKeyValueError } from '@/misc/is-duplicate-key-value-error.js';
|
||||||
import { GlobalEventService } from '@/core/GlobalEventService.js';
|
import { GlobalEventService } from '@/core/GlobalEventService.js';
|
||||||
import { NotificationService } from '@/core/NotificationService.js';
|
import { NotificationService } from '@/core/NotificationService.js';
|
||||||
|
@ -31,6 +31,7 @@ import { isQuote, isRenote } from '@/misc/is-renote.js';
|
||||||
import { ReactionsBufferingService } from '@/core/ReactionsBufferingService.js';
|
import { ReactionsBufferingService } from '@/core/ReactionsBufferingService.js';
|
||||||
import { PER_NOTE_REACTION_USER_PAIR_CACHE_MAX } from '@/const.js';
|
import { PER_NOTE_REACTION_USER_PAIR_CACHE_MAX } from '@/const.js';
|
||||||
import { CacheService } from '@/core/CacheService.js';
|
import { CacheService } from '@/core/CacheService.js';
|
||||||
|
import type { DataSource } from 'typeorm';
|
||||||
|
|
||||||
const FALLBACK = '\u2764';
|
const FALLBACK = '\u2764';
|
||||||
|
|
||||||
|
@ -89,6 +90,9 @@ export class ReactionService {
|
||||||
@Inject(DI.emojisRepository)
|
@Inject(DI.emojisRepository)
|
||||||
private emojisRepository: EmojisRepository,
|
private emojisRepository: EmojisRepository,
|
||||||
|
|
||||||
|
@Inject(DI.db)
|
||||||
|
private readonly db: DataSource,
|
||||||
|
|
||||||
private utilityService: UtilityService,
|
private utilityService: UtilityService,
|
||||||
private customEmojiService: CustomEmojiService,
|
private customEmojiService: CustomEmojiService,
|
||||||
private roleService: RoleService,
|
private roleService: RoleService,
|
||||||
|
@ -176,26 +180,28 @@ export class ReactionService {
|
||||||
reaction,
|
reaction,
|
||||||
};
|
};
|
||||||
|
|
||||||
try {
|
const result = await this.db.transaction(async tem => {
|
||||||
await this.noteReactionsRepository.insert(record);
|
await tem.createQueryBuilder(MiNoteReaction, 'noteReaction')
|
||||||
} catch (e) {
|
.insert()
|
||||||
if (isDuplicateKeyValueError(e)) {
|
.values(record)
|
||||||
const exists = await this.noteReactionsRepository.findOneByOrFail({
|
.orIgnore()
|
||||||
noteId: note.id,
|
.execute();
|
||||||
userId: user.id,
|
|
||||||
|
return await tem.createQueryBuilder(MiNoteReaction, 'noteReaction')
|
||||||
|
.select()
|
||||||
|
.where({ noteId: note.id, userId: user.id })
|
||||||
|
.getOneOrFail();
|
||||||
});
|
});
|
||||||
|
|
||||||
if (exists.reaction !== reaction) {
|
if (result.id !== record.id) {
|
||||||
|
// Conflict with the same ID => nothing to do.
|
||||||
|
if (result.reaction === record.reaction) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
// 別のリアクションがすでにされていたら置き換える
|
// 別のリアクションがすでにされていたら置き換える
|
||||||
await this.delete(user, note);
|
await this.delete(user, note);
|
||||||
await this.noteReactionsRepository.insert(record);
|
await this.noteReactionsRepository.insert(record);
|
||||||
} else {
|
|
||||||
// 同じリアクションがすでにされていたらエラー
|
|
||||||
throw new IdentifiableError('51c42bb4-931a-456b-bff7-e5a8a70dd298');
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
throw e;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Increment reactions count
|
// Increment reactions count
|
||||||
|
|
Loading…
Add table
Reference in a new issue