summaryrefslogtreecommitdiff
path: root/src/processor
diff options
context:
space:
mode:
Diffstat (limited to 'src/processor')
-rw-r--r--src/processor/http/follow.ts102
1 files changed, 51 insertions, 51 deletions
diff --git a/src/processor/http/follow.ts b/src/processor/http/follow.ts
index 8bf890efbc..ed36fa18d4 100644
--- a/src/processor/http/follow.ts
+++ b/src/processor/http/follow.ts
@@ -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 FollowingLog from '../../models/following-log';
import FollowedLog from '../../models/followed-log';
@@ -7,63 +7,63 @@ import notify from '../../publishers/notify';
import context from '../../remote/activitypub/renderer/context';
import render from '../../remote/activitypub/renderer/follow';
import request from '../../remote/request';
+import Logger from '../../utils/logger';
-export default ({ data }) => Following.findOne({ _id: data.following }).then(({ followerId, followeeId }) => {
- const promisedFollower = User.findOne({ _id: followerId });
- const promisedFollowee = User.findOne({ _id: followeeId });
-
- return Promise.all([
- // Increment following count
- User.update(followerId, {
- $inc: {
- followingCount: 1
- }
- }),
+export default async ({ data }) => {
+ const { followerId, followeeId } = await Following.findOne({ _id: data.following });
+ const [follower, followee] = await Promise.all([
+ User.findOne({ _id: followerId }),
+ User.findOne({ _id: followeeId })
+ ]);
- promisedFollower.then(({ followingCount }) => FollowingLog.insert({
- createdAt: data.following.createdAt,
- userId: followerId,
- count: followingCount + 1
- })),
+ if (isLocalUser(follower) && isRemoteUser(followee)) {
+ const rendered = render(follower, followee);
+ rendered['@context'] = context;
- // Increment followers count
- User.update({ _id: followeeId }, {
- $inc: {
- followersCount: 1
- }
- }),
+ await request(follower, followee.account.inbox, rendered);
+ }
- promisedFollowee.then(({ followersCount }) => FollowedLog.insert({
- createdAt: data.following.createdAt,
- userId: followerId,
- count: followersCount + 1
- })),
+ try {
+ await Promise.all([
+ // Increment following count
+ User.update(followerId, {
+ $inc: {
+ followingCount: 1
+ }
+ }),
- // Notify
- promisedFollowee.then(followee => followee.host === null ?
- notify(followeeId, followerId, 'follow') : null),
+ FollowingLog.insert({
+ createdAt: data.following.createdAt,
+ userId: followerId,
+ count: follower.followingCount + 1
+ }),
- // Publish follow event
- Promise.all([promisedFollower, promisedFollowee]).then(([follower, followee]) => {
- let followerEvent;
- let followeeEvent;
+ // Increment followers count
+ User.update({ _id: followeeId }, {
+ $inc: {
+ followersCount: 1
+ }
+ }),
- if (isLocalUser(follower)) {
- followerEvent = packUser(followee, follower)
- .then(packed => event(follower._id, 'follow', packed));
- }
+ FollowedLog.insert({
+ createdAt: data.following.createdAt,
+ userId: followerId,
+ count: followee.followersCount + 1
+ }),
- if (isLocalUser(followee)) {
- followeeEvent = packUser(follower, followee)
- .then(packed => event(followee._id, 'followed', packed));
- } else if (isLocalUser(follower)) {
- const rendered = render(follower, followee);
- rendered['@context'] = context;
+ // Publish follow event
+ isLocalUser(follower) && packUser(followee, follower)
+ .then(packed => event(follower._id, 'follow', packed)),
- followeeEvent = request(follower, followee.account.inbox, rendered);
- }
+ isLocalUser(followee) && Promise.all([
+ packUser(follower, followee)
+ .then(packed => event(followee._id, 'followed', packed)),
- return Promise.all([followerEvent, followeeEvent]);
- })
- ]);
-});
+ // Notify
+ isLocalUser(followee) && notify(followeeId, followerId, 'follow')
+ ])
+ ]);
+ } catch (error) {
+ Logger.error(error.toString());
+ }
+};