1
0
mirror of https://github.com/misskey-dev/misskey synced 2024-12-27 04:59:10 +09:00
misskey/src/remote/activitypub/act/follow.ts

52 lines
1.1 KiB
TypeScript
Raw Normal View History

2018-04-02 19:50:40 +09:00
import { MongoError } from 'mongodb';
import parseAcct from '../../../acct/parse';
import Following from '../../../models/following';
import User from '../../../models/user';
import config from '../../../config';
import queue from '../../../queue';
export default async (actor, activity) => {
const prefix = config.url + '/@';
const id = activity.object.id || activity.object;
let following;
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();
}
try {
following = await Following.insert({
createdAt: new Date(),
followerId: actor._id,
followeeId: followee._id
});
} catch (exception) {
// duplicate key error
if (exception instanceof MongoError && exception.code === 11000) {
return null;
}
throw exception;
}
await new Promise((resolve, reject) => {
queue.create('http', { type: 'follow', following: following._id }).save(error => {
if (error) {
reject(error);
} else {
resolve(null);
}
});
});
};