2019-02-05 06:14:23 +01:00
|
|
|
import { publishMainStream } 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';
|
2019-02-22 03:46:58 +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';
|
|
|
|
import { genId } from '../../misc/gen-id';
|
|
|
|
import { createNotification } from '../create-notification';
|
|
|
|
import { isDuplicateKeyValueError } from '../../misc/is-duplicate-key-value-error';
|
2019-04-12 18:43:22 +02:00
|
|
|
import { ensure } from '../../prelude/ensure';
|
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
|
|
|
|
2019-04-07 14:50:36 +02:00
|
|
|
export async function insertFollowingDoc(followee: User, follower: User) {
|
|
|
|
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
|
|
|
|
2019-04-07 14:50:36 +02:00
|
|
|
await Followings.save({
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2019-04-07 14:50:36 +02:00
|
|
|
await FollowRequests.delete({
|
|
|
|
followeeId: followee.id,
|
|
|
|
followerId: follower.id
|
2018-10-13 13:11:00 +02:00
|
|
|
});
|
2018-04-04 16:59:38 +02: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)) {
|
|
|
|
Users.pack(followee, follower, {
|
2018-10-30 06:34:32 +01:00
|
|
|
detail: true
|
2019-04-07 14:50:36 +02:00
|
|
|
}).then(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)) {
|
|
|
|
Users.pack(follower, followee).then(packed => publishMainStream(followee.id, 'followed', packed)),
|
2018-05-31 15:56:02 +02:00
|
|
|
|
2018-10-13 13:11:00 +02:00
|
|
|
// 通知を作成
|
2019-04-07 14:50:36 +02:00
|
|
|
createNotification(followee.id, follower.id, 'follow');
|
2018-10-13 13:11:00 +02:00
|
|
|
}
|
2019-02-09 04:04:03 +01:00
|
|
|
}
|
|
|
|
|
2019-04-07 14:50:36 +02:00
|
|
|
export default async function(follower: User, followee: User, requestId?: string) {
|
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
|
|
|
}
|
|
|
|
|
2019-04-12 18:43:22 +02:00
|
|
|
const followeeProfile = await UserProfiles.findOne(followee.id).then(ensure);
|
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
|
|
|
}
|
|
|
|
}
|