1
0
mirror of https://github.com/misskey-dev/misskey.git synced 2026-05-03 19:36:25 +02:00

Implement unfollow by remote account

This commit is contained in:
Akihiko Odaki
2018-04-03 16:33:16 +09:00
parent 1a347ae9a0
commit 6b66ec1231
6 changed files with 108 additions and 37 deletions

View File

@@ -1,6 +1,6 @@
import { MongoError } from 'mongodb';
import parseAcct from '../../../acct/parse';
import Following from '../../../models/following';
import Following, { IFollowing } from '../../../models/following';
import User from '../../../models/user';
import config from '../../../config';
import queue from '../../../queue';
@@ -8,7 +8,7 @@ import context from '../renderer/context';
import renderAccept from '../renderer/accept';
import request from '../../request';
export default async (actor, activity) => {
export default async (resolver, actor, activity, distribute) => {
const prefix = config.url + '/@';
const id = activity.object.id || activity.object;
@@ -26,33 +26,52 @@ export default async (actor, activity) => {
throw new Error();
}
if (!distribute) {
const { _id } = await Following.findOne({
followerId: actor._id,
followeeId: followee._id
})
return {
resolver,
object: { $ref: 'following', $id: _id }
};
}
const promisedFollowing = Following.insert({
createdAt: new Date(),
followerId: actor._id,
followeeId: followee._id
}).then(following => new Promise((resolve, reject) => {
queue.create('http', {
type: 'follow',
following: following._id
}).save(error => {
if (error) {
reject(error);
} else {
resolve(following);
}
});
}) as Promise<IFollowing>, async error => {
// duplicate key error
if (error instanceof MongoError && error.code === 11000) {
return Following.findOne({
followerId: actor._id,
followeeId: followee._id
});
}
throw error;
});
const accept = renderAccept(activity);
accept['@context'] = context;
await Promise.all([
request(followee, actor.account.inbox, accept),
await request(followee, actor.account.inbox, accept);
Following.insert({
createdAt: new Date(),
followerId: actor._id,
followeeId: followee._id
}).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;
return promisedFollowing.then(({ _id }) => ({
resolver,
object: { $ref: 'following', $id: _id }
}));
};