summaryrefslogtreecommitdiff
path: root/src/queue/processors/http/follow.ts
diff options
context:
space:
mode:
authorsyuilo <syuilotan@yahoo.co.jp>2018-04-04 23:12:35 +0900
committersyuilo <syuilotan@yahoo.co.jp>2018-04-04 23:12:35 +0900
commite8b42d7e1668679e6a6ee0a7aea1e2ff7f37005b (patch)
tree6973284192eb419bd7bfed2361a594e668b81f9a /src/queue/processors/http/follow.ts
parentMerge pull request #1393 from akihikodaki/duplicate (diff)
downloadmisskey-e8b42d7e1668679e6a6ee0a7aea1e2ff7f37005b.tar.gz
misskey-e8b42d7e1668679e6a6ee0a7aea1e2ff7f37005b.tar.bz2
misskey-e8b42d7e1668679e6a6ee0a7aea1e2ff7f37005b.zip
wip
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..8bf890efbc
--- /dev/null
+++ b/src/queue/processors/http/follow.ts
@@ -0,0 +1,69 @@
+import User, { isLocalUser, 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';
+
+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
+ }
+ }),
+
+ promisedFollower.then(({ followingCount }) => FollowingLog.insert({
+ createdAt: data.following.createdAt,
+ userId: followerId,
+ count: followingCount + 1
+ })),
+
+ // Increment followers count
+ User.update({ _id: followeeId }, {
+ $inc: {
+ followersCount: 1
+ }
+ }),
+
+ promisedFollowee.then(({ followersCount }) => FollowedLog.insert({
+ createdAt: data.following.createdAt,
+ userId: followerId,
+ count: followersCount + 1
+ })),
+
+ // Notify
+ promisedFollowee.then(followee => followee.host === null ?
+ notify(followeeId, followerId, 'follow') : null),
+
+ // Publish follow event
+ Promise.all([promisedFollower, promisedFollowee]).then(([follower, followee]) => {
+ let followerEvent;
+ let followeeEvent;
+
+ if (isLocalUser(follower)) {
+ followerEvent = packUser(followee, follower)
+ .then(packed => event(follower._id, 'follow', packed));
+ }
+
+ 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;
+
+ followeeEvent = request(follower, followee.account.inbox, rendered);
+ }
+
+ return Promise.all([followerEvent, followeeEvent]);
+ })
+ ]);
+});