diff --git a/src/lib/chat/matrix-client.ts b/src/lib/chat/matrix-client.ts index 961c25193..f077ca94f 100644 --- a/src/lib/chat/matrix-client.ts +++ b/src/lib/chat/matrix-client.ts @@ -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', @@ -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() { @@ -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) { @@ -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 { @@ -408,7 +414,9 @@ export class MatrixClient implements IChatClient { }; } - private mapChannel = (room: Room): Partial => this.mapToGeneralChannel(room); + private mapChannel = (room: Room): Partial => { + return this.mapToGeneralChannel(room); + }; private mapConversation = (room: Room): Partial => { return { @@ -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) { @@ -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]) + ); + } + } } diff --git a/src/lib/chat/matrix/utils.test.ts b/src/lib/chat/matrix/utils.test.ts index 2c0524f0f..4b49c2605 100644 --- a/src/lib/chat/matrix/utils.test.ts +++ b/src/lib/chat/matrix/utils.test.ts @@ -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: '', }, ]; @@ -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: '', }, ]); }); @@ -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: '', }, ]); }); @@ -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: '', }, ]); }); diff --git a/src/lib/chat/matrix/utils.ts b/src/lib/chat/matrix/utils.ts index eb3dfacc7..d6edf8548 100644 --- a/src/lib/chat/matrix/utils.ts +++ b/src/lib/chat/matrix/utils.ts @@ -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, }); } }