Implement Channel Watching

This commit is contained in:
syuilo
2017-11-01 19:33:08 +09:00
parent 2b3937d731
commit d6b03c43eb
9 changed files with 244 additions and 27 deletions

View File

@@ -3,6 +3,7 @@
*/
import $ from 'cafy';
import Post from '../../models/post';
import ChannelWatching from '../../models/channel-watching';
import getFriends from '../../common/get-friends';
import serialize from '../../serializers/post';
@@ -32,26 +33,43 @@ module.exports = (params, user, app) => new Promise(async (res, rej) => {
return rej('cannot set since_id and max_id');
}
// ID list of the user $self and other users who the user follows
// ID list of the user itself and other users who the user follows
const followingIds = await getFriends(user._id);
// Construct query
// Watchしているチャンネルを取得
const watches = await ChannelWatching.find({
user_id: user._id,
// 削除されたドキュメントは除く
deleted_at: { $exists: false }
});
//#region Construct query
const sort = {
_id: -1
};
const query = {
user_id: {
$in: followingIds
},
// TODO
$or: [{
channel_id: {
$exists: false
}
// フォローしている人のタイムラインへの投稿
user_id: {
$in: followingIds
},
// 「タイムラインへの」投稿に限定するためにチャンネルが指定されていないもののみに限る
$or: [{
channel_id: {
$exists: false
}
}, {
channel_id: null
}]
}, {
channel_id: null
// Watchしているチャンネルへの投稿
channel_id: {
$in: watches.map(w => w.channel_id)
}
}]
} as any;
if (sinceId) {
sort._id = 1;
query._id = {
@@ -62,6 +80,7 @@ module.exports = (params, user, app) => new Promise(async (res, rej) => {
$lt: maxId
};
}
//#endregion
// Issue query
const timeline = await Post