summaryrefslogtreecommitdiff
path: root/src/api/endpoints/posts
diff options
context:
space:
mode:
Diffstat (limited to 'src/api/endpoints/posts')
-rw-r--r--src/api/endpoints/posts/create.ts11
-rw-r--r--src/api/endpoints/posts/reactions/create.ts10
-rw-r--r--src/api/endpoints/posts/timeline.ts57
3 files changed, 51 insertions, 27 deletions
diff --git a/src/api/endpoints/posts/create.ts b/src/api/endpoints/posts/create.ts
index f982b9ee93..ae4959dae4 100644
--- a/src/api/endpoints/posts/create.ts
+++ b/src/api/endpoints/posts/create.ts
@@ -14,7 +14,7 @@ import ChannelWatching from '../../models/channel-watching';
import serialize from '../../serializers/post';
import notify from '../../common/notify';
import watch from '../../common/watch-post';
-import { default as event, publishChannelStream } from '../../event';
+import event, { pushSw, publishChannelStream } from '../../event';
import config from '../../../conf';
/**
@@ -44,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) {
@@ -236,7 +234,7 @@ module.exports = (params, user: IUser, app) => new Promise(async (res, rej) => {
const mentions = [];
- function addMention(mentionee, type) {
+ function addMention(mentionee, reason) {
// Reject if already added
if (mentions.some(x => x.equals(mentionee))) return;
@@ -245,7 +243,8 @@ module.exports = (params, user: IUser, app) => new Promise(async (res, rej) => {
// Publish event
if (!user._id.equals(mentionee)) {
- event(mentionee, type, postObj);
+ event(mentionee, reason, postObj);
+ pushSw(mentionee, reason, postObj);
}
}
diff --git a/src/api/endpoints/posts/reactions/create.ts b/src/api/endpoints/posts/reactions/create.ts
index eecb928123..d537463dfe 100644
--- a/src/api/endpoints/posts/reactions/create.ts
+++ b/src/api/endpoints/posts/reactions/create.ts
@@ -7,7 +7,9 @@ import Post from '../../../models/post';
import Watching from '../../../models/post-watching';
import notify from '../../../common/notify';
import watch from '../../../common/watch-post';
-import { publishPostStream } from '../../../event';
+import { publishPostStream, pushSw } from '../../../event';
+import serializePost from '../../../serializers/post';
+import serializeUser from '../../../serializers/user';
/**
* React to a post
@@ -87,6 +89,12 @@ module.exports = (params, user) => new Promise(async (res, rej) => {
reaction: reaction
});
+ pushSw(post.user_id, 'reaction', {
+ user: await serializeUser(user, post.user_id),
+ post: await serializePost(post, post.user_id),
+ reaction: reaction
+ });
+
// Fetch watchers
Watching
.find({
diff --git a/src/api/endpoints/posts/timeline.ts b/src/api/endpoints/posts/timeline.ts
index aa5aff5ba5..0d08b95463 100644
--- a/src/api/endpoints/posts/timeline.ts
+++ b/src/api/endpoints/posts/timeline.ts
@@ -2,6 +2,7 @@
* 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';
@@ -15,32 +16,41 @@ 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');
- }
+ // Get 'since_date' parameter
+ const [sinceDate, sinceDateErr] = $(params.since_date).optional.number().$;
+ if (sinceDateErr) throw 'invalid since_date param';
+
+ // Get 'max_date' parameter
+ const [maxDate, maxDateErr] = $(params.max_date).optional.number().$;
+ if (maxDateErr) throw 'invalid max_date param';
- // ID list of the user itself and other users who the user follows
- const followingIds = await getFriends(user._id);
+ // Check if only one of since_id, max_id, since_date, max_date specified
+ if ([sinceId, maxId, sinceDate, maxDate].filter(x => x != null).length > 1) {
+ throw 'only one of since_id, max_id, since_date, max_date can be specified';
+ }
- // Watchしているチャンネルを取得
- const watches = await ChannelWatching.find({
- user_id: user._id,
- // 削除されたドキュメントは除く
- deleted_at: { $exists: false }
+ const { followingIds, watchingChannelIds } = await rap({
+ // ID list of the user itself and other users who the user follows
+ followingIds: getFriends(user._id),
+ // Watchしているチャンネルを取得
+ watchingChannelIds: ChannelWatching.find({
+ user_id: user._id,
+ // 削除されたドキュメントは除く
+ deleted_at: { $exists: false }
+ }).then(watches => watches.map(w => w.channel_id))
});
//#region Construct query
@@ -65,7 +75,7 @@ module.exports = (params, user, app) => new Promise(async (res, rej) => {
}, {
// Watchしているチャンネルへの投稿
channel_id: {
- $in: watches.map(w => w.channel_id)
+ $in: watchingChannelIds
}
}]
} as any;
@@ -79,6 +89,15 @@ module.exports = (params, user, app) => new Promise(async (res, rej) => {
query._id = {
$lt: maxId
};
+ } else if (sinceDate) {
+ sort._id = 1;
+ query.created_at = {
+ $gt: new Date(sinceDate)
+ };
+ } else if (maxDate) {
+ query.created_at = {
+ $lt: new Date(maxDate)
+ };
}
//#endregion
@@ -90,7 +109,5 @@ module.exports = (params, user, app) => new Promise(async (res, rej) => {
});
// Serialize
- res(await Promise.all(timeline.map(async post =>
- await serialize(post, user)
- )));
-});
+ return await Promise.all(timeline.map(post => serialize(post, user)));
+};