misskey/src/models/reversi-matching.ts

45 lines
1.0 KiB
TypeScript
Raw Normal View History

2018-03-07 03:40:40 +01:00
import * as mongo from 'mongodb';
2018-05-26 09:04:47 +02:00
import * as deepcopy from 'deepcopy';
2018-03-29 13:32:18 +02:00
import db from '../db/mongodb';
2018-03-07 09:48:32 +01:00
import { IUser, pack as packUser } from './user';
2018-03-07 03:40:40 +01:00
2018-06-17 01:10:54 +02:00
const Matching = db.get<IMatching>('reversiMatchings');
2018-03-07 03:40:40 +01:00
export default Matching;
export interface IMatching {
_id: mongo.ObjectID;
2018-03-29 07:48:47 +02:00
createdAt: Date;
parentId: mongo.ObjectID;
childId: mongo.ObjectID;
2018-03-07 03:40:40 +01:00
}
2018-03-07 09:48:32 +01:00
/**
2018-06-17 01:10:54 +02:00
* Pack an reversi matching for API response
2018-03-07 09:48:32 +01:00
*/
export const pack = (
matching: any,
me?: string | mongo.ObjectID | IUser
) => new Promise<any>(async (resolve, reject) => {
// Me
const meId: mongo.ObjectID = me
? mongo.ObjectID.prototype.isPrototypeOf(me)
? me as mongo.ObjectID
: typeof me === 'string'
? new mongo.ObjectID(me)
: (me as IUser)._id
: null;
const _matching = deepcopy(matching);
2018-03-11 10:08:26 +01:00
// Rename _id to id
_matching.id = _matching._id;
2018-03-07 09:48:32 +01:00
delete _matching._id;
// Populate user
2018-03-29 07:48:47 +02:00
_matching.parent = await packUser(_matching.parentId, meId);
_matching.child = await packUser(_matching.childId, meId);
2018-03-07 09:48:32 +01:00
resolve(_matching);
});