refactor: introduce bindThis decorator to bind this automaticaly

This commit is contained in:
syuilo 2022-12-04 15:03:09 +09:00
parent e73581f715
commit bbb49457f9
199 changed files with 969 additions and 96 deletions

View file

@ -20,6 +20,7 @@ type PopulatedEmoji = {
name: string;
url: string;
};
import { bindThis } from '@/decorators.js';
@Injectable()
export class CustomEmojiService {
@ -43,6 +44,7 @@ export class CustomEmojiService {
this.cache = new Cache<Emoji | null>(1000 * 60 * 60 * 12);
}
@bindThis
public async add(data: {
driveFile: DriveFile;
name: string;
@ -67,6 +69,7 @@ export class CustomEmojiService {
return emoji;
}
@bindThis
private normalizeHost(src: string | undefined, noteUserHost: string | null): string | null {
// クエリに使うホスト
let host = src === '.' ? null // .はローカルホスト (ここがマッチするのはリアクションのみ)
@ -79,6 +82,7 @@ export class CustomEmojiService {
return host;
}
@bindThis
private parseEmojiStr(emojiName: string, noteUserHost: string | null) {
const match = emojiName.match(/^(\w+)(?:@([\w.-]+))?$/);
if (!match) return { name: null, host: null };
@ -97,6 +101,7 @@ export class CustomEmojiService {
* @param noteUserHost
* @returns , nullは未マッチを意味する
*/
@bindThis
public async populateEmoji(emojiName: string, noteUserHost: string | null): Promise<PopulatedEmoji | null> {
const { name, host } = this.parseEmojiStr(emojiName, noteUserHost);
if (name == null) return null;
@ -123,11 +128,13 @@ export class CustomEmojiService {
/**
* (, )
*/
@bindThis
public async populateEmojis(emojiNames: string[], noteUserHost: string | null): Promise<PopulatedEmoji[]> {
const emojis = await Promise.all(emojiNames.map(x => this.populateEmoji(x, noteUserHost)));
return emojis.filter((x): x is PopulatedEmoji => x != null);
}
@bindThis
public aggregateNoteEmojis(notes: Note[]) {
let emojis: { name: string | null; host: string | null; }[] = [];
for (const note of notes) {
@ -154,6 +161,7 @@ export class CustomEmojiService {
/**
*
*/
@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[] = [];