iceshrimp-161sh/src/services/following/requests/create.ts

51 lines
1.6 KiB
TypeScript
Raw Normal View History

2018-06-01 17:38:31 +02:00
import User, { isLocalUser, isRemoteUser, pack as packUser, IUser } from '../../../models/user';
import { publishMainStream } from '../../../stream';
2018-07-07 20:15:54 +02:00
import notify from '../../../notify';
2018-06-01 17:38:31 +02:00
import pack from '../../../remote/activitypub/renderer';
import renderFollow from '../../../remote/activitypub/renderer/follow';
import { deliver } from '../../../queue';
import FollowRequest from '../../../models/follow-request';
export default async function(follower: IUser, followee: IUser) {
await FollowRequest.insert({
createdAt: new Date(),
followerId: follower._id,
followeeId: followee._id,
// 非正規化
_follower: {
host: follower.host,
2018-07-21 12:33:56 +02:00
inbox: isRemoteUser(follower) ? follower.inbox : undefined,
sharedInbox: isRemoteUser(follower) ? follower.sharedInbox : undefined
2018-06-01 17:38:31 +02:00
},
_followee: {
host: followee.host,
2018-07-21 12:33:56 +02:00
inbox: isRemoteUser(followee) ? followee.inbox : undefined,
sharedInbox: isRemoteUser(followee) ? followee.sharedInbox : undefined
2018-06-01 17:38:31 +02:00
}
});
2018-06-02 09:01:32 +02:00
await User.update({ _id: followee._id }, {
2018-06-01 17:38:31 +02:00
$inc: {
pendingReceivedFollowRequestsCount: 1
}
});
2018-06-02 09:13:32 +02:00
// Publish receiveRequest event
2018-06-01 17:38:31 +02:00
if (isLocalUser(followee)) {
packUser(follower, followee).then(packed => publishMainStream(followee._id, 'receiveFollowRequest', packed));
2018-06-02 09:01:32 +02:00
packUser(followee, followee, {
detail: true
}).then(packed => publishMainStream(followee._id, 'meUpdated', packed));
2018-06-01 17:38:31 +02:00
// 通知を作成
2018-06-02 09:13:32 +02:00
notify(followee._id, follower._id, 'receiveFollowRequest');
2018-06-01 17:38:31 +02:00
}
if (isLocalUser(follower) && isRemoteUser(followee)) {
const content = pack(renderFollow(follower, followee));
deliver(follower, content, followee.inbox);
}
}