Make HTTP request first in unfollow job
This commit is contained in:
parent
1b6bae72c2
commit
86b1345c17
|
@ -1,56 +1,63 @@
|
||||||
import FollowedLog from '../../models/followed-log';
|
import FollowedLog from '../../models/followed-log';
|
||||||
import Following from '../../models/following';
|
import Following from '../../models/following';
|
||||||
import FollowingLog from '../../models/following-log';
|
import FollowingLog from '../../models/following-log';
|
||||||
import User, { isRemoteUser, pack as packUser } from '../../models/user';
|
import User, { isLocalUser, isRemoteUser, pack as packUser } from '../../models/user';
|
||||||
import stream from '../../publishers/stream';
|
import stream from '../../publishers/stream';
|
||||||
import renderFollow from '../../remote/activitypub/renderer/follow';
|
import renderFollow from '../../remote/activitypub/renderer/follow';
|
||||||
import renderUndo from '../../remote/activitypub/renderer/undo';
|
import renderUndo from '../../remote/activitypub/renderer/undo';
|
||||||
import context from '../../remote/activitypub/renderer/context';
|
import context from '../../remote/activitypub/renderer/context';
|
||||||
import request from '../../remote/request';
|
import request from '../../remote/request';
|
||||||
|
import Logger from '../../utils/logger';
|
||||||
|
|
||||||
export default async ({ data }) => {
|
export default async ({ data }) => {
|
||||||
// Delete following
|
const following = await Following.findOne({ _id: data.id });
|
||||||
const following = await Following.findOneAndDelete({ _id: data.id });
|
|
||||||
if (following === null) {
|
if (following === null) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
const promisedFollower = User.findOne({ _id: following.followerId });
|
const [follower, followee] = await Promise.all([
|
||||||
const promisedFollowee = User.findOne({ _id: following.followeeId });
|
User.findOne({ _id: following.followerId }),
|
||||||
|
User.findOne({ _id: following.followeeId })
|
||||||
|
]);
|
||||||
|
|
||||||
await Promise.all([
|
if (isLocalUser(follower) && isRemoteUser(followee)) {
|
||||||
// Decrement following count
|
|
||||||
User.update({ _id: following.followerId }, { $inc: { followingCount: -1 } }),
|
|
||||||
promisedFollower.then(({ followingCount }) => FollowingLog.insert({
|
|
||||||
createdAt: new Date(),
|
|
||||||
userId: following.followerId,
|
|
||||||
count: followingCount - 1
|
|
||||||
})),
|
|
||||||
|
|
||||||
// Decrement followers count
|
|
||||||
User.update({ _id: following.followeeId }, { $inc: { followersCount: -1 } }),
|
|
||||||
promisedFollowee.then(({ followersCount }) => FollowedLog.insert({
|
|
||||||
createdAt: new Date(),
|
|
||||||
userId: following.followeeId,
|
|
||||||
count: followersCount - 1
|
|
||||||
})),
|
|
||||||
|
|
||||||
// Publish follow event
|
|
||||||
Promise.all([promisedFollower, promisedFollowee]).then(async ([follower, followee]) => {
|
|
||||||
if (isRemoteUser(follower)) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
const promisedPackedUser = packUser(followee, follower);
|
|
||||||
|
|
||||||
if (isRemoteUser(followee)) {
|
|
||||||
const undo = renderUndo(renderFollow(follower, followee));
|
const undo = renderUndo(renderFollow(follower, followee));
|
||||||
undo['@context'] = context;
|
undo['@context'] = context;
|
||||||
|
|
||||||
await request(follower, followee.account.inbox, undo);
|
await request(follower, followee.account.inbox, undo);
|
||||||
}
|
}
|
||||||
|
|
||||||
stream(follower._id, 'unfollow', promisedPackedUser);
|
try {
|
||||||
|
await Promise.all([
|
||||||
|
// Delete following
|
||||||
|
Following.findOneAndDelete({ _id: data.id }),
|
||||||
|
|
||||||
|
// Decrement following count
|
||||||
|
User.update({ _id: follower._id }, { $inc: { followingCount: -1 } }),
|
||||||
|
FollowingLog.insert({
|
||||||
|
createdAt: new Date(),
|
||||||
|
userId: follower._id,
|
||||||
|
count: follower.followingCount - 1
|
||||||
|
}),
|
||||||
|
|
||||||
|
// Decrement followers count
|
||||||
|
User.update({ _id: followee._id }, { $inc: { followersCount: -1 } }),
|
||||||
|
FollowedLog.insert({
|
||||||
|
createdAt: new Date(),
|
||||||
|
userId: followee._id,
|
||||||
|
count: followee.followersCount - 1
|
||||||
})
|
})
|
||||||
]);
|
]);
|
||||||
|
|
||||||
|
if (isLocalUser(follower)) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const promisedPackedUser = packUser(followee, follower);
|
||||||
|
|
||||||
|
// Publish follow event
|
||||||
|
stream(follower._id, 'unfollow', promisedPackedUser);
|
||||||
|
} catch (error) {
|
||||||
|
Logger.error(error.toString());
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
Loading…
Reference in New Issue