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

@@ -49,6 +49,7 @@ type DecodedReaction = {
*/
host?: string | null;
};
import { bindThis } from '@/decorators.js';
@Injectable()
export class ReactionService {
@@ -81,6 +82,7 @@ export class ReactionService {
) {
}
@bindThis
public async create(user: { id: User['id']; host: User['host']; }, note: Note, reaction?: string) {
// Check blocking
if (note.userId !== user.id) {
@@ -196,6 +198,7 @@ export class ReactionService {
//#endregion
}
@bindThis
public async delete(user: { id: User['id']; host: User['host']; }, note: Note) {
// if already unreacted
const exist = await this.noteReactionsRepository.findOneBy({
@@ -244,11 +247,13 @@ export class ReactionService {
//#endregion
}
@bindThis
public async getFallbackReaction(): Promise<string> {
const meta = await this.metaService.fetch();
return meta.useStarForReactionFallback ? '⭐' : '👍';
}
@bindThis
public convertLegacyReactions(reactions: Record<string, number>) {
const _reactions = {} as Record<string, number>;
@@ -279,6 +284,7 @@ export class ReactionService {
return _reactions2;
}
@bindThis
public async toDbReaction(reaction?: string | null, reacterHost?: string | null): Promise<string> {
if (reaction == null) return await this.getFallbackReaction();
@@ -311,6 +317,7 @@ export class ReactionService {
return await this.getFallbackReaction();
}
@bindThis
public decodeReaction(str: string): DecodedReaction {
const custom = str.match(/^:([\w+-]+)(?:@([\w.-]+))?:$/);
@@ -332,6 +339,7 @@ export class ReactionService {
};
}
@bindThis
public convertLegacyReaction(reaction: string): string {
reaction = this.decodeReaction(reaction).reaction;
if (Object.keys(legacies).includes(reaction)) return legacies[reaction];