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