This commit is contained in:
syuilo 2018-04-04 23:12:35 +09:00
parent b60121527d
commit e8b42d7e16
20 changed files with 354 additions and 377 deletions

View file

@ -1,8 +1,14 @@
import parseAcct from '../acct/parse';
import Post from '../models/post';
import User from '../models/user';
import Post, { pack } from '../models/post';
import User, { isLocalUser, isRemoteUser, IUser } from '../models/user';
import stream from '../publishers/stream';
import Following from '../models/following';
import { createHttp } from '../queue';
import renderNote from '../remote/activitypub/renderer/note';
import renderCreate from '../remote/activitypub/renderer/create';
import context from '../remote/activitypub/renderer/context';
export default async (post, reply, repost, atMentions) => {
export default async (user: IUser, post, reply, repost, atMentions) => {
post.mentions = [];
function addMention(mentionee) {
@ -46,5 +52,98 @@ export default async (post, reply, repost, atMentions) => {
addMention(_id);
}));
return Post.insert(post);
const inserted = await Post.insert(post);
User.update({ _id: user._id }, {
// Increment my posts count
$inc: {
postsCount: 1
},
$set: {
latestPost: post._id
}
});
const postObj = await pack(inserted);
// タイムラインへの投稿
if (!post.channelId) {
// Publish event to myself's stream
stream(post.userId, 'post', postObj);
// Fetch all followers
const followers = await Following.aggregate([{
$lookup: {
from: 'users',
localField: 'followerId',
foreignField: '_id',
as: 'follower'
}
}, {
$match: {
followeeId: post.userId
}
}], {
_id: false
});
const note = await renderNote(user, post);
const content = renderCreate(note);
content['@context'] = context;
Promise.all(followers.map(({ follower }) => {
if (isLocalUser(follower)) {
// Publish event to followers stream
stream(follower._id, 'post', postObj);
} else {
// フォロワーがリモートユーザーかつ投稿者がローカルユーザーなら投稿を配信
if (isLocalUser(user)) {
createHttp({
type: 'deliver',
user,
content,
to: follower.account.inbox
}).save();
}
}
}));
}
// チャンネルへの投稿
/* TODO
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);
});
})
);
}*/
return Promise.all(promises);
};