This commit is contained in:
syuilo 2018-04-06 04:04:50 +09:00
parent 0154e44e1d
commit a6fb4f2e33
2 changed files with 69 additions and 43 deletions

View File

@ -52,6 +52,20 @@ export type IPost = {
speed: number; speed: number;
}; };
uri: string; uri: string;
_reply?: {
userId: mongo.ObjectID;
};
_repost?: {
userId: mongo.ObjectID;
};
_user: {
host: string;
hostLower: string;
account: {
inbox?: string;
};
};
}; };
/** /**

View File

@ -1,5 +1,5 @@
import Post, { pack, IPost } from '../../models/post'; import Post, { pack, IPost } from '../../models/post';
import User, { isLocalUser, IUser } from '../../models/user'; import User, { isLocalUser, IUser, isRemoteUser } from '../../models/user';
import stream from '../../publishers/stream'; import stream from '../../publishers/stream';
import Following from '../../models/following'; import Following from '../../models/following';
import { deliver } from '../../queue'; import { deliver } from '../../queue';
@ -17,7 +17,7 @@ import parse from '../../text/parse';
import html from '../../text/html'; import html from '../../text/html';
import { IApp } from '../../models/app'; import { IApp } from '../../models/app';
export default async (user: IUser, content: { export default async (user: IUser, data: {
createdAt?: Date; createdAt?: Date;
text?: string; text?: string;
reply?: IPost; reply?: IPost;
@ -32,16 +32,16 @@ export default async (user: IUser, content: {
uri?: string; uri?: string;
app?: IApp; app?: IApp;
}, silent = false) => new Promise<IPost>(async (res, rej) => { }, silent = false) => new Promise<IPost>(async (res, rej) => {
if (content.createdAt == null) content.createdAt = new Date(); if (data.createdAt == null) data.createdAt = new Date();
if (content.visibility == null) content.visibility = 'public'; if (data.visibility == null) data.visibility = 'public';
const tags = content.tags || []; const tags = data.tags || [];
let tokens = null; let tokens = null;
if (content.text) { if (data.text) {
// Analyze // Analyze
tokens = parse(content.text); tokens = parse(data.text);
// Extract hashtags // Extract hashtags
const hashtags = tokens const hashtags = tokens
@ -55,31 +55,38 @@ export default async (user: IUser, content: {
}); });
} }
const data: any = { const insert: any = {
createdAt: content.createdAt, createdAt: data.createdAt,
mediaIds: content.media ? content.media.map(file => file._id) : [], mediaIds: data.media ? data.media.map(file => file._id) : [],
replyId: content.reply ? content.reply._id : null, replyId: data.reply ? data.reply._id : null,
repostId: content.repost ? content.repost._id : null, repostId: data.repost ? data.repost._id : null,
text: content.text, text: data.text,
textHtml: tokens === null ? null : html(tokens), textHtml: tokens === null ? null : html(tokens),
poll: content.poll, poll: data.poll,
cw: content.cw, cw: data.cw,
tags, tags,
userId: user._id, userId: user._id,
viaMobile: content.viaMobile, viaMobile: data.viaMobile,
geo: content.geo || null, geo: data.geo || null,
appId: content.app ? content.app._id : null, appId: data.app ? data.app._id : null,
visibility: content.visibility, visibility: data.visibility,
// 以下非正規化データ // 以下非正規化データ
_reply: content.reply ? { userId: content.reply.userId } : null, _reply: data.reply ? { userId: data.reply.userId } : null,
_repost: content.repost ? { userId: content.repost.userId } : null, _repost: data.repost ? { userId: data.repost.userId } : null,
_user: {
host: user.host,
hostLower: user.hostLower,
account: isLocalUser(user) ? {} : {
inbox: user.account.inbox
}
}
}; };
if (content.uri != null) data.uri = content.uri; if (data.uri != null) insert.uri = data.uri;
// 投稿を作成 // 投稿を作成
const post = await Post.insert(data); const post = await Post.insert(insert);
res(post); res(post);
@ -125,6 +132,11 @@ export default async (user: IUser, content: {
const content = renderCreate(note); const content = renderCreate(note);
content['@context'] = context; content['@context'] = context;
// 投稿がリプライかつ投稿者がローカルユーザーかつリプライ先の投稿の投稿者がリモートユーザーなら配送
if (data.reply && isLocalUser(user) && isRemoteUser(data.reply._user)) {
deliver(user, content, data.reply._user.account.inbox).save();
}
Promise.all(followers.map(follower => { Promise.all(followers.map(follower => {
follower = follower.user[0]; follower = follower.user[0];
@ -199,22 +211,22 @@ export default async (user: IUser, content: {
} }
// If has in reply to post // If has in reply to post
if (content.reply) { if (data.reply) {
// Increment replies count // Increment replies count
Post.update({ _id: content.reply._id }, { Post.update({ _id: data.reply._id }, {
$inc: { $inc: {
repliesCount: 1 repliesCount: 1
} }
}); });
// (自分自身へのリプライでない限りは)通知を作成 // (自分自身へのリプライでない限りは)通知を作成
notify(content.reply.userId, user._id, 'reply', { notify(data.reply.userId, user._id, 'reply', {
postId: post._id postId: post._id
}); });
// Fetch watchers // Fetch watchers
PostWatching.find({ PostWatching.find({
postId: content.reply._id, postId: data.reply._id,
userId: { $ne: user._id }, userId: { $ne: user._id },
// 削除されたドキュメントは除く // 削除されたドキュメントは除く
deletedAt: { $exists: false } deletedAt: { $exists: false }
@ -232,24 +244,24 @@ export default async (user: IUser, content: {
// この投稿をWatchする // この投稿をWatchする
if (isLocalUser(user) && user.account.settings.autoWatch !== false) { if (isLocalUser(user) && user.account.settings.autoWatch !== false) {
watch(user._id, content.reply); watch(user._id, data.reply);
} }
// Add mention // Add mention
addMention(content.reply.userId, 'reply'); addMention(data.reply.userId, 'reply');
} }
// If it is repost // If it is repost
if (content.repost) { if (data.repost) {
// Notify // Notify
const type = content.text ? 'quote' : 'repost'; const type = data.text ? 'quote' : 'repost';
notify(content.repost.userId, user._id, type, { notify(data.repost.userId, user._id, type, {
post_id: post._id post_id: post._id
}); });
// Fetch watchers // Fetch watchers
PostWatching.find({ PostWatching.find({
postId: content.repost._id, postId: data.repost._id,
userId: { $ne: user._id }, userId: { $ne: user._id },
// 削除されたドキュメントは除く // 削除されたドキュメントは除く
deletedAt: { $exists: false } deletedAt: { $exists: false }
@ -267,24 +279,24 @@ export default async (user: IUser, content: {
// この投稿をWatchする // この投稿をWatchする
if (isLocalUser(user) && user.account.settings.autoWatch !== false) { if (isLocalUser(user) && user.account.settings.autoWatch !== false) {
watch(user._id, content.repost); watch(user._id, data.repost);
} }
// If it is quote repost // If it is quote repost
if (content.text) { if (data.text) {
// Add mention // Add mention
addMention(content.repost.userId, 'quote'); addMention(data.repost.userId, 'quote');
} else { } else {
// Publish event // Publish event
if (!user._id.equals(content.repost.userId)) { if (!user._id.equals(data.repost.userId)) {
event(content.repost.userId, 'repost', postObj); event(data.repost.userId, 'repost', postObj);
} }
} }
// 今までで同じ投稿をRepostしているか // 今までで同じ投稿をRepostしているか
const existRepost = await Post.findOne({ const existRepost = await Post.findOne({
userId: user._id, userId: user._id,
repostId: content.repost._id, repostId: data.repost._id,
_id: { _id: {
$ne: post._id $ne: post._id
} }
@ -292,7 +304,7 @@ export default async (user: IUser, content: {
if (!existRepost) { if (!existRepost) {
// Update repostee status // Update repostee status
Post.update({ _id: content.repost._id }, { Post.update({ _id: data.repost._id }, {
$inc: { $inc: {
repostCount: 1 repostCount: 1
} }
@ -301,7 +313,7 @@ export default async (user: IUser, content: {
} }
// If has text content // If has text content
if (content.text) { if (data.text) {
// Extract an '@' mentions // Extract an '@' mentions
const atMentions = tokens const atMentions = tokens
.filter(t => t.type == 'mention') .filter(t => t.type == 'mention')
@ -322,8 +334,8 @@ export default async (user: IUser, content: {
if (mentionee == null) return; if (mentionee == null) return;
// 既に言及されたユーザーに対する返信や引用repostの場合も無視 // 既に言及されたユーザーに対する返信や引用repostの場合も無視
if (content.reply && content.reply.userId.equals(mentionee._id)) return; if (data.reply && data.reply.userId.equals(mentionee._id)) return;
if (content.repost && content.repost.userId.equals(mentionee._id)) return; if (data.repost && data.repost.userId.equals(mentionee._id)) return;
// Add mention // Add mention
addMention(mentionee._id, 'mention'); addMention(mentionee._id, 'mention');