@@ -50,7 +50,7 @@ export const fileTypes: Record<string, FileMessageType> = {
5050
5151/** helper to validate the username format */
5252export function validateFederatedUsername ( username : string ) : username is `@${string } :${string } ` {
53- return / ^ @ [ a - z A - Z 0 - 9 _ - ] + : [ a - z A - Z 0 - 9 _ - ] + $ / . test ( username ) ;
53+ return / ^ @ [ a - z A - Z 0 - 9 . _ = - ] + : [ a - z A - Z 0 - 9 . : _ - ] + $ / . test ( username ) ;
5454}
5555
5656/**
@@ -62,12 +62,11 @@ export function validateFederatedUsername(username: string): username is `@${str
6262export async function createOrUpdateFederatedUser ( options : {
6363 username : `@${string } :${string } `;
6464 name ?: string ;
65- status ?: UserStatus ;
6665 origin : string ;
67- } ) : Promise < { insertedId : any } > {
68- const { username, name = username , status = UserStatus . OFFLINE , origin } = options ;
66+ } ) : Promise < string > {
67+ const { username, name = username } = options ;
6968
70- return Users . updateOne (
69+ const result = await Users . updateOne (
7170 {
7271 username,
7372 } ,
@@ -76,7 +75,7 @@ export async function createOrUpdateFederatedUser(options: {
7675 username,
7776 name,
7877 type : 'user' as const ,
79- status,
78+ status : UserStatus . OFFLINE ,
8079 active : true ,
8180 roles : [ 'user' ] ,
8281 requirePasswordChange : false ,
@@ -93,7 +92,16 @@ export async function createOrUpdateFederatedUser(options: {
9392 {
9493 upsert : true ,
9594 } ,
96- ) . then ( ( result ) => ( { insertedId : result . upsertedId } ) ) ;
95+ ) ;
96+
97+ const userId = result . upsertedId || ( await Users . findOneByUsername ( username , { projection : { _id : 1 } } ) ) ?. _id ;
98+ if ( ! userId ) {
99+ throw new Error ( `Failed to create or update federated user: ${ username } ` ) ;
100+ }
101+ if ( typeof userId !== 'string' ) {
102+ return userId . toString ( ) ;
103+ }
104+ return userId ;
97105}
98106
99107export { generateEd25519RandomSecretKey } from '@rocket.chat/federation-sdk' ;
0 commit comments