Skip to content

Commit b592ffb

Browse files
chore: commit modernized API changes, reaction toggle support, DOM warning fixes, and pinned messages features
1 parent 25aac06 commit b592ffb

10 files changed

Lines changed: 175 additions & 57 deletions

File tree

packages/api/src/EmbeddedChatApi.ts

Lines changed: 83 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,9 @@ export default class EmbeddedChatApi {
4949
}
5050

5151
private async _syncRestCredentials() {
52-
const { userId, authToken } = (await this.auth.getCurrentUser()) || {};
52+
const user = (await this.auth.getCurrentUser()) || {};
53+
const userId = user.userId || user.data?.userId;
54+
const authToken = user.authToken || user.data?.authToken;
5355
if (userId && authToken) {
5456
this.sdk.rest.setCredentials({
5557
"X-User-Id": userId,
@@ -145,8 +147,9 @@ export default class EmbeddedChatApi {
145147
if (response.error === "totp-required") {
146148
return response;
147149
}
148-
} catch (err) {
150+
} catch (err: any) {
149151
console.error(err instanceof Error ? err.message : err);
152+
return err;
150153
}
151154
}
152155

@@ -212,8 +215,9 @@ export default class EmbeddedChatApi {
212215
async logout() {
213216
try {
214217
await this.auth.logout();
215-
} catch (err) {
218+
} catch (err: any) {
216219
console.error(err instanceof Error ? err.message : err);
220+
return err;
217221
}
218222
}
219223

@@ -248,7 +252,15 @@ export default class EmbeddedChatApi {
248252
try {
249253
this.close(); // before connection, all previous subscriptions should be cancelled
250254
await this.sdk.connection.connect();
251-
const token = (await this.auth.getCurrentUser())?.authToken;
255+
256+
// Sync REST credentials first so HTTP headers are ready before any request fires
257+
await this._syncRestCredentials();
258+
259+
// Extract token from either flat or nested currentUser shape:
260+
// - auto-login (resume token): currentUser = { userId, authToken, me }
261+
// - password login: currentUser = { status, data: { userId, authToken, me } }
262+
const currentUser = (await this.auth.getCurrentUser()) as any;
263+
const token = currentUser?.authToken || currentUser?.data?.authToken;
252264
if (token) {
253265
await this.sdk.account.loginWithToken(token);
254266
}
@@ -448,8 +460,9 @@ export default class EmbeddedChatApi {
448460
return null;
449461
}
450462
return await response.json();
451-
} catch (err) {
463+
} catch (err: any) {
452464
console.error(err instanceof Error ? err.message : err);
465+
return err;
453466
}
454467
}
455468

@@ -465,8 +478,9 @@ export default class EmbeddedChatApi {
465478
data: { username: suggestedUsername.result },
466479
});
467480
}
468-
} catch (error) {
481+
} catch (error: any) {
469482
console.error(error instanceof Error ? error.message : error);
483+
return error;
470484
}
471485
}
472486

@@ -497,24 +511,27 @@ export default class EmbeddedChatApi {
497511
try {
498512
await this._syncRestCredentials();
499513
return await this.sdk.rest.get("/v1/rooms.info", { roomId: this.rid });
500-
} catch (err) {
514+
} catch (err: any) {
501515
console.error(err instanceof Error ? err.message : err);
516+
return err;
502517
}
503518
}
504519

505520
async getRoomInfo(): Promise<any> {
506521
try {
507522
return await this.channelInfo();
508-
} catch (err) {
523+
} catch (err: any) {
509524
console.error(err instanceof Error ? err.message : err);
525+
return err;
510526
}
511527
}
512528

513529
async permissionInfo() {
514530
try {
515531
return await this._restRequest("/v1/permissions.listAll");
516-
} catch (err) {
532+
} catch (err: any) {
517533
console.error(err instanceof Error ? err.message : err);
534+
return err;
518535
}
519536
}
520537

@@ -554,8 +571,9 @@ export default class EmbeddedChatApi {
554571
return await this._restRequest(
555572
`/v1/${roomType}.${endp}?roomId=${this.rid}${query}${field}`
556573
);
557-
} catch (err) {
558-
console.log(err);
574+
} catch (err: any) {
575+
console.error(err instanceof Error ? err.message : err);
576+
return err;
559577
}
560578
}
561579

@@ -585,8 +603,9 @@ export default class EmbeddedChatApi {
585603
return await this._restRequest(
586604
`/v1/${roomType}.${endp}?roomId=${this.rid}${query}${field}&offset=${offset}`
587605
);
588-
} catch (err) {
589-
console.log(err);
606+
} catch (err: any) {
607+
console.error(err instanceof Error ? err.message : String(err));
608+
return err;
590609
}
591610
}
592611

@@ -597,8 +616,9 @@ export default class EmbeddedChatApi {
597616
try {
598617
await this._syncRestCredentials();
599618
return await this.sdk.rest.get("/v1/chat.getThreadMessages", { tmid });
600-
} catch (err) {
601-
console.log(err);
619+
} catch (err: any) {
620+
console.error(err instanceof Error ? err.message : String(err));
621+
return err;
602622
}
603623
}
604624

@@ -608,16 +628,18 @@ export default class EmbeddedChatApi {
608628
return await this._restRequest(
609629
`/v1/${roomType}.roles?roomId=${this.rid}`
610630
);
611-
} catch (err) {
612-
console.log(err);
631+
} catch (err: any) {
632+
console.error(err instanceof Error ? err.message : String(err));
633+
return err;
613634
}
614635
}
615636

616637
async getUsersInRole(role: string) {
617638
try {
618639
return await this._restRequest(`/v1/roles.getUsersInRole?role=${role}`);
619-
} catch (err) {
620-
console.log(err);
640+
} catch (err: any) {
641+
console.error(err instanceof Error ? err.message : String(err));
642+
return err;
621643
}
622644
}
623645

@@ -664,8 +686,9 @@ export default class EmbeddedChatApi {
664686
return await this.sdk.rest.post("/v1/chat.sendMessage", {
665687
message: messageObj,
666688
});
667-
} catch (err) {
689+
} catch (err: any) {
668690
console.error(err instanceof Error ? err.message : err);
691+
return err;
669692
}
670693
}
671694

@@ -676,8 +699,9 @@ export default class EmbeddedChatApi {
676699
roomId: this.rid,
677700
msgId,
678701
});
679-
} catch (err) {
702+
} catch (err: any) {
680703
console.error(err instanceof Error ? err.message : err);
704+
return err;
681705
}
682706
}
683707

@@ -689,8 +713,9 @@ export default class EmbeddedChatApi {
689713
msgId,
690714
text,
691715
});
692-
} catch (err) {
716+
} catch (err: any) {
693717
console.error(err instanceof Error ? err.message : err);
718+
return err;
694719
}
695720
}
696721

@@ -702,17 +727,19 @@ export default class EmbeddedChatApi {
702727
? `/v1/${roomType}.files?roomId=${this.rid}`
703728
: `/v1/${roomType}.files?roomId=${this.rid}&typeGroup=${typeGroup}`;
704729
return await this._restRequest(endpoint);
705-
} catch (err) {
730+
} catch (err: any) {
706731
console.error(err instanceof Error ? err.message : err);
732+
return err;
707733
}
708734
}
709735

710736
async getAllImages(): Promise<any> {
711737
try {
712738
await this._syncRestCredentials();
713739
return await this.sdk.rest.get("/v1/rooms.images", { roomId: this.rid });
714-
} catch (err) {
740+
} catch (err: any) {
715741
console.error(err instanceof Error ? err.message : err);
742+
return err;
716743
}
717744
}
718745

@@ -721,8 +748,9 @@ export default class EmbeddedChatApi {
721748
return await this._restRequest("/v1/chat.starMessage", "POST", {
722749
messageId: mid,
723750
});
724-
} catch (err) {
751+
} catch (err: any) {
725752
console.error(err instanceof Error ? err.message : err);
753+
return err;
726754
}
727755
}
728756

@@ -731,8 +759,9 @@ export default class EmbeddedChatApi {
731759
return await this._restRequest("/v1/chat.unStarMessage", "POST", {
732760
messageId: mid,
733761
});
734-
} catch (err) {
762+
} catch (err: any) {
735763
console.error(err instanceof Error ? err.message : err);
764+
return err;
736765
}
737766
}
738767

@@ -742,8 +771,9 @@ export default class EmbeddedChatApi {
742771
return await this.sdk.rest.get("/v1/chat.getStarredMessages", {
743772
roomId: this.rid,
744773
});
745-
} catch (err) {
774+
} catch (err: any) {
746775
console.error(err instanceof Error ? err.message : err);
776+
return err;
747777
}
748778
}
749779

@@ -753,8 +783,9 @@ export default class EmbeddedChatApi {
753783
return await this.sdk.rest.get("/v1/chat.getPinnedMessages", {
754784
roomId: this.rid,
755785
});
756-
} catch (err) {
786+
} catch (err: any) {
757787
console.error(err instanceof Error ? err.message : err);
788+
return err;
758789
}
759790
}
760791

@@ -764,8 +795,9 @@ export default class EmbeddedChatApi {
764795
return await this.sdk.rest.get("/v1/chat.getMentionedMessages", {
765796
roomId: this.rid,
766797
});
767-
} catch (err) {
798+
} catch (err: any) {
768799
console.error(err instanceof Error ? err.message : err);
800+
return err;
769801
}
770802
}
771803

@@ -784,25 +816,30 @@ export default class EmbeddedChatApi {
784816
return await this._restRequest("/v1/chat.unPinMessage", "POST", {
785817
messageId: mid,
786818
});
787-
} catch (err) {
819+
} catch (err: any) {
788820
console.error(err instanceof Error ? err.message : err);
821+
return err;
789822
}
790823
}
791824

792825
async reactToMessage(
793826
emoji: string,
794827
messageId: string,
795-
shouldReact: string
828+
shouldReact: boolean | string
796829
): Promise<any> {
797830
try {
798831
await this._syncRestCredentials();
799832
return await this.sdk.rest.post("/v1/chat.react", {
800833
messageId,
801834
emoji,
802-
shouldReact: shouldReact === "true",
835+
shouldReact:
836+
typeof shouldReact === "string"
837+
? shouldReact === "true"
838+
: shouldReact,
803839
});
804-
} catch (err) {
840+
} catch (err: any) {
805841
console.error(err instanceof Error ? err.message : err);
842+
return err;
806843
}
807844
}
808845

@@ -813,8 +850,9 @@ export default class EmbeddedChatApi {
813850
messageId,
814851
description,
815852
});
816-
} catch (err) {
853+
} catch (err: any) {
817854
console.error(err instanceof Error ? err.message : err);
855+
return err;
818856
}
819857
}
820858

@@ -826,8 +864,9 @@ export default class EmbeddedChatApi {
826864
days: 1,
827865
maxUses: 10,
828866
});
829-
} catch (err) {
830-
console.log(err);
867+
} catch (err: any) {
868+
console.error(err instanceof Error ? err.message : String(err));
869+
return err;
831870
}
832871
}
833872

@@ -873,8 +912,9 @@ export default class EmbeddedChatApi {
873912
try {
874913
await this._syncRestCredentials();
875914
return await this.sdk.rest.get("/v1/me");
876-
} catch (err) {
915+
} catch (err: any) {
877916
console.error(err instanceof Error ? err.message : err);
917+
return err;
878918
}
879919
}
880920

@@ -884,8 +924,9 @@ export default class EmbeddedChatApi {
884924
return await this._restRequest(
885925
`/v1/${roomType}.members?roomId=${this.rid}`
886926
);
887-
} catch (err) {
927+
} catch (err: any) {
888928
console.error(err instanceof Error ? err.message : err);
929+
return err;
889930
}
890931
}
891932

@@ -896,16 +937,18 @@ export default class EmbeddedChatApi {
896937
roomId: this.rid,
897938
searchText: text,
898939
});
899-
} catch (err) {
940+
} catch (err: any) {
900941
console.error(err instanceof Error ? err.message : err);
942+
return err;
901943
}
902944
}
903945

904946
async getMessageLimit() {
905947
try {
906948
return await this._restRequest("/v1/settings/Message_MaxAllowedSize");
907-
} catch (err) {
949+
} catch (err: any) {
908950
console.error(err instanceof Error ? err.message : err);
951+
return err;
909952
}
910953
}
911954

packages/auth/src/RocketChatAuth.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,10 @@ class RocketChatAuth {
7474
}
7575
);
7676
this.setUser(response.data);
77-
this.notifyAuthListeners();
77+
// Note: setUser → save() already calls notifyAuthListeners().
78+
// Do NOT call it again here — a second notification would trigger
79+
// a second connect() after the first resolves, causing close() to
80+
// kill the active DDP session and all subscriptions.
7881
return this.currentUser;
7982
}
8083

@@ -95,7 +98,7 @@ class RocketChatAuth {
9598
credentials
9699
);
97100
this.setUser(response.data);
98-
this.notifyAuthListeners();
101+
// setUser → save() already calls notifyAuthListeners().
99102
return this.currentUser;
100103
}
101104

0 commit comments

Comments
 (0)