diff options
| author | syuilo <Syuilotan@yahoo.co.jp> | 2018-06-18 14:43:56 +0900 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2018-06-18 14:43:56 +0900 |
| commit | 5d3943ffa8cb4090c5c111397e266d255cc2212b (patch) | |
| tree | 46f692fb21005bdc89499ea012c2436e8a2e90c0 /src/server/api/endpoints/notes | |
| parent | New translations ja.yml (Spanish) (diff) | |
| parent | yatta (diff) | |
| download | misskey-5d3943ffa8cb4090c5c111397e266d255cc2212b.tar.gz misskey-5d3943ffa8cb4090c5c111397e266d255cc2212b.tar.bz2 misskey-5d3943ffa8cb4090c5c111397e266d255cc2212b.zip | |
Merge branch 'master' into l10n_master
Diffstat (limited to 'src/server/api/endpoints/notes')
20 files changed, 162 insertions, 213 deletions
diff --git a/src/server/api/endpoints/notes/conversation.ts b/src/server/api/endpoints/notes/conversation.ts index 02f7229ccf..b2bc6a2e72 100644 --- a/src/server/api/endpoints/notes/conversation.ts +++ b/src/server/api/endpoints/notes/conversation.ts @@ -1,13 +1,11 @@ -/** - * Module dependencies - */ import $ from 'cafy'; import ID from '../../../../cafy-id'; -import Note, { pack } from '../../../../models/note'; +import Note, { pack, INote } from '../../../../models/note'; +import { ILocalUser } from '../../../../models/user'; /** * Show conversation of a note */ -module.exports = (params, user) => new Promise(async (res, rej) => { +module.exports = (params: any, user: ILocalUser) => new Promise(async (res, rej) => { // Get 'noteId' parameter const [noteId, noteIdErr] = $.type(ID).get(params.noteId); if (noteIdErr) return rej('invalid noteId param'); @@ -29,10 +27,10 @@ module.exports = (params, user) => new Promise(async (res, rej) => { return rej('note not found'); } - const conversation = []; + const conversation: INote[] = []; let i = 0; - async function get(id) { + async function get(id: any) { i++; const p = await Note.findOne({ _id: id }); diff --git a/src/server/api/endpoints/notes/create.ts b/src/server/api/endpoints/notes/create.ts index 182359f637..64f3b5ce26 100644 --- a/src/server/api/endpoints/notes/create.ts +++ b/src/server/api/endpoints/notes/create.ts @@ -1,9 +1,6 @@ -/** - * Module dependencies - */ import $ from 'cafy'; import ID from '../../../../cafy-id'; import Note, { INote, isValidText, isValidCw, pack } from '../../../../models/note'; -import User, { ILocalUser } from '../../../../models/user'; +import User, { ILocalUser, IUser } from '../../../../models/user'; import DriveFile from '../../../../models/drive-file'; import create from '../../../../services/note/create'; import { IApp } from '../../../../models/app'; @@ -11,7 +8,7 @@ import { IApp } from '../../../../models/app'; /** * Create a note */ -module.exports = (params, user: ILocalUser, app: IApp) => new Promise(async (res, rej) => { +module.exports = (params: any, user: ILocalUser, app: IApp) => new Promise(async (res, rej) => { // Get 'visibility' parameter const [visibility = 'public', visibilityErr] = $.str.optional().or(['public', 'home', 'followers', 'specified', 'private']).get(params.visibility); if (visibilityErr) return rej('invalid visibility'); @@ -20,7 +17,7 @@ module.exports = (params, user: ILocalUser, app: IApp) => new Promise(async (res const [visibleUserIds, visibleUserIdsErr] = $.arr($.type(ID)).optional().unique().min(1).get(params.visibleUserIds); if (visibleUserIdsErr) return rej('invalid visibleUserIds'); - let visibleUsers = []; + let visibleUsers: IUser[] = []; if (visibleUserIds !== undefined) { visibleUsers = await Promise.all(visibleUserIds.map(id => User.findOne({ _id: id @@ -132,7 +129,7 @@ module.exports = (params, user: ILocalUser, app: IApp) => new Promise(async (res if (pollErr) return rej('invalid poll'); if (poll) { - (poll as any).choices = (poll as any).choices.map((choice, i) => ({ + (poll as any).choices = (poll as any).choices.map((choice: string, i: number) => ({ id: i, // IDを付与 text: choice.trim(), votes: 0 @@ -140,7 +137,7 @@ module.exports = (params, user: ILocalUser, app: IApp) => new Promise(async (res } // テキストが無いかつ添付ファイルが無いかつRenoteも無いかつ投票も無かったらエラー - if (text === undefined && files === null && renote === null && poll === undefined) { + if ((text === undefined || text === null) && files === null && renote === null && poll === undefined) { return rej('text, mediaIds, renoteId or poll is required'); } diff --git a/src/server/api/endpoints/notes/delete.ts b/src/server/api/endpoints/notes/delete.ts new file mode 100644 index 0000000000..70bcdbaab9 --- /dev/null +++ b/src/server/api/endpoints/notes/delete.ts @@ -0,0 +1,27 @@ +import $ from 'cafy'; import ID from '../../../../cafy-id'; +import Note from '../../../../models/note'; +import deleteNote from '../../../../services/note/delete'; +import { ILocalUser } from '../../../../models/user'; + +/** + * Delete a note + */ +module.exports = (params: any, user: ILocalUser) => new Promise(async (res, rej) => { + // Get 'noteId' parameter + const [noteId, noteIdErr] = $.type(ID).get(params.noteId); + if (noteIdErr) return rej('invalid noteId param'); + + // Fetch note + const note = await Note.findOne({ + _id: noteId, + userId: user._id + }); + + if (note === null) { + return rej('note not found'); + } + + await deleteNote(user, note); + + res(); +}); diff --git a/src/server/api/endpoints/notes/favorites/create.ts b/src/server/api/endpoints/notes/favorites/create.ts index 6832b52f75..23f7ac5f8d 100644 --- a/src/server/api/endpoints/notes/favorites/create.ts +++ b/src/server/api/endpoints/notes/favorites/create.ts @@ -1,14 +1,12 @@ -/** - * Module dependencies - */ import $ from 'cafy'; import ID from '../../../../../cafy-id'; import Favorite from '../../../../../models/favorite'; import Note from '../../../../../models/note'; +import { ILocalUser } from '../../../../../models/user'; /** * Favorite a note */ -module.exports = (params, user) => new Promise(async (res, rej) => { +module.exports = (params: any, user: ILocalUser) => new Promise(async (res, rej) => { // Get 'noteId' parameter const [noteId, noteIdErr] = $.type(ID).get(params.noteId); if (noteIdErr) return rej('invalid noteId param'); diff --git a/src/server/api/endpoints/notes/favorites/delete.ts b/src/server/api/endpoints/notes/favorites/delete.ts index 07112dae15..7d2d2b4cb5 100644 --- a/src/server/api/endpoints/notes/favorites/delete.ts +++ b/src/server/api/endpoints/notes/favorites/delete.ts @@ -1,14 +1,12 @@ -/** - * Module dependencies - */ import $ from 'cafy'; import ID from '../../../../../cafy-id'; import Favorite from '../../../../../models/favorite'; import Note from '../../../../../models/note'; +import { ILocalUser } from '../../../../../models/user'; /** * Unfavorite a note */ -module.exports = (params, user) => new Promise(async (res, rej) => { +module.exports = (params: any, user: ILocalUser) => new Promise(async (res, rej) => { // Get 'noteId' parameter const [noteId, noteIdErr] = $.type(ID).get(params.noteId); if (noteIdErr) return rej('invalid noteId param'); diff --git a/src/server/api/endpoints/notes/global-timeline.ts b/src/server/api/endpoints/notes/global-timeline.ts index d22a1763de..24ffdbcba7 100644 --- a/src/server/api/endpoints/notes/global-timeline.ts +++ b/src/server/api/endpoints/notes/global-timeline.ts @@ -1,15 +1,13 @@ -/** - * Module dependencies - */ import $ from 'cafy'; import ID from '../../../../cafy-id'; import Note from '../../../../models/note'; import Mute from '../../../../models/mute'; import { pack } from '../../../../models/note'; +import { ILocalUser } from '../../../../models/user'; /** * Get timeline of global */ -module.exports = async (params, user, app) => { +module.exports = async (params: any, user: ILocalUser) => { // Get 'limit' parameter const [limit = 10, limitErr] = $.num.optional().range(1, 100).get(params.limit); if (limitErr) throw 'invalid limit param'; @@ -35,10 +33,14 @@ module.exports = async (params, user, app) => { throw 'only one of sinceId, untilId, sinceDate, untilDate can be specified'; } + // Get 'mediaOnly' parameter + const [mediaOnly, mediaOnlyErr] = $.bool.optional().get(params.mediaOnly); + if (mediaOnlyErr) throw 'invalid mediaOnly param'; + // ミュートしているユーザーを取得 - const mutedUserIds = (await Mute.find({ + const mutedUserIds = user ? (await Mute.find({ muterId: user._id - })).map(m => m.muteeId); + })).map(m => m.muteeId) : null; //#region Construct query const sort = { @@ -46,17 +48,27 @@ module.exports = async (params, user, app) => { }; const query = { - // mute - userId: { + // public only + visibility: 'public' + } as any; + + if (mutedUserIds && mutedUserIds.length > 0) { + query.userId = { $nin: mutedUserIds - }, - '_reply.userId': { + }; + + query['_reply.userId'] = { $nin: mutedUserIds - }, - '_renote.userId': { + }; + + query['_renote.userId'] = { $nin: mutedUserIds - } - } as any; + }; + } + + if (mediaOnly) { + query.mediaIds = { $exists: true, $ne: [] }; + } if (sinceId) { sort._id = 1; diff --git a/src/server/api/endpoints/notes/local-timeline.ts b/src/server/api/endpoints/notes/local-timeline.ts index e7ebe5d960..48490638d2 100644 --- a/src/server/api/endpoints/notes/local-timeline.ts +++ b/src/server/api/endpoints/notes/local-timeline.ts @@ -1,15 +1,13 @@ -/** - * Module dependencies - */ import $ from 'cafy'; import ID from '../../../../cafy-id'; import Note from '../../../../models/note'; import Mute from '../../../../models/mute'; import { pack } from '../../../../models/note'; +import { ILocalUser } from '../../../../models/user'; /** * Get timeline of local */ -module.exports = async (params, user, app) => { +module.exports = async (params: any, user: ILocalUser) => { // Get 'limit' parameter const [limit = 10, limitErr] = $.num.optional().range(1, 100).get(params.limit); if (limitErr) throw 'invalid limit param'; @@ -35,10 +33,14 @@ module.exports = async (params, user, app) => { throw 'only one of sinceId, untilId, sinceDate, untilDate can be specified'; } + // Get 'mediaOnly' parameter + const [mediaOnly, mediaOnlyErr] = $.bool.optional().get(params.mediaOnly); + if (mediaOnlyErr) throw 'invalid mediaOnly param'; + // ミュートしているユーザーを取得 - const mutedUserIds = (await Mute.find({ + const mutedUserIds = user ? (await Mute.find({ muterId: user._id - })).map(m => m.muteeId); + })).map(m => m.muteeId) : null; //#region Construct query const sort = { @@ -46,21 +48,31 @@ module.exports = async (params, user, app) => { }; const query = { - // mute - userId: { - $nin: mutedUserIds - }, - '_reply.userId': { - $nin: mutedUserIds - }, - '_renote.userId': { - $nin: mutedUserIds - }, + // public only + visibility: 'public', // local '_user.host': null } as any; + if (mutedUserIds && mutedUserIds.length > 0) { + query.userId = { + $nin: mutedUserIds + }; + + query['_reply.userId'] = { + $nin: mutedUserIds + }; + + query['_renote.userId'] = { + $nin: mutedUserIds + }; + } + + if (mediaOnly) { + query.mediaIds = { $exists: true, $ne: [] }; + } + if (sinceId) { sort._id = 1; query._id = { diff --git a/src/server/api/endpoints/notes/mentions.ts b/src/server/api/endpoints/notes/mentions.ts index 163a6b4866..45511603af 100644 --- a/src/server/api/endpoints/notes/mentions.ts +++ b/src/server/api/endpoints/notes/mentions.ts @@ -1,19 +1,13 @@ -/** - * Module dependencies - */ import $ from 'cafy'; import ID from '../../../../cafy-id'; import Note from '../../../../models/note'; import { getFriendIds } from '../../common/get-friends'; import { pack } from '../../../../models/note'; +import { ILocalUser } from '../../../../models/user'; /** * Get mentions of myself - * - * @param {any} params - * @param {any} user - * @return {Promise<any>} */ -module.exports = (params, user) => new Promise(async (res, rej) => { +module.exports = (params: any, user: ILocalUser) => new Promise(async (res, rej) => { // Get 'following' parameter const [following = false, followingError] = $.bool.optional().get(params.following); diff --git a/src/server/api/endpoints/notes/polls/recommendation.ts b/src/server/api/endpoints/notes/polls/recommendation.ts index a272378d19..640140c3d1 100644 --- a/src/server/api/endpoints/notes/polls/recommendation.ts +++ b/src/server/api/endpoints/notes/polls/recommendation.ts @@ -1,14 +1,12 @@ -/** - * Module dependencies - */ import $ from 'cafy'; import Vote from '../../../../../models/poll-vote'; import Note, { pack } from '../../../../../models/note'; +import { ILocalUser } from '../../../../../models/user'; /** * Get recommended polls */ -module.exports = (params, user) => new Promise(async (res, rej) => { +module.exports = (params: any, user: ILocalUser) => new Promise(async (res, rej) => { // Get 'limit' parameter const [limit = 10, limitErr] = $.num.optional().range(1, 100).get(params.limit); if (limitErr) return rej('invalid limit param'); diff --git a/src/server/api/endpoints/notes/polls/vote.ts b/src/server/api/endpoints/notes/polls/vote.ts index f8f4515308..72ac6bb202 100644 --- a/src/server/api/endpoints/notes/polls/vote.ts +++ b/src/server/api/endpoints/notes/polls/vote.ts @@ -1,6 +1,3 @@ -/** - * Module dependencies - */ import $ from 'cafy'; import ID from '../../../../../cafy-id'; import Vote from '../../../../../models/poll-vote'; import Note from '../../../../../models/note'; @@ -8,11 +5,12 @@ import Watching from '../../../../../models/note-watching'; import watch from '../../../../../services/note/watch'; import { publishNoteStream } from '../../../../../publishers/stream'; import notify from '../../../../../publishers/notify'; +import { ILocalUser } from '../../../../../models/user'; /** * Vote poll of a note */ -module.exports = (params, user) => new Promise(async (res, rej) => { +module.exports = (params: any, user: ILocalUser) => new Promise(async (res, rej) => { // Get 'noteId' parameter const [noteId, noteIdErr] = $.type(ID).get(params.noteId); if (noteIdErr) return rej('invalid noteId param'); @@ -58,8 +56,8 @@ module.exports = (params, user) => new Promise(async (res, rej) => { // Send response res(); - const inc = {}; - inc[`poll.choices.${findWithAttr(note.poll.choices, 'id', choice)}.votes`] = 1; + const inc: any = {}; + inc[`poll.choices.${note.poll.choices.findIndex(c => c.id == choice)}.votes`] = 1; // Increment votes count await Note.update({ _id: note._id }, { @@ -100,12 +98,3 @@ module.exports = (params, user) => new Promise(async (res, rej) => { watch(user._id, note); } }); - -function findWithAttr(array, attr, value) { - for (let i = 0; i < array.length; i += 1) { - if (array[i][attr] === value) { - return i; - } - } - return -1; -} diff --git a/src/server/api/endpoints/notes/reactions.ts b/src/server/api/endpoints/notes/reactions.ts index 4ad952a7a1..d3b2d43432 100644 --- a/src/server/api/endpoints/notes/reactions.ts +++ b/src/server/api/endpoints/notes/reactions.ts @@ -1,18 +1,12 @@ -/** - * Module dependencies - */ import $ from 'cafy'; import ID from '../../../../cafy-id'; import Note from '../../../../models/note'; import Reaction, { pack } from '../../../../models/note-reaction'; +import { ILocalUser } from '../../../../models/user'; /** * Show reactions of a note - * - * @param {any} params - * @param {any} user - * @return {Promise<any>} */ -module.exports = (params, user) => new Promise(async (res, rej) => { +module.exports = (params: any, user: ILocalUser) => new Promise(async (res, rej) => { // Get 'noteId' parameter const [noteId, noteIdErr] = $.type(ID).get(params.noteId); if (noteIdErr) return rej('invalid noteId param'); diff --git a/src/server/api/endpoints/notes/reactions/create.ts b/src/server/api/endpoints/notes/reactions/create.ts index 21757cb427..33e457e308 100644 --- a/src/server/api/endpoints/notes/reactions/create.ts +++ b/src/server/api/endpoints/notes/reactions/create.ts @@ -1,15 +1,13 @@ -/** - * Module dependencies - */ import $ from 'cafy'; import ID from '../../../../../cafy-id'; import Note from '../../../../../models/note'; import create from '../../../../../services/note/reaction/create'; import { validateReaction } from '../../../../../models/note-reaction'; +import { ILocalUser } from '../../../../../models/user'; /** * React to a note */ -module.exports = (params, user) => new Promise(async (res, rej) => { +module.exports = (params: any, user: ILocalUser) => new Promise(async (res, rej) => { // Get 'noteId' parameter const [noteId, noteIdErr] = $.type(ID).get(params.noteId); if (noteIdErr) return rej('invalid noteId param'); diff --git a/src/server/api/endpoints/notes/reactions/delete.ts b/src/server/api/endpoints/notes/reactions/delete.ts index afb8629112..1f2d662511 100644 --- a/src/server/api/endpoints/notes/reactions/delete.ts +++ b/src/server/api/endpoints/notes/reactions/delete.ts @@ -1,14 +1,12 @@ -/** - * Module dependencies - */ import $ from 'cafy'; import ID from '../../../../../cafy-id'; import Reaction from '../../../../../models/note-reaction'; import Note from '../../../../../models/note'; +import { ILocalUser } from '../../../../../models/user'; /** * Unreact to a note */ -module.exports = (params, user) => new Promise(async (res, rej) => { +module.exports = (params: any, user: ILocalUser) => new Promise(async (res, rej) => { // Get 'noteId' parameter const [noteId, noteIdErr] = $.type(ID).get(params.noteId); if (noteIdErr) return rej('invalid noteId param'); @@ -45,7 +43,7 @@ module.exports = (params, user) => new Promise(async (res, rej) => { // Send response res(); - const dec = {}; + const dec: any = {}; dec[`reactionCounts.${exist.reaction}`] = -1; // Decrement reactions count diff --git a/src/server/api/endpoints/notes/replies.ts b/src/server/api/endpoints/notes/replies.ts index 11d221d8f7..4aaf1d322b 100644 --- a/src/server/api/endpoints/notes/replies.ts +++ b/src/server/api/endpoints/notes/replies.ts @@ -1,17 +1,11 @@ -/** - * Module dependencies - */ import $ from 'cafy'; import ID from '../../../../cafy-id'; import Note, { pack } from '../../../../models/note'; +import { ILocalUser } from '../../../../models/user'; /** - * Show a replies of a note - * - * @param {any} params - * @param {any} user - * @return {Promise<any>} + * Get replies of a note */ -module.exports = (params, user) => new Promise(async (res, rej) => { +module.exports = (params: any, user: ILocalUser) => new Promise(async (res, rej) => { // Get 'noteId' parameter const [noteId, noteIdErr] = $.type(ID).get(params.noteId); if (noteIdErr) return rej('invalid noteId param'); @@ -24,10 +18,6 @@ module.exports = (params, user) => new Promise(async (res, rej) => { const [offset = 0, offsetErr] = $.num.optional().min(0).get(params.offset); if (offsetErr) return rej('invalid offset param'); - // Get 'sort' parameter - const [sort = 'desc', sortError] = $.str.optional().or('desc asc').get(params.sort); - if (sortError) return rej('invalid sort param'); - // Lookup note const note = await Note.findOne({ _id: noteId @@ -37,17 +27,8 @@ module.exports = (params, user) => new Promise(async (res, rej) => { return rej('note not found'); } - // Issue query - const replies = await Note - .find({ replyId: note._id }, { - limit: limit, - skip: offset, - sort: { - _id: sort == 'asc' ? 1 : -1 - } - }); + const ids = (note._replyIds || []).slice(offset, offset + limit); // Serialize - res(await Promise.all(replies.map(async note => - await pack(note, user)))); + res(await Promise.all(ids.map(id => pack(id, user)))); }); diff --git a/src/server/api/endpoints/notes/reposts.ts b/src/server/api/endpoints/notes/reposts.ts index 3098211b61..ea3f174e1a 100644 --- a/src/server/api/endpoints/notes/reposts.ts +++ b/src/server/api/endpoints/notes/reposts.ts @@ -1,17 +1,11 @@ -/** - * Module dependencies - */ import $ from 'cafy'; import ID from '../../../../cafy-id'; import Note, { pack } from '../../../../models/note'; +import { ILocalUser } from '../../../../models/user'; /** * Show a renotes of a note - * - * @param {any} params - * @param {any} user - * @return {Promise<any>} */ -module.exports = (params, user) => new Promise(async (res, rej) => { +module.exports = (params: any, user: ILocalUser) => new Promise(async (res, rej) => { // Get 'noteId' parameter const [noteId, noteIdErr] = $.type(ID).get(params.noteId); if (noteIdErr) return rej('invalid noteId param'); diff --git a/src/server/api/endpoints/notes/search.ts b/src/server/api/endpoints/notes/search_by_tag.ts index 9705dcfd6e..9be7cfffb6 100644 --- a/src/server/api/endpoints/notes/search.ts +++ b/src/server/api/endpoints/notes/search_by_tag.ts @@ -1,25 +1,17 @@ -/** - * Module dependencies - */ import $ from 'cafy'; import ID from '../../../../cafy-id'; -const escapeRegexp = require('escape-regexp'); import Note from '../../../../models/note'; -import User from '../../../../models/user'; +import User, { ILocalUser } from '../../../../models/user'; import Mute from '../../../../models/mute'; import { getFriendIds } from '../../common/get-friends'; import { pack } from '../../../../models/note'; /** - * Search a note - * - * @param {any} params - * @param {any} me - * @return {Promise<any>} + * Search notes by tag */ -module.exports = (params, me) => new Promise(async (res, rej) => { - // Get 'text' parameter - const [text, textError] = $.str.optional().get(params.text); - if (textError) return rej('invalid text param'); +module.exports = (params: any, me: ILocalUser) => new Promise(async (res, rej) => { + // Get 'tag' parameter + const [tag, tagError] = $.str.get(params.tag); + if (tagError) return rej('invalid tag param'); // Get 'includeUserIds' parameter const [includeUserIds = [], includeUserIdsErr] = $.arr($.type(ID)).optional().get(params.includeUserIds); @@ -77,7 +69,6 @@ module.exports = (params, me) => new Promise(async (res, rej) => { const [limit = 10, limitErr] = $.num.optional().range(1, 30).get(params.limit); if (limitErr) return rej('invalid limit param'); - let includeUsers = includeUserIds; if (includeUserUsernames != null) { const ids = (await Promise.all(includeUserUsernames.map(async (username) => { const _user = await User.findOne({ @@ -85,10 +76,10 @@ module.exports = (params, me) => new Promise(async (res, rej) => { }); return _user ? _user._id : null; }))).filter(id => id != null); - includeUsers = includeUsers.concat(ids); + + ids.forEach(id => includeUserIds.push(id)); } - let excludeUsers = excludeUserIds; if (excludeUserUsernames != null) { const ids = (await Promise.all(excludeUserUsernames.map(async (username) => { const _user = await User.findOne({ @@ -96,50 +87,17 @@ module.exports = (params, me) => new Promise(async (res, rej) => { }); return _user ? _user._id : null; }))).filter(id => id != null); - excludeUsers = excludeUsers.concat(ids); - } - - search(res, rej, me, text, includeUsers, excludeUsers, following, - mute, reply, renote, media, poll, sinceDate, untilDate, offset, limit); -}); -async function search( - res, rej, me, text, includeUserIds, excludeUserIds, following, - mute, reply, renote, media, poll, sinceDate, untilDate, offset, max) { + ids.forEach(id => excludeUserIds.push(id)); + } let q: any = { - $and: [] + $and: [{ + tagsLower: tag.toLowerCase() + }] }; - const push = x => q.$and.push(x); - - if (text) { - // 完全一致検索 - if (/"""(.+?)"""/.test(text)) { - const x = text.match(/"""(.+?)"""/)[1]; - push({ - text: x - }); - } else { - const tags = text.split(' ').filter(x => x[0] == '#'); - if (tags) { - push({ - $and: tags.map(x => ({ - tags: x - })) - }); - } - - push({ - $and: text.split(' ').map(x => ({ - // キーワードが-で始まる場合そのキーワードを除外する - text: x[0] == '-' ? { - $not: new RegExp(escapeRegexp(x.substr(1))) - } : new RegExp(escapeRegexp(x)) - })) - }); - } - } + const push = (x: any) => q.$and.push(x); if (includeUserIds && includeUserIds.length != 0) { push({ @@ -354,11 +312,10 @@ async function search( sort: { _id: -1 }, - limit: max, + limit: limit, skip: offset }); // Serialize - res(await Promise.all(notes.map(async note => - await pack(note, me)))); -} + res(await Promise.all(notes.map(note => pack(note, me)))); +}); diff --git a/src/server/api/endpoints/notes/show.ts b/src/server/api/endpoints/notes/show.ts index 78dc55a703..1ba7145996 100644 --- a/src/server/api/endpoints/notes/show.ts +++ b/src/server/api/endpoints/notes/show.ts @@ -1,17 +1,11 @@ -/** - * Module dependencies - */ import $ from 'cafy'; import ID from '../../../../cafy-id'; import Note, { pack } from '../../../../models/note'; +import { ILocalUser } from '../../../../models/user'; /** * Show a note - * - * @param {any} params - * @param {any} user - * @return {Promise<any>} */ -module.exports = (params, user) => new Promise(async (res, rej) => { +module.exports = (params: any, user: ILocalUser) => new Promise(async (res, rej) => { // Get 'noteId' parameter const [noteId, noteIdErr] = $.type(ID).get(params.noteId); if (noteIdErr) return rej('invalid noteId param'); diff --git a/src/server/api/endpoints/notes/timeline.ts b/src/server/api/endpoints/notes/timeline.ts index 9f32555649..18c0acd379 100644 --- a/src/server/api/endpoints/notes/timeline.ts +++ b/src/server/api/endpoints/notes/timeline.ts @@ -1,16 +1,14 @@ -/** - * Module dependencies - */ import $ from 'cafy'; import ID from '../../../../cafy-id'; import Note from '../../../../models/note'; import Mute from '../../../../models/mute'; import { getFriends } from '../../common/get-friends'; import { pack } from '../../../../models/note'; +import { ILocalUser } from '../../../../models/user'; /** * Get timeline of myself */ -module.exports = async (params, user, app) => { +module.exports = async (params: any, user: ILocalUser) => { // Get 'limit' parameter const [limit = 10, limitErr] = $.num.optional().range(1, 100).get(params.limit); if (limitErr) throw 'invalid limit param'; @@ -44,6 +42,10 @@ module.exports = async (params, user, app) => { const [includeRenotedMyNotes = true, includeRenotedMyNotesErr] = $.bool.optional().get(params.includeRenotedMyNotes); if (includeRenotedMyNotesErr) throw 'invalid includeRenotedMyNotes param'; + // Get 'mediaOnly' parameter + const [mediaOnly, mediaOnlyErr] = $.bool.optional().get(params.mediaOnly); + if (mediaOnlyErr) throw 'invalid mediaOnly param'; + const [followings, mutedUserIds] = await Promise.all([ // フォローを取得 // Fetch following @@ -137,6 +139,12 @@ module.exports = async (params, user, app) => { }); } + if (mediaOnly) { + query.$and.push({ + mediaIds: { $exists: true, $ne: [] } + }); + } + if (sinceId) { sort._id = 1; query._id = { diff --git a/src/server/api/endpoints/notes/trend.ts b/src/server/api/endpoints/notes/trend.ts index 4735bec51e..9c0a1bb112 100644 --- a/src/server/api/endpoints/notes/trend.ts +++ b/src/server/api/endpoints/notes/trend.ts @@ -1,18 +1,12 @@ -/** - * Module dependencies - */ const ms = require('ms'); import $ from 'cafy'; import Note, { pack } from '../../../../models/note'; +import { ILocalUser } from '../../../../models/user'; /** * Get trend notes - * - * @param {any} params - * @param {any} user - * @return {Promise<any>} */ -module.exports = (params, user) => new Promise(async (res, rej) => { +module.exports = (params: any, user: ILocalUser) => new Promise(async (res, rej) => { // Get 'limit' parameter const [limit = 10, limitErr] = $.num.optional().range(1, 100).get(params.limit); if (limitErr) return rej('invalid limit param'); diff --git a/src/server/api/endpoints/notes/user-list-timeline.ts b/src/server/api/endpoints/notes/user-list-timeline.ts index 9f8397d679..8aa800b712 100644 --- a/src/server/api/endpoints/notes/user-list-timeline.ts +++ b/src/server/api/endpoints/notes/user-list-timeline.ts @@ -1,16 +1,14 @@ -/** - * Module dependencies - */ import $ from 'cafy'; import ID from '../../../../cafy-id'; import Note from '../../../../models/note'; import Mute from '../../../../models/mute'; import { pack } from '../../../../models/note'; import UserList from '../../../../models/user-list'; +import { ILocalUser } from '../../../../models/user'; /** * Get timeline of a user list */ -module.exports = async (params, user, app) => { +module.exports = async (params: any, user: ILocalUser) => { // Get 'limit' parameter const [limit = 10, limitErr] = $.num.optional().range(1, 100).get(params.limit); if (limitErr) throw 'invalid limit param'; @@ -44,6 +42,10 @@ module.exports = async (params, user, app) => { const [includeRenotedMyNotes = true, includeRenotedMyNotesErr] = $.bool.optional().get(params.includeRenotedMyNotes); if (includeRenotedMyNotesErr) throw 'invalid includeRenotedMyNotes param'; + // Get 'mediaOnly' parameter + const [mediaOnly, mediaOnlyErr] = $.bool.optional().get(params.mediaOnly); + if (mediaOnlyErr) throw 'invalid mediaOnly param'; + // Get 'listId' parameter const [listId, listIdErr] = $.type(ID).get(params.listId); if (listIdErr) throw 'invalid listId param'; @@ -146,6 +148,12 @@ module.exports = async (params, user, app) => { }); } + if (mediaOnly) { + query.$and.push({ + mediaIds: { $exists: true, $ne: [] } + }); + } + if (sinceId) { sort._id = 1; query._id = { |