diff options
| author | syuilo <Syuilotan@yahoo.co.jp> | 2018-02-04 15:02:14 +0900 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2018-02-04 15:02:14 +0900 |
| commit | 744c3e2ef8ef89355f72262b1e8c8f2fbb020f2a (patch) | |
| tree | de3630065fcddeb1916668ef3b0b43a219340e2e /src/api/models/auth-session.ts | |
| parent | Update dependencies :rocket: (diff) | |
| parent | wip (diff) | |
| download | sharkey-744c3e2ef8ef89355f72262b1e8c8f2fbb020f2a.tar.gz sharkey-744c3e2ef8ef89355f72262b1e8c8f2fbb020f2a.tar.bz2 sharkey-744c3e2ef8ef89355f72262b1e8c8f2fbb020f2a.zip | |
Merge pull request #1097 from syuilo/refactor
Refactor
Diffstat (limited to 'src/api/models/auth-session.ts')
| -rw-r--r-- | src/api/models/auth-session.ts | 44 |
1 files changed, 43 insertions, 1 deletions
diff --git a/src/api/models/auth-session.ts b/src/api/models/auth-session.ts index b264a133e9..997ec61c20 100644 --- a/src/api/models/auth-session.ts +++ b/src/api/models/auth-session.ts @@ -1,3 +1,45 @@ +import * as mongo from 'mongodb'; +import deepcopy = require('deepcopy'); import db from '../../db/mongodb'; +import { pack as packApp } from './app'; -export default db.get('auth_sessions') as any; // fuck type definition +const AuthSession = db.get('auth_sessions'); +export default AuthSession; + +export interface IAuthSession { + _id: mongo.ObjectID; +} + +/** + * Pack an auth session for API response + * + * @param {any} session + * @param {any} me? + * @return {Promise<any>} + */ +export const pack = ( + session: any, + me?: any +) => new Promise<any>(async (resolve, reject) => { + let _session: any; + + // TODO: Populate session if it ID + + _session = deepcopy(session); + + // Me + if (me && !mongo.ObjectID.prototype.isPrototypeOf(me)) { + if (typeof me === 'string') { + me = new mongo.ObjectID(me); + } else { + me = me._id; + } + } + + delete _session._id; + + // Populate app + _session.app = await packApp(_session.app_id, me); + + resolve(_session); +}); |