summaryrefslogtreecommitdiff
path: root/src/queue/processors/http/follow.ts
diff options
context:
space:
mode:
Diffstat (limited to 'src/queue/processors/http/follow.ts')
-rw-r--r--src/queue/processors/http/follow.ts69
1 files changed, 69 insertions, 0 deletions
diff --git a/src/queue/processors/http/follow.ts b/src/queue/processors/http/follow.ts
new file mode 100644
index 0000000000..4cb72828e7
--- /dev/null
+++ b/src/queue/processors/http/follow.ts
@@ -0,0 +1,69 @@
+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';
+import event from '../../../publishers/stream';
+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 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 })
+ ]);
+
+ 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
+ User.update(followerId, {
+ $inc: {
+ followingCount: 1
+ }
+ }),
+
+ FollowingLog.insert({
+ createdAt: data.following.createdAt,
+ userId: followerId,
+ count: follower.followingCount + 1
+ }),
+
+ // Increment followers count
+ User.update({ _id: followeeId }, {
+ $inc: {
+ followersCount: 1
+ }
+ }),
+
+ FollowedLog.insert({
+ createdAt: data.following.createdAt,
+ userId: followerId,
+ count: followee.followersCount + 1
+ }),
+
+ // Publish follow event
+ isLocalUser(follower) && packUser(followee, follower)
+ .then(packed => event(follower._id, 'follow', packed)),
+
+ isLocalUser(followee) && Promise.all([
+ packUser(follower, followee)
+ .then(packed => event(followee._id, 'followed', packed)),
+
+ // Notify
+ isLocalUser(followee) && notify(followeeId, followerId, 'follow')
+ ])
+ ]);
+ } catch (error) {
+ Logger.error(error.toString());
+ }
+};