2021-03-21 07:14:03 +01:00
|
|
|
import { publishMainStream, publishUserEvent } from '../stream';
|
2019-01-30 18:29:36 +01:00
|
|
|
import { renderActivity } from '../../remote/activitypub/renderer';
|
2018-04-04 18:22:41 +02:00
|
|
|
import renderFollow from '../../remote/activitypub/renderer/follow';
|
|
|
|
import renderUndo from '../../remote/activitypub/renderer/undo';
|
2018-04-05 16:24:51 +02:00
|
|
|
import { deliver } from '../../queue';
|
2019-03-02 10:51:59 +01:00
|
|
|
import Logger from '../logger';
|
2019-02-08 08:58:57 +01:00
|
|
|
import { registerOrFetchInstanceDoc } from '../register-or-fetch-instance-doc';
|
2019-04-07 14:50:36 +02:00
|
|
|
import { User } from '../../models/entities/user';
|
|
|
|
import { Followings, Users, Instances } from '../../models';
|
|
|
|
import { instanceChart, perUserFollowingChart } from '../chart';
|
2019-02-03 10:16:57 +01:00
|
|
|
|
|
|
|
const logger = new Logger('following/delete');
|
2018-04-04 18:22:41 +02:00
|
|
|
|
2019-04-07 14:50:36 +02:00
|
|
|
export default async function(follower: User, followee: User, silent = false) {
|
|
|
|
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)) {
|
|
|
|
Users.pack(followee, follower, {
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export async function decrementFollowing(follower: User, followee: User) {
|
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
|
|
|
}
|