chat4app-client-angular 1.0.15 → 1.0.17

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