2018-04-02 13:50:40 +03: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';
|
2018-04-03 06:07:29 +03:00
|
|
|
import context from '../renderer/context';
|
|
|
|
import renderAccept from '../renderer/accept';
|
|
|
|
import request from '../../request';
|
2018-04-02 13:50:40 +03:00
|
|
|
|
|
|
|
export default async (actor, activity) => {
|
|
|
|
const prefix = config.url + '/@';
|
|
|
|
const id = activity.object.id || activity.object;
|
|
|
|
|
|
|
|
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-03 06:07:29 +03:00
|
|
|
const accept = renderAccept(activity);
|
|
|
|
accept['@context'] = context;
|
|
|
|
|
|
|
|
await Promise.all([
|
|
|
|
request(followee, actor.account.inbox, accept),
|
|
|
|
|
|
|
|
Following.insert({
|
2018-04-02 13:50:40 +03:00
|
|
|
createdAt: new Date(),
|
|
|
|
followerId: actor._id,
|
|
|
|
followeeId: followee._id
|
2018-04-03 06:07:29 +03:00
|
|
|
}).then(following => new Promise((resolve, reject) => {
|
|
|
|
queue.create('http', { type: 'follow', following: following._id }).save(error => {
|
|
|
|
if (error) {
|
|
|
|
reject(error);
|
|
|
|
} else {
|
|
|
|
resolve();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}), error => {
|
|
|
|
// duplicate key error
|
|
|
|
if (error instanceof MongoError && error.code === 11000) {
|
|
|
|
return;
|
2018-04-02 13:50:40 +03:00
|
|
|
}
|
2018-04-03 06:07:29 +03:00
|
|
|
|
|
|
|
throw error;
|
|
|
|
})
|
|
|
|
]);
|
|
|
|
|
|
|
|
return null;
|
2018-04-02 13:50:40 +03:00
|
|
|
};
|