2018-05-31 04:08:47 -05:00
|
|
|
import * as mongo from 'mongodb';
|
2018-06-17 19:54:53 -05:00
|
|
|
const deepcopy = require('deepcopy');
|
2018-05-31 04:08:47 -05:00
|
|
|
import db from '../db/mongodb';
|
2018-10-15 21:38:09 -05:00
|
|
|
import isObjectId from '../misc/is-objectid';
|
2018-06-01 10:51:20 -05:00
|
|
|
import { pack as packUser } from './user';
|
2018-05-31 04:08:47 -05:00
|
|
|
|
|
|
|
const FollowRequest = db.get<IFollowRequest>('followRequests');
|
2018-10-29 07:53:40 -05:00
|
|
|
FollowRequest.createIndex('followerId');
|
|
|
|
FollowRequest.createIndex('followeeId');
|
2018-05-31 04:08:47 -05:00
|
|
|
FollowRequest.createIndex(['followerId', 'followeeId'], { unique: true });
|
|
|
|
export default FollowRequest;
|
|
|
|
|
|
|
|
export type IFollowRequest = {
|
|
|
|
_id: mongo.ObjectID;
|
|
|
|
createdAt: Date;
|
|
|
|
followeeId: mongo.ObjectID;
|
|
|
|
followerId: mongo.ObjectID;
|
2018-10-15 02:51:23 -05:00
|
|
|
requestId?: string; // id of Follow Activity
|
2018-05-31 04:08:47 -05:00
|
|
|
|
|
|
|
// 非正規化
|
|
|
|
_followee: {
|
|
|
|
host: string;
|
|
|
|
inbox?: string;
|
2018-07-21 05:33:56 -05:00
|
|
|
sharedInbox?: string;
|
2018-05-31 04:08:47 -05:00
|
|
|
},
|
|
|
|
_follower: {
|
|
|
|
host: string;
|
|
|
|
inbox?: string;
|
2018-07-21 05:33:56 -05:00
|
|
|
sharedInbox?: string;
|
2018-05-31 04:08:47 -05:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2018-06-01 10:51:20 -05: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
|
2018-10-15 21:38:09 -05:00
|
|
|
if (isObjectId(request)) {
|
2018-06-01 10:51:20 -05:00
|
|
|
_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
|
2018-06-01 23:34:53 -05:00
|
|
|
_request.follower = await packUser(_request.followerId, me);
|
2018-06-01 10:51:20 -05:00
|
|
|
|
|
|
|
// Populate followee
|
2018-06-01 23:34:53 -05:00
|
|
|
_request.followee = await packUser(_request.followeeId, me);
|
2018-06-01 10:51:20 -05:00
|
|
|
|
|
|
|
resolve(_request);
|
|
|
|
});
|