mirror of
https://github.com/misskey-dev/misskey.git
synced 2024-12-24 00:39:32 +09:00
adjust following and followers counts when unfollowing
This commit is contained in:
parent
83d70f3300
commit
6a2752ef73
@ -23,6 +23,7 @@ import { CacheService } from '@/core/CacheService.js';
|
||||
import type { Config } from '@/config.js';
|
||||
import Logger from '../logger.js';
|
||||
import { ApPersonService } from '@/core/activitypub/models/ApPersonService.js';
|
||||
import { IsNull } from 'typeorm';
|
||||
|
||||
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('フォロー解除がリクエストされましたがフォローしていませんでした');
|
||||
return;
|
||||
}
|
||||
@ -339,10 +340,7 @@ export class UserFollowingService implements OnModuleInit {
|
||||
|
||||
this.cacheService.userFollowingsCache.refresh(follower.id);
|
||||
|
||||
// Neither followee nor follower has moved.
|
||||
if (!following.followee?.movedToUri && !following.follower?.movedToUri) {
|
||||
this.decrementFollowing(follower, followee);
|
||||
}
|
||||
this.decrementFollowing(following.follower, following.followee);
|
||||
|
||||
// Publish unfollow event
|
||||
if (!silent && this.userEntityService.isLocalUser(follower)) {
|
||||
@ -374,37 +372,74 @@ export class UserFollowingService implements OnModuleInit {
|
||||
|
||||
@bindThis
|
||||
private async decrementFollowing(
|
||||
follower: { id: User['id']; host: User['host']; },
|
||||
followee: { id: User['id']; host: User['host']; },
|
||||
follower: User,
|
||||
followee: User,
|
||||
): Promise<void> {
|
||||
this.globalEventService.publishInternalEvent('unfollow', { followerId: follower.id, followeeId: followee.id });
|
||||
|
||||
//#region Decrement following / followers counts
|
||||
await Promise.all([
|
||||
this.usersRepository.decrement({ id: follower.id }, 'followingCount', 1),
|
||||
this.usersRepository.decrement({ id: followee.id }, 'followersCount', 1),
|
||||
]);
|
||||
//#endregion
|
||||
// Neither followee nor follower has moved.
|
||||
if (!follower.movedToUri && !followee.movedToUri) {
|
||||
//#region Decrement following / followers counts
|
||||
await Promise.all([
|
||||
this.usersRepository.decrement({ id: follower.id }, 'followingCount', 1),
|
||||
this.usersRepository.decrement({ id: followee.id }, 'followersCount', 1),
|
||||
]);
|
||||
//#endregion
|
||||
|
||||
//#region Update instance stats
|
||||
if (this.userEntityService.isRemoteUser(follower) && this.userEntityService.isLocalUser(followee)) {
|
||||
this.federatedInstanceService.fetch(follower.host).then(async i => {
|
||||
this.instancesRepository.decrement({ id: i.id }, 'followingCount', 1);
|
||||
if ((await this.metaService.fetch()).enableChartsForFederatedInstances) {
|
||||
this.instanceChart.updateFollowing(i.host, false);
|
||||
}
|
||||
});
|
||||
} else if (this.userEntityService.isLocalUser(follower) && this.userEntityService.isRemoteUser(followee)) {
|
||||
this.federatedInstanceService.fetch(followee.host).then(async i => {
|
||||
this.instancesRepository.decrement({ id: i.id }, 'followersCount', 1);
|
||||
if ((await this.metaService.fetch()).enableChartsForFederatedInstances) {
|
||||
this.instanceChart.updateFollowers(i.host, false);
|
||||
}
|
||||
});
|
||||
//#region Update instance stats
|
||||
if (this.userEntityService.isRemoteUser(follower) && this.userEntityService.isLocalUser(followee)) {
|
||||
this.federatedInstanceService.fetch(follower.host).then(async i => {
|
||||
this.instancesRepository.decrement({ id: i.id }, 'followingCount', 1);
|
||||
if ((await this.metaService.fetch()).enableChartsForFederatedInstances) {
|
||||
this.instanceChart.updateFollowing(i.host, false);
|
||||
}
|
||||
});
|
||||
} else if (this.userEntityService.isLocalUser(follower) && this.userEntityService.isRemoteUser(followee)) {
|
||||
this.federatedInstanceService.fetch(followee.host).then(async i => {
|
||||
this.instancesRepository.decrement({ id: i.id }, 'followersCount', 1);
|
||||
if ((await this.metaService.fetch()).enableChartsForFederatedInstances) {
|
||||
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
|
||||
@ -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);
|
||||
|
||||
// Neither followee nor follower has moved.
|
||||
if (!following.followee?.movedToUri && !following.follower?.movedToUri) {
|
||||
this.decrementFollowing(follower, followee);
|
||||
}
|
||||
this.decrementFollowing(following.follower, following.followee);
|
||||
}
|
||||
|
||||
/**
|
||||
|
Loading…
Reference in New Issue
Block a user