2018-06-01 15:51:20 +00:00
|
|
|
import $ from 'cafy'; import ID from '../../../../../cafy-id';
|
|
|
|
import cancelFollowRequest from '../../../../../services/following/requests/cancel';
|
2018-06-18 00:54:53 +00:00
|
|
|
import User, { pack, ILocalUser } from '../../../../../models/user';
|
2018-06-01 15:51:20 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Cancel a follow request
|
|
|
|
*/
|
2018-07-05 17:58:29 +00:00
|
|
|
export default (params: any, user: ILocalUser) => new Promise(async (res, rej) => {
|
2018-06-02 03:58:56 +00:00
|
|
|
// Get 'userId' parameter
|
|
|
|
const [followeeId, followeeIdErr] = $.type(ID).get(params.userId);
|
|
|
|
if (followeeIdErr) return rej('invalid userId param');
|
2018-06-01 15:51:20 +00:00
|
|
|
|
2018-06-02 03:58:56 +00:00
|
|
|
// Fetch followee
|
|
|
|
const followee = await User.findOne({
|
|
|
|
_id: followeeId
|
2018-06-01 15:51:20 +00:00
|
|
|
});
|
|
|
|
|
2018-06-02 03:58:56 +00:00
|
|
|
if (followee === null) {
|
|
|
|
return rej('followee not found');
|
2018-06-01 15:51:20 +00:00
|
|
|
}
|
|
|
|
|
2018-06-02 03:58:56 +00:00
|
|
|
await cancelFollowRequest(followee, user);
|
2018-06-01 15:51:20 +00:00
|
|
|
|
|
|
|
// Send response
|
2018-06-02 03:58:56 +00:00
|
|
|
res(await pack(followee._id, user));
|
2018-06-01 15:51:20 +00:00
|
|
|
});
|