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
1 changed files with 68 additions and 36 deletions

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,11 +372,13 @@ 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 });
// Neither followee nor follower has moved.
if (!follower.movedToUri && !followee.movedToUri) {
//#region Decrement following / followers counts //#region Decrement following / followers counts
await Promise.all([ await Promise.all([
this.usersRepository.decrement({ id: follower.id }, 'followingCount', 1), this.usersRepository.decrement({ id: follower.id }, 'followingCount', 1),
@ -405,6 +405,41 @@ export class UserFollowingService implements OnModuleInit {
//#endregion //#endregion
this.perUserFollowingChart.update(follower, followee, false); 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
}
} }
@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);
}
} }
/** /**