This commit is contained in:
syuilo
2018-06-02 00:51:20 +09:00
parent f14571dc42
commit a26c19cbd2
5 changed files with 97 additions and 1 deletions

View File

@@ -1,5 +1,7 @@
import * as mongo from 'mongodb';
import * as deepcopy from 'deepcopy';
import db from '../db/mongodb';
import { pack as packUser } from './user';
const FollowRequest = db.get<IFollowRequest>('followRequests');
FollowRequest.createIndex(['followerId', 'followeeId'], { unique: true });
@@ -48,3 +50,38 @@ export async function deleteFollowRequest(followRequest: string | mongo.ObjectID
_id: f._id
});
}
/**
* Pack a request for API response
*/
export const pack = (
request: any,
me?: any
) => new Promise<any>(async (resolve, reject) => {
let _request: any;
// Populate the request if 'request' is ID
if (mongo.ObjectID.prototype.isPrototypeOf(request)) {
_request = await FollowRequest.findOne({
_id: request
});
} else if (typeof request === 'string') {
_request = await FollowRequest.findOne({
_id: new mongo.ObjectID(request)
});
} else {
_request = deepcopy(request);
}
// Rename _id to id
_request.id = _request._id;
delete _request._id;
// Populate follower
_request.followerId = await packUser(_request.followerId, me);
// Populate followee
_request.followeeId = await packUser(_request.followeeId, me);
resolve(_request);
});