2022-09-17 18:27:08 +00:00
|
|
|
import { Inject, Injectable } from '@nestjs/common';
|
2022-09-20 20:33:11 +00:00
|
|
|
import type { UserListJoiningsRepository, UsersRepository } from '@/models/index.js';
|
2022-09-17 18:27:08 +00:00
|
|
|
import type { User } from '@/models/entities/User.js';
|
|
|
|
import type { UserList } from '@/models/entities/UserList.js';
|
|
|
|
import type { UserListJoining } from '@/models/entities/UserListJoining.js';
|
|
|
|
import { IdService } from '@/core/IdService.js';
|
|
|
|
import { UserFollowingService } from '@/core/UserFollowingService.js';
|
|
|
|
import { GlobalEventService } from '@/core/GlobalEventService.js';
|
|
|
|
import { DI } from '@/di-symbols.js';
|
2022-12-04 01:16:03 +00:00
|
|
|
import { UserEntityService } from '@/core/entities/UserEntityService.js';
|
|
|
|
import { ProxyAccountService } from '@/core/ProxyAccountService.js';
|
2022-12-04 06:03:09 +00:00
|
|
|
import { bindThis } from '@/decorators.js';
|
2023-01-14 08:38:16 +00:00
|
|
|
import { RoleService } from '@/core/RoleService.js';
|
2023-04-12 00:13:58 +00:00
|
|
|
import { QueueService } from '@/core/QueueService.js';
|
2022-09-17 18:27:08 +00:00
|
|
|
|
|
|
|
@Injectable()
|
|
|
|
export class UserListService {
|
2023-02-12 01:36:43 +00:00
|
|
|
public static TooManyUsersError = class extends Error {};
|
|
|
|
|
2022-09-17 18:27:08 +00:00
|
|
|
constructor(
|
|
|
|
@Inject(DI.usersRepository)
|
|
|
|
private usersRepository: UsersRepository,
|
|
|
|
|
|
|
|
@Inject(DI.userListJoiningsRepository)
|
|
|
|
private userListJoiningsRepository: UserListJoiningsRepository,
|
|
|
|
|
|
|
|
private userEntityService: UserEntityService,
|
|
|
|
private idService: IdService,
|
|
|
|
private userFollowingService: UserFollowingService,
|
2023-01-14 08:38:16 +00:00
|
|
|
private roleService: RoleService,
|
2023-02-04 01:02:03 +00:00
|
|
|
private globalEventService: GlobalEventService,
|
2022-09-17 18:27:08 +00:00
|
|
|
private proxyAccountService: ProxyAccountService,
|
2023-04-12 00:13:58 +00:00
|
|
|
private queueService: QueueService,
|
2022-09-17 18:27:08 +00:00
|
|
|
) {
|
|
|
|
}
|
|
|
|
|
2022-12-04 06:03:09 +00:00
|
|
|
@bindThis
|
2023-01-14 08:38:16 +00:00
|
|
|
public async push(target: User, list: UserList, me: User) {
|
|
|
|
const currentCount = await this.userListJoiningsRepository.countBy({
|
|
|
|
userListId: list.id,
|
|
|
|
});
|
2023-01-15 11:52:53 +00:00
|
|
|
if (currentCount > (await this.roleService.getUserPolicies(me.id)).userEachUserListsLimit) {
|
2023-02-12 01:36:43 +00:00
|
|
|
throw new UserListService.TooManyUsersError();
|
2023-01-14 08:38:16 +00:00
|
|
|
}
|
|
|
|
|
2022-09-17 18:27:08 +00:00
|
|
|
await this.userListJoiningsRepository.insert({
|
|
|
|
id: this.idService.genId(),
|
|
|
|
createdAt: new Date(),
|
|
|
|
userId: target.id,
|
|
|
|
userListId: list.id,
|
|
|
|
} as UserListJoining);
|
2023-04-12 00:13:58 +00:00
|
|
|
|
2023-02-04 01:02:03 +00:00
|
|
|
this.globalEventService.publishUserListStream(list.id, 'userAdded', await this.userEntityService.pack(target));
|
2023-04-12 00:13:58 +00:00
|
|
|
|
2022-09-17 18:27:08 +00:00
|
|
|
// このインスタンス内にこのリモートユーザーをフォローしているユーザーがいなくても投稿を受け取るためにダミーのユーザーがフォローしたということにする
|
|
|
|
if (this.userEntityService.isRemoteUser(target)) {
|
|
|
|
const proxy = await this.proxyAccountService.fetch();
|
|
|
|
if (proxy) {
|
2023-04-12 00:13:58 +00:00
|
|
|
this.queueService.createFollowJob([{ from: { id: proxy.id }, to: { id: target.id } }]);
|
2022-09-17 18:27:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|