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