2022-02-27 03:07:39 +01:00
|
|
|
import { publishMainStream } from '@/services/stream.js';
|
|
|
|
import { renderActivity } from '@/remote/activitypub/renderer/index.js';
|
|
|
|
import renderFollow from '@/remote/activitypub/renderer/follow.js';
|
|
|
|
import { deliver } from '@/queue/index.js';
|
|
|
|
import { User } from '@/models/entities/user.js';
|
|
|
|
import { Blockings, FollowRequests, Users } from '@/models/index.js';
|
|
|
|
import { genId } from '@/misc/gen-id.js';
|
|
|
|
import { createNotification } from '../../create-notification.js';
|
2019-04-07 14:50:36 +02:00
|
|
|
|
2021-03-24 03:05:37 +01:00
|
|
|
export default async function(follower: { id: User['id']; host: User['host']; uri: User['host']; inbox: User['inbox']; sharedInbox: User['sharedInbox']; }, followee: { id: User['id']; host: User['host']; uri: User['host']; inbox: User['inbox']; sharedInbox: User['sharedInbox']; }, requestId?: string) {
|
2019-04-07 14:50:36 +02:00
|
|
|
if (follower.id === followee.id) return;
|
2018-06-01 17:38:31 +02:00
|
|
|
|
2018-10-29 12:32:42 +01:00
|
|
|
// check blocking
|
2018-10-29 13:38:09 +01:00
|
|
|
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,
|
2018-10-29 12:32:42 +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
|
|
|
}),
|
2018-10-29 12:32:42 +01:00
|
|
|
]);
|
|
|
|
|
|
|
|
if (blocking != null) throw new Error('blocking');
|
|
|
|
if (blocked != null) throw new Error('blocked');
|
|
|
|
|
2022-01-25 16:51:26 +01:00
|
|
|
const followRequest = await FollowRequests.insert({
|
2019-04-07 14:50:36 +02:00
|
|
|
id: genId(),
|
2018-06-01 17:38:31 +02:00
|
|
|
createdAt: new Date(),
|
2019-04-07 14:50:36 +02:00
|
|
|
followerId: follower.id,
|
|
|
|
followeeId: followee.id,
|
2018-10-15 09:51:23 +02:00
|
|
|
requestId,
|
2018-06-01 17:38:31 +02:00
|
|
|
|
|
|
|
// 非正規化
|
2019-04-07 14:50:36 +02:00
|
|
|
followerHost: follower.host,
|
|
|
|
followerInbox: Users.isRemoteUser(follower) ? follower.inbox : undefined,
|
|
|
|
followerSharedInbox: Users.isRemoteUser(follower) ? follower.sharedInbox : undefined,
|
|
|
|
followeeHost: followee.host,
|
|
|
|
followeeInbox: Users.isRemoteUser(followee) ? followee.inbox : undefined,
|
2021-12-09 15:58:30 +01:00
|
|
|
followeeSharedInbox: Users.isRemoteUser(followee) ? followee.sharedInbox : undefined,
|
2022-03-26 07:34:00 +01:00
|
|
|
}).then(x => FollowRequests.findOneByOrFail(x.identifiers[0]));
|
2018-06-01 17:38:31 +02:00
|
|
|
|
2018-06-02 09:13:32 +02:00
|
|
|
// Publish receiveRequest 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, 'receiveFollowRequest', packed));
|
2018-06-02 09:01:32 +02:00
|
|
|
|
2021-03-24 03:05:37 +01:00
|
|
|
Users.pack(followee.id, followee, {
|
2021-12-09 15:58:30 +01:00
|
|
|
detail: true,
|
2019-04-07 14:50:36 +02:00
|
|
|
}).then(packed => publishMainStream(followee.id, 'meUpdated', packed));
|
2018-06-01 17:38:31 +02:00
|
|
|
|
|
|
|
// 通知を作成
|
2020-03-28 10:07:41 +01:00
|
|
|
createNotification(followee.id, 'receiveFollowRequest', {
|
|
|
|
notifierId: follower.id,
|
2021-12-09 15:58:30 +01:00
|
|
|
followRequestId: followRequest.id,
|
2020-01-29 20:37:25 +01:00
|
|
|
});
|
2018-06-01 17:38:31 +02:00
|
|
|
}
|
|
|
|
|
2019-04-07 14:50:36 +02:00
|
|
|
if (Users.isLocalUser(follower) && Users.isRemoteUser(followee)) {
|
2019-01-30 18:29:36 +01:00
|
|
|
const content = renderActivity(renderFollow(follower, followee));
|
2018-06-01 17:38:31 +02:00
|
|
|
deliver(follower, content, followee.inbox);
|
|
|
|
}
|
|
|
|
}
|