Extract http request from post delivery job

This commit is contained in:
Akihiko Odaki 2018-04-04 20:29:26 +09:00
parent 783453897f
commit df38c2f485
2 changed files with 104 additions and 92 deletions

View file

@ -1,8 +1,11 @@
import Channel from '../models/channel';
import ChannelWatching from '../models/channel-watching';
import Following from '../models/following';
import Mute from '../models/mute';
import Post, { pack } from '../models/post';
import Watching from '../models/post-watching';
import User from '../models/user';
import stream from '../publishers/stream';
import User, { isLocalUser } from '../models/user';
import stream, { publishChannelStream } from '../publishers/stream';
import notify from '../publishers/notify';
import pushSw from '../publishers/push-sw';
import queue from '../queue';
@ -21,10 +24,6 @@ export default async (user, mentions, post) => {
latestPost: post._id
}
}),
new Promise((resolve, reject) => queue.create('http', {
type: 'deliverPost',
id: post._id,
}).save(error => error ? reject(error) : resolve())),
] as Array<Promise<any>>;
function addMention(promisedMentionee, reason) {
@ -50,6 +49,91 @@ export default async (user, mentions, post) => {
}));
}
// タイムラインへの投稿
if (!post.channelId) {
promises.push(
// Publish event to myself's stream
promisedPostObj.then(postObj => {
stream(post.userId, 'post', postObj);
}),
Promise.all([
User.findOne({ _id: post.userId }),
// Fetch all followers
Following.aggregate([{
$lookup: {
from: 'users',
localField: 'followerId',
foreignField: '_id',
as: 'follower'
}
}, {
$match: {
followeeId: post.userId
}
}], {
_id: false
})
]).then(([user, followers]) => Promise.all(followers.map(following => {
if (isLocalUser(following.follower)) {
// Publish event to followers stream
return promisedPostObj.then(postObj => {
stream(following.followerId, 'post', postObj);
});
}
return new Promise((resolve, reject) => {
queue.create('http', {
type: 'deliverPost',
fromId: user._id,
toId: following.followerId,
postId: post._id
}).save(error => {
if (error) {
reject(error);
} else {
resolve();
}
});
});
})))
);
}
// チャンネルへの投稿
if (post.channelId) {
promises.push(
// Increment channel index(posts count)
Channel.update({ _id: post.channelId }, {
$inc: {
index: 1
}
}),
// Publish event to channel
promisedPostObj.then(postObj => {
publishChannelStream(post.channelId, 'post', postObj);
}),
Promise.all([
promisedPostObj,
// Get channel watchers
ChannelWatching.find({
channelId: post.channelId,
// 削除されたドキュメントは除く
deletedAt: { $exists: false }
})
]).then(([postObj, watches]) => {
// チャンネルの視聴者(のタイムライン)に配信
watches.forEach(w => {
stream(w.userId, 'post', postObj);
});
})
);
}
// If has in reply to post
if (post.replyId) {
promises.push(