39 lines
1.1 KiB
TypeScript
Raw Normal View History

2018-04-18 14:09:55 +09:00
import * as mongo from 'mongodb';
2018-04-07 16:14:35 +09:00
import User, { IRemoteUser } from '../../../../models/user';
2018-04-07 15:54:11 +09:00
import config from '../../../../config';
import unfollow from '../../../../services/following/delete';
2018-06-02 00:38:31 +09:00
import cancelRequest from '../../../../services/following/requests/cancel';
2018-04-07 16:14:35 +09:00
import { IFollow } from '../../type';
2018-06-02 00:38:31 +09:00
import FollowRequest from '../../../../models/follow-request';
2018-04-07 15:54:11 +09:00
2018-04-07 16:14:35 +09:00
export default async (actor: IRemoteUser, activity: IFollow): Promise<void> => {
2018-04-08 15:15:22 +09:00
const id = typeof activity.object == 'string' ? activity.object : activity.object.id;
2018-04-07 15:54:11 +09:00
2018-04-08 15:15:22 +09:00
if (!id.startsWith(config.url + '/')) {
2018-04-07 15:54:11 +09:00
return null;
}
2018-04-18 14:09:55 +09:00
const followee = await User.findOne({
_id: new mongo.ObjectID(id.split('/').pop())
});
2018-04-07 15:54:11 +09:00
if (followee === null) {
2018-04-08 15:15:22 +09:00
throw new Error('followee not found');
}
if (followee.host != null) {
throw new Error('フォロー解除しようとしているユーザーはローカルユーザーではありません');
2018-04-07 15:54:11 +09:00
}
2018-06-02 00:38:31 +09:00
const req = await FollowRequest.findOne({
followerId: actor._id,
followeeId: followee._id
});
if (req) {
await cancelRequest(followee, actor);
2018-06-02 00:38:31 +09:00
} else {
await unfollow(actor, followee);
}
2018-04-07 15:54:11 +09:00
};