2019-02-04 22:14:23 -07:00
|
|
|
import { publishMainStream } from '../../stream';
|
2019-01-30 10:29:36 -07:00
|
|
|
import { renderActivity } from '../../../remote/activitypub/renderer';
|
2018-06-01 09:38:31 -06:00
|
|
|
import renderFollow from '../../../remote/activitypub/renderer/follow';
|
|
|
|
import { deliver } from '../../../queue';
|
2019-04-07 06:50:36 -06: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 09:38:31 -06:00
|
|
|
|
2018-10-29 05:32:42 -06:00
|
|
|
// check blocking
|
2018-10-29 06:38:09 -06:00
|
|
|
const [blocking, blocked] = await Promise.all([
|
2019-04-07 06:50:36 -06:00
|
|
|
Blockings.findOne({
|
|
|
|
blockerId: follower.id,
|
|
|
|
blockeeId: followee.id,
|
2018-10-29 05:32:42 -06:00
|
|
|
}),
|
2019-04-07 06:50:36 -06:00
|
|
|
Blockings.findOne({
|
|
|
|
blockerId: followee.id,
|
|
|
|
blockeeId: follower.id,
|
2018-10-29 05:32:42 -06:00
|
|
|
})
|
|
|
|
]);
|
|
|
|
|
|
|
|
if (blocking != null) throw new Error('blocking');
|
|
|
|
if (blocked != null) throw new Error('blocked');
|
|
|
|
|
2019-04-07 06:50:36 -06:00
|
|
|
await FollowRequests.save({
|
|
|
|
id: genId(),
|
2018-06-01 09:38:31 -06:00
|
|
|
createdAt: new Date(),
|
2019-04-07 06:50:36 -06:00
|
|
|
followerId: follower.id,
|
|
|
|
followeeId: followee.id,
|
2018-10-15 01:51:23 -06:00
|
|
|
requestId,
|
2018-06-01 09:38:31 -06:00
|
|
|
|
|
|
|
// 非正規化
|
2019-04-07 06:50:36 -06: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 09:38:31 -06:00
|
|
|
});
|
|
|
|
|
2018-06-02 01:13:32 -06:00
|
|
|
// Publish receiveRequest event
|
2019-04-07 06:50:36 -06:00
|
|
|
if (Users.isLocalUser(followee)) {
|
|
|
|
Users.pack(follower, followee).then(packed => publishMainStream(followee.id, 'receiveFollowRequest', packed));
|
2018-06-02 01:01:32 -06:00
|
|
|
|
2019-04-07 06:50:36 -06:00
|
|
|
Users.pack(followee, followee, {
|
2018-06-02 01:01:32 -06:00
|
|
|
detail: true
|
2019-04-07 06:50:36 -06:00
|
|
|
}).then(packed => publishMainStream(followee.id, 'meUpdated', packed));
|
2018-06-01 09:38:31 -06:00
|
|
|
|
|
|
|
// 通知を作成
|
2019-04-07 06:50:36 -06:00
|
|
|
createNotification(followee.id, follower.id, 'receiveFollowRequest');
|
2018-06-01 09:38:31 -06:00
|
|
|
}
|
|
|
|
|
2019-04-07 06:50:36 -06:00
|
|
|
if (Users.isLocalUser(follower) && Users.isRemoteUser(followee)) {
|
2019-01-30 10:29:36 -07:00
|
|
|
const content = renderActivity(renderFollow(follower, followee));
|
2018-06-01 09:38:31 -06:00
|
|
|
deliver(follower, content, followee.inbox);
|
|
|
|
}
|
|
|
|
}
|