Skip to content

Commit

Permalink
Map ZERO users to matrix users (#1069)
Browse files Browse the repository at this point in the history
* temp

* mapRoomMembers in matrix-client

* update user mentions

* update
  • Loading branch information
ratik21 authored Oct 4, 2023
1 parent e485424 commit ce99176
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 26 deletions.
67 changes: 57 additions & 10 deletions src/lib/chat/matrix-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import { get } from '../api/rest';
import { MemberNetworks } from '../../store/users/types';
import { getFilteredMembersForAutoComplete, setAsDM } from './matrix/utils';
import { uploadImage } from '../../store/channels-list/api';
import { union } from 'lodash';

enum ConnectionStatus {
Connected = 'connected',
Expand Down Expand Up @@ -88,7 +89,10 @@ export class MatrixClient implements IChatClient {
for (const room of rooms) {
await room.loadMembersIfNeeded();
}
return rooms.map(this.mapChannel);
const channels = rooms.map(this.mapChannel);
await this.mapRoomMembers(channels);

return channels;
}

async getConversations() {
Expand All @@ -105,7 +109,9 @@ export class MatrixClient implements IChatClient {
}
}
}
return rooms.filter((r) => !failedToJoin.includes(r.roomId)).map(this.mapConversation);
const mappedRooms = rooms.filter((r) => !failedToJoin.includes(r.roomId)).map(this.mapConversation);
await this.mapRoomMembers(mappedRooms);
return mappedRooms;
}

private async autoJoinRoom(roomId: string) {
Expand Down Expand Up @@ -137,7 +143,7 @@ export class MatrixClient implements IChatClient {

async searchMentionableUsersForChannel(channelId: string, search: string, channelMembers: UserModel[]) {
const searchResults = await getFilteredMembersForAutoComplete(channelMembers, search);
return searchResults.map((u) => ({ id: u.matrixId, display: u.displayName, profileImage: u.avatar_url }));
return searchResults.map((u) => ({ id: u.id, display: u.displayName, profileImage: u.profileImage }));
}

async getMessagesByChannelId(channelId: string, _lastCreatedAt?: number): Promise<MessagesResponse> {
Expand Down Expand Up @@ -408,7 +414,9 @@ export class MatrixClient implements IChatClient {
};
}

private mapChannel = (room: Room): Partial<Channel> => this.mapToGeneralChannel(room);
private mapChannel = (room: Room): Partial<Channel> => {
return this.mapToGeneralChannel(room);
};

private mapConversation = (room: Room): Partial<Channel> => {
return {
Expand All @@ -417,18 +425,31 @@ export class MatrixClient implements IChatClient {
};
};

private mapUser(userId: string): UserModel {
const user = this.matrix.getUser(userId);
return {
userId: userId,
matrixId: userId,
private mapUser(matrixId: string, zeroUser?): UserModel {
const user = this.matrix.getUser(matrixId);
let mappedUser = {
userId: matrixId,
matrixId,
firstName: user?.displayName,
lastName: '',
profileId: '',
isOnline: user?.presence === 'online',
profileImage: '',
profileImage: user?.avatarUrl,
lastSeenAt: '',
};

if (zeroUser && zeroUser?.profileSummary) {
mappedUser = {
...mappedUser,
userId: zeroUser.id,
profileId: zeroUser.profileSummary.id,
firstName: zeroUser.profileSummary.firstName,
lastName: zeroUser.profileSummary.lastName,
profileImage: zeroUser.profileSummary.profileImage,
};
}

return mappedUser;
}

private mapMatrixEventToMessage(matrixEvent) {
Expand Down Expand Up @@ -525,4 +546,30 @@ export class MatrixClient implements IChatClient {
}
return null;
}

private async getZEROUsers(matrixIds: string[]) {
return await get('/matrix/users/zero', { matrixIds })
.catch((_error) => null)
.then((response) => response?.body || []);
}

private async mapRoomMembers(rooms) {
let allMatrixIds = [];
for (const room of rooms) {
const matrixIds = room.otherMembers.map((u) => u.matrixId);
allMatrixIds = union(allMatrixIds, matrixIds);
}

const zeroUsers = await this.getZEROUsers(allMatrixIds);
const zeroUsersMap = {};
for (const user of zeroUsers) {
zeroUsersMap[user.matrixId] = user;
}

for (const room of rooms) {
room.otherMembers = room.otherMembers.map((member) =>
this.mapUser(member.matrixId, zeroUsersMap[member.matrixId])
);
}
}
}
30 changes: 17 additions & 13 deletions src/lib/chat/matrix/utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,15 @@ const roomMembers: any[] = [
userId: '@domw:zero-synapse-development.zer0.io',
matrixId: '@domw:zero-synapse-development.zer0.io',
firstName: 'domw',
profileImage: '',
},
{
userId: '@dale.fukami:zero-synapse-development.zer0.io',
userId: '6fec1869-4608-4f7e-ab32-e50376b58e30',
matrixId: '@dale.fukami:zero-synapse-development.zer0.io',
firstName: 'dale.fukami',
lastName: '',
profileId: 'a9d1b1a5-dc43-4860-93b6-fb63ee3ca911',
profileImage: '',
},
];

Expand All @@ -28,9 +32,9 @@ describe('getFilteredMembersForAutoComplete', () => {
// Expect members with 'da' in display name
expect(result).toEqual([
{
matrixId: '@dale.fukami:zero-synapse-development.zer0.io',
displayName: 'dale.fukami',
avatar_url: '',
id: '6fec1869-4608-4f7e-ab32-e50376b58e30',
displayName: 'dale.fukami ',
profileImage: '',
},
]);
});
Expand All @@ -41,9 +45,9 @@ describe('getFilteredMembersForAutoComplete', () => {
// Expect members with 'ratik21' in display name
expect(result).toEqual([
{
matrixId: '@domw:zero-synapse-development.zer0.io',
displayName: 'domw',
avatar_url: '',
id: '@domw:zero-synapse-development.zer0.io',
displayName: 'domw ',
profileImage: '',
},
]);
});
Expand All @@ -54,14 +58,14 @@ describe('getFilteredMembersForAutoComplete', () => {
// Expect all members
expect(result).toEqual([
{
matrixId: '@domw:zero-synapse-development.zer0.io',
displayName: 'domw',
avatar_url: '',
id: '@domw:zero-synapse-development.zer0.io',
displayName: 'domw ',
profileImage: '',
},
{
matrixId: '@dale.fukami:zero-synapse-development.zer0.io',
displayName: 'dale.fukami',
avatar_url: '',
id: '6fec1869-4608-4f7e-ab32-e50376b58e30',
displayName: 'dale.fukami ',
profileImage: '',
},
]);
});
Expand Down
6 changes: 3 additions & 3 deletions src/lib/chat/matrix/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,12 @@ export async function getFilteredMembersForAutoComplete(roomMembers: ChannelMemb

const filteredResults = [];
for (const member of roomMembers) {
let displayName = member.matrixId?.match(/@([^:]+)/)[1] || '';
let displayName = `${member.firstName || ''} ${member.lastName || ''}`;
if (displayName.includes(normalizedFilter)) {
filteredResults.push({
matrixId: member.matrixId,
id: member.userId || member.matrixId,
displayName,
avatar_url: '',
profileImage: member.profileImage,
});
}
}
Expand Down

0 comments on commit ce99176

Please sign in to comment.