adjust following and followers counts when unfollowing

This commit is contained in:
Namekuji 2023-04-15 03:37:26 -04:00
parent 83d70f3300
commit 6a2752ef73

View file

@ -23,6 +23,7 @@ import { CacheService } from '@/core/CacheService.js';
import type { Config } from '@/config.js'; import type { Config } from '@/config.js';
import Logger from '../logger.js'; import Logger from '../logger.js';
import { ApPersonService } from '@/core/activitypub/models/ApPersonService.js'; import { ApPersonService } from '@/core/activitypub/models/ApPersonService.js';
import { IsNull } from 'typeorm';
const logger = new Logger('following/create'); const logger = new Logger('following/create');
@ -330,7 +331,7 @@ export class UserFollowingService implements OnModuleInit {
} }
}); });
if (following === null) { if (following === null || !following.follower || !following.followee) {
logger.warn('フォロー解除がリクエストされましたがフォローしていませんでした'); logger.warn('フォロー解除がリクエストされましたがフォローしていませんでした');
return; return;
} }
@ -339,10 +340,7 @@ export class UserFollowingService implements OnModuleInit {
this.cacheService.userFollowingsCache.refresh(follower.id); this.cacheService.userFollowingsCache.refresh(follower.id);
// Neither followee nor follower has moved. this.decrementFollowing(following.follower, following.followee);
if (!following.followee?.movedToUri && !following.follower?.movedToUri) {
this.decrementFollowing(follower, followee);
}
// Publish unfollow event // Publish unfollow event
if (!silent && this.userEntityService.isLocalUser(follower)) { if (!silent && this.userEntityService.isLocalUser(follower)) {
@ -374,37 +372,74 @@ export class UserFollowingService implements OnModuleInit {
@bindThis @bindThis
private async decrementFollowing( private async decrementFollowing(
follower: { id: User['id']; host: User['host']; }, follower: User,
followee: { id: User['id']; host: User['host']; }, followee: User,
): Promise<void> { ): Promise<void> {
this.globalEventService.publishInternalEvent('unfollow', { followerId: follower.id, followeeId: followee.id }); this.globalEventService.publishInternalEvent('unfollow', { followerId: follower.id, followeeId: followee.id });
//#region Decrement following / followers counts // Neither followee nor follower has moved.
await Promise.all([ if (!follower.movedToUri && !followee.movedToUri) {
this.usersRepository.decrement({ id: follower.id }, 'followingCount', 1), //#region Decrement following / followers counts
this.usersRepository.decrement({ id: followee.id }, 'followersCount', 1), await Promise.all([
]); this.usersRepository.decrement({ id: follower.id }, 'followingCount', 1),
//#endregion this.usersRepository.decrement({ id: followee.id }, 'followersCount', 1),
]);
//#endregion
//#region Update instance stats //#region Update instance stats
if (this.userEntityService.isRemoteUser(follower) && this.userEntityService.isLocalUser(followee)) { if (this.userEntityService.isRemoteUser(follower) && this.userEntityService.isLocalUser(followee)) {
this.federatedInstanceService.fetch(follower.host).then(async i => { this.federatedInstanceService.fetch(follower.host).then(async i => {
this.instancesRepository.decrement({ id: i.id }, 'followingCount', 1); this.instancesRepository.decrement({ id: i.id }, 'followingCount', 1);
if ((await this.metaService.fetch()).enableChartsForFederatedInstances) { if ((await this.metaService.fetch()).enableChartsForFederatedInstances) {
this.instanceChart.updateFollowing(i.host, false); this.instanceChart.updateFollowing(i.host, false);
} }
}); });
} else if (this.userEntityService.isLocalUser(follower) && this.userEntityService.isRemoteUser(followee)) { } else if (this.userEntityService.isLocalUser(follower) && this.userEntityService.isRemoteUser(followee)) {
this.federatedInstanceService.fetch(followee.host).then(async i => { this.federatedInstanceService.fetch(followee.host).then(async i => {
this.instancesRepository.decrement({ id: i.id }, 'followersCount', 1); this.instancesRepository.decrement({ id: i.id }, 'followersCount', 1);
if ((await this.metaService.fetch()).enableChartsForFederatedInstances) { if ((await this.metaService.fetch()).enableChartsForFederatedInstances) {
this.instanceChart.updateFollowers(i.host, false); this.instanceChart.updateFollowers(i.host, false);
} }
}); });
}
//#endregion
this.perUserFollowingChart.update(follower, followee, false);
} else {
// Adjust following/followers counts
for (const user of [follower, followee]) {
if (user.movedToUri) continue; // No need to update if the user has already moved.
const nonMovedFollowees = await this.followingsRepository.count({
relations: {
followee: true,
},
where: {
followerId: user.id,
followee: {
movedToUri: IsNull(),
}
}
});
const nonMovedFollowers = await this.followingsRepository.count({
relations: {
follower: true,
},
where: {
followeeId: user.id,
follower: {
movedToUri: IsNull(),
}
}
});
await this.usersRepository.update(
{ id: user.id },
{ followingCount: nonMovedFollowees, followersCount: nonMovedFollowers },
);
}
// TODO: adjust charts
} }
//#endregion
this.perUserFollowingChart.update(follower, followee, false);
} }
@bindThis @bindThis
@ -618,14 +653,11 @@ export class UserFollowingService implements OnModuleInit {
} }
}); });
if (!following) return; if (!following || !following.followee || !following.follower) return;
await this.followingsRepository.delete(following.id); await this.followingsRepository.delete(following.id);
// Neither followee nor follower has moved. this.decrementFollowing(following.follower, following.followee);
if (!following.followee?.movedToUri && !following.follower?.movedToUri) {
this.decrementFollowing(follower, followee);
}
} }
/** /**