This commit is contained in:
syuilo 2018-11-01 09:19:22 +09:00
parent 325cd03a59
commit 1fca8d322c
No known key found for this signature in database
GPG key ID: BDC4C49D06AB9D69
6 changed files with 27 additions and 39 deletions

View file

@ -22,27 +22,28 @@ export type IApp = {
/** /**
* Pack an app for API response * Pack an app for API response
*
* @param {any} app
* @param {any} me?
* @param {any} options?
* @return {Promise<any>}
*/ */
export const pack = ( export const pack = (
app: any, app: any,
me?: any, me?: any,
options?: { options?: {
detail?: boolean,
includeSecret?: boolean, includeSecret?: boolean,
includeProfileImageIds?: boolean includeProfileImageIds?: boolean
} }
) => new Promise<any>(async (resolve, reject) => { ) => new Promise<any>(async (resolve, reject) => {
const opts = options || { const opts = Object.assign({
detail: false,
includeSecret: false, includeSecret: false,
includeProfileImageIds: false includeProfileImageIds: false
}; }, options);
let _app: any; let _app: any;
const fields = opts.detail ? {} : {
name: true
};
// Populate the app if 'app' is ID // Populate the app if 'app' is ID
if (isObjectId(app)) { if (isObjectId(app)) {
_app = await App.findOne({ _app = await App.findOne({
@ -51,7 +52,7 @@ export const pack = (
} else if (typeof app === 'string') { } else if (typeof app === 'string') {
_app = await App.findOne({ _app = await App.findOne({
_id: new mongo.ObjectID(app) _id: new mongo.ObjectID(app)
}); }, { fields });
} else { } else {
_app = deepcopy(app); _app = deepcopy(app);
} }

View file

@ -223,33 +223,16 @@ export const pack = (
let _user: any; let _user: any;
const fields = opts.detail ? { const fields = opts.detail ? {} : {
} : { name: true,
usernameLower: false, username: true,
bannerColor: false, host: true,
bannerUrl: false, avatarColor: true,
description: false, avatarUrl: true,
notesCount: false, isCat: true,
followersCount: false, isBot: true,
followingCount: false, isAdmin: true,
lastUsedAt: false, isVerified: true
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
}; };
// Populate the user if 'user' is ID // Populate the user if 'user' is ID

View file

@ -44,6 +44,7 @@ export default async (params: any, user: ILocalUser) => new Promise(async (res,
// Response // Response
res(await pack(app, null, { res(await pack(app, null, {
detail: true,
includeSecret: true includeSecret: true
})); }));
}); });

View file

@ -21,6 +21,7 @@ export default (params: any, user: ILocalUser, app: IApp) => new Promise(async (
// Send response // Send response
res(await pack(ap, user, { res(await pack(ap, user, {
detail: true,
includeSecret: isSecure && ap.userId.equals(user._id) includeSecret: isSecure && ap.userId.equals(user._id)
})); }));
}); });

View file

@ -34,6 +34,7 @@ export default (params: any, user: ILocalUser) => new Promise(async (res, rej) =
}); });
// Serialize // Serialize
res(await Promise.all(tokens.map(async token => res(await Promise.all(tokens.map(token => pack(token.appId, user, {
await pack(token.appId)))); detail: true
}))));
}); });

View file

@ -35,6 +35,7 @@ export default (params: any, user: ILocalUser) => new Promise(async (res, rej) =
}); });
// Reply // Reply
res(await Promise.all(apps.map(async app => res(await Promise.all(apps.map(app => pack(app, user, {
await pack(app)))); detail: true
}))));
}); });