@@ -414,7 +414,10 @@ export default class EmbeddedChatApi {
414414
415415 if ( suggestedUsername . success ) {
416416 const response2 = await fetch ( `${ this . host } /api/v1/users.update` , {
417- body : `{"userId": "${ userid } ", "data": { "username": "${ suggestedUsername . result } " }}` ,
417+ body : JSON . stringify ( {
418+ userId : userid ,
419+ data : { username : suggestedUsername . result } ,
420+ } ) ,
418421 headers : {
419422 "Content-Type" : "application/json" ,
420423 "X-Auth-Token" : authToken ,
@@ -439,7 +442,10 @@ export default class EmbeddedChatApi {
439442 try {
440443 const { userId, authToken } = ( await this . auth . getCurrentUser ( ) ) || { } ;
441444 const response = await fetch ( `${ this . host } /api/v1/users.update` , {
442- body : `{"userId": "${ userid } ", "data": { "username": "${ newUserName } " }}` ,
445+ body : JSON . stringify ( {
446+ userId : userid ,
447+ data : { username : newUserName } ,
448+ } ) ,
443449 headers : {
444450 "Content-Type" : "application/json" ,
445451 "X-Auth-Token" : authToken ,
@@ -487,34 +493,7 @@ export default class EmbeddedChatApi {
487493
488494 async getRoomInfo ( ) {
489495 try {
490- const { userId, authToken } = ( await this . auth . getCurrentUser ( ) ) || { } ;
491- const response = await fetch (
492- `${ this . host } /api/v1/method.call/rooms%3Aget` ,
493- {
494- body : JSON . stringify ( {
495- message : JSON . stringify ( {
496- msg : "method" ,
497- id : null ,
498- method : "rooms/get" ,
499- params : [ ] ,
500- } ) ,
501- } ) ,
502- headers : {
503- "Content-Type" : "application/json" ,
504- "X-Auth-Token" : authToken ,
505- "X-User-Id" : userId ,
506- } ,
507- method : "POST" ,
508- }
509- ) ;
510-
511- const result = await response . json ( ) ;
512-
513- if ( result . success && result . message ) {
514- const parsedMessage = JSON . parse ( result . message ) ;
515- return parsedMessage ;
516- }
517- return null ;
496+ return await this . channelInfo ( ) ;
518497 } catch ( err ) {
519498 console . error ( err ) ;
520499 }
@@ -691,36 +670,14 @@ export default class EmbeddedChatApi {
691670
692671 async getUserRoles ( ) {
693672 try {
694- const { userId, authToken } = ( await this . auth . getCurrentUser ( ) ) || { } ;
695- const response = await fetch (
696- `${ this . host } /api/v1/method.call/getUserRoles` ,
697- {
698- body : JSON . stringify ( {
699- message : JSON . stringify ( {
700- msg : "method" ,
701- id : null ,
702- method : "getUserRoles" ,
703- params : [ ] ,
704- } ) ,
705- } ) ,
706- headers : {
707- "Content-Type" : "application/json" ,
708- "X-Auth-Token" : authToken ,
709- "X-User-Id" : userId ,
710- } ,
711- method : "POST" ,
712- }
713- ) ;
714-
715- const result = await response . json ( ) ;
716-
717- if ( result . success && result . message ) {
718- const parsedMessage = JSON . parse ( result . message ) ;
719- return parsedMessage ;
673+ const response = await this . getUsersInRole ( "admin" ) ;
674+ if ( response && response . success ) {
675+ return { result : response . users } ;
720676 }
721- return null ;
677+ return { result : [ ] } ;
722678 } catch ( err ) {
723679 console . error ( err ) ;
680+ return { result : [ ] } ;
724681 }
725682 }
726683
@@ -776,7 +733,7 @@ export default class EmbeddedChatApi {
776733 try {
777734 const { userId, authToken } = ( await this . auth . getCurrentUser ( ) ) || { } ;
778735 const response = await fetch ( `${ this . host } /api/v1/chat.delete` , {
779- body : `{" roomId": " ${ this . rid } ", " msgId": " ${ msgId } "}` ,
736+ body : JSON . stringify ( { roomId : this . rid , msgId } ) ,
780737 headers : {
781738 "Content-Type" : "application/json" ,
782739 "X-Auth-Token" : authToken ,
@@ -794,7 +751,7 @@ export default class EmbeddedChatApi {
794751 try {
795752 const { userId, authToken } = ( await this . auth . getCurrentUser ( ) ) || { } ;
796753 const response = await fetch ( `${ this . host } /api/v1/chat.update` , {
797- body : `{" roomId": " ${ this . rid } ", " msgId": " ${ msgId } ","text" : " ${ text } " }` ,
754+ body : JSON . stringify ( { roomId : this . rid , msgId, text } ) ,
798755 headers : {
799756 "Content-Type" : "application/json" ,
800757 "X-Auth-Token" : authToken ,
@@ -854,7 +811,7 @@ export default class EmbeddedChatApi {
854811 try {
855812 const { userId, authToken } = ( await this . auth . getCurrentUser ( ) ) || { } ;
856813 const response = await fetch ( `${ this . host } /api/v1/chat.starMessage` , {
857- body : `{" messageId": " ${ mid } "}` ,
814+ body : JSON . stringify ( { messageId : mid } ) ,
858815 headers : {
859816 "Content-Type" : "application/json" ,
860817 "X-Auth-Token" : authToken ,
@@ -872,7 +829,7 @@ export default class EmbeddedChatApi {
872829 try {
873830 const { userId, authToken } = ( await this . auth . getCurrentUser ( ) ) || { } ;
874831 const response = await fetch ( `${ this . host } /api/v1/chat.unStarMessage` , {
875- body : `{" messageId": " ${ mid } "}` ,
832+ body : JSON . stringify ( { messageId : mid } ) ,
876833 headers : {
877834 "Content-Type" : "application/json" ,
878835 "X-Auth-Token" : authToken ,
@@ -950,7 +907,7 @@ export default class EmbeddedChatApi {
950907 try {
951908 const { userId, authToken } = ( await this . auth . getCurrentUser ( ) ) || { } ;
952909 const response = await fetch ( `${ this . host } /api/v1/chat.pinMessage` , {
953- body : `{" messageId": " ${ mid } "}` ,
910+ body : JSON . stringify ( { messageId : mid } ) ,
954911 headers : {
955912 "Content-Type" : "application/json" ,
956913 "X-Auth-Token" : authToken ,
@@ -970,7 +927,7 @@ export default class EmbeddedChatApi {
970927 try {
971928 const { userId, authToken } = ( await this . auth . getCurrentUser ( ) ) || { } ;
972929 const response = await fetch ( `${ this . host } /api/v1/chat.unPinMessage` , {
973- body : `{" messageId": " ${ mid } "}` ,
930+ body : JSON . stringify ( { messageId : mid } ) ,
974931 headers : {
975932 "Content-Type" : "application/json" ,
976933 "X-Auth-Token" : authToken ,
@@ -988,7 +945,11 @@ export default class EmbeddedChatApi {
988945 try {
989946 const { userId, authToken } = ( await this . auth . getCurrentUser ( ) ) || { } ;
990947 const response = await fetch ( `${ this . host } /api/v1/chat.react` , {
991- body : `{"messageId": "${ messageId } ", "emoji": "${ emoji } ", "shouldReact": ${ shouldReact } }` ,
948+ body : JSON . stringify ( {
949+ messageId,
950+ emoji,
951+ shouldReact,
952+ } ) ,
992953 headers : {
993954 "Content-Type" : "application/json" ,
994955 "X-Auth-Token" : authToken ,
@@ -1006,7 +967,7 @@ export default class EmbeddedChatApi {
1006967 try {
1007968 const { userId, authToken } = ( await this . auth . getCurrentUser ( ) ) || { } ;
1008969 const response = await fetch ( `${ this . host } /api/v1/chat.reportMessage` , {
1009- body : `{"messageId": " ${ messageId } ", " description": " ${ description } "}` ,
970+ body : JSON . stringify ( { messageId, description } ) ,
1010971 headers : {
1011972 "Content-Type" : "application/json" ,
1012973 "X-Auth-Token" : authToken ,
@@ -1046,26 +1007,53 @@ export default class EmbeddedChatApi {
10461007 ) {
10471008 try {
10481009 const { userId, authToken } = ( await this . auth . getCurrentUser ( ) ) || { } ;
1049- const form = new FormData ( ) ;
1050- if ( threadId ) {
1051- form . append ( "tmid" , threadId ) ;
1010+ if ( ! userId || ! authToken ) {
1011+ console . error ( "sendAttachment: User not authenticated" ) ;
1012+ return ;
10521013 }
1014+
1015+ const form = new FormData ( ) ;
10531016 form . append ( "file" , file , fileName ) ;
1054- form . append (
1055- "description" ,
1056- fileDescription . length !== 0 ? fileDescription : ""
1017+
1018+ const uploadResponse = await fetch (
1019+ `${ this . host } /api/v1/rooms.media/${ this . rid } ` ,
1020+ {
1021+ method : "POST" ,
1022+ body : form ,
1023+ headers : {
1024+ "X-Auth-Token" : authToken ,
1025+ "X-User-Id" : userId ,
1026+ } ,
1027+ }
10571028 ) ;
1058- const response = fetch ( `${ this . host } /api/v1/rooms.upload/${ this . rid } ` , {
1059- method : "POST" ,
1060- body : form ,
1061- headers : {
1062- "X-Auth-Token" : authToken ,
1063- "X-User-Id" : userId ,
1064- } ,
1065- } ) . then ( ( r ) => r . json ( ) ) ;
1066- return response ;
1029+
1030+ const uploadResult = await uploadResponse . json ( ) ;
1031+
1032+ if ( ! uploadResult . success || ! uploadResult . file ?. _id ) {
1033+ console . error ( "sendAttachment: Upload failed" , uploadResult ) ;
1034+ return uploadResult ;
1035+ }
1036+
1037+ const confirmResponse = await fetch (
1038+ `${ this . host } /api/v1/rooms.mediaConfirm/${ this . rid } /${ uploadResult . file . _id } ` ,
1039+ {
1040+ method : "POST" ,
1041+ headers : {
1042+ "Content-Type" : "application/json" ,
1043+ "X-Auth-Token" : authToken ,
1044+ "X-User-Id" : userId ,
1045+ } ,
1046+ body : JSON . stringify (
1047+ threadId
1048+ ? { msg : "" , description : fileDescription || "" , tmid : threadId }
1049+ : { msg : "" , description : fileDescription || "" }
1050+ ) ,
1051+ }
1052+ ) ;
1053+
1054+ return await confirmResponse . json ( ) ;
10671055 } catch ( err ) {
1068- console . log ( err ) ;
1056+ console . error ( "sendAttachment error:" , err ) ;
10691057 }
10701058 }
10711059
0 commit comments