2023-07-27 00:31:52 -05:00
|
|
|
|
/*
|
2024-02-13 09:59:27 -06:00
|
|
|
|
* SPDX-FileCopyrightText: syuilo and misskey-project
|
2023-07-27 00:31:52 -05:00
|
|
|
|
* SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
*/
|
|
|
|
|
|
2023-06-09 23:45:11 -05:00
|
|
|
|
import { Inject, Injectable, OnApplicationShutdown } from '@nestjs/common';
|
2023-08-04 20:33:00 -05:00
|
|
|
|
import { In, IsNull } from 'typeorm';
|
2023-04-13 23:50:05 -05:00
|
|
|
|
import * as Redis from 'ioredis';
|
2022-09-17 13:27:08 -05:00
|
|
|
|
import { DI } from '@/di-symbols.js';
|
|
|
|
|
import { IdService } from '@/core/IdService.js';
|
2023-01-22 08:53:24 -06:00
|
|
|
|
import { EmojiEntityService } from '@/core/entities/EmojiEntityService.js';
|
|
|
|
|
import { GlobalEventService } from '@/core/GlobalEventService.js';
|
2023-09-19 21:33:36 -05:00
|
|
|
|
import type { MiDriveFile } from '@/models/DriveFile.js';
|
|
|
|
|
import type { MiEmoji } from '@/models/Emoji.js';
|
2023-09-23 20:57:24 -05:00
|
|
|
|
import type { EmojisRepository, MiRole, MiUser } from '@/models/_.js';
|
2022-12-04 02:05:32 -06:00
|
|
|
|
import { bindThis } from '@/decorators.js';
|
2023-04-05 21:14:43 -05:00
|
|
|
|
import { MemoryKVCache, RedisSingleCache } from '@/misc/cache.js';
|
2023-01-26 00:48:12 -06:00
|
|
|
|
import { UtilityService } from '@/core/UtilityService.js';
|
|
|
|
|
import { query } from '@/misc/prelude/url.js';
|
2023-09-28 21:29:54 -05:00
|
|
|
|
import type { Serialized } from '@/types.js';
|
2023-09-23 20:57:24 -05:00
|
|
|
|
import { ModerationLogService } from '@/core/ModerationLogService.js';
|
2022-09-17 13:27:08 -05:00
|
|
|
|
|
2023-05-18 04:45:49 -05:00
|
|
|
|
const parseEmojiStrRegexp = /^(\w+)(?:@([\w.-]+))?$/;
|
|
|
|
|
|
2022-09-17 13:27:08 -05:00
|
|
|
|
@Injectable()
|
2023-06-09 23:45:11 -05:00
|
|
|
|
export class CustomEmojiService implements OnApplicationShutdown {
|
2023-08-16 03:51:28 -05:00
|
|
|
|
private cache: MemoryKVCache<MiEmoji | null>;
|
|
|
|
|
public localEmojisCache: RedisSingleCache<Map<string, MiEmoji>>;
|
2023-01-26 00:48:12 -06:00
|
|
|
|
|
2022-09-17 13:27:08 -05:00
|
|
|
|
constructor(
|
2023-04-05 21:14:43 -05:00
|
|
|
|
@Inject(DI.redis)
|
|
|
|
|
private redisClient: Redis.Redis,
|
|
|
|
|
|
2022-09-17 13:27:08 -05:00
|
|
|
|
@Inject(DI.emojisRepository)
|
|
|
|
|
private emojisRepository: EmojisRepository,
|
|
|
|
|
|
2023-01-26 00:48:12 -06:00
|
|
|
|
private utilityService: UtilityService,
|
2022-09-17 13:27:08 -05:00
|
|
|
|
private idService: IdService,
|
2023-01-22 08:53:24 -06:00
|
|
|
|
private emojiEntityService: EmojiEntityService,
|
2023-09-23 20:57:24 -05:00
|
|
|
|
private moderationLogService: ModerationLogService,
|
2023-01-22 08:53:24 -06:00
|
|
|
|
private globalEventService: GlobalEventService,
|
2022-09-17 13:27:08 -05:00
|
|
|
|
) {
|
2023-08-16 03:51:28 -05:00
|
|
|
|
this.cache = new MemoryKVCache<MiEmoji | null>(1000 * 60 * 60 * 12);
|
2023-04-05 21:14:43 -05:00
|
|
|
|
|
2023-08-16 03:51:28 -05:00
|
|
|
|
this.localEmojisCache = new RedisSingleCache<Map<string, MiEmoji>>(this.redisClient, 'localEmojis', {
|
2023-04-05 21:14:43 -05:00
|
|
|
|
lifetime: 1000 * 60 * 30, // 30m
|
|
|
|
|
memoryCacheLifetime: 1000 * 60 * 3, // 3m
|
|
|
|
|
fetcher: () => this.emojisRepository.find({ where: { host: IsNull() } }).then(emojis => new Map(emojis.map(emoji => [emoji.name, emoji]))),
|
2023-04-10 17:39:46 -05:00
|
|
|
|
toRedisConverter: (value) => JSON.stringify(Array.from(value.values())),
|
2023-04-12 02:07:58 -05:00
|
|
|
|
fromRedisConverter: (value) => {
|
2023-08-16 03:51:28 -05:00
|
|
|
|
return new Map(JSON.parse(value).map((x: Serialized<MiEmoji>) => [x.name, {
|
2023-04-12 02:07:58 -05:00
|
|
|
|
...x,
|
2023-04-12 19:09:29 -05:00
|
|
|
|
updatedAt: x.updatedAt ? new Date(x.updatedAt) : null,
|
2023-04-12 02:07:58 -05:00
|
|
|
|
}]));
|
|
|
|
|
},
|
2023-04-05 21:14:43 -05:00
|
|
|
|
});
|
2022-09-17 13:27:08 -05:00
|
|
|
|
}
|
|
|
|
|
|
2022-12-04 00:03:09 -06:00
|
|
|
|
@bindThis
|
2022-09-17 13:27:08 -05:00
|
|
|
|
public async add(data: {
|
2023-08-16 03:51:28 -05:00
|
|
|
|
driveFile: MiDriveFile;
|
2022-09-17 13:27:08 -05:00
|
|
|
|
name: string;
|
|
|
|
|
category: string | null;
|
|
|
|
|
aliases: string[];
|
|
|
|
|
host: string | null;
|
2023-03-16 01:08:48 -05:00
|
|
|
|
license: string | null;
|
2023-05-18 04:45:49 -05:00
|
|
|
|
isSensitive: boolean;
|
|
|
|
|
localOnly: boolean;
|
2023-08-16 03:51:28 -05:00
|
|
|
|
roleIdsThatCanBeUsedThisEmojiAsReaction: MiRole['id'][];
|
2023-09-23 20:57:24 -05:00
|
|
|
|
}, moderator?: MiUser): Promise<MiEmoji> {
|
2022-09-17 13:27:08 -05:00
|
|
|
|
const emoji = await this.emojisRepository.insert({
|
2023-10-15 20:45:22 -05:00
|
|
|
|
id: this.idService.gen(),
|
2022-09-17 13:27:08 -05:00
|
|
|
|
updatedAt: new Date(),
|
|
|
|
|
name: data.name,
|
|
|
|
|
category: data.category,
|
|
|
|
|
host: data.host,
|
|
|
|
|
aliases: data.aliases,
|
|
|
|
|
originalUrl: data.driveFile.url,
|
|
|
|
|
publicUrl: data.driveFile.webpublicUrl ?? data.driveFile.url,
|
|
|
|
|
type: data.driveFile.webpublicType ?? data.driveFile.type,
|
2023-03-16 01:08:48 -05:00
|
|
|
|
license: data.license,
|
2023-05-18 04:45:49 -05:00
|
|
|
|
isSensitive: data.isSensitive,
|
|
|
|
|
localOnly: data.localOnly,
|
|
|
|
|
roleIdsThatCanBeUsedThisEmojiAsReaction: data.roleIdsThatCanBeUsedThisEmojiAsReaction,
|
2022-09-17 13:27:08 -05:00
|
|
|
|
}).then(x => this.emojisRepository.findOneByOrFail(x.identifiers[0]));
|
|
|
|
|
|
2023-01-25 23:29:28 -06:00
|
|
|
|
if (data.host == null) {
|
2023-04-05 21:14:43 -05:00
|
|
|
|
this.localEmojisCache.refresh();
|
2022-09-17 13:27:08 -05:00
|
|
|
|
|
2023-01-25 23:29:28 -06:00
|
|
|
|
this.globalEventService.publishBroadcastStream('emojiAdded', {
|
2023-02-17 00:36:36 -06:00
|
|
|
|
emoji: await this.emojiEntityService.packDetailed(emoji.id),
|
2023-01-25 23:29:28 -06:00
|
|
|
|
});
|
2023-09-23 20:57:24 -05:00
|
|
|
|
|
|
|
|
|
if (moderator) {
|
|
|
|
|
this.moderationLogService.log(moderator, 'addCustomEmoji', {
|
|
|
|
|
emojiId: emoji.id,
|
|
|
|
|
emoji: emoji,
|
|
|
|
|
});
|
|
|
|
|
}
|
2023-01-25 23:29:28 -06:00
|
|
|
|
}
|
2023-01-22 08:53:24 -06:00
|
|
|
|
|
2022-09-17 13:27:08 -05:00
|
|
|
|
return emoji;
|
|
|
|
|
}
|
2023-01-26 00:48:12 -06:00
|
|
|
|
|
2023-04-05 21:14:43 -05:00
|
|
|
|
@bindThis
|
2023-08-16 03:51:28 -05:00
|
|
|
|
public async update(id: MiEmoji['id'], data: {
|
|
|
|
|
driveFile?: MiDriveFile;
|
2023-04-05 21:14:43 -05:00
|
|
|
|
name?: string;
|
|
|
|
|
category?: string | null;
|
|
|
|
|
aliases?: string[];
|
|
|
|
|
license?: string | null;
|
2023-05-18 04:45:49 -05:00
|
|
|
|
isSensitive?: boolean;
|
|
|
|
|
localOnly?: boolean;
|
2023-08-16 03:51:28 -05:00
|
|
|
|
roleIdsThatCanBeUsedThisEmojiAsReaction?: MiRole['id'][];
|
2023-09-23 20:57:24 -05:00
|
|
|
|
}, moderator?: MiUser): Promise<void> {
|
2023-04-05 21:14:43 -05:00
|
|
|
|
const emoji = await this.emojisRepository.findOneByOrFail({ id: id });
|
|
|
|
|
const sameNameEmoji = await this.emojisRepository.findOneBy({ name: data.name, host: IsNull() });
|
|
|
|
|
if (sameNameEmoji != null && sameNameEmoji.id !== id) throw new Error('name already exists');
|
|
|
|
|
|
|
|
|
|
await this.emojisRepository.update(emoji.id, {
|
|
|
|
|
updatedAt: new Date(),
|
|
|
|
|
name: data.name,
|
|
|
|
|
category: data.category,
|
|
|
|
|
aliases: data.aliases,
|
|
|
|
|
license: data.license,
|
2023-05-18 04:45:49 -05:00
|
|
|
|
isSensitive: data.isSensitive,
|
|
|
|
|
localOnly: data.localOnly,
|
|
|
|
|
originalUrl: data.driveFile != null ? data.driveFile.url : undefined,
|
|
|
|
|
publicUrl: data.driveFile != null ? (data.driveFile.webpublicUrl ?? data.driveFile.url) : undefined,
|
|
|
|
|
type: data.driveFile != null ? (data.driveFile.webpublicType ?? data.driveFile.type) : undefined,
|
|
|
|
|
roleIdsThatCanBeUsedThisEmojiAsReaction: data.roleIdsThatCanBeUsedThisEmojiAsReaction ?? undefined,
|
2023-04-05 21:14:43 -05:00
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
this.localEmojisCache.refresh();
|
|
|
|
|
|
2023-09-24 20:29:12 -05:00
|
|
|
|
const packed = await this.emojiEntityService.packDetailed(emoji.id);
|
2023-04-05 21:14:43 -05:00
|
|
|
|
|
|
|
|
|
if (emoji.name === data.name) {
|
|
|
|
|
this.globalEventService.publishBroadcastStream('emojiUpdated', {
|
2023-09-24 20:29:12 -05:00
|
|
|
|
emojis: [packed],
|
2023-04-05 21:14:43 -05:00
|
|
|
|
});
|
|
|
|
|
} else {
|
|
|
|
|
this.globalEventService.publishBroadcastStream('emojiDeleted', {
|
|
|
|
|
emojis: [await this.emojiEntityService.packDetailed(emoji)],
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
this.globalEventService.publishBroadcastStream('emojiAdded', {
|
2023-09-24 20:29:12 -05:00
|
|
|
|
emoji: packed,
|
2023-07-07 17:08:16 -05:00
|
|
|
|
});
|
2023-04-05 21:14:43 -05:00
|
|
|
|
}
|
2023-09-23 20:57:24 -05:00
|
|
|
|
|
|
|
|
|
if (moderator) {
|
2023-09-24 20:29:12 -05:00
|
|
|
|
const updated = await this.emojisRepository.findOneByOrFail({ id: id });
|
2023-09-23 20:57:24 -05:00
|
|
|
|
this.moderationLogService.log(moderator, 'updateCustomEmoji', {
|
|
|
|
|
emojiId: emoji.id,
|
|
|
|
|
before: emoji,
|
|
|
|
|
after: updated,
|
|
|
|
|
});
|
|
|
|
|
}
|
2023-04-05 21:14:43 -05:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@bindThis
|
2023-08-16 03:51:28 -05:00
|
|
|
|
public async addAliasesBulk(ids: MiEmoji['id'][], aliases: string[]) {
|
2023-04-05 21:14:43 -05:00
|
|
|
|
const emojis = await this.emojisRepository.findBy({
|
|
|
|
|
id: In(ids),
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
for (const emoji of emojis) {
|
|
|
|
|
await this.emojisRepository.update(emoji.id, {
|
|
|
|
|
updatedAt: new Date(),
|
|
|
|
|
aliases: [...new Set(emoji.aliases.concat(aliases))],
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
this.localEmojisCache.refresh();
|
|
|
|
|
|
|
|
|
|
this.globalEventService.publishBroadcastStream('emojiUpdated', {
|
|
|
|
|
emojis: await this.emojiEntityService.packDetailedMany(ids),
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@bindThis
|
2023-08-16 03:51:28 -05:00
|
|
|
|
public async setAliasesBulk(ids: MiEmoji['id'][], aliases: string[]) {
|
2023-04-05 21:14:43 -05:00
|
|
|
|
await this.emojisRepository.update({
|
|
|
|
|
id: In(ids),
|
|
|
|
|
}, {
|
|
|
|
|
updatedAt: new Date(),
|
|
|
|
|
aliases: aliases,
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
this.localEmojisCache.refresh();
|
|
|
|
|
|
|
|
|
|
this.globalEventService.publishBroadcastStream('emojiUpdated', {
|
|
|
|
|
emojis: await this.emojiEntityService.packDetailedMany(ids),
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@bindThis
|
2023-08-16 03:51:28 -05:00
|
|
|
|
public async removeAliasesBulk(ids: MiEmoji['id'][], aliases: string[]) {
|
2023-04-05 21:14:43 -05:00
|
|
|
|
const emojis = await this.emojisRepository.findBy({
|
|
|
|
|
id: In(ids),
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
for (const emoji of emojis) {
|
|
|
|
|
await this.emojisRepository.update(emoji.id, {
|
|
|
|
|
updatedAt: new Date(),
|
|
|
|
|
aliases: emoji.aliases.filter(x => !aliases.includes(x)),
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
this.localEmojisCache.refresh();
|
2023-07-07 17:08:16 -05:00
|
|
|
|
|
2023-04-05 21:14:43 -05:00
|
|
|
|
this.globalEventService.publishBroadcastStream('emojiUpdated', {
|
|
|
|
|
emojis: await this.emojiEntityService.packDetailedMany(ids),
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@bindThis
|
2023-08-16 03:51:28 -05:00
|
|
|
|
public async setCategoryBulk(ids: MiEmoji['id'][], category: string | null) {
|
2023-04-05 21:14:43 -05:00
|
|
|
|
await this.emojisRepository.update({
|
|
|
|
|
id: In(ids),
|
|
|
|
|
}, {
|
|
|
|
|
updatedAt: new Date(),
|
|
|
|
|
category: category,
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
this.localEmojisCache.refresh();
|
|
|
|
|
|
|
|
|
|
this.globalEventService.publishBroadcastStream('emojiUpdated', {
|
|
|
|
|
emojis: await this.emojiEntityService.packDetailedMany(ids),
|
|
|
|
|
});
|
|
|
|
|
}
|
2023-07-07 17:08:16 -05:00
|
|
|
|
|
2023-04-18 18:25:24 -05:00
|
|
|
|
@bindThis
|
2023-08-16 03:51:28 -05:00
|
|
|
|
public async setLicenseBulk(ids: MiEmoji['id'][], license: string | null) {
|
2023-04-18 18:25:24 -05:00
|
|
|
|
await this.emojisRepository.update({
|
|
|
|
|
id: In(ids),
|
|
|
|
|
}, {
|
|
|
|
|
updatedAt: new Date(),
|
|
|
|
|
license: license,
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
this.localEmojisCache.refresh();
|
|
|
|
|
|
|
|
|
|
this.globalEventService.publishBroadcastStream('emojiUpdated', {
|
|
|
|
|
emojis: await this.emojiEntityService.packDetailedMany(ids),
|
|
|
|
|
});
|
|
|
|
|
}
|
2023-04-05 21:14:43 -05:00
|
|
|
|
|
|
|
|
|
@bindThis
|
2023-09-23 20:57:24 -05:00
|
|
|
|
public async delete(id: MiEmoji['id'], moderator?: MiUser) {
|
2023-04-05 21:14:43 -05:00
|
|
|
|
const emoji = await this.emojisRepository.findOneByOrFail({ id: id });
|
|
|
|
|
|
|
|
|
|
await this.emojisRepository.delete(emoji.id);
|
|
|
|
|
|
|
|
|
|
this.localEmojisCache.refresh();
|
|
|
|
|
|
|
|
|
|
this.globalEventService.publishBroadcastStream('emojiDeleted', {
|
|
|
|
|
emojis: [await this.emojiEntityService.packDetailed(emoji)],
|
|
|
|
|
});
|
2023-09-23 20:57:24 -05:00
|
|
|
|
|
|
|
|
|
if (moderator) {
|
|
|
|
|
this.moderationLogService.log(moderator, 'deleteCustomEmoji', {
|
|
|
|
|
emojiId: emoji.id,
|
|
|
|
|
emoji: emoji,
|
|
|
|
|
});
|
|
|
|
|
}
|
2023-04-05 21:14:43 -05:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@bindThis
|
2023-09-23 20:57:24 -05:00
|
|
|
|
public async deleteBulk(ids: MiEmoji['id'][], moderator?: MiUser) {
|
2023-04-05 21:14:43 -05:00
|
|
|
|
const emojis = await this.emojisRepository.findBy({
|
|
|
|
|
id: In(ids),
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
for (const emoji of emojis) {
|
|
|
|
|
await this.emojisRepository.delete(emoji.id);
|
2023-09-23 20:57:24 -05:00
|
|
|
|
|
|
|
|
|
if (moderator) {
|
|
|
|
|
this.moderationLogService.log(moderator, 'deleteCustomEmoji', {
|
|
|
|
|
emojiId: emoji.id,
|
|
|
|
|
emoji: emoji,
|
|
|
|
|
});
|
|
|
|
|
}
|
2023-04-05 21:14:43 -05:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
this.localEmojisCache.refresh();
|
|
|
|
|
|
|
|
|
|
this.globalEventService.publishBroadcastStream('emojiDeleted', {
|
|
|
|
|
emojis: await this.emojiEntityService.packDetailedMany(emojis),
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-26 00:48:12 -06:00
|
|
|
|
@bindThis
|
|
|
|
|
private normalizeHost(src: string | undefined, noteUserHost: string | null): string | null {
|
|
|
|
|
// クエリに使うホスト
|
|
|
|
|
let host = src === '.' ? null // .はローカルホスト (ここがマッチするのはリアクションのみ)
|
|
|
|
|
: src === undefined ? noteUserHost // ノートなどでホスト省略表記の場合はローカルホスト (ここがリアクションにマッチすることはない)
|
|
|
|
|
: this.utilityService.isSelfHost(src) ? null // 自ホスト指定
|
|
|
|
|
: (src || noteUserHost); // 指定されたホスト || ノートなどの所有者のホスト (こっちがリアクションにマッチすることはない)
|
|
|
|
|
|
|
|
|
|
host = this.utilityService.toPunyNullable(host);
|
|
|
|
|
|
|
|
|
|
return host;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@bindThis
|
2023-04-05 21:14:43 -05:00
|
|
|
|
public parseEmojiStr(emojiName: string, noteUserHost: string | null) {
|
2023-05-18 04:45:49 -05:00
|
|
|
|
const match = emojiName.match(parseEmojiStrRegexp);
|
2023-01-26 00:48:12 -06:00
|
|
|
|
if (!match) return { name: null, host: null };
|
|
|
|
|
|
|
|
|
|
const name = match[1];
|
|
|
|
|
|
|
|
|
|
// ホスト正規化
|
|
|
|
|
const host = this.utilityService.toPunyNullable(this.normalizeHost(match[2], noteUserHost));
|
|
|
|
|
|
|
|
|
|
return { name, host };
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 添付用(リモート)カスタム絵文字URLを解決する
|
|
|
|
|
* @param emojiName ノートやユーザープロフィールに添付された、またはリアクションのカスタム絵文字名 (:は含めない, リアクションでローカルホストの場合は@.を付ける (これはdecodeReactionで可能))
|
|
|
|
|
* @param noteUserHost ノートやユーザープロフィールの所有者のホスト
|
|
|
|
|
* @returns URL, nullは未マッチを意味する
|
|
|
|
|
*/
|
|
|
|
|
@bindThis
|
|
|
|
|
public async populateEmoji(emojiName: string, noteUserHost: string | null): Promise<string | null> {
|
|
|
|
|
const { name, host } = this.parseEmojiStr(emojiName, noteUserHost);
|
|
|
|
|
if (name == null) return null;
|
|
|
|
|
if (host == null) return null;
|
|
|
|
|
|
|
|
|
|
const queryOrNull = async () => (await this.emojisRepository.findOneBy({
|
|
|
|
|
name,
|
2023-10-17 19:54:18 -05:00
|
|
|
|
host,
|
2023-01-26 00:48:12 -06:00
|
|
|
|
})) ?? null;
|
|
|
|
|
|
|
|
|
|
const emoji = await this.cache.fetch(`${name} ${host}`, queryOrNull);
|
|
|
|
|
|
|
|
|
|
if (emoji == null) return null;
|
2023-04-11 20:58:56 -05:00
|
|
|
|
return emoji.publicUrl || emoji.originalUrl; // || emoji.originalUrl してるのは後方互換性のため(publicUrlはstringなので??はだめ)
|
2023-01-26 00:48:12 -06:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 複数の添付用(リモート)カスタム絵文字URLを解決する (キャシュ付き, 存在しないものは結果から除外される)
|
|
|
|
|
*/
|
|
|
|
|
@bindThis
|
|
|
|
|
public async populateEmojis(emojiNames: string[], noteUserHost: string | null): Promise<Record<string, string>> {
|
|
|
|
|
const emojis = await Promise.all(emojiNames.map(x => this.populateEmoji(x, noteUserHost)));
|
|
|
|
|
const res = {} as any;
|
|
|
|
|
for (let i = 0; i < emojiNames.length; i++) {
|
|
|
|
|
if (emojis[i] != null) {
|
|
|
|
|
res[emojiNames[i]] = emojis[i];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return res;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 与えられた絵文字のリストをデータベースから取得し、キャッシュに追加します
|
|
|
|
|
*/
|
|
|
|
|
@bindThis
|
|
|
|
|
public async prefetchEmojis(emojis: { name: string; host: string | null; }[]): Promise<void> {
|
|
|
|
|
const notCachedEmojis = emojis.filter(emoji => this.cache.get(`${emoji.name} ${emoji.host}`) == null);
|
|
|
|
|
const emojisQuery: any[] = [];
|
|
|
|
|
const hosts = new Set(notCachedEmojis.map(e => e.host));
|
|
|
|
|
for (const host of hosts) {
|
|
|
|
|
if (host == null) continue;
|
|
|
|
|
emojisQuery.push({
|
|
|
|
|
name: In(notCachedEmojis.filter(e => e.host === host).map(e => e.name)),
|
|
|
|
|
host: host,
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
const _emojis = emojisQuery.length > 0 ? await this.emojisRepository.find({
|
|
|
|
|
where: emojisQuery,
|
|
|
|
|
select: ['name', 'host', 'originalUrl', 'publicUrl'],
|
|
|
|
|
}) : [];
|
|
|
|
|
for (const emoji of _emojis) {
|
|
|
|
|
this.cache.set(`${emoji.name} ${emoji.host}`, emoji);
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-06-09 23:45:11 -05:00
|
|
|
|
|
2023-10-06 07:05:44 -05:00
|
|
|
|
/**
|
|
|
|
|
* ローカル内の絵文字に重複がないかチェックします
|
|
|
|
|
* @param name 絵文字名
|
|
|
|
|
*/
|
|
|
|
|
@bindThis
|
|
|
|
|
public checkDuplicate(name: string): Promise<boolean> {
|
2024-02-08 01:04:41 -06:00
|
|
|
|
return this.emojisRepository.exists({ where: { name, host: IsNull() } });
|
2023-10-06 07:05:44 -05:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@bindThis
|
|
|
|
|
public getEmojiById(id: string): Promise<MiEmoji | null> {
|
|
|
|
|
return this.emojisRepository.findOneBy({ id });
|
|
|
|
|
}
|
|
|
|
|
|
2024-02-25 03:06:26 -06:00
|
|
|
|
@bindThis
|
|
|
|
|
public getEmojiByName(name: string): Promise<MiEmoji | null> {
|
|
|
|
|
return this.emojisRepository.findOneBy({ name, host: IsNull() });
|
|
|
|
|
}
|
|
|
|
|
|
2023-06-09 23:45:11 -05:00
|
|
|
|
@bindThis
|
|
|
|
|
public dispose(): void {
|
|
|
|
|
this.cache.dispose();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@bindThis
|
|
|
|
|
public onApplicationShutdown(signal?: string | undefined): void {
|
|
|
|
|
this.dispose();
|
|
|
|
|
}
|
2022-09-17 13:27:08 -05:00
|
|
|
|
}
|