summaryrefslogtreecommitdiff
path: root/src/api/endpoints/posts
diff options
context:
space:
mode:
authorこぴなたみぽ <Syuilotan@yahoo.co.jp>2017-11-06 19:14:04 +0900
committerGitHub <noreply@github.com>2017-11-06 19:14:04 +0900
commit9b7edcefb991f9c463b330cc7f87d99f9a357196 (patch)
tree0f4451044edf5d265e84adfc13c4c3f013ffb007 /src/api/endpoints/posts
parentchore(package): update @types/body-parser to version 1.16.7 (diff)
parentMerge pull request #875 from syuilo/greenkeeper/@types/gulp-util-3.0.33 (diff)
downloadsharkey-9b7edcefb991f9c463b330cc7f87d99f9a357196.tar.gz
sharkey-9b7edcefb991f9c463b330cc7f87d99f9a357196.tar.bz2
sharkey-9b7edcefb991f9c463b330cc7f87d99f9a357196.zip
Merge branch 'master' into greenkeeper/@types/body-parser-1.16.7
Diffstat (limited to 'src/api/endpoints/posts')
-rw-r--r--src/api/endpoints/posts/context.ts8
-rw-r--r--src/api/endpoints/posts/create.ts166
-rw-r--r--src/api/endpoints/posts/replies.ts2
-rw-r--r--src/api/endpoints/posts/timeline.ts58
-rw-r--r--src/api/endpoints/posts/trend.ts2
5 files changed, 169 insertions, 67 deletions
diff --git a/src/api/endpoints/posts/context.ts b/src/api/endpoints/posts/context.ts
index cd5f15f481..bad59a6bee 100644
--- a/src/api/endpoints/posts/context.ts
+++ b/src/api/endpoints/posts/context.ts
@@ -49,13 +49,13 @@ module.exports = (params, user) => new Promise(async (res, rej) => {
return;
}
- if (p.reply_to_id) {
- await get(p.reply_to_id);
+ if (p.reply_id) {
+ await get(p.reply_id);
}
}
- if (post.reply_to_id) {
- await get(post.reply_to_id);
+ if (post.reply_id) {
+ await get(post.reply_id);
}
// Serialize
diff --git a/src/api/endpoints/posts/create.ts b/src/api/endpoints/posts/create.ts
index 805dba7f83..4f4b7e2e83 100644
--- a/src/api/endpoints/posts/create.ts
+++ b/src/api/endpoints/posts/create.ts
@@ -4,16 +4,17 @@
import $ from 'cafy';
import deepEqual = require('deep-equal');
import parse from '../../common/text';
-import Post from '../../models/post';
-import { isValidText } from '../../models/post';
+import { default as Post, IPost, isValidText } from '../../models/post';
import { default as User, IUser } from '../../models/user';
+import { default as Channel, IChannel } from '../../models/channel';
import Following from '../../models/following';
import DriveFile from '../../models/drive-file';
import Watching from '../../models/post-watching';
+import ChannelWatching from '../../models/channel-watching';
import serialize from '../../serializers/post';
import notify from '../../common/notify';
import watch from '../../common/watch-post';
-import event from '../../event';
+import { default as event, publishChannelStream } from '../../event';
import config from '../../../conf';
/**
@@ -43,9 +44,7 @@ module.exports = (params, user: IUser, app) => new Promise(async (res, rej) => {
// SELECT _id
const entity = await DriveFile.findOne({
_id: mediaId,
- user_id: user._id
- }, {
- _id: true
+ 'metadata.user_id': user._id
});
if (entity === null) {
@@ -62,7 +61,8 @@ module.exports = (params, user: IUser, app) => new Promise(async (res, rej) => {
const [repostId, repostIdErr] = $(params.repost_id).optional.id().$;
if (repostIdErr) return rej('invalid repost_id');
- let repost = null;
+ let repost: IPost = null;
+ let isQuote = false;
if (repostId !== undefined) {
// Fetch repost to post
repost = await Post.findOne({
@@ -84,43 +84,86 @@ module.exports = (params, user: IUser, app) => new Promise(async (res, rej) => {
}
});
+ isQuote = text != null || files != null;
+
// 直近と同じRepost対象かつ引用じゃなかったらエラー
if (latestPost &&
latestPost.repost_id &&
latestPost.repost_id.equals(repost._id) &&
- text === undefined && files === null) {
+ !isQuote) {
return rej('cannot repost same post that already reposted in your latest post');
}
// 直近がRepost対象かつ引用じゃなかったらエラー
if (latestPost &&
latestPost._id.equals(repost._id) &&
- text === undefined && files === null) {
+ !isQuote) {
return rej('cannot repost your latest post');
}
}
- // Get 'in_reply_to_post_id' parameter
- const [inReplyToPostId, inReplyToPostIdErr] = $(params.reply_to_id).optional.id().$;
- if (inReplyToPostIdErr) return rej('invalid in_reply_to_post_id');
+ // Get 'reply_id' parameter
+ const [replyId, replyIdErr] = $(params.reply_id).optional.id().$;
+ if (replyIdErr) return rej('invalid reply_id');
- let inReplyToPost = null;
- if (inReplyToPostId !== undefined) {
+ let reply: IPost = null;
+ if (replyId !== undefined) {
// Fetch reply
- inReplyToPost = await Post.findOne({
- _id: inReplyToPostId
+ reply = await Post.findOne({
+ _id: replyId
});
- if (inReplyToPost === null) {
+ if (reply === null) {
return rej('in reply to post is not found');
}
// 返信対象が引用でないRepostだったらエラー
- if (inReplyToPost.repost_id && !inReplyToPost.text && !inReplyToPost.media_ids) {
+ if (reply.repost_id && !reply.text && !reply.media_ids) {
return rej('cannot reply to repost');
}
}
+ // 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');
+ }
+
+ // 返信対象の投稿がこのチャンネルじゃなかったらダメ
+ if (reply && !channelId.equals(reply.channel_id)) {
+ return rej('チャンネル内部からチャンネル外部の投稿に返信することはできません');
+ }
+
+ // Repost対象の投稿がこのチャンネルじゃなかったらダメ
+ if (repost && !channelId.equals(repost.channel_id)) {
+ return rej('チャンネル内部からチャンネル外部の投稿をRepostすることはできません');
+ }
+
+ // 引用ではないRepostはダメ
+ if (repost && !isQuote) {
+ return rej('チャンネル内部では引用ではないRepostをすることはできません');
+ }
+ } else {
+ // 返信対象の投稿がチャンネルへの投稿だったらダメ
+ if (reply && reply.channel_id != null) {
+ return rej('チャンネル外部からチャンネル内部の投稿に返信することはできません');
+ }
+
+ // Repost対象の投稿がチャンネルへの投稿だったらダメ
+ if (repost && repost.channel_id != null) {
+ return rej('チャンネル外部からチャンネル内部の投稿をRepostすることはできません');
+ }
+ }
+
// Get 'poll' parameter
const [poll, pollErr] = $(params.poll).optional.strict.object()
.have('choices', $().array('string')
@@ -148,15 +191,15 @@ module.exports = (params, user: IUser, app) => new Promise(async (res, rej) => {
if (user.latest_post) {
if (deepEqual({
text: user.latest_post.text,
- reply: user.latest_post.reply_to_id ? user.latest_post.reply_to_id.toString() : null,
+ reply: user.latest_post.reply_id ? user.latest_post.reply_id.toString() : null,
repost: user.latest_post.repost_id ? user.latest_post.repost_id.toString() : null,
media_ids: (user.latest_post.media_ids || []).map(id => id.toString())
}, {
- text: text,
- reply: inReplyToPost ? inReplyToPost._id.toString() : null,
- repost: repost ? repost._id.toString() : null,
- media_ids: (files || []).map(file => file._id.toString())
- })) {
+ text: text,
+ reply: reply ? reply._id.toString() : null,
+ repost: repost ? repost._id.toString() : null,
+ media_ids: (files || []).map(file => file._id.toString())
+ })) {
return rej('duplicate');
}
}
@@ -164,8 +207,10 @@ module.exports = (params, user: IUser, app) => new Promise(async (res, rej) => {
// 投稿を作成
const post = await Post.insert({
created_at: new Date(),
+ channel_id: channel ? channel._id : undefined,
+ index: channel ? channel.index + 1 : undefined,
media_ids: files ? files.map(file => file._id) : undefined,
- reply_to_id: inReplyToPost ? inReplyToPost._id : undefined,
+ reply_id: reply ? reply._id : undefined,
repost_id: repost ? repost._id : undefined,
poll: poll,
text: text,
@@ -179,8 +224,7 @@ module.exports = (params, user: IUser, app) => new Promise(async (res, rej) => {
// Reponse
res(postObj);
- // -----------------------------------------------------------
- // Post processes
+ //#region Post processes
User.update({ _id: user._id }, {
$set: {
@@ -203,23 +247,51 @@ module.exports = (params, user: IUser, app) => new Promise(async (res, rej) => {
}
}
- // Publish event to myself's stream
- event(user._id, 'post', postObj);
+ // タイムラインへの投稿
+ if (!channel) {
+ // Publish event to myself's stream
+ event(user._id, 'post', postObj);
- // Fetch all followers
- const followers = await Following
- .find({
- followee_id: user._id,
+ // Fetch all followers
+ const followers = await Following
+ .find({
+ followee_id: user._id,
+ // 削除されたドキュメントは除く
+ deleted_at: { $exists: false }
+ }, {
+ follower_id: true,
+ _id: false
+ });
+
+ // Publish event to followers stream
+ followers.forEach(following =>
+ event(following.follower_id, 'post', postObj));
+ }
+
+ // チャンネルへの投稿
+ 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 }
- }, {
- follower_id: true,
- _id: false
});
- // Publish event to followers stream
- followers.forEach(following =>
- event(following.follower_id, 'post', postObj));
+ // チャンネルの視聴者(のタイムライン)に配信
+ watches.forEach(w => {
+ event(w.user_id, 'post', postObj);
+ });
+ }
// Increment my posts count
User.update({ _id: user._id }, {
@@ -229,23 +301,23 @@ module.exports = (params, user: IUser, app) => new Promise(async (res, rej) => {
});
// If has in reply to post
- if (inReplyToPost) {
+ if (reply) {
// Increment replies count
- Post.update({ _id: inReplyToPost._id }, {
+ Post.update({ _id: reply._id }, {
$inc: {
replies_count: 1
}
});
// 自分自身へのリプライでない限りは通知を作成
- notify(inReplyToPost.user_id, user._id, 'reply', {
+ notify(reply.user_id, user._id, 'reply', {
post_id: post._id
});
// Fetch watchers
Watching
.find({
- post_id: inReplyToPost._id,
+ post_id: reply._id,
user_id: { $ne: user._id },
// 削除されたドキュメントは除く
deleted_at: { $exists: false }
@@ -265,10 +337,10 @@ module.exports = (params, user: IUser, app) => new Promise(async (res, rej) => {
// この投稿をWatchする
// TODO: ユーザーが「返信したときに自動でWatchする」設定を
// オフにしていた場合はしない
- watch(user._id, inReplyToPost);
+ watch(user._id, reply);
// Add mention
- addMention(inReplyToPost.user_id, 'reply');
+ addMention(reply.user_id, 'reply');
}
// If it is repost
@@ -369,7 +441,7 @@ module.exports = (params, user: IUser, app) => new Promise(async (res, rej) => {
if (mentionee == null) return;
// 既に言及されたユーザーに対する返信や引用repostの場合も無視
- if (inReplyToPost && inReplyToPost.user_id.equals(mentionee._id)) return;
+ if (reply && reply.user_id.equals(mentionee._id)) return;
if (repost && repost.user_id.equals(mentionee._id)) return;
// Add mention
@@ -406,4 +478,6 @@ module.exports = (params, user: IUser, app) => new Promise(async (res, rej) => {
}
});
}
+
+ //#endregion
});
diff --git a/src/api/endpoints/posts/replies.ts b/src/api/endpoints/posts/replies.ts
index 89f4d99841..3fd6a46769 100644
--- a/src/api/endpoints/posts/replies.ts
+++ b/src/api/endpoints/posts/replies.ts
@@ -40,7 +40,7 @@ module.exports = (params, user) => new Promise(async (res, rej) => {
// Issue query
const replies = await Post
- .find({ reply_to_id: post._id }, {
+ .find({ reply_id: post._id }, {
limit: limit,
skip: offset,
sort: {
diff --git a/src/api/endpoints/posts/timeline.ts b/src/api/endpoints/posts/timeline.ts
index 314e992344..203413e23a 100644
--- a/src/api/endpoints/posts/timeline.ts
+++ b/src/api/endpoints/posts/timeline.ts
@@ -2,7 +2,9 @@
* Module dependencies
*/
import $ from 'cafy';
+import rap from '@prezzemolo/rap';
import Post from '../../models/post';
+import ChannelWatching from '../../models/channel-watching';
import getFriends from '../../common/get-friends';
import serialize from '../../serializers/post';
@@ -14,36 +16,62 @@ import serialize from '../../serializers/post';
* @param {any} app
* @return {Promise<any>}
*/
-module.exports = (params, user, app) => new Promise(async (res, rej) => {
+module.exports = async (params, user, app) => {
// Get 'limit' parameter
const [limit = 10, limitErr] = $(params.limit).optional.number().range(1, 100).$;
- if (limitErr) return rej('invalid limit param');
+ if (limitErr) throw 'invalid limit param';
// Get 'since_id' parameter
const [sinceId, sinceIdErr] = $(params.since_id).optional.id().$;
- if (sinceIdErr) return rej('invalid since_id param');
+ if (sinceIdErr) throw 'invalid since_id param';
// Get 'max_id' parameter
const [maxId, maxIdErr] = $(params.max_id).optional.id().$;
- if (maxIdErr) return rej('invalid max_id param');
+ if (maxIdErr) throw 'invalid max_id param';
// Check if both of since_id and max_id is specified
if (sinceId && maxId) {
- return rej('cannot set since_id and max_id');
+ throw 'cannot set since_id and max_id';
}
- // ID list of the user $self and other users who the user follows
- const followingIds = await getFriends(user._id);
+ const { followingIds, watchChannelIds } = await rap({
+ // ID list of the user itself and other users who the user follows
+ followingIds: getFriends(user._id),
+ // Watchしているチャンネルを取得
+ watchChannelIds: ChannelWatching.find({
+ user_id: user._id,
+ // 削除されたドキュメントは除く
+ deleted_at: { $exists: false }
+ }).then(watches => watches.map(w => w.channel_id))
+ });
- // Construct query
+ //#region Construct query
const sort = {
_id: -1
};
+
const query = {
- user_id: {
- $in: followingIds
- }
+ $or: [{
+ // フォローしている人のタイムラインへの投稿
+ user_id: {
+ $in: followingIds
+ },
+ // 「タイムラインへの」投稿に限定するためにチャンネルが指定されていないもののみに限る
+ $or: [{
+ channel_id: {
+ $exists: false
+ }
+ }, {
+ channel_id: null
+ }]
+ }, {
+ // Watchしているチャンネルへの投稿
+ channel_id: {
+ $in: watchChannelIds
+ }
+ }]
} as any;
+
if (sinceId) {
sort._id = 1;
query._id = {
@@ -54,6 +82,7 @@ module.exports = (params, user, app) => new Promise(async (res, rej) => {
$lt: maxId
};
}
+ //#endregion
// Issue query
const timeline = await Post
@@ -63,7 +92,6 @@ module.exports = (params, user, app) => new Promise(async (res, rej) => {
});
// Serialize
- res(await Promise.all(timeline.map(async post =>
- await serialize(post, user)
- )));
-});
+ const _timeline = await Promise.all(timeline.map(post => serialize(post, user)));
+ return _timeline;
+};
diff --git a/src/api/endpoints/posts/trend.ts b/src/api/endpoints/posts/trend.ts
index 3277206d26..64a195dff1 100644
--- a/src/api/endpoints/posts/trend.ts
+++ b/src/api/endpoints/posts/trend.ts
@@ -48,7 +48,7 @@ module.exports = (params, user) => new Promise(async (res, rej) => {
} as any;
if (reply != undefined) {
- query.reply_to_id = reply ? { $exists: true, $ne: null } : null;
+ query.reply_id = reply ? { $exists: true, $ne: null } : null;
}
if (repost != undefined) {