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-06-01 17:38:31 +02:00
|
|
|
import renderFollow from '../../../remote/activitypub/renderer/follow';
|
|
|
|
import { deliver } from '../../../queue';
|
2019-04-07 14:50:36 +02:00
|
|
|
import { User } from '../../../models/entities/user';
|
|
|
|
import { Blockings, FollowRequests, Users } from '../../../models';
|
|
|
|
import { genId } from '../../../misc/gen-id';
|
|
|
|
import { createNotification } from '../../create-notification';
|
|
|
|
|
|
|
|
export default async function(follower: User, followee: User, requestId?: string) {
|
|
|
|
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([
|
2019-04-07 14:50:36 +02:00
|
|
|
Blockings.findOne({
|
|
|
|
blockerId: follower.id,
|
|
|
|
blockeeId: followee.id,
|
2018-10-29 12:32:42 +01:00
|
|
|
}),
|
2019-04-07 14:50:36 +02:00
|
|
|
Blockings.findOne({
|
|
|
|
blockerId: followee.id,
|
|
|
|
blockeeId: follower.id,
|
2018-10-29 12:32:42 +01:00
|
|
|
})
|
|
|
|
]);
|
|
|
|
|
|
|
|
if (blocking != null) throw new Error('blocking');
|
|
|
|
if (blocked != null) throw new Error('blocked');
|
|
|
|
|
2020-01-29 20:37:25 +01:00
|
|
|
const followRequest = await FollowRequests.save({
|
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,
|
|
|
|
followeeSharedInbox: Users.isRemoteUser(followee) ? followee.sharedInbox : undefined
|
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)) {
|
|
|
|
Users.pack(follower, followee).then(packed => publishMainStream(followee.id, 'receiveFollowRequest', packed));
|
2018-06-02 09:01:32 +02:00
|
|
|
|
2019-04-07 14:50:36 +02:00
|
|
|
Users.pack(followee, followee, {
|
2018-06-02 09:01:32 +02: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-01-29 20:37:25 +01:00
|
|
|
createNotification(followee.id, follower.id, 'receiveFollowRequest', {
|
|
|
|
followRequestId: followRequest.id
|
|
|
|
});
|
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);
|
|
|
|
}
|
|
|
|
}
|