2016-12-28 15:49:51 -07:00
|
|
|
/**
|
|
|
|
* Module dependencies
|
|
|
|
*/
|
2017-03-08 11:50:09 -07:00
|
|
|
import $ from 'cafy';
|
2017-03-25 00:56:26 -06:00
|
|
|
import deepEqual = require('deep-equal');
|
2017-03-18 05:13:38 -06:00
|
|
|
import parse from '../../common/text';
|
2017-10-31 07:09:09 -06:00
|
|
|
import { default as Post, IPost, isValidText } from '../../models/post';
|
2018-03-27 01:51:12 -06:00
|
|
|
import { default as User, ILocalAccount, IUser } from '../../models/user';
|
2017-10-31 07:09:09 -06:00
|
|
|
import { default as Channel, IChannel } from '../../models/channel';
|
2016-12-28 15:49:51 -07:00
|
|
|
import Following from '../../models/following';
|
2017-12-21 15:43:56 -07:00
|
|
|
import Mute from '../../models/mute';
|
2016-12-28 15:49:51 -07:00
|
|
|
import DriveFile from '../../models/drive-file';
|
2017-06-06 10:20:07 -06:00
|
|
|
import Watching from '../../models/post-watching';
|
2017-11-01 04:33:08 -06:00
|
|
|
import ChannelWatching from '../../models/channel-watching';
|
2018-02-01 16:21:30 -07:00
|
|
|
import { pack } from '../../models/post';
|
2016-12-28 15:49:51 -07:00
|
|
|
import notify from '../../common/notify';
|
2017-06-06 10:20:07 -06:00
|
|
|
import watch from '../../common/watch-post';
|
2017-11-20 11:40:09 -07:00
|
|
|
import event, { pushSw, publishChannelStream } from '../../event';
|
2018-03-27 01:51:12 -06:00
|
|
|
import getAcct from '../../../common/user/get-acct';
|
|
|
|
import parseAcct from '../../../common/user/parse-acct';
|
2017-01-16 19:11:22 -07:00
|
|
|
import config from '../../../conf';
|
2016-12-28 15:49:51 -07:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a post
|
|
|
|
*
|
2017-03-01 01:37:01 -07:00
|
|
|
* @param {any} params
|
|
|
|
* @param {any} user
|
|
|
|
* @param {any} app
|
|
|
|
* @return {Promise<any>}
|
2016-12-28 15:49:51 -07:00
|
|
|
*/
|
2017-09-07 13:13:01 -06:00
|
|
|
module.exports = (params, user: IUser, app) => new Promise(async (res, rej) => {
|
2016-12-28 15:49:51 -07:00
|
|
|
// Get 'text' parameter
|
2017-03-08 11:50:09 -07:00
|
|
|
const [text, textErr] = $(params.text).optional.string().pipe(isValidText).$;
|
2017-03-01 11:16:39 -07:00
|
|
|
if (textErr) return rej('invalid text');
|
2016-12-28 15:49:51 -07:00
|
|
|
|
2018-03-03 17:39:25 -07:00
|
|
|
// Get 'via_mobile' parameter
|
|
|
|
const [viaMobile = false, viaMobileErr] = $(params.via_mobile).optional.boolean().$;
|
|
|
|
if (viaMobileErr) return rej('invalid via_mobile');
|
|
|
|
|
2018-02-25 08:39:05 -07:00
|
|
|
// Get 'tags' parameter
|
|
|
|
const [tags = [], tagsErr] = $(params.tags).optional.array('string').unique().eachQ(t => t.range(1, 32)).$;
|
|
|
|
if (tagsErr) return rej('invalid tags');
|
|
|
|
|
2018-03-04 16:44:37 -07:00
|
|
|
// Get 'geo' parameter
|
|
|
|
const [geo, geoErr] = $(params.geo).optional.nullable.strict.object()
|
2018-03-04 17:22:28 -07:00
|
|
|
.have('latitude', $().number().range(-90, 90))
|
|
|
|
.have('longitude', $().number().range(-180, 180))
|
2018-03-04 16:44:37 -07:00
|
|
|
.have('altitude', $().nullable.number())
|
|
|
|
.have('accuracy', $().nullable.number())
|
|
|
|
.have('altitudeAccuracy', $().nullable.number())
|
|
|
|
.have('heading', $().nullable.number().range(0, 360))
|
|
|
|
.have('speed', $().nullable.number())
|
|
|
|
.$;
|
|
|
|
if (geoErr) return rej('invalid geo');
|
|
|
|
|
2016-12-28 15:49:51 -07:00
|
|
|
// Get 'media_ids' parameter
|
2017-03-08 11:50:09 -07:00
|
|
|
const [mediaIds, mediaIdsErr] = $(params.media_ids).optional.array('id').unique().range(1, 4).$;
|
2017-03-01 11:16:39 -07:00
|
|
|
if (mediaIdsErr) return rej('invalid media_ids');
|
2017-02-23 07:39:58 -07:00
|
|
|
|
2017-03-01 11:16:39 -07:00
|
|
|
let files = [];
|
2017-03-03 04:28:42 -07:00
|
|
|
if (mediaIds !== undefined) {
|
2016-12-28 15:49:51 -07:00
|
|
|
// Fetch files
|
|
|
|
// forEach だと途中でエラーなどがあっても return できないので
|
|
|
|
// 敢えて for を使っています。
|
2017-05-24 05:50:17 -06:00
|
|
|
for (const mediaId of mediaIds) {
|
2016-12-28 15:49:51 -07:00
|
|
|
// Fetch file
|
|
|
|
// SELECT _id
|
|
|
|
const entity = await DriveFile.findOne({
|
2017-03-01 13:11:37 -07:00
|
|
|
_id: mediaId,
|
2017-11-06 00:22:18 -07:00
|
|
|
'metadata.user_id': user._id
|
2017-05-05 01:46:50 -06:00
|
|
|
});
|
2016-12-28 15:49:51 -07:00
|
|
|
|
|
|
|
if (entity === null) {
|
|
|
|
return rej('file not found');
|
|
|
|
} else {
|
|
|
|
files.push(entity);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
files = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get 'repost_id' parameter
|
2017-03-08 11:50:09 -07:00
|
|
|
const [repostId, repostIdErr] = $(params.repost_id).optional.id().$;
|
2017-03-01 13:11:37 -07:00
|
|
|
if (repostIdErr) return rej('invalid repost_id');
|
2017-01-17 13:39:50 -07:00
|
|
|
|
2017-10-31 07:09:09 -06:00
|
|
|
let repost: IPost = null;
|
|
|
|
let isQuote = false;
|
2017-03-03 04:28:42 -07:00
|
|
|
if (repostId !== undefined) {
|
2016-12-28 15:49:51 -07:00
|
|
|
// Fetch repost to post
|
|
|
|
repost = await Post.findOne({
|
2017-03-01 13:11:37 -07:00
|
|
|
_id: repostId
|
2016-12-28 15:49:51 -07:00
|
|
|
});
|
|
|
|
|
|
|
|
if (repost == null) {
|
|
|
|
return rej('repostee is not found');
|
|
|
|
} else if (repost.repost_id && !repost.text && !repost.media_ids) {
|
|
|
|
return rej('cannot repost to repost');
|
|
|
|
}
|
|
|
|
|
|
|
|
// Fetch recently post
|
|
|
|
const latestPost = await Post.findOne({
|
|
|
|
user_id: user._id
|
2017-01-16 19:11:22 -07:00
|
|
|
}, {
|
2017-05-05 01:46:50 -06:00
|
|
|
sort: {
|
|
|
|
_id: -1
|
|
|
|
}
|
|
|
|
});
|
2016-12-28 15:49:51 -07:00
|
|
|
|
2017-10-31 07:09:09 -06:00
|
|
|
isQuote = text != null || files != null;
|
|
|
|
|
2016-12-28 15:49:51 -07:00
|
|
|
// 直近と同じRepost対象かつ引用じゃなかったらエラー
|
|
|
|
if (latestPost &&
|
2017-04-14 05:45:37 -06:00
|
|
|
latestPost.repost_id &&
|
|
|
|
latestPost.repost_id.equals(repost._id) &&
|
2017-10-31 07:09:09 -06:00
|
|
|
!isQuote) {
|
2017-05-27 16:09:56 -06:00
|
|
|
return rej('cannot repost same post that already reposted in your latest post');
|
2016-12-28 15:49:51 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// 直近がRepost対象かつ引用じゃなかったらエラー
|
|
|
|
if (latestPost &&
|
2017-04-14 05:45:37 -06:00
|
|
|
latestPost._id.equals(repost._id) &&
|
2017-10-31 07:09:09 -06:00
|
|
|
!isQuote) {
|
2017-05-27 16:09:56 -06:00
|
|
|
return rej('cannot repost your latest post');
|
2016-12-28 15:49:51 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-31 19:45:01 -06:00
|
|
|
// Get 'reply_id' parameter
|
|
|
|
const [replyId, replyIdErr] = $(params.reply_id).optional.id().$;
|
|
|
|
if (replyIdErr) return rej('invalid reply_id');
|
2017-01-17 13:39:50 -07:00
|
|
|
|
2017-10-31 19:45:01 -06:00
|
|
|
let reply: IPost = null;
|
|
|
|
if (replyId !== undefined) {
|
2017-01-17 13:39:50 -07:00
|
|
|
// Fetch reply
|
2017-10-31 19:45:01 -06:00
|
|
|
reply = await Post.findOne({
|
|
|
|
_id: replyId
|
2016-12-28 15:49:51 -07:00
|
|
|
});
|
|
|
|
|
2017-10-31 19:45:01 -06:00
|
|
|
if (reply === null) {
|
2017-03-01 13:11:37 -07:00
|
|
|
return rej('in reply to post is not found');
|
2016-12-28 15:49:51 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// 返信対象が引用でないRepostだったらエラー
|
2017-10-31 19:45:01 -06:00
|
|
|
if (reply.repost_id && !reply.text && !reply.media_ids) {
|
2016-12-28 15:49:51 -07:00
|
|
|
return rej('cannot reply to repost');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-31 07:09:09 -06:00
|
|
|
// Get 'channel_id' parameter
|
|
|
|
const [channelId, channelIdErr] = $(params.channel_id).optional.id().$;
|
|
|
|
if (channelIdErr) return rej('invalid channel_id');
|
|
|
|
|
|
|
|
let channel: IChannel = null;
|
|
|
|
if (channelId !== undefined) {
|
|
|
|
// Fetch channel
|
|
|
|
channel = await Channel.findOne({
|
|
|
|
_id: channelId
|
|
|
|
});
|
|
|
|
|
|
|
|
if (channel === null) {
|
|
|
|
return rej('channel not found');
|
|
|
|
}
|
|
|
|
|
|
|
|
// 返信対象の投稿がこのチャンネルじゃなかったらダメ
|
2017-10-31 19:45:01 -06:00
|
|
|
if (reply && !channelId.equals(reply.channel_id)) {
|
2017-10-31 07:09:09 -06:00
|
|
|
return rej('チャンネル内部からチャンネル外部の投稿に返信することはできません');
|
|
|
|
}
|
|
|
|
|
|
|
|
// Repost対象の投稿がこのチャンネルじゃなかったらダメ
|
|
|
|
if (repost && !channelId.equals(repost.channel_id)) {
|
|
|
|
return rej('チャンネル内部からチャンネル外部の投稿をRepostすることはできません');
|
|
|
|
}
|
|
|
|
|
|
|
|
// 引用ではないRepostはダメ
|
|
|
|
if (repost && !isQuote) {
|
|
|
|
return rej('チャンネル内部では引用ではないRepostをすることはできません');
|
|
|
|
}
|
2017-10-31 10:38:19 -06:00
|
|
|
} else {
|
|
|
|
// 返信対象の投稿がチャンネルへの投稿だったらダメ
|
2017-10-31 19:45:01 -06:00
|
|
|
if (reply && reply.channel_id != null) {
|
2017-10-31 10:38:19 -06:00
|
|
|
return rej('チャンネル外部からチャンネル内部の投稿に返信することはできません');
|
|
|
|
}
|
|
|
|
|
|
|
|
// Repost対象の投稿がチャンネルへの投稿だったらダメ
|
|
|
|
if (repost && repost.channel_id != null) {
|
|
|
|
return rej('チャンネル外部からチャンネル内部の投稿をRepostすることはできません');
|
|
|
|
}
|
2017-10-31 07:09:09 -06:00
|
|
|
}
|
|
|
|
|
2017-02-13 21:59:26 -07:00
|
|
|
// Get 'poll' parameter
|
2017-03-08 11:59:12 -07:00
|
|
|
const [poll, pollErr] = $(params.poll).optional.strict.object()
|
2017-03-08 11:50:09 -07:00
|
|
|
.have('choices', $().array('string')
|
|
|
|
.unique()
|
|
|
|
.range(2, 10)
|
|
|
|
.each(c => c.length > 0 && c.length < 50))
|
|
|
|
.$;
|
2017-03-01 13:11:37 -07:00
|
|
|
if (pollErr) return rej('invalid poll');
|
|
|
|
|
2017-03-08 11:50:09 -07:00
|
|
|
if (poll) {
|
|
|
|
(poll as any).choices = (poll as any).choices.map((choice, i) => ({
|
2017-02-13 21:59:26 -07:00
|
|
|
id: i, // IDを付与
|
2017-03-01 13:11:37 -07:00
|
|
|
text: choice.trim(),
|
2017-02-13 21:59:26 -07:00
|
|
|
votes: 0
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
|
|
|
|
// テキストが無いかつ添付ファイルが無いかつRepostも無いかつ投票も無かったらエラー
|
2017-03-08 11:50:09 -07:00
|
|
|
if (text === undefined && files === null && repost === null && poll === undefined) {
|
2017-02-13 21:59:26 -07:00
|
|
|
return rej('text, media_ids, repost_id or poll is required');
|
2016-12-28 15:49:51 -07:00
|
|
|
}
|
|
|
|
|
2017-03-25 00:56:26 -06:00
|
|
|
// 直近の投稿と重複してたらエラー
|
|
|
|
// TODO: 直近の投稿が一日前くらいなら重複とは見なさない
|
|
|
|
if (user.latest_post) {
|
|
|
|
if (deepEqual({
|
|
|
|
text: user.latest_post.text,
|
2017-10-31 19:22:40 -06:00
|
|
|
reply: user.latest_post.reply_id ? user.latest_post.reply_id.toString() : null,
|
2017-03-25 08:06:26 -06:00
|
|
|
repost: user.latest_post.repost_id ? user.latest_post.repost_id.toString() : null,
|
2017-03-25 00:56:26 -06:00
|
|
|
media_ids: (user.latest_post.media_ids || []).map(id => id.toString())
|
|
|
|
}, {
|
2017-10-31 07:14:12 -06:00
|
|
|
text: text,
|
2017-10-31 19:45:01 -06:00
|
|
|
reply: reply ? reply._id.toString() : null,
|
2017-10-31 07:14:12 -06:00
|
|
|
repost: repost ? repost._id.toString() : null,
|
|
|
|
media_ids: (files || []).map(file => file._id.toString())
|
|
|
|
})) {
|
2017-03-25 00:56:26 -06:00
|
|
|
return rej('duplicate');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-25 08:39:05 -07:00
|
|
|
let tokens = null;
|
|
|
|
if (text) {
|
|
|
|
// Analyze
|
|
|
|
tokens = parse(text);
|
|
|
|
|
|
|
|
// Extract hashtags
|
|
|
|
const hashtags = tokens
|
|
|
|
.filter(t => t.type == 'hashtag')
|
|
|
|
.map(t => t.hashtag);
|
|
|
|
|
|
|
|
hashtags.forEach(tag => {
|
|
|
|
if (tags.indexOf(tag) == -1) {
|
|
|
|
tags.push(tag);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2016-12-28 15:49:51 -07:00
|
|
|
// 投稿を作成
|
2017-01-16 19:11:22 -07:00
|
|
|
const post = await Post.insert({
|
2016-12-28 15:49:51 -07:00
|
|
|
created_at: new Date(),
|
2017-10-31 07:09:09 -06:00
|
|
|
channel_id: channel ? channel._id : undefined,
|
2017-10-31 10:38:19 -06:00
|
|
|
index: channel ? channel.index + 1 : undefined,
|
2017-02-23 21:39:48 -07:00
|
|
|
media_ids: files ? files.map(file => file._id) : undefined,
|
2017-10-31 19:45:01 -06:00
|
|
|
reply_id: reply ? reply._id : undefined,
|
2016-12-28 15:49:51 -07:00
|
|
|
repost_id: repost ? repost._id : undefined,
|
2017-03-08 11:50:09 -07:00
|
|
|
poll: poll,
|
2016-12-28 15:49:51 -07:00
|
|
|
text: text,
|
2018-02-25 08:39:05 -07:00
|
|
|
tags: tags,
|
2016-12-28 15:49:51 -07:00
|
|
|
user_id: user._id,
|
2017-12-21 13:41:21 -07:00
|
|
|
app_id: app ? app._id : null,
|
2018-03-03 17:39:25 -07:00
|
|
|
via_mobile: viaMobile,
|
2018-03-04 16:44:37 -07:00
|
|
|
geo,
|
2017-12-21 13:41:21 -07:00
|
|
|
|
|
|
|
// 以下非正規化データ
|
|
|
|
_reply: reply ? { user_id: reply.user_id } : undefined,
|
|
|
|
_repost: repost ? { user_id: repost.user_id } : undefined,
|
2016-12-28 15:49:51 -07:00
|
|
|
});
|
|
|
|
|
|
|
|
// Serialize
|
2018-02-01 16:21:30 -07:00
|
|
|
const postObj = await pack(post);
|
2016-12-28 15:49:51 -07:00
|
|
|
|
|
|
|
// Reponse
|
2017-12-13 21:31:17 -07:00
|
|
|
res({
|
|
|
|
created_post: postObj
|
|
|
|
});
|
2016-12-28 15:49:51 -07:00
|
|
|
|
2017-11-01 21:56:07 -06:00
|
|
|
//#region Post processes
|
2016-12-28 15:49:51 -07:00
|
|
|
|
2017-03-25 00:56:26 -06:00
|
|
|
User.update({ _id: user._id }, {
|
|
|
|
$set: {
|
|
|
|
latest_post: post
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2017-05-24 05:50:17 -06:00
|
|
|
const mentions = [];
|
2016-12-28 15:49:51 -07:00
|
|
|
|
2017-12-21 15:43:56 -07:00
|
|
|
async function addMention(mentionee, reason) {
|
2016-12-28 15:49:51 -07:00
|
|
|
// Reject if already added
|
|
|
|
if (mentions.some(x => x.equals(mentionee))) return;
|
|
|
|
|
|
|
|
// Add mention
|
|
|
|
mentions.push(mentionee);
|
|
|
|
|
|
|
|
// Publish event
|
|
|
|
if (!user._id.equals(mentionee)) {
|
2017-12-21 15:43:56 -07:00
|
|
|
const mentioneeMutes = await Mute.find({
|
|
|
|
muter_id: mentionee,
|
|
|
|
deleted_at: { $exists: false }
|
|
|
|
});
|
|
|
|
const mentioneesMutedUserIds = mentioneeMutes.map(m => m.mutee_id.toString());
|
|
|
|
if (mentioneesMutedUserIds.indexOf(user._id.toString()) == -1) {
|
|
|
|
event(mentionee, reason, postObj);
|
|
|
|
pushSw(mentionee, reason, postObj);
|
|
|
|
}
|
2016-12-28 15:49:51 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-01 04:33:08 -06:00
|
|
|
// タイムラインへの投稿
|
2017-10-31 22:39:05 -06:00
|
|
|
if (!channel) {
|
|
|
|
// Publish event to myself's stream
|
|
|
|
event(user._id, 'post', postObj);
|
2016-12-28 15:49:51 -07:00
|
|
|
|
2017-10-31 22:20:55 -06:00
|
|
|
// Fetch all followers
|
|
|
|
const followers = await Following
|
|
|
|
.find({
|
|
|
|
followee_id: user._id,
|
|
|
|
// 削除されたドキュメントは除く
|
|
|
|
deleted_at: { $exists: false }
|
|
|
|
}, {
|
|
|
|
follower_id: true,
|
|
|
|
_id: false
|
|
|
|
});
|
2016-12-28 15:49:51 -07:00
|
|
|
|
2017-10-31 22:20:55 -06:00
|
|
|
// Publish event to followers stream
|
|
|
|
followers.forEach(following =>
|
|
|
|
event(following.follower_id, 'post', postObj));
|
|
|
|
}
|
2016-12-28 15:49:51 -07:00
|
|
|
|
2017-11-01 04:33:08 -06:00
|
|
|
// チャンネルへの投稿
|
|
|
|
if (channel) {
|
|
|
|
// Increment channel index(posts count)
|
|
|
|
Channel.update({ _id: channel._id }, {
|
|
|
|
$inc: {
|
|
|
|
index: 1
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
// Publish event to channel
|
|
|
|
publishChannelStream(channel._id, 'post', postObj);
|
|
|
|
|
|
|
|
// Get channel watchers
|
|
|
|
const watches = await ChannelWatching.find({
|
|
|
|
channel_id: channel._id,
|
|
|
|
// 削除されたドキュメントは除く
|
|
|
|
deleted_at: { $exists: false }
|
|
|
|
});
|
|
|
|
|
|
|
|
// チャンネルの視聴者(のタイムライン)に配信
|
|
|
|
watches.forEach(w => {
|
|
|
|
event(w.user_id, 'post', postObj);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2016-12-28 15:49:51 -07:00
|
|
|
// Increment my posts count
|
2017-01-16 19:11:22 -07:00
|
|
|
User.update({ _id: user._id }, {
|
2016-12-28 15:49:51 -07:00
|
|
|
$inc: {
|
|
|
|
posts_count: 1
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
// If has in reply to post
|
2017-10-31 19:45:01 -06:00
|
|
|
if (reply) {
|
2016-12-28 15:49:51 -07:00
|
|
|
// Increment replies count
|
2017-10-31 19:45:01 -06:00
|
|
|
Post.update({ _id: reply._id }, {
|
2016-12-28 15:49:51 -07:00
|
|
|
$inc: {
|
|
|
|
replies_count: 1
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
// 自分自身へのリプライでない限りは通知を作成
|
2017-10-31 19:45:01 -06:00
|
|
|
notify(reply.user_id, user._id, 'reply', {
|
2016-12-28 15:49:51 -07:00
|
|
|
post_id: post._id
|
|
|
|
});
|
|
|
|
|
2017-06-06 10:20:07 -06:00
|
|
|
// Fetch watchers
|
|
|
|
Watching
|
|
|
|
.find({
|
2017-10-31 19:45:01 -06:00
|
|
|
post_id: reply._id,
|
2017-06-06 10:20:07 -06:00
|
|
|
user_id: { $ne: user._id },
|
|
|
|
// 削除されたドキュメントは除く
|
|
|
|
deleted_at: { $exists: false }
|
|
|
|
}, {
|
|
|
|
fields: {
|
|
|
|
user_id: true
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.then(watchers => {
|
|
|
|
watchers.forEach(watcher => {
|
|
|
|
notify(watcher.user_id, user._id, 'reply', {
|
|
|
|
post_id: post._id
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
// この投稿をWatchする
|
2018-03-27 01:51:12 -06:00
|
|
|
if ((user.account as ILocalAccount).settings.auto_watch !== false) {
|
2018-03-04 16:07:09 -07:00
|
|
|
watch(user._id, reply);
|
|
|
|
}
|
2017-06-06 10:20:07 -06:00
|
|
|
|
2016-12-28 15:49:51 -07:00
|
|
|
// Add mention
|
2017-10-31 19:45:01 -06:00
|
|
|
addMention(reply.user_id, 'reply');
|
2016-12-28 15:49:51 -07:00
|
|
|
}
|
|
|
|
|
2017-03-08 11:59:12 -07:00
|
|
|
// If it is repost
|
2016-12-28 15:49:51 -07:00
|
|
|
if (repost) {
|
|
|
|
// Notify
|
|
|
|
const type = text ? 'quote' : 'repost';
|
|
|
|
notify(repost.user_id, user._id, type, {
|
|
|
|
post_id: post._id
|
|
|
|
});
|
|
|
|
|
2017-06-06 10:20:07 -06:00
|
|
|
// Fetch watchers
|
|
|
|
Watching
|
|
|
|
.find({
|
|
|
|
post_id: repost._id,
|
|
|
|
user_id: { $ne: user._id },
|
|
|
|
// 削除されたドキュメントは除く
|
|
|
|
deleted_at: { $exists: false }
|
|
|
|
}, {
|
|
|
|
fields: {
|
|
|
|
user_id: true
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.then(watchers => {
|
|
|
|
watchers.forEach(watcher => {
|
|
|
|
notify(watcher.user_id, user._id, type, {
|
|
|
|
post_id: post._id
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
// この投稿をWatchする
|
|
|
|
// TODO: ユーザーが「Repostしたときに自動でWatchする」設定を
|
|
|
|
// オフにしていた場合はしない
|
|
|
|
watch(user._id, repost);
|
|
|
|
|
2017-03-08 11:59:12 -07:00
|
|
|
// If it is quote repost
|
2016-12-28 15:49:51 -07:00
|
|
|
if (text) {
|
|
|
|
// Add mention
|
|
|
|
addMention(repost.user_id, 'quote');
|
|
|
|
} else {
|
|
|
|
// Publish event
|
|
|
|
if (!user._id.equals(repost.user_id)) {
|
|
|
|
event(repost.user_id, 'repost', postObj);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// 今までで同じ投稿をRepostしているか
|
|
|
|
const existRepost = await Post.findOne({
|
|
|
|
user_id: user._id,
|
|
|
|
repost_id: repost._id,
|
|
|
|
_id: {
|
|
|
|
$ne: post._id
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
if (!existRepost) {
|
|
|
|
// Update repostee status
|
2017-01-16 19:11:22 -07:00
|
|
|
Post.update({ _id: repost._id }, {
|
2016-12-28 15:49:51 -07:00
|
|
|
$inc: {
|
|
|
|
repost_count: 1
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// If has text content
|
|
|
|
if (text) {
|
2017-04-14 05:45:37 -06:00
|
|
|
/*
|
|
|
|
// Extract a hashtags
|
|
|
|
const hashtags = tokens
|
|
|
|
.filter(t => t.type == 'hashtag')
|
|
|
|
.map(t => t.hashtag)
|
|
|
|
// Drop dupulicates
|
|
|
|
.filter((v, i, s) => s.indexOf(v) == i);
|
|
|
|
|
|
|
|
// ハッシュタグをデータベースに登録
|
|
|
|
registerHashtags(user, hashtags);
|
|
|
|
*/
|
2016-12-28 15:49:51 -07:00
|
|
|
// Extract an '@' mentions
|
|
|
|
const atMentions = tokens
|
|
|
|
.filter(t => t.type == 'mention')
|
2018-03-27 01:51:12 -06:00
|
|
|
.map(getAcct)
|
2016-12-28 15:49:51 -07:00
|
|
|
// Drop dupulicates
|
|
|
|
.filter((v, i, s) => s.indexOf(v) == i);
|
|
|
|
|
|
|
|
// Resolve all mentions
|
|
|
|
await Promise.all(atMentions.map(async (mention) => {
|
|
|
|
// Fetch mentioned user
|
|
|
|
// SELECT _id
|
|
|
|
const mentionee = await User
|
2018-03-27 01:51:12 -06:00
|
|
|
.findOne(parseAcct(mention), { _id: true });
|
2016-12-28 15:49:51 -07:00
|
|
|
|
|
|
|
// When mentioned user not found
|
|
|
|
if (mentionee == null) return;
|
|
|
|
|
|
|
|
// 既に言及されたユーザーに対する返信や引用repostの場合も無視
|
2017-10-31 19:45:01 -06:00
|
|
|
if (reply && reply.user_id.equals(mentionee._id)) return;
|
2016-12-28 15:49:51 -07:00
|
|
|
if (repost && repost.user_id.equals(mentionee._id)) return;
|
|
|
|
|
|
|
|
// Add mention
|
|
|
|
addMention(mentionee._id, 'mention');
|
|
|
|
|
|
|
|
// Create notification
|
|
|
|
notify(mentionee._id, user._id, 'mention', {
|
|
|
|
post_id: post._id
|
|
|
|
});
|
|
|
|
|
|
|
|
return;
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Register to search database
|
|
|
|
if (text && config.elasticsearch.enable) {
|
|
|
|
const es = require('../../../db/elasticsearch');
|
|
|
|
|
|
|
|
es.index({
|
|
|
|
index: 'misskey',
|
|
|
|
type: 'post',
|
|
|
|
id: post._id.toString(),
|
|
|
|
body: {
|
|
|
|
text: post.text
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
// Append mentions data
|
|
|
|
if (mentions.length > 0) {
|
2017-01-16 19:11:22 -07:00
|
|
|
Post.update({ _id: post._id }, {
|
2016-12-28 15:49:51 -07:00
|
|
|
$set: {
|
|
|
|
mentions: mentions
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2017-11-01 21:56:07 -06:00
|
|
|
|
|
|
|
//#endregion
|
2016-12-28 15:49:51 -07:00
|
|
|
});
|