Skip to content

Commit

Permalink
feat(UserRepository): retain username after deleting account
Browse files Browse the repository at this point in the history
  • Loading branch information
olafsulich committed Nov 13, 2024
1 parent 71d6bcc commit d6125e5
Showing 1 changed file with 27 additions and 15 deletions.
42 changes: 27 additions & 15 deletions src/script/user/UserRepository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -217,18 +217,6 @@ export class UserRepository extends TypedEventEmitter<Events> {
const nonQualifiedUsers = await this.userService.clearNonQualifiedUsers();

const dbUsers = await this.userService.loadUserFromDb();
/* prior to April 2023, we were only storing the availability in the DB, we need to refetch those users */
const [localUsers] = partition(dbUsers, user => !!user.qualified_id);

// We can remove users that are not linked to any "known users" from the local database.
// Known users are the users that are part of the conversations or the connections (or some extra users)
const orphanUsers = localUsers.filter(
localUser => !users.find(user => matchQualifiedIds(user, localUser.qualified_id)),
);

for (const orphanUser of orphanUsers) {
await this.userService.removeUserFromDb(orphanUser.qualified_id);
}

// The self user doesn't need to be re-fetched
const usersToFetch = users.filter(user => !matchQualifiedIds(selfUser.qualifiedId, user));
Expand Down Expand Up @@ -586,7 +574,26 @@ export class UserRepository extends TypedEventEmitter<Events> {
);
}

private mapUserResponse(found: APIClientUser[], failed: QualifiedId[], dbUsers?: UserRecord[]): User[] {
private replaceDefaultUserNames(apiUsers: APIClientUser[], dbUsers: UserRecord[]): UserRecord[] {
return apiUsers.map(user => {
if (user.name !== 'default') {
return user;
}

const localUser = dbUsers.find(dbUser => dbUser.id === user.id);

if (localUser) {
return {
...user,
name: localUser.name,
};
}

return user;
});
}

private mapUserResponse(found: APIClientUser[], failed: QualifiedId[], dbUsers: UserRecord[]): User[] {
const selfUser = this.userState.self();

if (!selfUser) {
Expand All @@ -607,7 +614,10 @@ export class UserRepository extends TypedEventEmitter<Events> {
return new User(userId.id, userId.domain);
});

const mappedUsers = this.userMapper.mapUsersFromJson(found, selfDomain).concat(failedToLoad);
const users = this.replaceDefaultUserNames(found, dbUsers);

const mappedUsers = this.userMapper.mapUsersFromJson(users, selfDomain).concat(failedToLoad);

if (this.teamState.isTeam()) {
this.mapGuestStatus(mappedUsers);
}
Expand All @@ -622,7 +632,9 @@ export class UserRepository extends TypedEventEmitter<Events> {
*/
private async fetchUsers(userIds: QualifiedId[]): Promise<User[]> {
const {found, failed} = await this.fetchRawUsers(userIds, this.userState.self().domain);
const users = this.mapUserResponse(found, failed);
const dbUsers = await this.userService.loadUserFromDb();
const users = this.mapUserResponse(found, failed, dbUsers);

let fetchedUserEntities = this.saveUsers(users);
// If there is a difference then we most likely have a case with a suspended user
const isAllUserIds = userIds.length === fetchedUserEntities.length;
Expand Down

0 comments on commit d6125e5

Please sign in to comment.