From 1fca8d322c5fe3c104ab465c96130344a97e6ad7 Mon Sep 17 00:00:00 2001 From: syuilo Date: Thu, 1 Nov 2018 09:19:22 +0900 Subject: [PATCH] Clean up --- src/models/app.ts | 17 +++++---- src/models/user.ts | 37 +++++-------------- src/server/api/endpoints/app/create.ts | 1 + src/server/api/endpoints/app/show.ts | 1 + src/server/api/endpoints/i/authorized_apps.ts | 5 ++- src/server/api/endpoints/my/apps.ts | 5 ++- 6 files changed, 27 insertions(+), 39 deletions(-) diff --git a/src/models/app.ts b/src/models/app.ts index 45686fe405..cf08ba1bda 100644 --- a/src/models/app.ts +++ b/src/models/app.ts @@ -22,27 +22,28 @@ export type IApp = { /** * Pack an app for API response - * - * @param {any} app - * @param {any} me? - * @param {any} options? - * @return {Promise} */ export const pack = ( app: any, me?: any, options?: { + detail?: boolean, includeSecret?: boolean, includeProfileImageIds?: boolean } ) => new Promise(async (resolve, reject) => { - const opts = options || { + const opts = Object.assign({ + detail: false, includeSecret: false, includeProfileImageIds: false - }; + }, options); let _app: any; + const fields = opts.detail ? {} : { + name: true + }; + // Populate the app if 'app' is ID if (isObjectId(app)) { _app = await App.findOne({ @@ -51,7 +52,7 @@ export const pack = ( } else if (typeof app === 'string') { _app = await App.findOne({ _id: new mongo.ObjectID(app) - }); + }, { fields }); } else { _app = deepcopy(app); } diff --git a/src/models/user.ts b/src/models/user.ts index c1c42e891e..f629ddbd20 100644 --- a/src/models/user.ts +++ b/src/models/user.ts @@ -223,33 +223,16 @@ export const pack = ( let _user: any; - const fields = opts.detail ? { - } : { - usernameLower: false, - bannerColor: false, - bannerUrl: false, - description: false, - notesCount: false, - followersCount: false, - followingCount: false, - lastUsedAt: false, - settings: false, - clientSettings: false, - profile: false, - keywords: false, - domains: false, - pinnedNoteIds: false, - wallpaperColor: false, - wallpaperId: false, - wallpaperUrl: false, - twitter: false, - pendingReceivedFollowRequestsCount: false, - featured: false, - sharedInbox: false, - endpoints: false, - inbox: false, - twoFactorTempSecret: false, - twoFactorSecret: false + const fields = opts.detail ? {} : { + name: true, + username: true, + host: true, + avatarColor: true, + avatarUrl: true, + isCat: true, + isBot: true, + isAdmin: true, + isVerified: true }; // Populate the user if 'user' is ID diff --git a/src/server/api/endpoints/app/create.ts b/src/server/api/endpoints/app/create.ts index afe3ab35af..19795f1728 100644 --- a/src/server/api/endpoints/app/create.ts +++ b/src/server/api/endpoints/app/create.ts @@ -44,6 +44,7 @@ export default async (params: any, user: ILocalUser) => new Promise(async (res, // Response res(await pack(app, null, { + detail: true, includeSecret: true })); }); diff --git a/src/server/api/endpoints/app/show.ts b/src/server/api/endpoints/app/show.ts index 072fbaeb79..8cc2abdc28 100644 --- a/src/server/api/endpoints/app/show.ts +++ b/src/server/api/endpoints/app/show.ts @@ -21,6 +21,7 @@ export default (params: any, user: ILocalUser, app: IApp) => new Promise(async ( // Send response res(await pack(ap, user, { + detail: true, includeSecret: isSecure && ap.userId.equals(user._id) })); }); diff --git a/src/server/api/endpoints/i/authorized_apps.ts b/src/server/api/endpoints/i/authorized_apps.ts index 313bb474f4..9c15670d17 100644 --- a/src/server/api/endpoints/i/authorized_apps.ts +++ b/src/server/api/endpoints/i/authorized_apps.ts @@ -34,6 +34,7 @@ export default (params: any, user: ILocalUser) => new Promise(async (res, rej) = }); // Serialize - res(await Promise.all(tokens.map(async token => - await pack(token.appId)))); + res(await Promise.all(tokens.map(token => pack(token.appId, user, { + detail: true + })))); }); diff --git a/src/server/api/endpoints/my/apps.ts b/src/server/api/endpoints/my/apps.ts index 412dff6164..30c08aa09c 100644 --- a/src/server/api/endpoints/my/apps.ts +++ b/src/server/api/endpoints/my/apps.ts @@ -35,6 +35,7 @@ export default (params: any, user: ILocalUser) => new Promise(async (res, rej) = }); // Reply - res(await Promise.all(apps.map(async app => - await pack(app)))); + res(await Promise.all(apps.map(app => pack(app, user, { + detail: true + })))); });