yumechi-no-kuni/src/services/following/requests/accept.ts

79 lines
2.1 KiB
TypeScript
Raw Normal View History

2018-06-18 00:28:43 -05:00
import User, { IUser, isRemoteUser, ILocalUser, pack as packUser } from '../../../models/user';
import FollowRequest from '../../../models/follow-request';
2018-06-01 10:15:17 -05:00
import pack from '../../../remote/activitypub/renderer';
import renderFollow from '../../../remote/activitypub/renderer/follow';
import renderAccept from '../../../remote/activitypub/renderer/accept';
import { deliver } from '../../../queue';
2018-06-18 00:28:43 -05:00
import Following from '../../../models/following';
import FollowingLog from '../../../models/following-log';
import FollowedLog from '../../../models/followed-log';
2018-07-07 05:19:00 -05:00
import event from '../../../stream';
2018-05-31 08:56:02 -05:00
export default async function(followee: IUser, follower: IUser) {
const following = await Following.insert({
createdAt: new Date(),
followerId: follower._id,
followeeId: followee._id,
// 非正規化
_follower: {
host: follower.host,
2018-07-21 05:33:56 -05:00
inbox: isRemoteUser(follower) ? follower.inbox : undefined,
sharedInbox: isRemoteUser(follower) ? follower.sharedInbox : undefined
2018-05-31 08:56:02 -05:00
},
_followee: {
host: followee.host,
2018-07-21 05:33:56 -05:00
inbox: isRemoteUser(followee) ? followee.inbox : undefined,
sharedInbox: isRemoteUser(followee) ? followee.sharedInbox : undefined
2018-05-31 08:56:02 -05:00
}
});
if (isRemoteUser(follower)) {
const content = pack(renderAccept(renderFollow(follower, followee)));
deliver(followee as ILocalUser, content, follower.inbox);
}
2018-06-02 02:01:32 -05:00
await FollowRequest.remove({
2018-05-31 08:56:02 -05:00
followeeId: followee._id,
followerId: follower._id
});
//#region Increment following count
2018-06-02 02:01:32 -05:00
await User.update({ _id: follower._id }, {
2018-05-31 08:56:02 -05:00
$inc: {
followingCount: 1
}
});
FollowingLog.insert({
createdAt: following.createdAt,
userId: follower._id,
count: follower.followingCount + 1
});
//#endregion
//#region Increment followers count
2018-06-02 02:01:32 -05:00
await User.update({ _id: followee._id }, {
2018-05-31 08:56:02 -05:00
$inc: {
followersCount: 1
}
});
2018-06-02 02:01:32 -05:00
2018-05-31 08:56:02 -05:00
FollowedLog.insert({
createdAt: following.createdAt,
userId: followee._id,
count: followee.followersCount + 1
});
//#endregion
2018-06-02 02:01:32 -05:00
2018-07-27 17:56:33 -05:00
await User.update({ _id: followee._id }, {
$inc: {
pendingReceivedFollowRequestsCount: -1
}
});
2018-06-02 02:01:32 -05:00
packUser(followee, followee, {
detail: true
}).then(packed => event(followee._id, 'meUpdated', packed));
2018-05-31 08:56:02 -05:00
}