Make HTTP request first in follow processor

This commit is contained in:
Akihiko Odaki 2018-04-04 21:56:04 +09:00
parent df38c2f485
commit 1b6bae72c2
1 changed files with 59 additions and 59 deletions

View File

@ -1,4 +1,4 @@
import User, { isLocalUser, pack as packUser } from '../../models/user'; import User, { isLocalUser, isRemoteUser, pack as packUser } from '../../models/user';
import Following from '../../models/following'; import Following from '../../models/following';
import FollowingLog from '../../models/following-log'; import FollowingLog from '../../models/following-log';
import FollowedLog from '../../models/followed-log'; import FollowedLog from '../../models/followed-log';
@ -7,12 +7,24 @@ import notify from '../../publishers/notify';
import context from '../../remote/activitypub/renderer/context'; import context from '../../remote/activitypub/renderer/context';
import render from '../../remote/activitypub/renderer/follow'; import render from '../../remote/activitypub/renderer/follow';
import request from '../../remote/request'; import request from '../../remote/request';
import Logger from '../../utils/logger';
export default ({ data }) => Following.findOne({ _id: data.following }).then(({ followerId, followeeId }) => { export default async ({ data }) => {
const promisedFollower = User.findOne({ _id: followerId }); const { followerId, followeeId } = await Following.findOne({ _id: data.following });
const promisedFollowee = User.findOne({ _id: followeeId }); const [follower, followee] = await Promise.all([
User.findOne({ _id: followerId }),
User.findOne({ _id: followeeId })
]);
return Promise.all([ if (isLocalUser(follower) && isRemoteUser(followee)) {
const rendered = render(follower, followee);
rendered['@context'] = context;
await request(follower, followee.account.inbox, rendered);
}
try {
await Promise.all([
// Increment following count // Increment following count
User.update(followerId, { User.update(followerId, {
$inc: { $inc: {
@ -20,11 +32,11 @@ export default ({ data }) => Following.findOne({ _id: data.following }).then(({
} }
}), }),
promisedFollower.then(({ followingCount }) => FollowingLog.insert({ FollowingLog.insert({
createdAt: data.following.createdAt, createdAt: data.following.createdAt,
userId: followerId, userId: followerId,
count: followingCount + 1 count: follower.followingCount + 1
})), }),
// Increment followers count // Increment followers count
User.update({ _id: followeeId }, { User.update({ _id: followeeId }, {
@ -33,37 +45,25 @@ export default ({ data }) => Following.findOne({ _id: data.following }).then(({
} }
}), }),
promisedFollowee.then(({ followersCount }) => FollowedLog.insert({ FollowedLog.insert({
createdAt: data.following.createdAt, createdAt: data.following.createdAt,
userId: followerId, userId: followerId,
count: followersCount + 1 count: followee.followersCount + 1
})), }),
// Notify
promisedFollowee.then(followee => followee.host === null ?
notify(followeeId, followerId, 'follow') : null),
// Publish follow event // Publish follow event
Promise.all([promisedFollower, promisedFollowee]).then(([follower, followee]) => { isLocalUser(follower) && packUser(followee, follower)
let followerEvent; .then(packed => event(follower._id, 'follow', packed)),
let followeeEvent;
if (isLocalUser(follower)) { isLocalUser(followee) && Promise.all([
followerEvent = packUser(followee, follower) packUser(follower, followee)
.then(packed => event(follower._id, 'follow', packed)); .then(packed => event(followee._id, 'followed', packed)),
}
if (isLocalUser(followee)) { // Notify
followeeEvent = packUser(follower, followee) isLocalUser(followee) && notify(followeeId, followerId, 'follow')
.then(packed => event(followee._id, 'followed', packed)); ])
} else if (isLocalUser(follower)) {
const rendered = render(follower, followee);
rendered['@context'] = context;
followeeEvent = request(follower, followee.account.inbox, rendered);
}
return Promise.all([followerEvent, followeeEvent]);
})
]); ]);
}); } catch (error) {
Logger.error(error.toString());
}
};