mizzkey/src/remote/activitypub/act/follow.ts

27 lines
732 B
TypeScript
Raw Normal View History

2018-04-02 12:50:40 +02:00
import parseAcct from '../../../acct/parse';
2018-04-07 09:14:35 +02:00
import User, { IRemoteUser } from '../../../models/user';
2018-04-02 12:50:40 +02:00
import config from '../../../config';
2018-04-05 20:10:25 +02:00
import follow from '../../../services/following/create';
2018-04-07 09:14:35 +02:00
import { IFollow } from '../type';
2018-04-02 12:50:40 +02:00
2018-04-07 09:14:35 +02:00
export default async (actor: IRemoteUser, activity: IFollow): Promise<void> => {
2018-04-02 12:50:40 +02:00
const prefix = config.url + '/@';
2018-04-07 09:14:35 +02:00
const id = typeof activity == 'string' ? activity : activity.id;
2018-04-02 12:50:40 +02:00
if (!id.startsWith(prefix)) {
return null;
}
const { username, host } = parseAcct(id.slice(prefix.length));
if (host !== null) {
throw new Error();
}
const followee = await User.findOne({ username, host });
if (followee === null) {
throw new Error();
}
2018-04-04 16:59:38 +02:00
await follow(actor, followee, activity);
2018-04-02 12:50:40 +02:00
};