2023-07-27 07:31:52 +02:00
|
|
|
/*
|
2024-02-12 03:37:45 +01:00
|
|
|
* SPDX-FileCopyrightText: syuilo and misskey-project
|
2023-07-27 07:31:52 +02:00
|
|
|
* SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
*/
|
2022-09-17 20:27:08 +02:00
|
|
|
|
2023-04-05 06:57:22 +02:00
|
|
|
import { Inject, Injectable, OnModuleInit } from '@nestjs/common';
|
|
|
|
import { ModuleRef } from '@nestjs/core';
|
2022-09-17 20:27:08 +02:00
|
|
|
import { IdService } from '@/core/IdService.js';
|
2023-09-20 04:33:36 +02:00
|
|
|
import type { MiUser } from '@/models/User.js';
|
|
|
|
import type { MiBlocking } from '@/models/Blocking.js';
|
2022-09-17 20:27:08 +02:00
|
|
|
import { QueueService } from '@/core/QueueService.js';
|
|
|
|
import { GlobalEventService } from '@/core/GlobalEventService.js';
|
|
|
|
import { DI } from '@/di-symbols.js';
|
2023-10-03 13:26:11 +02:00
|
|
|
import type { FollowRequestsRepository, BlockingsRepository, UserListsRepository, UserListMembershipsRepository } from '@/models/_.js';
|
2022-09-24 00:12:11 +02:00
|
|
|
import Logger from '@/logger.js';
|
2022-12-04 02:16:03 +01:00
|
|
|
import { UserEntityService } from '@/core/entities/UserEntityService.js';
|
|
|
|
import { ApRendererService } from '@/core/activitypub/ApRendererService.js';
|
|
|
|
import { LoggerService } from '@/core/LoggerService.js';
|
|
|
|
import { WebhookService } from '@/core/WebhookService.js';
|
2022-12-04 07:03:09 +01:00
|
|
|
import { bindThis } from '@/decorators.js';
|
2023-04-05 03:21:10 +02:00
|
|
|
import { CacheService } from '@/core/CacheService.js';
|
|
|
|
import { UserFollowingService } from '@/core/UserFollowingService.js';
|
2022-09-17 20:27:08 +02:00
|
|
|
|
|
|
|
@Injectable()
|
2023-04-05 06:57:22 +02:00
|
|
|
export class UserBlockingService implements OnModuleInit {
|
2022-09-24 00:12:11 +02:00
|
|
|
private logger: Logger;
|
2023-04-05 06:57:22 +02:00
|
|
|
private userFollowingService: UserFollowingService;
|
2022-09-24 00:12:11 +02:00
|
|
|
|
2022-09-17 20:27:08 +02:00
|
|
|
constructor(
|
2023-04-05 06:57:22 +02:00
|
|
|
private moduleRef: ModuleRef,
|
2023-04-12 02:13:58 +02:00
|
|
|
|
2022-09-17 20:27:08 +02:00
|
|
|
@Inject(DI.followRequestsRepository)
|
|
|
|
private followRequestsRepository: FollowRequestsRepository,
|
|
|
|
|
|
|
|
@Inject(DI.blockingsRepository)
|
|
|
|
private blockingsRepository: BlockingsRepository,
|
|
|
|
|
|
|
|
@Inject(DI.userListsRepository)
|
|
|
|
private userListsRepository: UserListsRepository,
|
|
|
|
|
2023-10-03 13:26:11 +02:00
|
|
|
@Inject(DI.userListMembershipsRepository)
|
|
|
|
private userListMembershipsRepository: UserListMembershipsRepository,
|
2022-09-17 20:27:08 +02:00
|
|
|
|
2023-04-05 03:21:10 +02:00
|
|
|
private cacheService: CacheService,
|
2022-09-17 20:27:08 +02:00
|
|
|
private userEntityService: UserEntityService,
|
|
|
|
private idService: IdService,
|
|
|
|
private queueService: QueueService,
|
2023-02-04 02:02:03 +01:00
|
|
|
private globalEventService: GlobalEventService,
|
2022-09-17 20:27:08 +02:00
|
|
|
private webhookService: WebhookService,
|
|
|
|
private apRendererService: ApRendererService,
|
2022-09-24 00:12:11 +02:00
|
|
|
private loggerService: LoggerService,
|
2022-09-17 20:27:08 +02:00
|
|
|
) {
|
2022-09-24 00:12:11 +02:00
|
|
|
this.logger = this.loggerService.getLogger('user-block');
|
2022-09-17 20:27:08 +02:00
|
|
|
}
|
|
|
|
|
2023-04-05 06:57:22 +02:00
|
|
|
onModuleInit() {
|
|
|
|
this.userFollowingService = this.moduleRef.get('UserFollowingService');
|
|
|
|
}
|
|
|
|
|
2022-12-04 07:03:09 +01:00
|
|
|
@bindThis
|
2023-08-16 10:51:28 +02:00
|
|
|
public async block(blocker: MiUser, blockee: MiUser, silent = false) {
|
2022-09-17 20:27:08 +02:00
|
|
|
await Promise.all([
|
2023-04-12 02:13:58 +02:00
|
|
|
this.cancelRequest(blocker, blockee, silent),
|
|
|
|
this.cancelRequest(blockee, blocker, silent),
|
|
|
|
this.userFollowingService.unfollow(blocker, blockee, silent),
|
|
|
|
this.userFollowingService.unfollow(blockee, blocker, silent),
|
2022-09-18 20:11:50 +02:00
|
|
|
this.removeFromList(blockee, blocker),
|
2022-09-17 20:27:08 +02:00
|
|
|
]);
|
|
|
|
|
|
|
|
const blocking = {
|
2023-10-16 03:45:22 +02:00
|
|
|
id: this.idService.gen(),
|
2022-09-17 20:27:08 +02:00
|
|
|
blocker,
|
|
|
|
blockerId: blocker.id,
|
|
|
|
blockee,
|
|
|
|
blockeeId: blockee.id,
|
2023-08-16 10:51:28 +02:00
|
|
|
} as MiBlocking;
|
2022-09-17 20:27:08 +02:00
|
|
|
|
|
|
|
await this.blockingsRepository.insert(blocking);
|
|
|
|
|
2023-04-05 03:21:10 +02:00
|
|
|
this.cacheService.userBlockingCache.refresh(blocker.id);
|
|
|
|
this.cacheService.userBlockedCache.refresh(blockee.id);
|
|
|
|
|
2023-02-04 04:40:40 +01:00
|
|
|
this.globalEventService.publishInternalEvent('blockingCreated', {
|
|
|
|
blockerId: blocker.id,
|
|
|
|
blockeeId: blockee.id,
|
|
|
|
});
|
|
|
|
|
2022-09-17 20:27:08 +02:00
|
|
|
if (this.userEntityService.isLocalUser(blocker) && this.userEntityService.isRemoteUser(blockee)) {
|
2023-02-12 10:47:30 +01:00
|
|
|
const content = this.apRendererService.addContext(this.apRendererService.renderBlock(blocking));
|
2023-03-14 11:11:31 +01:00
|
|
|
this.queueService.deliver(blocker, content, blockee.inbox, false);
|
2022-09-17 20:27:08 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-04 07:03:09 +01:00
|
|
|
@bindThis
|
2023-08-16 10:51:28 +02:00
|
|
|
private async cancelRequest(follower: MiUser, followee: MiUser, silent = false) {
|
2022-09-17 20:27:08 +02:00
|
|
|
const request = await this.followRequestsRepository.findOneBy({
|
|
|
|
followeeId: followee.id,
|
|
|
|
followerId: follower.id,
|
|
|
|
});
|
|
|
|
|
|
|
|
if (request == null) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
await this.followRequestsRepository.delete({
|
|
|
|
followeeId: followee.id,
|
|
|
|
followerId: follower.id,
|
|
|
|
});
|
|
|
|
|
|
|
|
if (this.userEntityService.isLocalUser(followee)) {
|
|
|
|
this.userEntityService.pack(followee, followee, {
|
2024-01-31 07:45:35 +01:00
|
|
|
schema: 'MeDetailed',
|
2023-02-04 02:02:03 +01:00
|
|
|
}).then(packed => this.globalEventService.publishMainStream(followee.id, 'meUpdated', packed));
|
2022-09-17 20:27:08 +02:00
|
|
|
}
|
|
|
|
|
2023-04-12 02:13:58 +02:00
|
|
|
if (this.userEntityService.isLocalUser(follower) && !silent) {
|
2022-09-17 20:27:08 +02:00
|
|
|
this.userEntityService.pack(followee, follower, {
|
2024-01-31 07:45:35 +01:00
|
|
|
schema: 'UserDetailedNotMe',
|
2022-09-17 20:27:08 +02:00
|
|
|
}).then(async packed => {
|
2023-02-04 02:02:03 +01:00
|
|
|
this.globalEventService.publishMainStream(follower.id, 'unfollow', packed);
|
2022-09-17 20:27:08 +02:00
|
|
|
|
|
|
|
const webhooks = (await this.webhookService.getActiveWebhooks()).filter(x => x.userId === follower.id && x.on.includes('unfollow'));
|
|
|
|
for (const webhook of webhooks) {
|
|
|
|
this.queueService.webhookDeliver(webhook, 'unfollow', {
|
|
|
|
user: packed,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
// リモートにフォローリクエストをしていたらUndoFollow送信
|
|
|
|
if (this.userEntityService.isLocalUser(follower) && this.userEntityService.isRemoteUser(followee)) {
|
2023-02-12 10:47:30 +01:00
|
|
|
const content = this.apRendererService.addContext(this.apRendererService.renderUndo(this.apRendererService.renderFollow(follower, followee), follower));
|
2023-03-14 11:11:31 +01:00
|
|
|
this.queueService.deliver(follower, content, followee.inbox, false);
|
2022-09-17 20:27:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// リモートからフォローリクエストを受けていたらReject送信
|
|
|
|
if (this.userEntityService.isRemoteUser(follower) && this.userEntityService.isLocalUser(followee)) {
|
2023-02-12 10:47:30 +01:00
|
|
|
const content = this.apRendererService.addContext(this.apRendererService.renderReject(this.apRendererService.renderFollow(follower, followee, request.requestId!), followee));
|
2023-03-14 11:11:31 +01:00
|
|
|
this.queueService.deliver(followee, content, follower.inbox, false);
|
2022-09-17 20:27:08 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-04 07:03:09 +01:00
|
|
|
@bindThis
|
2023-08-16 10:51:28 +02:00
|
|
|
private async removeFromList(listOwner: MiUser, user: MiUser) {
|
2022-09-17 20:27:08 +02:00
|
|
|
const userLists = await this.userListsRepository.findBy({
|
|
|
|
userId: listOwner.id,
|
|
|
|
});
|
|
|
|
|
|
|
|
for (const userList of userLists) {
|
2023-10-03 13:26:11 +02:00
|
|
|
await this.userListMembershipsRepository.delete({
|
2022-09-17 20:27:08 +02:00
|
|
|
userListId: userList.id,
|
|
|
|
userId: user.id,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-04 07:03:09 +01:00
|
|
|
@bindThis
|
2023-08-16 10:51:28 +02:00
|
|
|
public async unblock(blocker: MiUser, blockee: MiUser) {
|
2022-09-17 20:27:08 +02:00
|
|
|
const blocking = await this.blockingsRepository.findOneBy({
|
|
|
|
blockerId: blocker.id,
|
|
|
|
blockeeId: blockee.id,
|
|
|
|
});
|
2022-12-26 09:05:44 +01:00
|
|
|
|
2022-09-17 20:27:08 +02:00
|
|
|
if (blocking == null) {
|
2022-09-24 00:12:11 +02:00
|
|
|
this.logger.warn('ブロック解除がリクエストされましたがブロックしていませんでした');
|
2022-09-17 20:27:08 +02:00
|
|
|
return;
|
|
|
|
}
|
2022-12-26 09:05:44 +01:00
|
|
|
|
2022-09-17 20:27:08 +02:00
|
|
|
// Since we already have the blocker and blockee, we do not need to fetch
|
|
|
|
// them in the query above and can just manually insert them here.
|
|
|
|
blocking.blocker = blocker;
|
|
|
|
blocking.blockee = blockee;
|
2022-12-26 09:05:44 +01:00
|
|
|
|
2022-09-17 20:27:08 +02:00
|
|
|
await this.blockingsRepository.delete(blocking.id);
|
2022-12-26 09:05:44 +01:00
|
|
|
|
2023-04-05 03:21:10 +02:00
|
|
|
this.cacheService.userBlockingCache.refresh(blocker.id);
|
|
|
|
this.cacheService.userBlockedCache.refresh(blockee.id);
|
|
|
|
|
2023-02-04 04:40:40 +01:00
|
|
|
this.globalEventService.publishInternalEvent('blockingDeleted', {
|
|
|
|
blockerId: blocker.id,
|
|
|
|
blockeeId: blockee.id,
|
|
|
|
});
|
|
|
|
|
2022-09-17 20:27:08 +02:00
|
|
|
// deliver if remote bloking
|
|
|
|
if (this.userEntityService.isLocalUser(blocker) && this.userEntityService.isRemoteUser(blockee)) {
|
2023-02-12 10:47:30 +01:00
|
|
|
const content = this.apRendererService.addContext(this.apRendererService.renderUndo(this.apRendererService.renderBlock(blocking), blocker));
|
2023-03-14 11:11:31 +01:00
|
|
|
this.queueService.deliver(blocker, content, blockee.inbox, false);
|
2022-09-17 20:27:08 +02:00
|
|
|
}
|
2022-12-26 09:05:44 +01:00
|
|
|
}
|
2023-02-04 04:40:40 +01:00
|
|
|
|
|
|
|
@bindThis
|
2023-08-16 10:51:28 +02:00
|
|
|
public async checkBlocked(blockerId: MiUser['id'], blockeeId: MiUser['id']): Promise<boolean> {
|
2023-04-05 03:21:10 +02:00
|
|
|
return (await this.cacheService.userBlockingCache.fetch(blockerId)).has(blockeeId);
|
2023-02-04 04:40:40 +01:00
|
|
|
}
|
2022-09-17 20:27:08 +02:00
|
|
|
}
|