2021-08-19 14:55:45 +02:00
|
|
|
import { publishMainStream, publishUserEvent } from '@/services/stream';
|
|
|
|
import { renderActivity } from '@/remote/activitypub/renderer/index';
|
|
|
|
import renderFollow from '@/remote/activitypub/renderer/follow';
|
|
|
|
import renderUndo from '@/remote/activitypub/renderer/undo';
|
2021-12-03 03:14:44 +01:00
|
|
|
import renderReject from '@/remote/activitypub/renderer/reject';
|
2021-08-19 14:55:45 +02:00
|
|
|
import { deliver } from '@/queue/index';
|
|
|
|
import Logger from '../logger';
|
|
|
|
import { registerOrFetchInstanceDoc } from '../register-or-fetch-instance-doc';
|
|
|
|
import { User } from '@/models/entities/user';
|
|
|
|
import { Followings, Users, Instances } from '@/models/index';
|
|
|
|
import { instanceChart, perUserFollowingChart } from '@/services/chart/index';
|
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) {
|
2019-04-07 14:50:36 +02:00
|
|
|
const following = await Followings.findOne({
|
|
|
|
followerId: follower.id,
|
|
|
|
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, {
|
2019-11-09 10:24:41 +01:00
|
|
|
detail: true
|
2021-03-21 07:14:03 +01:00
|
|
|
}).then(packed => {
|
|
|
|
publishUserEvent(follower.id, 'unfollow', packed);
|
|
|
|
publishMainStream(follower.id, 'unfollow', packed);
|
|
|
|
});
|
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']; }) {
|
2018-04-04 18:22:41 +02:00
|
|
|
//#region Decrement following count
|
2019-04-07 14:50:36 +02:00
|
|
|
Users.decrement({ id: follower.id }, 'followingCount', 1);
|
2018-04-04 18:22:41 +02:00
|
|
|
//#endregion
|
|
|
|
|
|
|
|
//#region Decrement followers count
|
2019-04-07 14:50:36 +02:00
|
|
|
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
|
|
|
}
|