chat4app-client-angular 1.0.14 → 1.0.16

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,9 +1,8 @@
1
- import { __awaiter } from 'tslib';
2
1
  import * as i0 from '@angular/core';
3
2
  import { Injectable, Component, NgModule } from '@angular/core';
4
3
  import * as i1 from '@angular/common/http';
5
4
  import { HttpHeaders, HttpClient } from '@angular/common/http';
6
- import { lastValueFrom } from 'rxjs';
5
+ import { Observable } from 'rxjs';
7
6
 
8
7
  var commonjsGlobal = typeof globalThis !== 'undefined' ? globalThis : typeof window !== 'undefined' ? window : typeof global !== 'undefined' ? global : typeof self !== 'undefined' ? self : {};
9
8
 
@@ -752,6 +751,7 @@ class Chat4appClientAngularService {
752
751
  this.chatUrl = "";
753
752
  this.debug = false;
754
753
  this.chatUserExternalId = "";
754
+ this.configId = "";
755
755
  this.timeoutReconnect = 500;
756
756
  }
757
757
  ngOnInit() {
@@ -761,19 +761,33 @@ class Chat4appClientAngularService {
761
761
  this.chatUrl = chat4AppConfig.endpoint + "/api/v1/";
762
762
  this.debug = chat4AppConfig.debug;
763
763
  }
764
- init(securityChatToken, callback, chatCallback, connectionClientId) {
765
- return __awaiter(this, void 0, void 0, function* () {
766
- if (!this.chatServer || this.chatServer.trim().length == 0) {
767
- throw new Error("Endpoint is empty");
768
- }
769
- if (!securityChatToken || securityChatToken.trim().length == 0) {
770
- throw new Error("SecurityChatToken is empty");
764
+ init(securityChatToken, chatCallback, connectionClientId) {
765
+ return new Observable((subscriber) => {
766
+ try {
767
+ // 1. Validações Iniciais
768
+ if (!this.chatServer || this.chatServer.trim().length === 0) {
769
+ subscriber.error(new Error("Endpoint is empty"));
770
+ return;
771
+ }
772
+ if (!securityChatToken || securityChatToken.trim().length === 0) {
773
+ subscriber.error(new Error("SecurityChatToken is empty"));
774
+ return;
775
+ }
776
+ // 2. Lógica de Negócio
777
+ this.setTokenChat4app(securityChatToken);
778
+ if (chatCallback && connectionClientId) {
779
+ this.chatUserExternalId = connectionClientId;
780
+ this.chatCallback = chatCallback;
781
+ this.connectWebsocket();
782
+ }
783
+ // 3. Emite o token e finaliza o fluxo
784
+ subscriber.next(securityChatToken);
785
+ subscriber.complete();
771
786
  }
772
- this.setTokenChat4app(securityChatToken);
773
- if (chatCallback && connectionClientId) {
774
- yield this.connectWebsocket(chatCallback, connectionClientId);
787
+ catch (err) {
788
+ // Captura erros inesperados e envia para o canal de erro do Observable
789
+ subscriber.error(err);
775
790
  }
776
- callback(securityChatToken);
777
791
  });
778
792
  }
779
793
  getChatUrlDownload(messageFileId) {
@@ -1063,182 +1077,29 @@ class Chat4appClientAngularService {
1063
1077
  setTokenChat4app(chat4AppSessionToken) {
1064
1078
  window.sessionStorage.setItem("chat4AppSessionToken", chat4AppSessionToken);
1065
1079
  }
1066
- /* Connection */
1067
- onMessageFirebaseReceived(data, chatCallback) {
1068
- //alert("onMessageReceived connectFirebase");
1069
- if (data.type == "msg") {
1070
- let chat4appMessage = new Chat4AppMessageReceive();
1071
- chat4appMessage.data.chatId = data.chatId;
1072
- chat4appMessage.data.chatMessageId = data.chatMessageId;
1073
- chat4appMessage.data.externalId = data.externalId;
1074
- chat4appMessage.data.fromId = data.fromId;
1075
- chat4appMessage.data.toId = data.toId;
1076
- chat4appMessage.data.groupId = data.groupId;
1077
- chat4appMessage.data.dateHour = data.dateHour;
1078
- chat4appMessage.data.type = data.type;
1079
- chat4appMessage.notification.body = data.body;
1080
- if (data.fileId && data.fileId.length > 0) {
1081
- chat4appMessage.data.fileId = data.fileId;
1082
- chat4appMessage.data.fileName = data.fileName;
1083
- chat4appMessage.data.fileSize = data.fileSize;
1084
- chat4appMessage.data.fileType = data.fileType;
1085
- }
1086
- let datTmp = new Map();
1087
- for (let key in data) {
1088
- datTmp.set(key, data[key]);
1089
- }
1090
- chat4appMessage.data.all = datTmp;
1091
- chatCallback.onMessage(chat4appMessage);
1092
- }
1093
- else if ((data.type == "on" || data.type == "off")) {
1094
- let chat4appMessage = new Chat4AppMessageReceive();
1095
- chat4appMessage.data.chatId = data.chatId;
1096
- chat4appMessage.data.chatMessageId = data.chatMessageId;
1097
- chat4appMessage.data.externalId = data.externalId;
1098
- chat4appMessage.data.fromId = data.fromId;
1099
- chat4appMessage.data.toId = data.toId;
1100
- chat4appMessage.data.groupId = data.groupId;
1101
- chat4appMessage.data.dateHour = data.dateHour;
1102
- chat4appMessage.data.type = data.type;
1103
- let datTmp = new Map();
1104
- for (let key in data) {
1105
- datTmp.set(key, data[key]);
1106
- }
1107
- chat4appMessage.data.all = datTmp;
1108
- chatCallback.onOffline(chat4appMessage);
1109
- }
1110
- else if (data.type == "read") {
1111
- let chat4appMessage = new Chat4AppMessageReceive();
1112
- chat4appMessage.data.chatId = data.chatId;
1113
- chat4appMessage.data.chatMessageId = data.chatMessageId;
1114
- chat4appMessage.data.externalId = data.externalId;
1115
- chat4appMessage.data.fromId = data.fromId;
1116
- chat4appMessage.data.toId = data.toId;
1117
- chat4appMessage.data.groupId = data.groupId;
1118
- chat4appMessage.data.dateHour = data.dateHour;
1119
- chat4appMessage.data.type = data.type;
1120
- let datTmp = new Map();
1121
- for (let key in data) {
1122
- datTmp.set(key, data[key]);
1123
- }
1124
- chat4appMessage.data.all = datTmp;
1125
- chatCallback.onRead(chat4appMessage);
1126
- }
1127
- else if (data.type == "startTyping" || data.type == "stopTyping") {
1128
- let chat4appMessage = new Chat4AppMessageReceive();
1129
- chat4appMessage.data.chatId = data.chatId;
1130
- chat4appMessage.data.chatMessageId = data.chatMessageId;
1131
- chat4appMessage.data.externalId = data.externalId;
1132
- chat4appMessage.data.fromId = data.fromId;
1133
- chat4appMessage.data.toId = data.toId;
1134
- chat4appMessage.data.groupId = data.groupId;
1135
- chat4appMessage.data.dateHour = data.dateHour;
1136
- chat4appMessage.data.type = data.type;
1137
- let datTmp = new Map();
1138
- for (let key in data) {
1139
- datTmp.set(key, data[key]);
1140
- }
1141
- chat4appMessage.data.all = datTmp;
1142
- chatCallback.onStartStopTyping(chat4appMessage);
1080
+ ensureConn() {
1081
+ if (this.debug) {
1082
+ console.log(`chatServer: ${this.chatServer}`);
1083
+ console.log(`userId: ${this.chatUserExternalId}`);
1084
+ console.log(`getBearerToken: ${this.getBearerToken()}`);
1143
1085
  }
1144
- else if (data.type == "new_user") {
1145
- let chat4appMessage = new Chat4AppMessageReceive();
1146
- chat4appMessage.data.chatId = data.chatId;
1147
- chat4appMessage.data.chatMessageId = data.chatMessageId;
1148
- chat4appMessage.data.externalId = data.externalId;
1149
- chat4appMessage.data.fromId = data.fromId;
1150
- chat4appMessage.data.toId = data.toId;
1151
- chat4appMessage.data.groupId = data.groupId;
1152
- chat4appMessage.data.dateHour = data.dateHour;
1153
- chat4appMessage.data.type = data.type;
1154
- let datTmp = new Map();
1155
- for (let key in data) {
1156
- datTmp.set(key, data[key]);
1157
- }
1158
- chat4appMessage.data.all = datTmp;
1159
- chatCallback.onNewUser(chat4appMessage);
1160
- }
1161
- else if (data.type == "remove_user") {
1162
- let chat4appMessage = new Chat4AppMessageReceive();
1163
- chat4appMessage.data.chatId = data.chatId;
1164
- chat4appMessage.data.chatMessageId = data.chatMessageId;
1165
- chat4appMessage.data.externalId = data.externalId;
1166
- chat4appMessage.data.fromId = data.fromId;
1167
- chat4appMessage.data.toId = data.toId;
1168
- chat4appMessage.data.groupId = data.groupId;
1169
- chat4appMessage.data.dateHour = data.dateHour;
1170
- chat4appMessage.data.type = data.type;
1171
- let datTmp = new Map();
1172
- for (let key in data) {
1173
- datTmp.set(key, data[key]);
1174
- }
1175
- chat4appMessage.data.all = datTmp;
1176
- chatCallback.onRemoveUser(chat4appMessage);
1177
- }
1178
- else if (data.type == "chat_closed") {
1179
- let chat4appMessage = new Chat4AppMessageReceive();
1180
- chat4appMessage.data.chatId = data.chatId;
1181
- chat4appMessage.data.chatMessageId = data.chatMessageId;
1182
- chat4appMessage.data.externalId = data.externalId;
1183
- chat4appMessage.data.fromId = data.fromId;
1184
- chat4appMessage.data.toId = data.toId;
1185
- chat4appMessage.data.groupId = data.groupId;
1186
- chat4appMessage.data.dateHour = data.dateHour;
1187
- chat4appMessage.data.type = data.type;
1188
- let datTmp = new Map();
1189
- for (let key in data) {
1190
- datTmp.set(key, data[key]);
1191
- }
1192
- chat4appMessage.data.all = datTmp;
1193
- chatCallback.onChatClosed(chat4appMessage);
1194
- }
1195
- else if (data.type == "count_notread") {
1196
- let chat4appMessage = new Chat4AppMessageReceive();
1197
- chat4appMessage.data.chatId = data.chatId;
1198
- chat4appMessage.data.chatMessageId = data.chatMessageId;
1199
- chat4appMessage.data.externalId = data.externalId;
1200
- chat4appMessage.data.fromId = data.fromId;
1201
- chat4appMessage.data.toId = data.toId;
1202
- chat4appMessage.data.groupId = data.groupId;
1203
- chat4appMessage.data.dateHour = data.dateHour;
1204
- chat4appMessage.data.type = data.type;
1205
- let datTmp = new Map();
1206
- for (let key in data) {
1207
- datTmp.set(key, data[key]);
1208
- }
1209
- chat4appMessage.data.all = datTmp;
1210
- chatCallback.onCountNotRead(chat4appMessage);
1086
+ const that = this;
1087
+ let uriStep1 = this.chatServer.replace(/^https/, 'wss');
1088
+ let server = uriStep1.replace(/(\/\/.*?)\//, '$1\/ws/');
1089
+ if (this.debug) {
1090
+ console.log(`ws server: ${server}`);
1211
1091
  }
1212
- }
1213
- ensureConn(chatUserExternalId, forceReconnectNats) {
1214
- return __awaiter(this, void 0, void 0, function* () {
1215
- if (this.debug) {
1216
- console.log(`chatServer: ${this.chatServer}`);
1217
- console.log(`userId: ${chatUserExternalId}`);
1218
- console.log(`getBearerToken: ${this.getBearerToken()}`);
1219
- }
1220
- const that = this;
1221
- let uriStep1 = this.chatServer.replace(/^https/, 'wss');
1222
- let server = uriStep1.replace(/(\/\/.*?)\//, '$1\/ws/');
1223
- if (this.debug) {
1224
- console.log(`ws server: ${server}`);
1225
- }
1226
- if (forceReconnectNats || !this.nc || (this.nc.isClose && this.nc.isClose())) {
1227
- yield that.disconnectInternal(true);
1228
- that.nc = yield nats_min.connect({ url: server, user: chatUserExternalId, pass: this.getBearerToken(), reconnect: false });
1229
- }
1230
- this.chatUserExternalId = chatUserExternalId;
1231
- return that.nc;
1232
- });
1233
- }
1234
- disconnect() {
1235
- return __awaiter(this, void 0, void 0, function* () {
1236
- return this.disconnectInternal(false);
1237
- });
1238
- }
1239
- disconnectInternal(ignoreOffLine) {
1240
- return __awaiter(this, void 0, void 0, function* () {
1241
- // Check how we can disconnect
1092
+ that.disconnectInternal(true);
1093
+ nats_min.connect({ url: server, user: this.chatUserExternalId, pass: this.getBearerToken(), reconnect: false })
1094
+ .then((nc) => {
1095
+ that.nc = nc;
1096
+ const subject = `chat4app.config.${this.configId}.user.${this.chatUserExternalId}`;
1097
+ const sub = that.nc.subscribe(subject, (msg) => {
1098
+ if (this.debug) {
1099
+ console.log(msg);
1100
+ }
1101
+ this.processAndCallback(JSON.parse(msg.data));
1102
+ });
1242
1103
  if (this.interval) {
1243
1104
  try {
1244
1105
  clearInterval(this.interval);
@@ -1246,34 +1107,68 @@ class Chat4appClientAngularService {
1246
1107
  catch (e) {
1247
1108
  }
1248
1109
  }
1249
- if (!ignoreOffLine && this.nc && this.chatUserExternalId && this.chatUserExternalId.length > 0 && this.getBearerToken() && this.getBearerToken().length > 0) {
1250
- try {
1251
- yield lastValueFrom(this.setOffLine(this.chatUserExternalId));
1252
- if (this.debug)
1253
- console.log("had set offline");
1254
- }
1255
- catch (e) {
1256
- if (this.debug)
1257
- console.error("Erro ao setar offline", e);
1110
+ that.interval = setInterval(() => {
1111
+ if (that.debug) {
1112
+ console.log(`publish chat4app.user.online ${this.chatUserExternalId} `);
1258
1113
  }
1259
- }
1260
- if (this.nc) {
1261
1114
  try {
1262
- yield this.nc.close();
1115
+ that.nc.publish(`chat4app.user.online`, this.chatUserExternalId);
1263
1116
  }
1264
1117
  catch (e) {
1265
1118
  if (this.debug) {
1266
- console.error(e);
1119
+ console.log("try reconnect...");
1267
1120
  }
1121
+ this.ensureConn();
1268
1122
  }
1269
- this.nc = undefined;
1270
- }
1123
+ }, 1000);
1271
1124
  if (this.debug) {
1272
- console.log("disconect");
1125
+ console.log(`Subscribe online chat4app.user.${this.chatUserExternalId}.online`);
1273
1126
  }
1127
+ })
1128
+ .catch((err) => {
1129
+ // Notifica o chamador sobre o erro na conexão
1130
+ console.error("NATS Error:", err);
1274
1131
  });
1275
1132
  }
1276
- processAndCallback(msgObj, chatCallback) {
1133
+ disconnect() {
1134
+ return this.disconnectInternal(false);
1135
+ }
1136
+ disconnectInternal(ignoreOffLine) {
1137
+ // Check how we can disconnect
1138
+ if (this.interval) {
1139
+ try {
1140
+ clearInterval(this.interval);
1141
+ }
1142
+ catch (e) {
1143
+ }
1144
+ }
1145
+ if (!ignoreOffLine && this.nc && this.chatUserExternalId && this.chatUserExternalId.length > 0 && this.getBearerToken() && this.getBearerToken().length > 0) {
1146
+ try {
1147
+ this.setOffLine(this.chatUserExternalId).toPromise();
1148
+ if (this.debug)
1149
+ console.log("had set offline");
1150
+ }
1151
+ catch (e) {
1152
+ if (this.debug)
1153
+ console.error("Erro ao setar offline", e);
1154
+ }
1155
+ }
1156
+ if (this.nc) {
1157
+ try {
1158
+ this.nc.close();
1159
+ }
1160
+ catch (e) {
1161
+ if (this.debug) {
1162
+ console.error(e);
1163
+ }
1164
+ }
1165
+ this.nc = undefined;
1166
+ }
1167
+ if (this.debug) {
1168
+ console.log("disconect");
1169
+ }
1170
+ }
1171
+ processAndCallback(msgObj) {
1277
1172
  if (this.debug && msgObj) {
1278
1173
  console.log("Msg Received: " + JSON.stringify(msgObj));
1279
1174
  }
@@ -1284,92 +1179,45 @@ class Chat4appClientAngularService {
1284
1179
  }
1285
1180
  msgObj.data.all = datTmp;
1286
1181
  if (msgObj.data.type == "msg") {
1287
- chatCallback.onMessage(msgObj);
1182
+ this.chatCallback.onMessage(msgObj);
1288
1183
  }
1289
1184
  else if ((msgObj.data.type == "on" || msgObj.data.type == "off")) {
1290
- chatCallback.onOffline(msgObj);
1185
+ this.chatCallback.onOffline(msgObj);
1291
1186
  }
1292
1187
  else if (msgObj.data.type == "read") {
1293
- chatCallback.onRead(msgObj);
1188
+ this.chatCallback.onRead(msgObj);
1294
1189
  }
1295
1190
  else if (msgObj.data.type == "startTyping" || msgObj.data.type == "stopTyping") {
1296
- chatCallback.onStartStopTyping(msgObj);
1191
+ this.chatCallback.onStartStopTyping(msgObj);
1297
1192
  }
1298
1193
  else if (msgObj.data.type == "new_user") {
1299
- chatCallback.onNewUser(msgObj);
1194
+ this.chatCallback.onNewUser(msgObj);
1300
1195
  }
1301
1196
  else if (msgObj.data.type == "remove_user") {
1302
- chatCallback.onRemoveUser(msgObj);
1197
+ this.chatCallback.onRemoveUser(msgObj);
1303
1198
  }
1304
1199
  else if (msgObj.data.type == "chat_closed") {
1305
- chatCallback.onChatClosed(msgObj);
1200
+ this.chatCallback.onChatClosed(msgObj);
1306
1201
  }
1307
1202
  else if (msgObj.data.type == "count_notread") {
1308
- chatCallback.onCountNotRead(msgObj);
1203
+ this.chatCallback.onCountNotRead(msgObj);
1309
1204
  }
1310
1205
  //}
1311
1206
  }
1312
- connectWebsocket(chatCallback, userId) {
1313
- return __awaiter(this, void 0, void 0, function* () {
1314
- const that = this;
1315
- this.disconnectInternal(true);
1316
- let securityToken = this.getBearerToken();
1317
- let tokenAuth = "";
1318
- if (securityToken && securityToken.startsWith("Bearer ")) {
1319
- tokenAuth = securityToken;
1320
- }
1321
- else {
1322
- tokenAuth = "Bearer " + securityToken;
1323
- }
1324
- const configId = this.chatServer.split("/")[3];
1325
- const nc = yield this.ensureConn(userId);
1326
- //TODO Get the user id from jwt
1327
- const subject = `chat4app.config.${configId}.user.${userId}`;
1328
- const sub = nc.subscribe(subject, (msg) => {
1329
- if (this.debug) {
1330
- console.log(msg);
1331
- }
1332
- this.processAndCallback(JSON.parse(msg.data), chatCallback);
1333
- });
1334
- if (this.interval) {
1335
- try {
1336
- clearInterval(this.interval);
1337
- }
1338
- catch (e) {
1339
- }
1340
- }
1341
- that.interval = setInterval(() => {
1342
- if (that.debug) {
1343
- console.log(`publish chat4app.user.online ${userId} `);
1344
- }
1345
- try {
1346
- that.nc.publish(`chat4app.user.online`, userId);
1347
- }
1348
- catch (e) {
1349
- if (this.debug) {
1350
- console.log("try reconnect...");
1351
- }
1352
- this.ensureConn(userId, true);
1353
- }
1354
- }, 1000);
1355
- if (this.debug) {
1356
- console.log(`Subscribe online chat4app.user.${userId}.online`);
1357
- }
1358
- });
1359
- }
1360
- /*
1361
- disconnect() {
1362
- // console.log("disconnect")
1363
- try {
1364
- if (this.stompClient) {
1365
- this.stompClient.disconnect();
1207
+ connectWebsocket() {
1208
+ const that = this;
1209
+ this.disconnectInternal(true);
1210
+ let securityToken = this.getBearerToken();
1211
+ let tokenAuth = "";
1212
+ if (securityToken && securityToken.startsWith("Bearer ")) {
1213
+ tokenAuth = securityToken;
1366
1214
  }
1367
-
1368
- } catch (e) {
1369
- console.log(e);
1370
- }
1215
+ else {
1216
+ tokenAuth = "Bearer " + securityToken;
1217
+ }
1218
+ this.configId = this.chatServer.split("/")[3];
1219
+ this.ensureConn();
1371
1220
  }
1372
- */
1373
1221
  convertChat4AppMessageReceiveToChatMessage4App(msg) {
1374
1222
  var _a;
1375
1223
  let message = new ChatMessage4App();