2021-03-21 08:14:03 +02:00
|
|
|
import { publishMainStream, publishUserEvent } from '../stream';
|
2019-01-30 19:29:36 +02:00
|
|
|
import { renderActivity } from '../../remote/activitypub/renderer';
|
2018-04-04 19:22:41 +03:00
|
|
|
import renderFollow from '../../remote/activitypub/renderer/follow';
|
|
|
|
import renderUndo from '../../remote/activitypub/renderer/undo';
|
2018-04-05 17:24:51 +03:00
|
|
|
import { deliver } from '../../queue';
|
2019-03-02 11:51:59 +02:00
|
|
|
import Logger from '../logger';
|
2019-02-08 09:58:57 +02:00
|
|
|
import { registerOrFetchInstanceDoc } from '../register-or-fetch-instance-doc';
|
2019-04-07 15:50:36 +03:00
|
|
|
import { User } from '../../models/entities/user';
|
|
|
|
import { Followings, Users, Instances } from '../../models';
|
|
|
|
import { instanceChart, perUserFollowingChart } from '../chart';
|
2019-02-03 11:16:57 +02:00
|
|
|
|
|
|
|
const logger = new Logger('following/delete');
|
2018-04-04 19:22:41 +03:00
|
|
|
|
2019-04-07 15:50:36 +03: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 19:22:41 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
if (following == null) {
|
2019-02-03 11:16:57 +02:00
|
|
|
logger.warn('フォロー解除がリクエストされましたがフォローしていませんでした');
|
2018-04-04 19:22:41 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-06-21 22:17:02 +03:00
|
|
|
await Followings.delete(following.id);
|
2018-04-04 19:22:41 +03:00
|
|
|
|
2019-11-09 11:24:41 +02:00
|
|
|
decrementFollowing(follower, followee);
|
|
|
|
|
|
|
|
// Publish unfollow event
|
|
|
|
if (!silent && Users.isLocalUser(follower)) {
|
|
|
|
Users.pack(followee, follower, {
|
|
|
|
detail: true
|
2021-03-21 08:14:03 +02:00
|
|
|
}).then(packed => {
|
|
|
|
publishUserEvent(follower.id, 'unfollow', packed);
|
|
|
|
publishMainStream(follower.id, 'unfollow', packed);
|
|
|
|
});
|
2019-11-09 11:24:41 +02: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 19:22:41 +03:00
|
|
|
//#region Decrement following count
|
2019-04-07 15:50:36 +03:00
|
|
|
Users.decrement({ id: follower.id }, 'followingCount', 1);
|
2018-04-04 19:22:41 +03:00
|
|
|
//#endregion
|
|
|
|
|
|
|
|
//#region Decrement followers count
|
2019-04-07 15:50:36 +03:00
|
|
|
Users.decrement({ id: followee.id }, 'followersCount', 1);
|
2018-04-04 19:22:41 +03:00
|
|
|
//#endregion
|
|
|
|
|
2019-02-08 09:58:57 +02:00
|
|
|
//#region Update instance stats
|
2019-04-07 15:50:36 +03:00
|
|
|
if (Users.isRemoteUser(follower) && Users.isLocalUser(followee)) {
|
2019-02-08 09:58:57 +02:00
|
|
|
registerOrFetchInstanceDoc(follower.host).then(i => {
|
2019-04-07 15:50:36 +03:00
|
|
|
Instances.decrement({ id: i.id }, 'followingCount', 1);
|
2019-02-08 09:58:57 +02:00
|
|
|
instanceChart.updateFollowing(i.host, false);
|
|
|
|
});
|
2019-04-07 15:50:36 +03:00
|
|
|
} else if (Users.isLocalUser(follower) && Users.isRemoteUser(followee)) {
|
2019-02-08 09:58:57 +02:00
|
|
|
registerOrFetchInstanceDoc(followee.host).then(i => {
|
2019-04-07 15:50:36 +03:00
|
|
|
Instances.decrement({ id: i.id }, 'followersCount', 1);
|
2019-02-08 09:58:57 +02:00
|
|
|
instanceChart.updateFollowers(i.host, false);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
//#endregion
|
|
|
|
|
2018-10-22 23:36:35 +03:00
|
|
|
perUserFollowingChart.update(follower, followee, false);
|
2018-04-04 19:22:41 +03:00
|
|
|
}
|