fix emoji updating bug
This commit is contained in:
parent
d376aab45e
commit
3dc915ae89
|
@ -103,19 +103,33 @@ export class CustomEmojiService implements OnApplicationShutdown {
|
||||||
}
|
}
|
||||||
|
|
||||||
@bindThis
|
@bindThis
|
||||||
public async update(id: MiEmoji['id'], data: {
|
public async update(data: (
|
||||||
|
{ id: MiEmoji['id'], name?: string; } | { name: string; id?: MiEmoji['id'], }
|
||||||
|
) & {
|
||||||
driveFile?: MiDriveFile;
|
driveFile?: MiDriveFile;
|
||||||
name?: string;
|
|
||||||
category?: string | null;
|
category?: string | null;
|
||||||
aliases?: string[];
|
aliases?: string[];
|
||||||
license?: string | null;
|
license?: string | null;
|
||||||
isSensitive?: boolean;
|
isSensitive?: boolean;
|
||||||
localOnly?: boolean;
|
localOnly?: boolean;
|
||||||
roleIdsThatCanBeUsedThisEmojiAsReaction?: MiRole['id'][];
|
roleIdsThatCanBeUsedThisEmojiAsReaction?: MiRole['id'][];
|
||||||
}, moderator?: MiUser): Promise<void> {
|
}, moderator?: MiUser): Promise<
|
||||||
const emoji = await this.emojisRepository.findOneByOrFail({ id: id });
|
undefined
|
||||||
const sameNameEmoji = await this.emojisRepository.findOneBy({ name: data.name, host: IsNull() });
|
| "NO_SUCH_EMOJI"
|
||||||
if (sameNameEmoji != null && sameNameEmoji.id !== id) throw new Error('name already exists');
|
| "SAME_NAME_EMOJI_EXISTS"
|
||||||
|
> {
|
||||||
|
const emoji = data.id
|
||||||
|
? await this.getEmojiById(data.id)
|
||||||
|
: await this.getEmojiByName(data.name);
|
||||||
|
if (emoji === null) return "NO_SUCH_EMOJI";
|
||||||
|
const id = emoji.id;
|
||||||
|
|
||||||
|
// IDと絵文字名が両方指定されている場合は絵文字名の変更を行うため重複チェックが必要
|
||||||
|
const doNameUpdate = data.id && data.name && (data.name !== emoji.name);
|
||||||
|
if (doNameUpdate) {
|
||||||
|
const isDuplicate = await this.checkDuplicate(data.name);
|
||||||
|
if (isDuplicate) return "SAME_NAME_EMOJI_EXISTS";
|
||||||
|
}
|
||||||
|
|
||||||
await this.emojisRepository.update(emoji.id, {
|
await this.emojisRepository.update(emoji.id, {
|
||||||
updatedAt: new Date(),
|
updatedAt: new Date(),
|
||||||
|
@ -135,7 +149,7 @@ export class CustomEmojiService implements OnApplicationShutdown {
|
||||||
|
|
||||||
const packed = await this.emojiEntityService.packDetailed(emoji.id);
|
const packed = await this.emojiEntityService.packDetailed(emoji.id);
|
||||||
|
|
||||||
if (emoji.name === data.name) {
|
if (!doNameUpdate) {
|
||||||
this.globalEventService.publishBroadcastStream('emojiUpdated', {
|
this.globalEventService.publishBroadcastStream('emojiUpdated', {
|
||||||
emojis: [packed],
|
emojis: [packed],
|
||||||
});
|
});
|
||||||
|
|
|
@ -78,25 +78,10 @@ export default class extends Endpoint<typeof meta, typeof paramDef> { // eslint-
|
||||||
if (driveFile == null) throw new ApiError(meta.errors.noSuchFile);
|
if (driveFile == null) throw new ApiError(meta.errors.noSuchFile);
|
||||||
}
|
}
|
||||||
|
|
||||||
let emojiId;
|
const error = await this.customEmojiService.update({
|
||||||
if (ps.id) {
|
id: ps.id,
|
||||||
emojiId = ps.id;
|
|
||||||
const emoji = await this.customEmojiService.getEmojiById(ps.id);
|
|
||||||
if (!emoji) throw new ApiError(meta.errors.noSuchEmoji);
|
|
||||||
if (ps.name && (ps.name !== emoji.name)) {
|
|
||||||
const isDuplicate = await this.customEmojiService.checkDuplicate(ps.name);
|
|
||||||
if (isDuplicate) throw new ApiError(meta.errors.sameNameEmojiExists);
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
if (!ps.name) throw new Error('Invalid Params unexpectedly passed. This is a BUG. Please report it to the development team.');
|
|
||||||
const emoji = await this.customEmojiService.getEmojiByName(ps.name);
|
|
||||||
if (!emoji) throw new ApiError(meta.errors.noSuchEmoji);
|
|
||||||
emojiId = emoji.id;
|
|
||||||
}
|
|
||||||
|
|
||||||
await this.customEmojiService.update(emojiId, {
|
|
||||||
driveFile,
|
|
||||||
name: ps.name,
|
name: ps.name,
|
||||||
|
driveFile,
|
||||||
category: ps.category,
|
category: ps.category,
|
||||||
aliases: ps.aliases,
|
aliases: ps.aliases,
|
||||||
license: ps.license,
|
license: ps.license,
|
||||||
|
@ -104,6 +89,12 @@ export default class extends Endpoint<typeof meta, typeof paramDef> { // eslint-
|
||||||
localOnly: ps.localOnly,
|
localOnly: ps.localOnly,
|
||||||
roleIdsThatCanBeUsedThisEmojiAsReaction: ps.roleIdsThatCanBeUsedThisEmojiAsReaction,
|
roleIdsThatCanBeUsedThisEmojiAsReaction: ps.roleIdsThatCanBeUsedThisEmojiAsReaction,
|
||||||
}, me);
|
}, me);
|
||||||
|
|
||||||
|
switch (error) {
|
||||||
|
case undefined: return;
|
||||||
|
case "NO_SUCH_EMOJI": throw new ApiError(meta.errors.noSuchEmoji);
|
||||||
|
case "SAME_NAME_EMOJI_EXISTS": throw new ApiError(meta.errors.sameNameEmojiExists);
|
||||||
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue