summaryrefslogtreecommitdiff
path: root/src/api/endpoints/posts/timeline.ts
diff options
context:
space:
mode:
Diffstat (limited to 'src/api/endpoints/posts/timeline.ts')
-rw-r--r--src/api/endpoints/posts/timeline.ts58
1 files changed, 43 insertions, 15 deletions
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;
+};