2023-04-05 04:57:22 +00:00
|
|
|
import { Inject, Injectable, OnModuleInit, forwardRef } from '@nestjs/common';
|
|
|
|
import { ModuleRef } from '@nestjs/core';
|
2023-02-13 06:50:22 +00:00
|
|
|
import type { LocalUser, RemoteUser, User } from '@/models/entities/User.js';
|
2022-09-17 18:27:08 +00:00
|
|
|
import { IdentifiableError } from '@/misc/identifiable-error.js';
|
|
|
|
import { QueueService } from '@/core/QueueService.js';
|
|
|
|
import PerUserFollowingChart from '@/core/chart/charts/per-user-following.js';
|
|
|
|
import { GlobalEventService } from '@/core/GlobalEventService.js';
|
|
|
|
import { IdService } from '@/core/IdService.js';
|
|
|
|
import { isDuplicateKeyValueError } from '@/misc/is-duplicate-key-value-error.js';
|
2023-03-10 05:22:37 +00:00
|
|
|
import type { Packed } from '@/misc/json-schema.js';
|
2022-09-17 18:27:08 +00:00
|
|
|
import InstanceChart from '@/core/chart/charts/instance.js';
|
|
|
|
import { FederatedInstanceService } from '@/core/FederatedInstanceService.js';
|
|
|
|
import { WebhookService } from '@/core/WebhookService.js';
|
2023-03-16 05:24:11 +00:00
|
|
|
import { NotificationService } from '@/core/NotificationService.js';
|
2022-09-17 18:27:08 +00:00
|
|
|
import { DI } from '@/di-symbols.js';
|
2023-02-04 03:40:40 +00:00
|
|
|
import type { FollowingsRepository, FollowRequestsRepository, InstancesRepository, UserProfilesRepository, UsersRepository } from '@/models/index.js';
|
2022-12-04 01:16:03 +00:00
|
|
|
import { UserEntityService } from '@/core/entities/UserEntityService.js';
|
|
|
|
import { ApRendererService } from '@/core/activitypub/ApRendererService.js';
|
2022-12-04 08:05:32 +00:00
|
|
|
import { bindThis } from '@/decorators.js';
|
2023-02-04 03:40:40 +00:00
|
|
|
import { UserBlockingService } from '@/core/UserBlockingService.js';
|
2023-03-24 10:08:08 +00:00
|
|
|
import { MetaService } from '@/core/MetaService.js';
|
2023-04-05 01:21:10 +00:00
|
|
|
import { CacheService } from '@/core/CacheService.js';
|
2023-04-12 11:22:50 +00:00
|
|
|
import type { Config } from '@/config.js';
|
2022-12-04 08:05:32 +00:00
|
|
|
import Logger from '../logger.js';
|
2022-09-17 18:27:08 +00:00
|
|
|
|
|
|
|
const logger = new Logger('following/create');
|
|
|
|
|
2023-02-13 06:50:22 +00:00
|
|
|
type Local = LocalUser | {
|
|
|
|
id: LocalUser['id'];
|
|
|
|
host: LocalUser['host'];
|
|
|
|
uri: LocalUser['uri']
|
2022-09-17 18:27:08 +00:00
|
|
|
};
|
2023-02-13 06:50:22 +00:00
|
|
|
type Remote = RemoteUser | {
|
|
|
|
id: RemoteUser['id'];
|
|
|
|
host: RemoteUser['host'];
|
|
|
|
uri: RemoteUser['uri'];
|
|
|
|
inbox: RemoteUser['inbox'];
|
2022-09-17 18:27:08 +00:00
|
|
|
};
|
|
|
|
type Both = Local | Remote;
|
|
|
|
|
|
|
|
@Injectable()
|
2023-04-05 04:57:22 +00:00
|
|
|
export class UserFollowingService implements OnModuleInit {
|
|
|
|
private userBlockingService: UserBlockingService;
|
|
|
|
|
2022-09-17 18:27:08 +00:00
|
|
|
constructor(
|
2023-04-05 04:57:22 +00:00
|
|
|
private moduleRef: ModuleRef,
|
2023-04-12 00:13:58 +00:00
|
|
|
|
2023-04-12 11:22:50 +00:00
|
|
|
@Inject(DI.config)
|
|
|
|
private config: Config,
|
|
|
|
|
2022-09-17 18:27:08 +00:00
|
|
|
@Inject(DI.usersRepository)
|
|
|
|
private usersRepository: UsersRepository,
|
|
|
|
|
|
|
|
@Inject(DI.userProfilesRepository)
|
|
|
|
private userProfilesRepository: UserProfilesRepository,
|
|
|
|
|
|
|
|
@Inject(DI.followingsRepository)
|
|
|
|
private followingsRepository: FollowingsRepository,
|
|
|
|
|
|
|
|
@Inject(DI.followRequestsRepository)
|
|
|
|
private followRequestsRepository: FollowRequestsRepository,
|
|
|
|
|
|
|
|
@Inject(DI.instancesRepository)
|
|
|
|
private instancesRepository: InstancesRepository,
|
|
|
|
|
2023-04-05 01:21:10 +00:00
|
|
|
private cacheService: CacheService,
|
2022-09-17 18:27:08 +00:00
|
|
|
private userEntityService: UserEntityService,
|
|
|
|
private idService: IdService,
|
|
|
|
private queueService: QueueService,
|
2023-02-04 01:02:03 +00:00
|
|
|
private globalEventService: GlobalEventService,
|
2023-03-24 10:08:08 +00:00
|
|
|
private metaService: MetaService,
|
2023-03-16 05:24:11 +00:00
|
|
|
private notificationService: NotificationService,
|
2022-09-17 18:27:08 +00:00
|
|
|
private federatedInstanceService: FederatedInstanceService,
|
|
|
|
private webhookService: WebhookService,
|
|
|
|
private apRendererService: ApRendererService,
|
|
|
|
private perUserFollowingChart: PerUserFollowingChart,
|
|
|
|
private instanceChart: InstanceChart,
|
|
|
|
) {
|
|
|
|
}
|
|
|
|
|
2023-04-05 04:57:22 +00:00
|
|
|
onModuleInit() {
|
|
|
|
this.userBlockingService = this.moduleRef.get('UserBlockingService');
|
|
|
|
}
|
|
|
|
|
2022-12-04 06:03:09 +00:00
|
|
|
@bindThis
|
2023-04-12 00:13:58 +00:00
|
|
|
public async follow(_follower: { id: User['id'] }, _followee: { id: User['id'] }, requestId?: string, silent = false): Promise<void> {
|
2022-09-17 18:27:08 +00:00
|
|
|
const [follower, followee] = await Promise.all([
|
|
|
|
this.usersRepository.findOneByOrFail({ id: _follower.id }),
|
|
|
|
this.usersRepository.findOneByOrFail({ id: _followee.id }),
|
|
|
|
]);
|
|
|
|
|
|
|
|
// check blocking
|
|
|
|
const [blocking, blocked] = await Promise.all([
|
2023-02-04 03:40:40 +00:00
|
|
|
this.userBlockingService.checkBlocked(follower.id, followee.id),
|
|
|
|
this.userBlockingService.checkBlocked(followee.id, follower.id),
|
2022-09-17 18:27:08 +00:00
|
|
|
]);
|
|
|
|
|
|
|
|
if (this.userEntityService.isRemoteUser(follower) && this.userEntityService.isLocalUser(followee) && blocked) {
|
2023-02-04 03:40:40 +00:00
|
|
|
// リモートフォローを受けてブロックしていた場合は、エラーにするのではなくRejectを送り返しておしまい。
|
2023-02-12 09:47:30 +00:00
|
|
|
const content = this.apRendererService.addContext(this.apRendererService.renderReject(this.apRendererService.renderFollow(follower, followee, requestId), followee));
|
2023-03-14 10:11:31 +00:00
|
|
|
this.queueService.deliver(followee, content, follower.inbox, false);
|
2022-09-17 18:27:08 +00:00
|
|
|
return;
|
|
|
|
} else if (this.userEntityService.isRemoteUser(follower) && this.userEntityService.isLocalUser(followee) && blocking) {
|
2023-02-04 03:40:40 +00:00
|
|
|
// リモートフォローを受けてブロックされているはずの場合だったら、ブロック解除しておく。
|
|
|
|
await this.userBlockingService.unblock(follower, followee);
|
2022-09-17 18:27:08 +00:00
|
|
|
} else {
|
2023-02-04 03:40:40 +00:00
|
|
|
// それ以外は単純に例外
|
2023-02-04 10:19:30 +00:00
|
|
|
if (blocking) throw new IdentifiableError('710e8fb0-b8c3-4922-be49-d5d93d8e6a6e', 'blocking');
|
|
|
|
if (blocked) throw new IdentifiableError('3338392a-f764-498d-8855-db939dcf8c48', 'blocked');
|
2022-09-17 18:27:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const followeeProfile = await this.userProfilesRepository.findOneByOrFail({ userId: followee.id });
|
|
|
|
|
|
|
|
// フォロー対象が鍵アカウントである or
|
|
|
|
// フォロワーがBotであり、フォロー対象がBotからのフォローに慎重である or
|
|
|
|
// フォロワーがローカルユーザーであり、フォロー対象がリモートユーザーである
|
|
|
|
// 上記のいずれかに当てはまる場合はすぐフォローせずにフォローリクエストを発行しておく
|
|
|
|
if (followee.isLocked || (followeeProfile.carefulBot && follower.isBot) || (this.userEntityService.isLocalUser(follower) && this.userEntityService.isRemoteUser(followee))) {
|
|
|
|
let autoAccept = false;
|
|
|
|
|
|
|
|
// 鍵アカウントであっても、既にフォローされていた場合はスルー
|
|
|
|
const following = await this.followingsRepository.findOneBy({
|
|
|
|
followerId: follower.id,
|
|
|
|
followeeId: followee.id,
|
|
|
|
});
|
|
|
|
if (following) {
|
|
|
|
autoAccept = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// フォローしているユーザーは自動承認オプション
|
|
|
|
if (!autoAccept && (this.userEntityService.isLocalUser(followee) && followeeProfile.autoAcceptFollowed)) {
|
|
|
|
const followed = await this.followingsRepository.findOneBy({
|
|
|
|
followerId: followee.id,
|
|
|
|
followeeId: follower.id,
|
|
|
|
});
|
|
|
|
|
|
|
|
if (followed) autoAccept = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!autoAccept) {
|
|
|
|
await this.createFollowRequest(follower, followee, requestId);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-12 00:13:58 +00:00
|
|
|
await this.insertFollowingDoc(followee, follower, silent);
|
2022-09-17 18:27:08 +00:00
|
|
|
|
|
|
|
if (this.userEntityService.isRemoteUser(follower) && this.userEntityService.isLocalUser(followee)) {
|
2023-02-12 09:47:30 +00:00
|
|
|
const content = this.apRendererService.addContext(this.apRendererService.renderAccept(this.apRendererService.renderFollow(follower, followee, requestId), followee));
|
2023-03-14 10:11:31 +00:00
|
|
|
this.queueService.deliver(followee, content, follower.inbox, false);
|
2022-09-17 18:27:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-04 06:03:09 +00:00
|
|
|
@bindThis
|
2022-09-18 18:11:50 +00:00
|
|
|
private async insertFollowingDoc(
|
2022-09-17 18:27:08 +00:00
|
|
|
followee: {
|
|
|
|
id: User['id']; host: User['host']; uri: User['host']; inbox: User['inbox']; sharedInbox: User['sharedInbox']
|
|
|
|
},
|
|
|
|
follower: {
|
|
|
|
id: User['id']; host: User['host']; uri: User['host']; inbox: User['inbox']; sharedInbox: User['sharedInbox']
|
|
|
|
},
|
2023-04-12 00:13:58 +00:00
|
|
|
silent = false,
|
2022-09-17 18:27:08 +00:00
|
|
|
): Promise<void> {
|
|
|
|
if (follower.id === followee.id) return;
|
2023-03-16 05:24:11 +00:00
|
|
|
|
2022-09-17 18:27:08 +00:00
|
|
|
let alreadyFollowed = false as boolean;
|
2023-03-16 05:24:11 +00:00
|
|
|
|
2022-09-17 18:27:08 +00:00
|
|
|
await this.followingsRepository.insert({
|
|
|
|
id: this.idService.genId(),
|
|
|
|
createdAt: new Date(),
|
|
|
|
followerId: follower.id,
|
|
|
|
followeeId: followee.id,
|
2023-03-16 05:24:11 +00:00
|
|
|
|
2022-09-17 18:27:08 +00:00
|
|
|
// 非正規化
|
|
|
|
followerHost: follower.host,
|
|
|
|
followerInbox: this.userEntityService.isRemoteUser(follower) ? follower.inbox : null,
|
|
|
|
followerSharedInbox: this.userEntityService.isRemoteUser(follower) ? follower.sharedInbox : null,
|
|
|
|
followeeHost: followee.host,
|
|
|
|
followeeInbox: this.userEntityService.isRemoteUser(followee) ? followee.inbox : null,
|
|
|
|
followeeSharedInbox: this.userEntityService.isRemoteUser(followee) ? followee.sharedInbox : null,
|
|
|
|
}).catch(err => {
|
|
|
|
if (isDuplicateKeyValueError(err) && this.userEntityService.isRemoteUser(follower) && this.userEntityService.isLocalUser(followee)) {
|
|
|
|
logger.info(`Insert duplicated ignore. ${follower.id} => ${followee.id}`);
|
|
|
|
alreadyFollowed = true;
|
|
|
|
} else {
|
|
|
|
throw err;
|
|
|
|
}
|
|
|
|
});
|
2023-03-16 05:24:11 +00:00
|
|
|
|
2023-04-05 01:21:10 +00:00
|
|
|
this.cacheService.userFollowingsCache.refresh(follower.id);
|
|
|
|
|
2022-09-17 18:27:08 +00:00
|
|
|
const req = await this.followRequestsRepository.findOneBy({
|
|
|
|
followeeId: followee.id,
|
|
|
|
followerId: follower.id,
|
|
|
|
});
|
2023-03-16 05:24:11 +00:00
|
|
|
|
2022-09-17 18:27:08 +00:00
|
|
|
if (req) {
|
|
|
|
await this.followRequestsRepository.delete({
|
|
|
|
followeeId: followee.id,
|
|
|
|
followerId: follower.id,
|
|
|
|
});
|
2023-03-16 05:24:11 +00:00
|
|
|
|
2022-09-17 18:27:08 +00:00
|
|
|
// 通知を作成
|
2023-03-16 05:24:11 +00:00
|
|
|
this.notificationService.createNotification(follower.id, 'followRequestAccepted', {
|
2022-09-17 18:27:08 +00:00
|
|
|
notifierId: followee.id,
|
|
|
|
});
|
|
|
|
}
|
2023-03-16 05:24:11 +00:00
|
|
|
|
2022-09-17 18:27:08 +00:00
|
|
|
if (alreadyFollowed) return;
|
2023-01-13 23:27:23 +00:00
|
|
|
|
|
|
|
this.globalEventService.publishInternalEvent('follow', { followerId: follower.id, followeeId: followee.id });
|
2023-03-16 05:24:11 +00:00
|
|
|
|
2022-09-17 18:27:08 +00:00
|
|
|
//#region Increment counts
|
|
|
|
await Promise.all([
|
|
|
|
this.usersRepository.increment({ id: follower.id }, 'followingCount', 1),
|
|
|
|
this.usersRepository.increment({ id: followee.id }, 'followersCount', 1),
|
|
|
|
]);
|
|
|
|
//#endregion
|
2023-03-16 05:24:11 +00:00
|
|
|
|
2022-09-17 18:27:08 +00:00
|
|
|
//#region Update instance stats
|
|
|
|
if (this.userEntityService.isRemoteUser(follower) && this.userEntityService.isLocalUser(followee)) {
|
2023-03-24 10:08:08 +00:00
|
|
|
this.federatedInstanceService.fetch(follower.host).then(async i => {
|
2022-09-17 18:27:08 +00:00
|
|
|
this.instancesRepository.increment({ id: i.id }, 'followingCount', 1);
|
2023-03-24 10:08:08 +00:00
|
|
|
if ((await this.metaService.fetch()).enableChartsForFederatedInstances) {
|
|
|
|
this.instanceChart.updateFollowing(i.host, true);
|
|
|
|
}
|
2022-09-17 18:27:08 +00:00
|
|
|
});
|
|
|
|
} else if (this.userEntityService.isLocalUser(follower) && this.userEntityService.isRemoteUser(followee)) {
|
2023-03-24 10:08:08 +00:00
|
|
|
this.federatedInstanceService.fetch(followee.host).then(async i => {
|
2022-09-17 18:27:08 +00:00
|
|
|
this.instancesRepository.increment({ id: i.id }, 'followersCount', 1);
|
2023-03-24 10:08:08 +00:00
|
|
|
if ((await this.metaService.fetch()).enableChartsForFederatedInstances) {
|
|
|
|
this.instanceChart.updateFollowers(i.host, true);
|
|
|
|
}
|
2022-09-17 18:27:08 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
//#endregion
|
2023-03-16 05:24:11 +00:00
|
|
|
|
2022-09-17 18:27:08 +00:00
|
|
|
this.perUserFollowingChart.update(follower, followee, true);
|
2023-03-16 05:24:11 +00:00
|
|
|
|
2022-09-17 18:27:08 +00:00
|
|
|
// Publish follow event
|
2023-04-12 00:13:58 +00:00
|
|
|
if (this.userEntityService.isLocalUser(follower) && !silent) {
|
2022-09-17 18:27:08 +00:00
|
|
|
this.userEntityService.pack(followee.id, follower, {
|
|
|
|
detail: true,
|
|
|
|
}).then(async packed => {
|
2023-02-04 01:02:03 +00:00
|
|
|
this.globalEventService.publishMainStream(follower.id, 'follow', packed as Packed<'UserDetailedNotMe'>);
|
2023-03-16 05:24:11 +00:00
|
|
|
|
2022-09-17 18:27:08 +00:00
|
|
|
const webhooks = (await this.webhookService.getActiveWebhooks()).filter(x => x.userId === follower.id && x.on.includes('follow'));
|
|
|
|
for (const webhook of webhooks) {
|
|
|
|
this.queueService.webhookDeliver(webhook, 'follow', {
|
|
|
|
user: packed,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2023-03-16 05:24:11 +00:00
|
|
|
|
2022-09-17 18:27:08 +00:00
|
|
|
// Publish followed event
|
|
|
|
if (this.userEntityService.isLocalUser(followee)) {
|
|
|
|
this.userEntityService.pack(follower.id, followee).then(async packed => {
|
2023-02-04 01:02:03 +00:00
|
|
|
this.globalEventService.publishMainStream(followee.id, 'followed', packed);
|
2023-03-16 05:24:11 +00:00
|
|
|
|
2022-09-17 18:27:08 +00:00
|
|
|
const webhooks = (await this.webhookService.getActiveWebhooks()).filter(x => x.userId === followee.id && x.on.includes('followed'));
|
|
|
|
for (const webhook of webhooks) {
|
|
|
|
this.queueService.webhookDeliver(webhook, 'followed', {
|
|
|
|
user: packed,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
2023-03-16 05:24:11 +00:00
|
|
|
|
2022-09-17 18:27:08 +00:00
|
|
|
// 通知を作成
|
2023-03-16 05:24:11 +00:00
|
|
|
this.notificationService.createNotification(followee.id, 'follow', {
|
2022-09-17 18:27:08 +00:00
|
|
|
notifierId: follower.id,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-04 06:03:09 +00:00
|
|
|
@bindThis
|
2022-09-17 18:27:08 +00:00
|
|
|
public async unfollow(
|
|
|
|
follower: {
|
|
|
|
id: User['id']; host: User['host']; uri: User['host']; inbox: User['inbox']; sharedInbox: User['sharedInbox'];
|
|
|
|
},
|
|
|
|
followee: {
|
|
|
|
id: User['id']; host: User['host']; uri: User['host']; inbox: User['inbox']; sharedInbox: User['sharedInbox'];
|
|
|
|
},
|
|
|
|
silent = false,
|
|
|
|
): Promise<void> {
|
|
|
|
const following = await this.followingsRepository.findOneBy({
|
|
|
|
followerId: follower.id,
|
|
|
|
followeeId: followee.id,
|
|
|
|
});
|
2023-03-16 05:24:11 +00:00
|
|
|
|
2022-09-17 18:27:08 +00:00
|
|
|
if (following == null) {
|
|
|
|
logger.warn('フォロー解除がリクエストされましたがフォローしていませんでした');
|
|
|
|
return;
|
|
|
|
}
|
2023-03-16 05:24:11 +00:00
|
|
|
|
2022-09-17 18:27:08 +00:00
|
|
|
await this.followingsRepository.delete(following.id);
|
2023-03-16 05:24:11 +00:00
|
|
|
|
2023-04-05 01:21:10 +00:00
|
|
|
this.cacheService.userFollowingsCache.refresh(follower.id);
|
|
|
|
|
2022-09-18 18:11:50 +00:00
|
|
|
this.decrementFollowing(follower, followee);
|
2023-03-16 05:24:11 +00:00
|
|
|
|
2022-09-17 18:27:08 +00:00
|
|
|
// Publish unfollow event
|
|
|
|
if (!silent && this.userEntityService.isLocalUser(follower)) {
|
|
|
|
this.userEntityService.pack(followee.id, follower, {
|
|
|
|
detail: true,
|
|
|
|
}).then(async packed => {
|
2023-02-04 01:02:03 +00:00
|
|
|
this.globalEventService.publishMainStream(follower.id, 'unfollow', packed);
|
2023-03-16 05:24:11 +00:00
|
|
|
|
2022-09-17 18:27:08 +00: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,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2023-03-16 05:24:11 +00:00
|
|
|
|
2022-09-17 18:27:08 +00:00
|
|
|
if (this.userEntityService.isLocalUser(follower) && this.userEntityService.isRemoteUser(followee)) {
|
2023-02-12 09:47:30 +00:00
|
|
|
const content = this.apRendererService.addContext(this.apRendererService.renderUndo(this.apRendererService.renderFollow(follower, followee), follower));
|
2023-03-14 10:11:31 +00:00
|
|
|
this.queueService.deliver(follower, content, followee.inbox, false);
|
2022-09-17 18:27:08 +00:00
|
|
|
}
|
2023-03-16 05:24:11 +00:00
|
|
|
|
2022-09-17 18:27:08 +00:00
|
|
|
if (this.userEntityService.isLocalUser(followee) && this.userEntityService.isRemoteUser(follower)) {
|
|
|
|
// local user has null host
|
2023-02-12 09:47:30 +00:00
|
|
|
const content = this.apRendererService.addContext(this.apRendererService.renderReject(this.apRendererService.renderFollow(follower, followee), followee));
|
2023-03-14 10:11:31 +00:00
|
|
|
this.queueService.deliver(followee, content, follower.inbox, false);
|
2022-09-17 18:27:08 +00:00
|
|
|
}
|
|
|
|
}
|
2023-03-16 05:24:11 +00:00
|
|
|
|
2022-12-04 06:03:09 +00:00
|
|
|
@bindThis
|
2022-09-18 18:11:50 +00:00
|
|
|
private async decrementFollowing(
|
2023-03-16 05:24:11 +00:00
|
|
|
follower: { id: User['id']; host: User['host']; },
|
2022-09-17 18:27:08 +00:00
|
|
|
followee: { id: User['id']; host: User['host']; },
|
|
|
|
): Promise<void> {
|
2023-01-13 23:27:23 +00:00
|
|
|
this.globalEventService.publishInternalEvent('unfollow', { followerId: follower.id, followeeId: followee.id });
|
2023-03-16 05:24:11 +00:00
|
|
|
|
2022-09-17 18:27:08 +00:00
|
|
|
//#region Decrement following / followers counts
|
|
|
|
await Promise.all([
|
|
|
|
this.usersRepository.decrement({ id: follower.id }, 'followingCount', 1),
|
|
|
|
this.usersRepository.decrement({ id: followee.id }, 'followersCount', 1),
|
|
|
|
]);
|
|
|
|
//#endregion
|
2023-03-16 05:24:11 +00:00
|
|
|
|
2022-09-17 18:27:08 +00:00
|
|
|
//#region Update instance stats
|
|
|
|
if (this.userEntityService.isRemoteUser(follower) && this.userEntityService.isLocalUser(followee)) {
|
2023-03-24 10:08:08 +00:00
|
|
|
this.federatedInstanceService.fetch(follower.host).then(async i => {
|
2022-09-17 18:27:08 +00:00
|
|
|
this.instancesRepository.decrement({ id: i.id }, 'followingCount', 1);
|
2023-03-24 10:08:08 +00:00
|
|
|
if ((await this.metaService.fetch()).enableChartsForFederatedInstances) {
|
|
|
|
this.instanceChart.updateFollowing(i.host, false);
|
|
|
|
}
|
2022-09-17 18:27:08 +00:00
|
|
|
});
|
|
|
|
} else if (this.userEntityService.isLocalUser(follower) && this.userEntityService.isRemoteUser(followee)) {
|
2023-03-24 10:08:08 +00:00
|
|
|
this.federatedInstanceService.fetch(followee.host).then(async i => {
|
2022-09-17 18:27:08 +00:00
|
|
|
this.instancesRepository.decrement({ id: i.id }, 'followersCount', 1);
|
2023-03-24 10:08:08 +00:00
|
|
|
if ((await this.metaService.fetch()).enableChartsForFederatedInstances) {
|
|
|
|
this.instanceChart.updateFollowers(i.host, false);
|
|
|
|
}
|
2022-09-17 18:27:08 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
//#endregion
|
2023-03-16 05:24:11 +00:00
|
|
|
|
2022-09-17 18:27:08 +00:00
|
|
|
this.perUserFollowingChart.update(follower, followee, false);
|
|
|
|
}
|
|
|
|
|
2022-12-04 06:03:09 +00:00
|
|
|
@bindThis
|
2022-09-17 18:27:08 +00:00
|
|
|
public async createFollowRequest(
|
|
|
|
follower: {
|
|
|
|
id: User['id']; host: User['host']; uri: User['host']; inbox: User['inbox']; sharedInbox: User['sharedInbox'];
|
|
|
|
},
|
|
|
|
followee: {
|
|
|
|
id: User['id']; host: User['host']; uri: User['host']; inbox: User['inbox']; sharedInbox: User['sharedInbox'];
|
|
|
|
},
|
|
|
|
requestId?: string,
|
|
|
|
): Promise<void> {
|
|
|
|
if (follower.id === followee.id) return;
|
2023-03-16 05:24:11 +00:00
|
|
|
|
2022-09-17 18:27:08 +00:00
|
|
|
// check blocking
|
|
|
|
const [blocking, blocked] = await Promise.all([
|
2023-02-04 03:40:40 +00:00
|
|
|
this.userBlockingService.checkBlocked(follower.id, followee.id),
|
|
|
|
this.userBlockingService.checkBlocked(followee.id, follower.id),
|
2022-09-17 18:27:08 +00:00
|
|
|
]);
|
2023-03-16 05:24:11 +00:00
|
|
|
|
2023-02-04 10:19:30 +00:00
|
|
|
if (blocking) throw new Error('blocking');
|
|
|
|
if (blocked) throw new Error('blocked');
|
2023-03-16 05:24:11 +00:00
|
|
|
|
2022-09-17 18:27:08 +00:00
|
|
|
const followRequest = await this.followRequestsRepository.insert({
|
|
|
|
id: this.idService.genId(),
|
|
|
|
createdAt: new Date(),
|
|
|
|
followerId: follower.id,
|
|
|
|
followeeId: followee.id,
|
|
|
|
requestId,
|
2023-03-16 05:24:11 +00:00
|
|
|
|
2022-09-17 18:27:08 +00:00
|
|
|
// 非正規化
|
|
|
|
followerHost: follower.host,
|
|
|
|
followerInbox: this.userEntityService.isRemoteUser(follower) ? follower.inbox : undefined,
|
|
|
|
followerSharedInbox: this.userEntityService.isRemoteUser(follower) ? follower.sharedInbox : undefined,
|
|
|
|
followeeHost: followee.host,
|
|
|
|
followeeInbox: this.userEntityService.isRemoteUser(followee) ? followee.inbox : undefined,
|
|
|
|
followeeSharedInbox: this.userEntityService.isRemoteUser(followee) ? followee.sharedInbox : undefined,
|
|
|
|
}).then(x => this.followRequestsRepository.findOneByOrFail(x.identifiers[0]));
|
2023-03-16 05:24:11 +00:00
|
|
|
|
2022-09-17 18:27:08 +00:00
|
|
|
// Publish receiveRequest event
|
|
|
|
if (this.userEntityService.isLocalUser(followee)) {
|
2023-02-04 01:02:03 +00:00
|
|
|
this.userEntityService.pack(follower.id, followee).then(packed => this.globalEventService.publishMainStream(followee.id, 'receiveFollowRequest', packed));
|
2023-03-16 05:24:11 +00:00
|
|
|
|
2022-09-17 18:27:08 +00:00
|
|
|
this.userEntityService.pack(followee.id, followee, {
|
|
|
|
detail: true,
|
2023-02-04 01:02:03 +00:00
|
|
|
}).then(packed => this.globalEventService.publishMainStream(followee.id, 'meUpdated', packed));
|
2023-03-16 05:24:11 +00:00
|
|
|
|
2022-09-17 18:27:08 +00:00
|
|
|
// 通知を作成
|
2023-03-16 05:24:11 +00:00
|
|
|
this.notificationService.createNotification(followee.id, 'receiveFollowRequest', {
|
2022-09-17 18:27:08 +00:00
|
|
|
notifierId: follower.id,
|
|
|
|
followRequestId: followRequest.id,
|
|
|
|
});
|
|
|
|
}
|
2023-03-16 05:24:11 +00:00
|
|
|
|
2022-09-17 18:27:08 +00:00
|
|
|
if (this.userEntityService.isLocalUser(follower) && this.userEntityService.isRemoteUser(followee)) {
|
2023-04-12 11:22:50 +00:00
|
|
|
const content = this.apRendererService.addContext(this.apRendererService.renderFollow(follower, followee, requestId ?? `${this.config.url}/follows/${followRequest.id}`));
|
2023-03-14 10:11:31 +00:00
|
|
|
this.queueService.deliver(follower, content, followee.inbox, false);
|
2022-09-17 18:27:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-04 06:03:09 +00:00
|
|
|
@bindThis
|
2022-09-17 18:27:08 +00:00
|
|
|
public async cancelFollowRequest(
|
|
|
|
followee: {
|
|
|
|
id: User['id']; host: User['host']; uri: User['host']; inbox: User['inbox']
|
|
|
|
},
|
|
|
|
follower: {
|
|
|
|
id: User['id']; host: User['host']; uri: User['host']
|
|
|
|
},
|
|
|
|
): Promise<void> {
|
|
|
|
if (this.userEntityService.isRemoteUser(followee)) {
|
2023-02-12 09:47:30 +00:00
|
|
|
const content = this.apRendererService.addContext(this.apRendererService.renderUndo(this.apRendererService.renderFollow(follower, followee), follower));
|
2023-03-16 05:24:11 +00:00
|
|
|
|
2022-09-17 18:27:08 +00:00
|
|
|
if (this.userEntityService.isLocalUser(follower)) { // 本来このチェックは不要だけどTSに怒られるので
|
2023-03-14 10:11:31 +00:00
|
|
|
this.queueService.deliver(follower, content, followee.inbox, false);
|
2022-09-17 18:27:08 +00:00
|
|
|
}
|
|
|
|
}
|
2023-03-16 05:24:11 +00:00
|
|
|
|
2022-09-17 18:27:08 +00:00
|
|
|
const request = await this.followRequestsRepository.findOneBy({
|
|
|
|
followeeId: followee.id,
|
|
|
|
followerId: follower.id,
|
|
|
|
});
|
2023-03-16 05:24:11 +00:00
|
|
|
|
2022-09-17 18:27:08 +00:00
|
|
|
if (request == null) {
|
|
|
|
throw new IdentifiableError('17447091-ce07-46dd-b331-c1fd4f15b1e7', 'request not found');
|
|
|
|
}
|
2023-03-16 05:24:11 +00:00
|
|
|
|
2022-09-17 18:27:08 +00:00
|
|
|
await this.followRequestsRepository.delete({
|
|
|
|
followeeId: followee.id,
|
|
|
|
followerId: follower.id,
|
|
|
|
});
|
2023-03-16 05:24:11 +00:00
|
|
|
|
2022-09-17 18:27:08 +00:00
|
|
|
this.userEntityService.pack(followee.id, followee, {
|
|
|
|
detail: true,
|
2023-02-04 01:02:03 +00:00
|
|
|
}).then(packed => this.globalEventService.publishMainStream(followee.id, 'meUpdated', packed));
|
2022-09-17 18:27:08 +00:00
|
|
|
}
|
|
|
|
|
2022-12-04 06:03:09 +00:00
|
|
|
@bindThis
|
2022-09-17 18:27:08 +00:00
|
|
|
public async acceptFollowRequest(
|
|
|
|
followee: {
|
|
|
|
id: User['id']; host: User['host']; uri: User['host']; inbox: User['inbox']; sharedInbox: User['sharedInbox'];
|
|
|
|
},
|
2023-02-13 06:28:07 +00:00
|
|
|
follower: User,
|
2022-09-17 18:27:08 +00:00
|
|
|
): Promise<void> {
|
|
|
|
const request = await this.followRequestsRepository.findOneBy({
|
|
|
|
followeeId: followee.id,
|
|
|
|
followerId: follower.id,
|
|
|
|
});
|
2023-03-16 05:24:11 +00:00
|
|
|
|
2022-09-17 18:27:08 +00:00
|
|
|
if (request == null) {
|
|
|
|
throw new IdentifiableError('8884c2dd-5795-4ac9-b27e-6a01d38190f9', 'No follow request.');
|
|
|
|
}
|
2023-03-16 05:24:11 +00:00
|
|
|
|
2022-09-18 18:11:50 +00:00
|
|
|
await this.insertFollowingDoc(followee, follower);
|
2023-03-16 05:24:11 +00:00
|
|
|
|
2022-09-17 18:27:08 +00:00
|
|
|
if (this.userEntityService.isRemoteUser(follower) && this.userEntityService.isLocalUser(followee)) {
|
2023-02-12 09:47:30 +00:00
|
|
|
const content = this.apRendererService.addContext(this.apRendererService.renderAccept(this.apRendererService.renderFollow(follower, followee, request.requestId!), followee));
|
2023-03-14 10:11:31 +00:00
|
|
|
this.queueService.deliver(followee, content, follower.inbox, false);
|
2022-09-17 18:27:08 +00:00
|
|
|
}
|
2023-03-16 05:24:11 +00:00
|
|
|
|
2022-09-17 18:27:08 +00:00
|
|
|
this.userEntityService.pack(followee.id, followee, {
|
|
|
|
detail: true,
|
2023-02-04 01:02:03 +00:00
|
|
|
}).then(packed => this.globalEventService.publishMainStream(followee.id, 'meUpdated', packed));
|
2022-09-17 18:27:08 +00:00
|
|
|
}
|
|
|
|
|
2022-12-04 06:03:09 +00:00
|
|
|
@bindThis
|
2022-09-17 18:27:08 +00:00
|
|
|
public async acceptAllFollowRequests(
|
|
|
|
user: {
|
|
|
|
id: User['id']; host: User['host']; uri: User['host']; inbox: User['inbox']; sharedInbox: User['sharedInbox'];
|
|
|
|
},
|
|
|
|
): Promise<void> {
|
|
|
|
const requests = await this.followRequestsRepository.findBy({
|
|
|
|
followeeId: user.id,
|
|
|
|
});
|
2023-03-16 05:24:11 +00:00
|
|
|
|
2022-09-17 18:27:08 +00:00
|
|
|
for (const request of requests) {
|
|
|
|
const follower = await this.usersRepository.findOneByOrFail({ id: request.followerId });
|
|
|
|
this.acceptFollowRequest(user, follower);
|
|
|
|
}
|
|
|
|
}
|
2023-03-16 05:24:11 +00:00
|
|
|
|
2022-09-17 18:27:08 +00:00
|
|
|
/**
|
|
|
|
* API following/request/reject
|
|
|
|
*/
|
2022-12-04 06:03:09 +00:00
|
|
|
@bindThis
|
2022-09-17 18:27:08 +00:00
|
|
|
public async rejectFollowRequest(user: Local, follower: Both): Promise<void> {
|
|
|
|
if (this.userEntityService.isRemoteUser(follower)) {
|
2022-09-18 18:11:50 +00:00
|
|
|
this.deliverReject(user, follower);
|
2022-09-17 18:27:08 +00:00
|
|
|
}
|
|
|
|
|
2022-09-18 18:11:50 +00:00
|
|
|
await this.removeFollowRequest(user, follower);
|
2022-09-17 18:27:08 +00:00
|
|
|
|
|
|
|
if (this.userEntityService.isLocalUser(follower)) {
|
2022-09-18 18:11:50 +00:00
|
|
|
this.publishUnfollow(user, follower);
|
2022-09-17 18:27:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* API following/reject
|
|
|
|
*/
|
2022-12-04 06:03:09 +00:00
|
|
|
@bindThis
|
2022-09-17 18:27:08 +00:00
|
|
|
public async rejectFollow(user: Local, follower: Both): Promise<void> {
|
|
|
|
if (this.userEntityService.isRemoteUser(follower)) {
|
2022-09-18 18:11:50 +00:00
|
|
|
this.deliverReject(user, follower);
|
2022-09-17 18:27:08 +00:00
|
|
|
}
|
|
|
|
|
2022-09-18 18:11:50 +00:00
|
|
|
await this.removeFollow(user, follower);
|
2022-09-17 18:27:08 +00:00
|
|
|
|
|
|
|
if (this.userEntityService.isLocalUser(follower)) {
|
2022-09-18 18:11:50 +00:00
|
|
|
this.publishUnfollow(user, follower);
|
2022-09-17 18:27:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* AP Reject/Follow
|
|
|
|
*/
|
2022-12-04 06:03:09 +00:00
|
|
|
@bindThis
|
2022-09-17 18:27:08 +00:00
|
|
|
public async remoteReject(actor: Remote, follower: Local): Promise<void> {
|
2022-09-18 18:11:50 +00:00
|
|
|
await this.removeFollowRequest(actor, follower);
|
|
|
|
await this.removeFollow(actor, follower);
|
|
|
|
this.publishUnfollow(actor, follower);
|
2022-09-17 18:27:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Remove follow request record
|
|
|
|
*/
|
2022-12-04 06:03:09 +00:00
|
|
|
@bindThis
|
2022-09-18 18:11:50 +00:00
|
|
|
private async removeFollowRequest(followee: Both, follower: Both): Promise<void> {
|
2022-09-17 18:27:08 +00:00
|
|
|
const request = await this.followRequestsRepository.findOneBy({
|
|
|
|
followeeId: followee.id,
|
|
|
|
followerId: follower.id,
|
|
|
|
});
|
|
|
|
|
|
|
|
if (!request) return;
|
|
|
|
|
|
|
|
await this.followRequestsRepository.delete(request.id);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Remove follow record
|
|
|
|
*/
|
2022-12-04 06:03:09 +00:00
|
|
|
@bindThis
|
2022-09-18 18:11:50 +00:00
|
|
|
private async removeFollow(followee: Both, follower: Both): Promise<void> {
|
2022-09-17 18:27:08 +00:00
|
|
|
const following = await this.followingsRepository.findOneBy({
|
|
|
|
followeeId: followee.id,
|
|
|
|
followerId: follower.id,
|
|
|
|
});
|
|
|
|
|
|
|
|
if (!following) return;
|
|
|
|
|
|
|
|
await this.followingsRepository.delete(following.id);
|
2022-09-18 18:11:50 +00:00
|
|
|
this.decrementFollowing(follower, followee);
|
2022-09-17 18:27:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Deliver Reject to remote
|
|
|
|
*/
|
2022-12-04 06:03:09 +00:00
|
|
|
@bindThis
|
2022-09-18 18:11:50 +00:00
|
|
|
private async deliverReject(followee: Local, follower: Remote): Promise<void> {
|
2022-09-17 18:27:08 +00:00
|
|
|
const request = await this.followRequestsRepository.findOneBy({
|
|
|
|
followeeId: followee.id,
|
|
|
|
followerId: follower.id,
|
|
|
|
});
|
|
|
|
|
2023-02-12 09:47:30 +00:00
|
|
|
const content = this.apRendererService.addContext(this.apRendererService.renderReject(this.apRendererService.renderFollow(follower, followee, request?.requestId ?? undefined), followee));
|
2023-03-14 10:11:31 +00:00
|
|
|
this.queueService.deliver(followee, content, follower.inbox, false);
|
2022-09-17 18:27:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Publish unfollow to local
|
|
|
|
*/
|
2022-12-04 06:03:09 +00:00
|
|
|
@bindThis
|
2022-09-18 18:11:50 +00:00
|
|
|
private async publishUnfollow(followee: Both, follower: Local): Promise<void> {
|
2022-09-17 18:27:08 +00:00
|
|
|
const packedFollowee = await this.userEntityService.pack(followee.id, follower, {
|
|
|
|
detail: true,
|
|
|
|
});
|
|
|
|
|
2023-02-04 01:02:03 +00:00
|
|
|
this.globalEventService.publishMainStream(follower.id, 'unfollow', packedFollowee);
|
2022-09-17 18:27:08 +00: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: packedFollowee,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|