2018-05-31 11:08:47 +02:00
|
|
|
import * as mongo from 'mongodb';
|
2018-06-01 17:51:20 +02:00
|
|
|
import * as deepcopy from 'deepcopy';
|
2018-05-31 11:08:47 +02:00
|
|
|
import db from '../db/mongodb';
|
2018-06-01 17:51:20 +02:00
|
|
|
import { pack as packUser } from './user';
|
2018-05-31 11:08:47 +02:00
|
|
|
|
|
|
|
const FollowRequest = db.get<IFollowRequest>('followRequests');
|
|
|
|
FollowRequest.createIndex(['followerId', 'followeeId'], { unique: true });
|
|
|
|
export default FollowRequest;
|
|
|
|
|
|
|
|
export type IFollowRequest = {
|
|
|
|
_id: mongo.ObjectID;
|
|
|
|
createdAt: Date;
|
|
|
|
followeeId: mongo.ObjectID;
|
|
|
|
followerId: mongo.ObjectID;
|
|
|
|
|
|
|
|
// 非正規化
|
|
|
|
_followee: {
|
|
|
|
host: string;
|
|
|
|
inbox?: string;
|
|
|
|
},
|
|
|
|
_follower: {
|
|
|
|
host: string;
|
|
|
|
inbox?: string;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* FollowRequestを物理削除します
|
|
|
|
*/
|
|
|
|
export async function deleteFollowRequest(followRequest: string | mongo.ObjectID | IFollowRequest) {
|
|
|
|
let f: IFollowRequest;
|
|
|
|
|
|
|
|
// Populate
|
|
|
|
if (mongo.ObjectID.prototype.isPrototypeOf(followRequest)) {
|
|
|
|
f = await FollowRequest.findOne({
|
|
|
|
_id: followRequest
|
|
|
|
});
|
|
|
|
} else if (typeof followRequest === 'string') {
|
|
|
|
f = await FollowRequest.findOne({
|
|
|
|
_id: new mongo.ObjectID(followRequest)
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
f = followRequest as IFollowRequest;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (f == null) return;
|
|
|
|
|
|
|
|
// このFollowingを削除
|
|
|
|
await FollowRequest.remove({
|
|
|
|
_id: f._id
|
|
|
|
});
|
|
|
|
}
|
2018-06-01 17:51:20 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 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);
|
|
|
|
});
|