2022-02-27 03:07:39 +01:00
|
|
|
import { publishMainStream, publishUserEvent } from '@/services/stream.js';
|
|
|
|
import { renderActivity } from '@/remote/activitypub/renderer/index.js';
|
|
|
|
import renderFollow from '@/remote/activitypub/renderer/follow.js';
|
|
|
|
import renderAccept from '@/remote/activitypub/renderer/accept.js';
|
|
|
|
import renderReject from '@/remote/activitypub/renderer/reject.js';
|
|
|
|
import { deliver } from '@/queue/index.js';
|
|
|
|
import createFollowRequest from './requests/create.js';
|
|
|
|
import { registerOrFetchInstanceDoc } from '../register-or-fetch-instance-doc.js';
|
|
|
|
import Logger from '../logger.js';
|
|
|
|
import { IdentifiableError } from '@/misc/identifiable-error.js';
|
|
|
|
import { User } from '@/models/entities/user.js';
|
|
|
|
import { Followings, Users, FollowRequests, Blockings, Instances, UserProfiles } from '@/models/index.js';
|
|
|
|
import { instanceChart, perUserFollowingChart } from '@/services/chart/index.js';
|
|
|
|
import { genId } from '@/misc/gen-id.js';
|
|
|
|
import { createNotification } from '../create-notification.js';
|
|
|
|
import { isDuplicateKeyValueError } from '@/misc/is-duplicate-key-value-error.js';
|
|
|
|
import { Packed } from '@/misc/schema.js';
|
2022-04-02 08:28:49 +02:00
|
|
|
import { getActiveWebhooks } from '@/misc/webhook-cache.js';
|
|
|
|
import { webhookDeliver } from '@/queue/index.js';
|
2018-04-04 16:59:38 +02:00
|
|
|
|
2019-02-09 04:04:03 +01:00
|
|
|
const logger = new Logger('following/create');
|
2018-10-29 12:32:42 +01:00
|
|
|
|
2021-03-24 03:05:37 +01:00
|
|
|
export async function insertFollowingDoc(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'] }) {
|
2019-04-07 14:50:36 +02:00
|
|
|
if (follower.id === followee.id) return;
|
|
|
|
|
2019-02-09 04:04:03 +01:00
|
|
|
let alreadyFollowed = false;
|
2018-04-04 16:59:38 +02:00
|
|
|
|
2021-03-21 13:27:09 +01:00
|
|
|
await Followings.insert({
|
2019-04-07 14:50:36 +02:00
|
|
|
id: genId(),
|
2018-10-13 13:11:00 +02:00
|
|
|
createdAt: new Date(),
|
2019-04-07 14:50:36 +02:00
|
|
|
followerId: follower.id,
|
|
|
|
followeeId: followee.id,
|
2018-04-04 16:59:38 +02:00
|
|
|
|
2018-10-13 13:11:00 +02:00
|
|
|
// 非正規化
|
2019-04-07 14:50:36 +02:00
|
|
|
followerHost: follower.host,
|
|
|
|
followerInbox: Users.isRemoteUser(follower) ? follower.inbox : null,
|
|
|
|
followerSharedInbox: Users.isRemoteUser(follower) ? follower.sharedInbox : null,
|
|
|
|
followeeHost: followee.host,
|
|
|
|
followeeInbox: Users.isRemoteUser(followee) ? followee.inbox : null,
|
2021-12-09 15:58:30 +01:00
|
|
|
followeeSharedInbox: Users.isRemoteUser(followee) ? followee.sharedInbox : null,
|
2019-02-09 04:04:03 +01:00
|
|
|
}).catch(e => {
|
2019-04-07 14:50:36 +02:00
|
|
|
if (isDuplicateKeyValueError(e) && Users.isRemoteUser(follower) && Users.isLocalUser(followee)) {
|
|
|
|
logger.info(`Insert duplicated ignore. ${follower.id} => ${followee.id}`);
|
2019-02-09 04:04:03 +01:00
|
|
|
alreadyFollowed = true;
|
|
|
|
} else {
|
|
|
|
throw e;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2022-03-26 07:34:00 +01:00
|
|
|
const req = await FollowRequests.findOneBy({
|
2019-04-07 14:50:36 +02:00
|
|
|
followeeId: followee.id,
|
2021-12-09 15:58:30 +01:00
|
|
|
followerId: follower.id,
|
2018-10-13 13:11:00 +02:00
|
|
|
});
|
2018-04-04 16:59:38 +02:00
|
|
|
|
2020-01-29 20:37:25 +01:00
|
|
|
if (req) {
|
|
|
|
await FollowRequests.delete({
|
|
|
|
followeeId: followee.id,
|
2021-12-09 15:58:30 +01:00
|
|
|
followerId: follower.id,
|
2020-01-29 20:37:25 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
// 通知を作成
|
2020-03-28 10:07:41 +01:00
|
|
|
createNotification(follower.id, 'followRequestAccepted', {
|
|
|
|
notifierId: followee.id,
|
|
|
|
});
|
2020-01-29 20:37:25 +01:00
|
|
|
}
|
|
|
|
|
2019-02-09 04:04:03 +01:00
|
|
|
if (alreadyFollowed) return;
|
|
|
|
|
|
|
|
//#region Increment counts
|
2019-04-07 14:50:36 +02:00
|
|
|
Users.increment({ id: follower.id }, 'followingCount', 1);
|
|
|
|
Users.increment({ id: followee.id }, 'followersCount', 1);
|
2018-10-13 13:11:00 +02:00
|
|
|
//#endregion
|
|
|
|
|
2019-02-07 07:00:44 +01:00
|
|
|
//#region Update instance stats
|
2019-04-07 14:50:36 +02:00
|
|
|
if (Users.isRemoteUser(follower) && Users.isLocalUser(followee)) {
|
2019-02-07 07:00:44 +01:00
|
|
|
registerOrFetchInstanceDoc(follower.host).then(i => {
|
2019-04-07 14:50:36 +02:00
|
|
|
Instances.increment({ id: i.id }, 'followingCount', 1);
|
2019-02-08 08:58:57 +01:00
|
|
|
instanceChart.updateFollowing(i.host, true);
|
2019-02-07 07:00:44 +01:00
|
|
|
});
|
2019-04-07 14:50:36 +02:00
|
|
|
} else if (Users.isLocalUser(follower) && Users.isRemoteUser(followee)) {
|
2019-02-07 07:00:44 +01:00
|
|
|
registerOrFetchInstanceDoc(followee.host).then(i => {
|
2019-04-07 14:50:36 +02:00
|
|
|
Instances.increment({ id: i.id }, 'followersCount', 1);
|
2019-02-08 08:58:57 +01:00
|
|
|
instanceChart.updateFollowers(i.host, true);
|
2019-02-07 07:00:44 +01:00
|
|
|
});
|
|
|
|
}
|
|
|
|
//#endregion
|
|
|
|
|
2018-10-22 22:36:35 +02:00
|
|
|
perUserFollowingChart.update(follower, followee, true);
|
2018-10-21 10:51:35 +02:00
|
|
|
|
2018-10-13 13:11:00 +02:00
|
|
|
// Publish follow event
|
2019-04-07 14:50:36 +02:00
|
|
|
if (Users.isLocalUser(follower)) {
|
2021-03-24 03:05:37 +01:00
|
|
|
Users.pack(followee.id, follower, {
|
2021-12-09 15:58:30 +01:00
|
|
|
detail: true,
|
2022-04-02 08:28:49 +02:00
|
|
|
}).then(async packed => {
|
2022-01-18 14:27:10 +01:00
|
|
|
publishUserEvent(follower.id, 'follow', packed as Packed<"UserDetailedNotMe">);
|
|
|
|
publishMainStream(follower.id, 'follow', packed as Packed<"UserDetailedNotMe">);
|
2022-04-02 08:28:49 +02:00
|
|
|
|
|
|
|
const webhooks = (await getActiveWebhooks()).filter(x => x.userId === follower.id && x.on.includes('follow'));
|
|
|
|
for (const webhook of webhooks) {
|
2022-04-03 15:36:30 +02:00
|
|
|
webhookDeliver(webhook, 'follow', {
|
2022-04-02 08:28:49 +02:00
|
|
|
user: packed,
|
|
|
|
});
|
|
|
|
}
|
2021-03-21 07:14:03 +01:00
|
|
|
});
|
2018-10-13 13:11:00 +02:00
|
|
|
}
|
2018-04-04 16:59:38 +02:00
|
|
|
|
2018-10-13 13:11:00 +02:00
|
|
|
// Publish followed event
|
2019-04-07 14:50:36 +02:00
|
|
|
if (Users.isLocalUser(followee)) {
|
2022-04-02 08:28:49 +02:00
|
|
|
Users.pack(follower.id, followee).then(async packed => {
|
2022-04-03 08:33:22 +02:00
|
|
|
publishMainStream(followee.id, 'followed', packed);
|
2022-04-02 08:28:49 +02:00
|
|
|
|
|
|
|
const webhooks = (await getActiveWebhooks()).filter(x => x.userId === followee.id && x.on.includes('followed'));
|
|
|
|
for (const webhook of webhooks) {
|
2022-04-03 15:36:30 +02:00
|
|
|
webhookDeliver(webhook, 'followed', {
|
2022-04-02 08:28:49 +02:00
|
|
|
user: packed,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
2018-05-31 15:56:02 +02:00
|
|
|
|
2018-10-13 13:11:00 +02:00
|
|
|
// 通知を作成
|
2020-03-28 10:07:41 +01:00
|
|
|
createNotification(followee.id, 'follow', {
|
2021-12-09 15:58:30 +01:00
|
|
|
notifierId: follower.id,
|
2020-03-28 10:07:41 +01:00
|
|
|
});
|
2018-10-13 13:11:00 +02:00
|
|
|
}
|
2019-02-09 04:04:03 +01:00
|
|
|
}
|
|
|
|
|
2021-03-24 03:05:37 +01:00
|
|
|
export default async function(_follower: { id: User['id'] }, _followee: { id: User['id'] }, requestId?: string) {
|
|
|
|
const [follower, followee] = await Promise.all([
|
2022-03-26 07:34:00 +01:00
|
|
|
Users.findOneByOrFail({ id: _follower.id }),
|
|
|
|
Users.findOneByOrFail({ id: _followee.id }),
|
2021-03-24 03:05:37 +01:00
|
|
|
]);
|
|
|
|
|
2019-02-09 04:04:03 +01:00
|
|
|
// check blocking
|
|
|
|
const [blocking, blocked] = await Promise.all([
|
2022-03-26 07:34:00 +01:00
|
|
|
Blockings.findOneBy({
|
2019-04-07 14:50:36 +02:00
|
|
|
blockerId: follower.id,
|
|
|
|
blockeeId: followee.id,
|
2019-02-09 04:04:03 +01:00
|
|
|
}),
|
2022-03-26 07:34:00 +01:00
|
|
|
Blockings.findOneBy({
|
2019-04-07 14:50:36 +02:00
|
|
|
blockerId: followee.id,
|
|
|
|
blockeeId: follower.id,
|
2021-12-09 15:58:30 +01:00
|
|
|
}),
|
2019-02-09 04:04:03 +01:00
|
|
|
]);
|
|
|
|
|
2019-04-07 14:50:36 +02:00
|
|
|
if (Users.isRemoteUser(follower) && Users.isLocalUser(followee) && blocked) {
|
2019-02-09 04:04:03 +01:00
|
|
|
// リモートフォローを受けてブロックしていた場合は、エラーにするのではなくRejectを送り返しておしまい。
|
|
|
|
const content = renderActivity(renderReject(renderFollow(follower, followee, requestId), followee));
|
|
|
|
deliver(followee , content, follower.inbox);
|
|
|
|
return;
|
2019-04-07 14:50:36 +02:00
|
|
|
} else if (Users.isRemoteUser(follower) && Users.isLocalUser(followee) && blocking) {
|
2019-02-09 04:04:03 +01:00
|
|
|
// リモートフォローを受けてブロックされているはずの場合だったら、ブロック解除しておく。
|
2019-04-07 14:50:36 +02:00
|
|
|
await Blockings.delete(blocking.id);
|
2019-02-09 04:04:03 +01:00
|
|
|
} else {
|
|
|
|
// それ以外は単純に例外
|
2019-02-22 03:46:58 +01:00
|
|
|
if (blocking != null) throw new IdentifiableError('710e8fb0-b8c3-4922-be49-d5d93d8e6a6e', 'blocking');
|
|
|
|
if (blocked != null) throw new IdentifiableError('3338392a-f764-498d-8855-db939dcf8c48', 'blocked');
|
2019-02-09 04:04:03 +01:00
|
|
|
}
|
|
|
|
|
2022-03-26 07:34:00 +01:00
|
|
|
const followeeProfile = await UserProfiles.findOneByOrFail({ userId: followee.id });
|
2019-04-10 08:04:27 +02:00
|
|
|
|
2019-02-09 04:04:03 +01:00
|
|
|
// フォロー対象が鍵アカウントである or
|
|
|
|
// フォロワーがBotであり、フォロー対象がBotからのフォローに慎重である or
|
|
|
|
// フォロワーがローカルユーザーであり、フォロー対象がリモートユーザーである
|
|
|
|
// 上記のいずれかに当てはまる場合はすぐフォローせずにフォローリクエストを発行しておく
|
2019-04-10 08:04:27 +02:00
|
|
|
if (followee.isLocked || (followeeProfile.carefulBot && follower.isBot) || (Users.isLocalUser(follower) && Users.isRemoteUser(followee))) {
|
2019-02-09 04:04:03 +01:00
|
|
|
let autoAccept = false;
|
|
|
|
|
2019-02-14 21:56:28 +01:00
|
|
|
// 鍵アカウントであっても、既にフォローされていた場合はスルー
|
2022-03-26 07:34:00 +01:00
|
|
|
const following = await Followings.findOneBy({
|
2019-04-07 14:50:36 +02:00
|
|
|
followerId: follower.id,
|
|
|
|
followeeId: followee.id,
|
2019-02-14 21:56:28 +01:00
|
|
|
});
|
|
|
|
if (following) {
|
|
|
|
autoAccept = true;
|
|
|
|
}
|
|
|
|
|
2019-02-09 04:04:03 +01:00
|
|
|
// フォローしているユーザーは自動承認オプション
|
2019-04-10 08:04:27 +02:00
|
|
|
if (!autoAccept && (Users.isLocalUser(followee) && followeeProfile.autoAcceptFollowed)) {
|
2022-03-26 07:34:00 +01:00
|
|
|
const followed = await Followings.findOneBy({
|
2019-04-07 14:50:36 +02:00
|
|
|
followerId: followee.id,
|
2021-12-09 15:58:30 +01:00
|
|
|
followeeId: follower.id,
|
2019-02-09 04:04:03 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
if (followed) autoAccept = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!autoAccept) {
|
|
|
|
await createFollowRequest(follower, followee, requestId);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
await insertFollowingDoc(followee, follower);
|
2018-04-04 16:59:38 +02:00
|
|
|
|
2019-04-07 14:50:36 +02:00
|
|
|
if (Users.isRemoteUser(follower) && Users.isLocalUser(followee)) {
|
2019-01-30 18:29:36 +01:00
|
|
|
const content = renderActivity(renderAccept(renderFollow(follower, followee, requestId), followee));
|
2018-10-13 13:11:00 +02:00
|
|
|
deliver(followee, content, follower.inbox);
|
2018-04-04 16:59:38 +02:00
|
|
|
}
|
|
|
|
}
|