From 5ddb047794bb3aaef250c15dfbae56872faedfd9 Mon Sep 17 00:00:00 2001 From: syuilo Date: Sun, 8 Apr 2018 05:02:50 +0900 Subject: Fix bugs --- src/server/api/endpoints/following/create.ts | 2 +- src/server/api/endpoints/i/update.ts | 24 +---- src/server/api/endpoints/users/notes.ts | 137 +++++++++++++++++++++++++++ src/server/api/endpoints/users/posts.ts | 137 --------------------------- 4 files changed, 142 insertions(+), 158 deletions(-) create mode 100644 src/server/api/endpoints/users/notes.ts delete mode 100644 src/server/api/endpoints/users/posts.ts (limited to 'src/server/api') diff --git a/src/server/api/endpoints/following/create.ts b/src/server/api/endpoints/following/create.ts index d3cc549ca7..0a642f50b2 100644 --- a/src/server/api/endpoints/following/create.ts +++ b/src/server/api/endpoints/following/create.ts @@ -31,7 +31,7 @@ module.exports = (params, user) => new Promise(async (res, rej) => { }, { fields: { data: false, - 'profile': false + profile: false } }); diff --git a/src/server/api/endpoints/i/update.ts b/src/server/api/endpoints/i/update.ts index a8caa0ebc4..36be2774f6 100644 --- a/src/server/api/endpoints/i/update.ts +++ b/src/server/api/endpoints/i/update.ts @@ -4,7 +4,6 @@ import $ from 'cafy'; import User, { isValidName, isValidDescription, isValidLocation, isValidBirthday, pack } from '../../../../models/user'; import event from '../../../../publishers/stream'; -import config from '../../../../config'; /** * Update myself @@ -17,7 +16,7 @@ import config from '../../../../config'; */ module.exports = async (params, user, _, isSecure) => new Promise(async (res, rej) => { // Get 'name' parameter - const [name, nameErr] = $(params.name).optional.string().pipe(isValidName).$; + const [name, nameErr] = $(params.name).optional.nullable.string().pipe(isValidName).$; if (nameErr) return rej('invalid name param'); if (name) user.name = name; @@ -62,9 +61,9 @@ module.exports = async (params, user, _, isSecure) => new Promise(async (res, re description: user.description, avatarId: user.avatarId, bannerId: user.bannerId, - 'profile': user.profile, - 'isBot': user.isBot, - 'settings': user.settings + profile: user.profile, + isBot: user.isBot, + settings: user.settings } }); @@ -79,19 +78,4 @@ module.exports = async (params, user, _, isSecure) => new Promise(async (res, re // Publish i updated event event(user._id, 'i_updated', iObj); - - // Update search index - if (config.elasticsearch.enable) { - const es = require('../../../db/elasticsearch'); - - es.index({ - index: 'misskey', - type: 'user', - id: user._id.toString(), - body: { - name: user.name, - bio: user.bio - } - }); - } }); diff --git a/src/server/api/endpoints/users/notes.ts b/src/server/api/endpoints/users/notes.ts new file mode 100644 index 0000000000..f9f6345e34 --- /dev/null +++ b/src/server/api/endpoints/users/notes.ts @@ -0,0 +1,137 @@ +/** + * Module dependencies + */ +import $ from 'cafy'; +import getHostLower from '../../common/get-host-lower'; +import Note, { pack } from '../../../../models/note'; +import User from '../../../../models/user'; + +/** + * Get notes of a user + * + * @param {any} params + * @param {any} me + * @return {Promise} + */ +module.exports = (params, me) => new Promise(async (res, rej) => { + // Get 'userId' parameter + const [userId, userIdErr] = $(params.userId).optional.id().$; + if (userIdErr) return rej('invalid userId param'); + + // Get 'username' parameter + const [username, usernameErr] = $(params.username).optional.string().$; + if (usernameErr) return rej('invalid username param'); + + if (userId === undefined && username === undefined) { + return rej('userId or pair of username and host is required'); + } + + // Get 'host' parameter + const [host, hostErr] = $(params.host).optional.string().$; + if (hostErr) return rej('invalid host param'); + + if (userId === undefined && host === undefined) { + return rej('userId or pair of username and host is required'); + } + + // Get 'includeReplies' parameter + const [includeReplies = true, includeRepliesErr] = $(params.includeReplies).optional.boolean().$; + if (includeRepliesErr) return rej('invalid includeReplies param'); + + // Get 'withMedia' parameter + const [withMedia = false, withMediaErr] = $(params.withMedia).optional.boolean().$; + if (withMediaErr) return rej('invalid withMedia param'); + + // Get 'limit' parameter + const [limit = 10, limitErr] = $(params.limit).optional.number().range(1, 100).$; + if (limitErr) return rej('invalid limit param'); + + // Get 'sinceId' parameter + const [sinceId, sinceIdErr] = $(params.sinceId).optional.id().$; + if (sinceIdErr) return rej('invalid sinceId param'); + + // Get 'untilId' parameter + const [untilId, untilIdErr] = $(params.untilId).optional.id().$; + if (untilIdErr) return rej('invalid untilId param'); + + // Get 'sinceDate' parameter + const [sinceDate, sinceDateErr] = $(params.sinceDate).optional.number().$; + if (sinceDateErr) throw 'invalid sinceDate param'; + + // Get 'untilDate' parameter + const [untilDate, untilDateErr] = $(params.untilDate).optional.number().$; + if (untilDateErr) throw 'invalid untilDate param'; + + // Check if only one of sinceId, untilId, sinceDate, untilDate specified + if ([sinceId, untilId, sinceDate, untilDate].filter(x => x != null).length > 1) { + throw 'only one of sinceId, untilId, sinceDate, untilDate can be specified'; + } + + const q = userId !== undefined + ? { _id: userId } + : { usernameLower: username.toLowerCase(), hostLower: getHostLower(host) } ; + + // Lookup user + const user = await User.findOne(q, { + fields: { + _id: true + } + }); + + if (user === null) { + return rej('user not found'); + } + + //#region Construct query + const sort = { + _id: -1 + }; + + const query = { + userId: user._id + } as any; + + if (sinceId) { + sort._id = 1; + query._id = { + $gt: sinceId + }; + } else if (untilId) { + query._id = { + $lt: untilId + }; + } else if (sinceDate) { + sort._id = 1; + query.createdAt = { + $gt: new Date(sinceDate) + }; + } else if (untilDate) { + query.createdAt = { + $lt: new Date(untilDate) + }; + } + + if (!includeReplies) { + query.replyId = null; + } + + if (withMedia) { + query.mediaIds = { + $exists: true, + $ne: null + }; + } + //#endregion + + // Issue query + const notes = await Note + .find(query, { + limit: limit, + sort: sort + }); + + // Serialize + res(await Promise.all(notes.map(async (note) => + await pack(note, me) + ))); +}); diff --git a/src/server/api/endpoints/users/posts.ts b/src/server/api/endpoints/users/posts.ts deleted file mode 100644 index f9f6345e34..0000000000 --- a/src/server/api/endpoints/users/posts.ts +++ /dev/null @@ -1,137 +0,0 @@ -/** - * Module dependencies - */ -import $ from 'cafy'; -import getHostLower from '../../common/get-host-lower'; -import Note, { pack } from '../../../../models/note'; -import User from '../../../../models/user'; - -/** - * Get notes of a user - * - * @param {any} params - * @param {any} me - * @return {Promise} - */ -module.exports = (params, me) => new Promise(async (res, rej) => { - // Get 'userId' parameter - const [userId, userIdErr] = $(params.userId).optional.id().$; - if (userIdErr) return rej('invalid userId param'); - - // Get 'username' parameter - const [username, usernameErr] = $(params.username).optional.string().$; - if (usernameErr) return rej('invalid username param'); - - if (userId === undefined && username === undefined) { - return rej('userId or pair of username and host is required'); - } - - // Get 'host' parameter - const [host, hostErr] = $(params.host).optional.string().$; - if (hostErr) return rej('invalid host param'); - - if (userId === undefined && host === undefined) { - return rej('userId or pair of username and host is required'); - } - - // Get 'includeReplies' parameter - const [includeReplies = true, includeRepliesErr] = $(params.includeReplies).optional.boolean().$; - if (includeRepliesErr) return rej('invalid includeReplies param'); - - // Get 'withMedia' parameter - const [withMedia = false, withMediaErr] = $(params.withMedia).optional.boolean().$; - if (withMediaErr) return rej('invalid withMedia param'); - - // Get 'limit' parameter - const [limit = 10, limitErr] = $(params.limit).optional.number().range(1, 100).$; - if (limitErr) return rej('invalid limit param'); - - // Get 'sinceId' parameter - const [sinceId, sinceIdErr] = $(params.sinceId).optional.id().$; - if (sinceIdErr) return rej('invalid sinceId param'); - - // Get 'untilId' parameter - const [untilId, untilIdErr] = $(params.untilId).optional.id().$; - if (untilIdErr) return rej('invalid untilId param'); - - // Get 'sinceDate' parameter - const [sinceDate, sinceDateErr] = $(params.sinceDate).optional.number().$; - if (sinceDateErr) throw 'invalid sinceDate param'; - - // Get 'untilDate' parameter - const [untilDate, untilDateErr] = $(params.untilDate).optional.number().$; - if (untilDateErr) throw 'invalid untilDate param'; - - // Check if only one of sinceId, untilId, sinceDate, untilDate specified - if ([sinceId, untilId, sinceDate, untilDate].filter(x => x != null).length > 1) { - throw 'only one of sinceId, untilId, sinceDate, untilDate can be specified'; - } - - const q = userId !== undefined - ? { _id: userId } - : { usernameLower: username.toLowerCase(), hostLower: getHostLower(host) } ; - - // Lookup user - const user = await User.findOne(q, { - fields: { - _id: true - } - }); - - if (user === null) { - return rej('user not found'); - } - - //#region Construct query - const sort = { - _id: -1 - }; - - const query = { - userId: user._id - } as any; - - if (sinceId) { - sort._id = 1; - query._id = { - $gt: sinceId - }; - } else if (untilId) { - query._id = { - $lt: untilId - }; - } else if (sinceDate) { - sort._id = 1; - query.createdAt = { - $gt: new Date(sinceDate) - }; - } else if (untilDate) { - query.createdAt = { - $lt: new Date(untilDate) - }; - } - - if (!includeReplies) { - query.replyId = null; - } - - if (withMedia) { - query.mediaIds = { - $exists: true, - $ne: null - }; - } - //#endregion - - // Issue query - const notes = await Note - .find(query, { - limit: limit, - sort: sort - }); - - // Serialize - res(await Promise.all(notes.map(async (note) => - await pack(note, me) - ))); -}); -- cgit v1.2.3-freya