2022-02-27 03:07:39 +01:00
|
|
|
import { publishMainStream, publishUserEvent } from '@/services/stream.js';
|
|
|
|
import { renderActivity } from '@/remote/activitypub/renderer/index.js';
|
|
|
|
import renderFollow from '@/remote/activitypub/renderer/follow.js';
|
|
|
|
import renderUndo from '@/remote/activitypub/renderer/undo.js';
|
|
|
|
import renderReject from '@/remote/activitypub/renderer/reject.js';
|
2022-04-02 08:28:49 +02:00
|
|
|
import { deliver, webhookDeliver } from '@/queue/index.js';
|
2022-02-27 03:07:39 +01:00
|
|
|
import Logger from '../logger.js';
|
|
|
|
import { registerOrFetchInstanceDoc } from '../register-or-fetch-instance-doc.js';
|
|
|
|
import { User } from '@/models/entities/user.js';
|
|
|
|
import { Followings, Users, Instances } from '@/models/index.js';
|
|
|
|
import { instanceChart, perUserFollowingChart } from '@/services/chart/index.js';
|
2022-04-02 08:28:49 +02:00
|
|
|
import { getActiveWebhooks } from '@/misc/webhook-cache.js';
|
2019-02-03 10:16:57 +01:00
|
|
|
|
|
|
|
const logger = new Logger('following/delete');
|
2018-04-04 18:22:41 +02:00
|
|
|
|
2021-03-24 03:05:37 +01:00
|
|
|
export default async function(follower: { id: User['id']; host: User['host']; uri: User['host']; inbox: User['inbox']; sharedInbox: User['sharedInbox']; }, followee: { id: User['id']; host: User['host']; uri: User['host']; inbox: User['inbox']; sharedInbox: User['sharedInbox']; }, silent = false) {
|
2022-03-26 07:34:00 +01:00
|
|
|
const following = await Followings.findOneBy({
|
2019-04-07 14:50:36 +02:00
|
|
|
followerId: follower.id,
|
2021-12-09 15:58:30 +01:00
|
|
|
followeeId: followee.id,
|
2018-04-04 18:22:41 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
if (following == null) {
|
2019-02-03 10:16:57 +01:00
|
|
|
logger.warn('フォロー解除がリクエストされましたがフォローしていませんでした');
|
2018-04-04 18:22:41 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-06-21 21:17:02 +02:00
|
|
|
await Followings.delete(following.id);
|
2018-04-04 18:22:41 +02:00
|
|
|
|
2019-11-09 10:24:41 +01:00
|
|
|
decrementFollowing(follower, followee);
|
|
|
|
|
|
|
|
// Publish unfollow event
|
|
|
|
if (!silent && Users.isLocalUser(follower)) {
|
2021-03-24 03:05:37 +01:00
|
|
|
Users.pack(followee.id, follower, {
|
2021-12-09 15:58:30 +01:00
|
|
|
detail: true,
|
2022-04-02 08:28:49 +02:00
|
|
|
}).then(async packed => {
|
2021-03-21 07:14:03 +01:00
|
|
|
publishUserEvent(follower.id, 'unfollow', packed);
|
|
|
|
publishMainStream(follower.id, 'unfollow', packed);
|
2022-04-02 08:28:49 +02:00
|
|
|
|
|
|
|
const webhooks = (await getActiveWebhooks()).filter(x => x.userId === follower.id && x.on.includes('unfollow'));
|
|
|
|
for (const webhook of webhooks) {
|
2022-04-03 15:36:30 +02:00
|
|
|
webhookDeliver(webhook, 'unfollow', {
|
2022-04-02 08:28:49 +02:00
|
|
|
user: packed,
|
|
|
|
});
|
|
|
|
}
|
2021-03-21 07:14:03 +01:00
|
|
|
});
|
2019-11-09 10:24:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (Users.isLocalUser(follower) && Users.isRemoteUser(followee)) {
|
|
|
|
const content = renderActivity(renderUndo(renderFollow(follower, followee), follower));
|
|
|
|
deliver(follower, content, followee.inbox);
|
|
|
|
}
|
2021-12-03 03:14:44 +01:00
|
|
|
|
|
|
|
if (Users.isLocalUser(followee) && Users.isRemoteUser(follower)) {
|
|
|
|
// local user has null host
|
|
|
|
const content = renderActivity(renderReject(renderFollow(follower, followee), followee));
|
|
|
|
deliver(followee, content, follower.inbox);
|
|
|
|
}
|
2019-11-09 10:24:41 +01:00
|
|
|
}
|
|
|
|
|
2021-03-24 03:05:37 +01:00
|
|
|
export async function decrementFollowing(follower: { id: User['id']; host: User['host']; }, followee: { id: User['id']; host: User['host']; }) {
|
2022-04-19 11:46:41 +02:00
|
|
|
//#region Decrement following / followers counts
|
|
|
|
await Promises.all([
|
|
|
|
Users.decrement({ id: follower.id }, 'followingCount', 1),
|
|
|
|
Users.decrement({ id: followee.id }, 'followersCount', 1),
|
|
|
|
]);
|
2018-04-04 18:22:41 +02:00
|
|
|
//#endregion
|
|
|
|
|
2019-02-08 08:58:57 +01:00
|
|
|
//#region Update instance stats
|
2019-04-07 14:50:36 +02:00
|
|
|
if (Users.isRemoteUser(follower) && Users.isLocalUser(followee)) {
|
2019-02-08 08:58:57 +01:00
|
|
|
registerOrFetchInstanceDoc(follower.host).then(i => {
|
2019-04-07 14:50:36 +02:00
|
|
|
Instances.decrement({ id: i.id }, 'followingCount', 1);
|
2019-02-08 08:58:57 +01:00
|
|
|
instanceChart.updateFollowing(i.host, false);
|
|
|
|
});
|
2019-04-07 14:50:36 +02:00
|
|
|
} else if (Users.isLocalUser(follower) && Users.isRemoteUser(followee)) {
|
2019-02-08 08:58:57 +01:00
|
|
|
registerOrFetchInstanceDoc(followee.host).then(i => {
|
2019-04-07 14:50:36 +02:00
|
|
|
Instances.decrement({ id: i.id }, 'followersCount', 1);
|
2019-02-08 08:58:57 +01:00
|
|
|
instanceChart.updateFollowers(i.host, false);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
//#endregion
|
|
|
|
|
2018-10-22 22:36:35 +02:00
|
|
|
perUserFollowingChart.update(follower, followee, false);
|
2018-04-04 18:22:41 +02:00
|
|
|
}
|