@@ -26,7 +26,7 @@ import { Users, Subscriptions, Messages, Rooms } from '@rocket.chat/models';
2626import emojione from 'emojione' ;
2727
2828import { getWellKnownRoutes } from './api/.well-known/server' ;
29- import { getMatrixInviteRoutes } from './api/_matrix/invite' ;
29+ import { acceptInvite , getMatrixInviteRoutes } from './api/_matrix/invite' ;
3030import { getKeyServerRoutes } from './api/_matrix/key/server' ;
3131import { getMatrixMediaRoutes } from './api/_matrix/media' ;
3232import { getMatrixProfilesRoutes } from './api/_matrix/profiles' ;
@@ -88,6 +88,24 @@ export const extractDomainFromMatrixUserId = (mxid: string): string => {
8888 return mxid . substring ( separatorIndex + 1 ) ;
8989} ;
9090
91+ /**
92+ * Extract the username and the servername from a matrix user id
93+ * if the serverName is the same as the serverName in the mxid, return only the username (rocket.chat regular username)
94+ * otherwise, return the full mxid and the servername
95+ */
96+ export const getUsernameServername = ( mxid : string , serverName : string ) : [ mxid : string , serverName : string , isLocal : boolean ] => {
97+ const senderServerName = extractDomainFromMatrixUserId ( mxid ) ;
98+ // if the serverName is the same as the serverName in the mxid, return only the username (rocket.chat regular username)
99+ if ( serverName === senderServerName ) {
100+ const separatorIndex = mxid . indexOf ( ':' , 1 ) ;
101+ if ( separatorIndex === - 1 ) {
102+ throw new Error ( `Invalid federated username: ${ mxid } ` ) ;
103+ }
104+ return [ mxid . substring ( 1 , separatorIndex ) , senderServerName , true ] ; // removers also the @
105+ }
106+
107+ return [ mxid , senderServerName , false ] ;
108+ } ;
91109/**
92110 * Helper function to create a federated user
93111 *
@@ -99,7 +117,7 @@ export async function createOrUpdateFederatedUser(options: {
99117 name ?: string ;
100118 origin : string ;
101119} ) : Promise < string > {
102- const { username, name = username } = options ;
120+ const { username, name = username , origin } = options ;
103121
104122 const result = await Users . updateOne (
105123 {
@@ -108,7 +126,7 @@ export async function createOrUpdateFederatedUser(options: {
108126 {
109127 $set : {
110128 username,
111- name,
129+ name : name || username ,
112130 type : 'user' as const ,
113131 status : UserStatus . OFFLINE ,
114132 active : true ,
@@ -294,7 +312,12 @@ export class FederationMatrix extends ServiceClass implements IFederationMatrixS
294312
295313 async created ( ) : Promise < void > {
296314 try {
297- registerEvents ( this . eventHandler , this . serverName , { typing : this . processEDUTyping , presence : this . processEDUPresence } ) ;
315+ registerEvents (
316+ this . eventHandler ,
317+ this . serverName ,
318+ { typing : this . processEDUTyping , presence : this . processEDUPresence } ,
319+ this . homeserverServices ,
320+ ) ;
298321 } catch ( error ) {
299322 this . logger . warn ( 'Homeserver module not available, running in limited mode' ) ;
300323 }
@@ -669,24 +692,23 @@ export class FederationMatrix extends ServiceClass implements IFederationMatrixS
669692 }
670693 }
671694
672- async inviteUsersToRoom ( room : IRoomNativeFederated , usersUserName : string [ ] , inviter : IUser ) : Promise < void > {
695+ async inviteUsersToRoom ( room : IRoomNativeFederated , matrixUsersUsername : string [ ] , inviter : IUser ) : Promise < void > {
673696 try {
674697 const inviterUserId = `@${ inviter . username } :${ this . serverName } ` ;
675698
676699 await Promise . all (
677- usersUserName
678- . filter ( ( username ) => {
679- const isExternalUser = username . includes ( ':' ) ;
680- return isExternalUser ;
681- } )
682- . map ( async ( username ) => {
683- const alreadyMember = await Subscriptions . findOneByRoomIdAndUsername ( room . _id , username , { projection : { _id : 1 } } ) ;
684- if ( alreadyMember ) {
685- return ;
686- }
700+ matrixUsersUsername . map ( async ( username ) => {
701+ if ( validateFederatedUsername ( username ) ) {
702+ return this . homeserverServices . invite . inviteUserToRoom ( username , room . federation . mrid , inviterUserId ) ;
703+ }
704+ const result = await this . homeserverServices . invite . inviteUserToRoom (
705+ `@${ username } :${ this . serverName } ` ,
706+ room . federation . mrid ,
707+ inviterUserId ,
708+ ) ;
687709
688- await this . homeserverServices . invite . inviteUserToRoom ( username , room . federation . mrid , inviterUserId ) ;
689- } ) ,
710+ return acceptInvite ( result . event , username , this . homeserverServices ) ;
711+ } ) ,
690712 ) ;
691713 } catch ( error ) {
692714 this . logger . error ( { msg : 'Failed to invite an user to Matrix:' , err : error } ) ;
0 commit comments