egirlskey/packages/backend/src/core/UserMutingService.ts

48 lines
1.3 KiB
TypeScript
Raw Normal View History

2022-09-17 18:27:08 +00:00
import { Inject, Injectable } from '@nestjs/common';
2023-04-05 01:21:10 +00:00
import { In } from 'typeorm';
import type { MutingsRepository, Muting } from '@/models/index.js';
2022-09-17 18:27:08 +00:00
import { IdService } from '@/core/IdService.js';
import type { User } from '@/models/entities/User.js';
import { DI } from '@/di-symbols.js';
import { bindThis } from '@/decorators.js';
2023-04-05 04:50:05 +00:00
import { CacheService } from '@/core/CacheService.js';
2022-09-17 18:27:08 +00:00
@Injectable()
export class UserMutingService {
constructor(
@Inject(DI.mutingsRepository)
private mutingsRepository: MutingsRepository,
private idService: IdService,
2023-04-05 01:21:10 +00:00
private cacheService: CacheService,
2022-09-17 18:27:08 +00:00
) {
}
@bindThis
2023-04-05 01:21:10 +00:00
public async mute(user: User, target: User, expiresAt: Date | null = null): Promise<void> {
2022-09-17 18:27:08 +00:00
await this.mutingsRepository.insert({
id: this.idService.genId(),
createdAt: new Date(),
2023-04-05 01:21:10 +00:00
expiresAt: expiresAt ?? null,
2022-09-17 18:27:08 +00:00
muterId: user.id,
muteeId: target.id,
});
2023-04-05 01:21:10 +00:00
this.cacheService.userMutingsCache.refresh(user.id);
}
@bindThis
public async unmute(mutings: Muting[]): Promise<void> {
if (mutings.length === 0) return;
await this.mutingsRepository.delete({
id: In(mutings.map(m => m.id)),
});
const muterIds = [...new Set(mutings.map(m => m.muterId))];
for (const muterId of muterIds) {
this.cacheService.userMutingsCache.refresh(muterId);
}
2022-09-17 18:27:08 +00:00
}
}