2021-03-21 07:14:03 +01:00
|
|
|
import { publishMainStream, publishUserEvent } from '../stream';
|
2019-01-30 18:29:36 +01:00
|
|
|
import { renderActivity } from '../../remote/activitypub/renderer';
|
2018-04-04 16:59:38 +02:00
|
|
|
import renderFollow from '../../remote/activitypub/renderer/follow';
|
|
|
|
import renderAccept from '../../remote/activitypub/renderer/accept';
|
2018-10-29 12:32:42 +01:00
|
|
|
import renderReject from '../../remote/activitypub/renderer/reject';
|
2018-04-05 16:24:51 +02:00
|
|
|
import { deliver } from '../../queue';
|
2018-06-01 17:38:31 +02:00
|
|
|
import createFollowRequest from './requests/create';
|
2019-02-07 07:00:44 +01:00
|
|
|
import { registerOrFetchInstanceDoc } from '../register-or-fetch-instance-doc';
|
2019-03-02 10:51:59 +01:00
|
|
|
import Logger from '../logger';
|
2021-03-23 09:43:07 +01:00
|
|
|
import { IdentifiableError } from '@/misc/identifiable-error';
|
2019-04-07 14:50:36 +02:00
|
|
|
import { User } from '../../models/entities/user';
|
2019-04-10 08:04:27 +02:00
|
|
|
import { Followings, Users, FollowRequests, Blockings, Instances, UserProfiles } from '../../models';
|
2019-04-07 14:50:36 +02:00
|
|
|
import { instanceChart, perUserFollowingChart } from '../chart';
|
2021-03-23 09:43:07 +01:00
|
|
|
import { genId } from '@/misc/gen-id';
|
2019-04-07 14:50:36 +02:00
|
|
|
import { createNotification } from '../create-notification';
|
2021-03-23 09:43:07 +01:00
|
|
|
import { isDuplicateKeyValueError } from '@/misc/is-duplicate-key-value-error';
|
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,
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2020-01-29 20:37:25 +01:00
|
|
|
const req = await FollowRequests.findOne({
|
2019-04-07 14:50:36 +02:00
|
|
|
followeeId: followee.id,
|
|
|
|
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,
|
|
|
|
followerId: follower.id
|
|
|
|
});
|
|
|
|
|
|
|
|
// 通知を作成
|
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, {
|
2018-10-30 06:34:32 +01:00
|
|
|
detail: true
|
2021-03-21 07:14:03 +01:00
|
|
|
}).then(packed => {
|
|
|
|
publishUserEvent(follower.id, 'follow', packed);
|
|
|
|
publishMainStream(follower.id, 'follow', packed);
|
|
|
|
});
|
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)) {
|
2021-03-24 03:05:37 +01:00
|
|
|
Users.pack(follower.id, followee).then(packed => publishMainStream(followee.id, 'followed', 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', {
|
|
|
|
notifierId: follower.id
|
|
|
|
});
|
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([
|
|
|
|
Users.findOneOrFail(_follower.id),
|
|
|
|
Users.findOneOrFail(_followee.id)
|
|
|
|
]);
|
|
|
|
|
2019-02-09 04:04:03 +01:00
|
|
|
// check blocking
|
|
|
|
const [blocking, blocked] = await Promise.all([
|
2019-04-07 14:50:36 +02:00
|
|
|
Blockings.findOne({
|
|
|
|
blockerId: follower.id,
|
|
|
|
blockeeId: followee.id,
|
2019-02-09 04:04:03 +01:00
|
|
|
}),
|
2019-04-07 14:50:36 +02:00
|
|
|
Blockings.findOne({
|
|
|
|
blockerId: followee.id,
|
|
|
|
blockeeId: follower.id,
|
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
|
|
|
}
|
|
|
|
|
2021-02-13 07:33:38 +01:00
|
|
|
const followeeProfile = await UserProfiles.findOneOrFail(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
|
|
|
// 鍵アカウントであっても、既にフォローされていた場合はスルー
|
2019-04-07 14:50:36 +02:00
|
|
|
const following = await Followings.findOne({
|
|
|
|
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)) {
|
2019-04-07 14:50:36 +02:00
|
|
|
const followed = await Followings.findOne({
|
|
|
|
followerId: followee.id,
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|