Accept remote follow

This commit is contained in:
Akihiko Odaki 2018-04-03 12:07:29 +09:00
parent 1d62b15889
commit c8077538f2
2 changed files with 32 additions and 21 deletions

View File

@ -4,11 +4,13 @@ import Following from '../../../models/following';
import User from '../../../models/user';
import config from '../../../config';
import queue from '../../../queue';
import context from '../renderer/context';
import renderAccept from '../renderer/accept';
import request from '../../request';
export default async (actor, activity) => {
const prefix = config.url + '/@';
const id = activity.object.id || activity.object;
let following;
if (!id.startsWith(prefix)) {
return null;
@ -24,28 +26,33 @@ export default async (actor, activity) => {
throw new Error();
}
try {
following = await Following.insert({
const accept = renderAccept(activity);
accept['@context'] = context;
await Promise.all([
request(followee, actor.account.inbox, accept),
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);
}).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;
}
});
});
throw error;
})
]);
return null;
};

View File

@ -0,0 +1,4 @@
export default object => ({
type: 'Accept',
object
});