// SPDX-License-Identifier: AGPL-3.0-or-later import type {UserID} from '@app/api/BrandedTypes'; import {mapGuildMemberToResponse} from '@app/api/guild/GuildModel'; import type {IGuildRepositoryAggregate} from '@app/api/guild/repositories/IGuildRepositoryAggregate'; import type {IGatewayService} from '@app/api/infrastructure/IGatewayService'; import type {UserCacheService} from '@app/api/infrastructure/UserCacheService'; import {createRequestCache} from '@app/api/middleware/RequestCacheMiddleware'; import type {User} from '@app/api/models/User'; import type {IUserRepository} from '@app/api/user/IUserRepository'; import {BaseUserUpdatePropagator} from '@app/api/user/services/BaseUserUpdatePropagator'; import {hasPartialUserFieldsChanged} from '@app/api/user/UserMappers'; interface AdminUserUpdatePropagatorDeps { userCacheService: UserCacheService; userRepository: IUserRepository; guildRepository: IGuildRepositoryAggregate; gatewayService: IGatewayService; } export class AdminUserUpdatePropagator extends BaseUserUpdatePropagator { constructor(private readonly deps: AdminUserUpdatePropagatorDeps) { super({ userCacheService: deps.userCacheService, gatewayService: deps.gatewayService, }); } async propagateUserUpdate({ userId, oldUser, updatedUser, }: { userId: UserID; oldUser: User; updatedUser: User; }): Promise { await this.dispatchUserUpdate(updatedUser); if (hasPartialUserFieldsChanged(oldUser, updatedUser)) { await this.updateUserCache(updatedUser); await this.propagateToGuilds(userId); } } private async propagateToGuilds(userId: UserID): Promise { const {userRepository, guildRepository, gatewayService, userCacheService} = this.deps; const guildIds = await userRepository.getUserGuildIds(userId); if (guildIds.length === 0) { return; } const requestCache = createRequestCache(); for (const guildId of guildIds) { const member = await guildRepository.getMember(guildId, userId); if (!member) { continue; } const memberResponse = await mapGuildMemberToResponse(member, userCacheService, requestCache); await gatewayService.dispatchGuild({ guildId, event: 'GUILD_MEMBER_UPDATE', data: memberResponse, }); } requestCache.clear(); } }