@ermis-network/ermis-chat-sdk 1.0.1 → 1.0.3

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.
@@ -3547,17 +3547,24 @@ var ErmisChat = class _ErmisChat {
3547
3547
  async unpinChannel(channelType, channelId) {
3548
3548
  return await this.post(this.baseURL + `/channels/${channelType}/${channelId}/unpin`);
3549
3549
  }
3550
- channel(channelType, channelID, custom = {}) {
3550
+ channel(channelType, channelIDOrCustom, custom) {
3551
3551
  if (!this.userID) {
3552
3552
  throw Error("Call connectUser before creating a channel");
3553
3553
  }
3554
3554
  if (~channelType.indexOf(":")) {
3555
3555
  throw Error(`Invalid channel group ${channelType}, can't contain the : character`);
3556
3556
  }
3557
- return this.getChannelById(channelType, channelID, custom);
3557
+ let channelID = void 0;
3558
+ let customData = custom || {};
3559
+ if (typeof channelIDOrCustom === "string") {
3560
+ channelID = channelIDOrCustom;
3561
+ } else if (typeof channelIDOrCustom === "object" && channelIDOrCustom !== null) {
3562
+ customData = channelIDOrCustom;
3563
+ }
3564
+ return this.getChannelById(channelType, channelID, customData);
3558
3565
  }
3559
3566
  getChannelById = (channelType, channelID, custom) => {
3560
- const cid = `${channelType}:${channelID}`;
3567
+ const cid = `${channelType}:${channelID || ""}`;
3561
3568
  if (cid in this.activeChannels && !this.activeChannels[cid].disconnected) {
3562
3569
  const channel2 = this.activeChannels[cid];
3563
3570
  if (Object.keys(custom).length > 0) {
@@ -3600,7 +3607,7 @@ var ErmisChat = class _ErmisChat {
3600
3607
  return pinExpires;
3601
3608
  }
3602
3609
  getUserAgent() {
3603
- return this.userAgent || `ermis-chat-sdk-javascript-client-${this.node ? "node" : "browser"}-${"1.0.1"}`;
3610
+ return this.userAgent || `ermis-chat-sdk-javascript-client-${this.node ? "node" : "browser"}-${"1.0.3"}`;
3604
3611
  }
3605
3612
  setUserAgent(userAgent) {
3606
3613
  this.userAgent = userAgent;
@@ -6309,21 +6316,38 @@ var ErmisCallNode = class {
6309
6316
  const mediaConstraints = await this.getMediaConstraints();
6310
6317
  try {
6311
6318
  const stream = await navigator.mediaDevices.getUserMedia(mediaConstraints);
6312
- if (this.callStatus === "ended" /* ENDED */) {
6313
- stream.getTracks().forEach((track) => track.stop());
6314
- this.destroy();
6315
- return;
6319
+ return this.applyLocalStream(stream);
6320
+ } catch (error) {
6321
+ console.warn("Error getting user media:", error?.message);
6322
+ if (this.callType === "video" && mediaConstraints.video) {
6323
+ try {
6324
+ const audioOnlyStream = await navigator.mediaDevices.getUserMedia({
6325
+ audio: mediaConstraints.audio,
6326
+ video: false
6327
+ });
6328
+ this.setConnectionMessage("Camera not available, using audio only");
6329
+ return this.applyLocalStream(audioOnlyStream);
6330
+ } catch {
6331
+ }
6316
6332
  }
6317
- if (this.onLocalStream) {
6318
- this.onLocalStream(stream);
6333
+ if (typeof this.onError === "function") {
6334
+ this.onError("No microphone or camera found. Please check your device.");
6319
6335
  }
6320
- this.localStream = stream;
6321
- return stream;
6322
- } catch (error) {
6323
- console.error("Error getting user media:", error);
6324
6336
  return null;
6325
6337
  }
6326
6338
  }
6339
+ applyLocalStream(stream) {
6340
+ if (this.callStatus === "ended" /* ENDED */) {
6341
+ stream.getTracks().forEach((track) => track.stop());
6342
+ this.destroy();
6343
+ return;
6344
+ }
6345
+ if (this.onLocalStream) {
6346
+ this.onLocalStream(stream);
6347
+ }
6348
+ this.localStream = stream;
6349
+ return stream;
6350
+ }
6327
6351
  setConnectionMessage(message) {
6328
6352
  if (typeof this.onConnectionMessageChange === "function") {
6329
6353
  this.onConnectionMessageChange(message);
@@ -6368,20 +6392,10 @@ var ErmisCallNode = class {
6368
6392
  this.isDestroyed = false;
6369
6393
  this.callStatus = "";
6370
6394
  this.callType = is_video ? "video" : "audio";
6371
- await this.startLocalStream();
6372
- if (this.callStatus === "ended" /* ENDED */) return;
6373
6395
  this.setUserInfo(cid, eventUserId);
6374
6396
  this.setCallStatus("ringing" /* RINGING */);
6375
6397
  this.cid = cid || "";
6376
6398
  this.metadata = metadata || {};
6377
- console.log("----metadata---", metadata);
6378
- if (eventUserId !== this.userID) {
6379
- await this.initialize();
6380
- }
6381
- if (this.localStream && this.mediaSender && this.mediaReceiver) {
6382
- this.mediaSender?.initEncoders(this.localStream);
6383
- this.mediaReceiver?.initDecoders(this.callType);
6384
- }
6385
6399
  if (typeof this.onCallEvent === "function") {
6386
6400
  this.onCallEvent({
6387
6401
  type: eventUserId !== this.userID ? "incoming" : "outgoing",
@@ -6392,6 +6406,15 @@ var ErmisCallNode = class {
6392
6406
  metadata: this.metadata
6393
6407
  });
6394
6408
  }
6409
+ await this.startLocalStream();
6410
+ if (this.callStatus === "ended" /* ENDED */) return;
6411
+ if (eventUserId !== this.userID) {
6412
+ await this.initialize();
6413
+ }
6414
+ if (this.localStream && this.mediaSender && this.mediaReceiver) {
6415
+ this.mediaSender?.initEncoders(this.localStream);
6416
+ this.mediaReceiver?.initDecoders(this.callType);
6417
+ }
6395
6418
  if (eventUserId === this.userID) {
6396
6419
  if (this.missCallTimeout) clearTimeout(this.missCallTimeout);
6397
6420
  this.missCallTimeout = setTimeout(async () => {
@@ -6652,21 +6675,30 @@ var ErmisCallNode = class {
6652
6675
  }
6653
6676
  async stopScreenShare() {
6654
6677
  const mediaConstraints = await this.getMediaConstraints();
6655
- const cameraStream = await navigator.mediaDevices.getUserMedia(mediaConstraints);
6656
- const cameraTrack = cameraStream.getVideoTracks()[0];
6657
- if (this.localStream) {
6658
- this.localStream.getVideoTracks().forEach((track) => track.stop());
6659
- this.localStream.removeTrack(this.localStream.getVideoTracks()[0]);
6660
- this.localStream.addTrack(cameraTrack);
6661
- } else {
6662
- this.localStream = cameraStream;
6663
- }
6664
- if (this.onLocalStream) {
6665
- this.onLocalStream(this.localStream);
6666
- this.mediaSender?.replaceVideoTrack(this.localStream.getVideoTracks()[0]);
6667
- }
6668
- if (typeof this.onScreenShareChange === "function") {
6669
- this.onScreenShareChange(false);
6678
+ try {
6679
+ const cameraStream = await navigator.mediaDevices.getUserMedia({
6680
+ video: mediaConstraints.video,
6681
+ audio: false
6682
+ });
6683
+ const cameraTrack = cameraStream.getVideoTracks()[0];
6684
+ if (this.localStream) {
6685
+ this.localStream.getVideoTracks().forEach((track) => {
6686
+ track.stop();
6687
+ this.localStream?.removeTrack(track);
6688
+ });
6689
+ this.localStream.addTrack(cameraTrack);
6690
+ } else {
6691
+ this.localStream = cameraStream;
6692
+ }
6693
+ if (this.onLocalStream) {
6694
+ this.onLocalStream(this.localStream);
6695
+ this.mediaSender?.replaceVideoTrack(this.localStream.getVideoTracks()[0]);
6696
+ }
6697
+ if (typeof this.onScreenShareChange === "function") {
6698
+ this.onScreenShareChange(false);
6699
+ }
6700
+ } catch (error) {
6701
+ console.error("Error stopping screen share and reverting to camera:", error);
6670
6702
  }
6671
6703
  }
6672
6704
  async toggleMic(enabled) {
@@ -6933,7 +6965,7 @@ var ErmisAuthProvider = class {
6933
6965
  return data;
6934
6966
  }
6935
6967
  getUserAgent() {
6936
- return this.userAgent || `ermis-chat-sdk-javascript-client-${this.node ? "node" : "browser"}-${"1.0.1"}`;
6968
+ return this.userAgent || `ermis-chat-sdk-javascript-client-${this.node ? "node" : "browser"}-${"1.0.3"}`;
6937
6969
  }
6938
6970
  setUserAgent(userAgent) {
6939
6971
  this.userAgent = userAgent;
@@ -7166,70 +7198,116 @@ function parseSystemMessage(value, userMap) {
7166
7198
  }
7167
7199
 
7168
7200
  // src/signal_message.ts
7169
- function formatDuration(durationSec) {
7170
- const sec = parseInt(durationSec, 10);
7171
- if (isNaN(sec) || sec < 0) return durationSec;
7172
- const minutes = Math.floor(sec / 60);
7173
- const seconds = sec % 60;
7174
- return `${minutes}:${seconds.toString().padStart(2, "0")}`;
7175
- }
7176
- function resolveUser2(userId, userMap) {
7177
- return userMap[userId] ?? userId;
7201
+ var CallType = {
7202
+ AUDIO: "audio",
7203
+ VIDEO: "video"
7204
+ };
7205
+ function formatDuration(durationMs) {
7206
+ if (!durationMs) return "";
7207
+ const ms = parseInt(durationMs, 10);
7208
+ if (isNaN(ms) || ms <= 0) return "";
7209
+ const totalSeconds = Math.floor(ms / 1e3);
7210
+ const minutes = Math.floor(totalSeconds / 60);
7211
+ const seconds = totalSeconds % 60;
7212
+ return `${minutes} min, ${seconds} sec`;
7178
7213
  }
7179
- function parseSignalMessage(value, userMap) {
7180
- if (!value || typeof value !== "string") return value ?? "";
7214
+ function parseSignalMessage(value, myUserId) {
7215
+ if (!value || typeof value !== "string") return null;
7181
7216
  const trimmed = value.trim();
7182
- if (!trimmed) return "";
7217
+ if (!trimmed) return null;
7183
7218
  const parts = trimmed.split(" ");
7184
- const formatId = parts[0];
7185
- const userId = parts[1] ?? "";
7186
- const userName = userId ? resolveUser2(userId, userMap) : "User";
7187
- switch (formatId) {
7188
- // 1: Audio call started
7189
- case "1":
7190
- return `\u{1F4DE} ${userName} started an audio call.`;
7191
- // 2: Audio call missed
7192
- case "2":
7193
- return `\u{1F4DE} Missed audio call from ${userName}.`;
7194
- // 3: Audio call ended (caller_id ender_id duration)
7195
- case "3": {
7196
- const enderId = parts[2] ?? "";
7197
- const duration = parts[3] ?? "0";
7198
- const enderName = enderId ? resolveUser2(enderId, userMap) : "User";
7199
- return `\u{1F4DE} Audio call by ${userName}, ended by ${enderName}. Duration: ${formatDuration(duration)}.`;
7200
- }
7201
- // 4: Video call started
7202
- case "4":
7203
- return `\u{1F4F9} ${userName} started a video call.`;
7204
- // 5: Video call missed
7205
- case "5":
7206
- return `\u{1F4F9} Missed video call from ${userName}.`;
7207
- // 6: Video call ended (caller_id ender_id duration)
7208
- case "6": {
7209
- const enderId = parts[2] ?? "";
7210
- const duration = parts[3] ?? "0";
7211
- const enderName = enderId ? resolveUser2(enderId, userMap) : "User";
7212
- return `\u{1F4F9} Video call by ${userName}, ended by ${enderName}. Duration: ${formatDuration(duration)}.`;
7213
- }
7214
- // 7: Audio call rejected
7215
- case "7":
7216
- return `\u{1F4DE} Audio call from ${userName} was rejected.`;
7217
- // 8: Video call rejected
7218
- case "8":
7219
- return `\u{1F4F9} Video call from ${userName} was rejected.`;
7220
- // 9: Audio call busy
7221
- case "9":
7222
- return `\u{1F4DE} Audio call from ${userName} \u2014 recipient was busy.`;
7223
- // 10: Video call busy
7224
- case "10":
7225
- return `\u{1F4F9} Video call from ${userName} \u2014 recipient was busy.`;
7219
+ const number = parseInt(parts[0], 10);
7220
+ const callerId = parts[1] ?? "";
7221
+ const isMe = myUserId === callerId;
7222
+ let enderId = "";
7223
+ let duration = "";
7224
+ let callType = "";
7225
+ let color = "";
7226
+ if (number === 3 || number === 6) {
7227
+ enderId = parts[2] ?? "";
7228
+ duration = parts[3] === "0" ? "" : parts[3] ?? "";
7229
+ }
7230
+ let text;
7231
+ switch (number) {
7232
+ case 1:
7233
+ text = isMe ? "Calling..." : "Incoming audio call...";
7234
+ callType = CallType.AUDIO;
7235
+ color = "#54D62C";
7236
+ break;
7237
+ case 2:
7238
+ text = isMe ? "Outgoing audio call" : "You missed audio call";
7239
+ callType = CallType.AUDIO;
7240
+ color = "#FF4842";
7241
+ break;
7242
+ case 3:
7243
+ if (duration) {
7244
+ text = isMe ? "Outgoing audio call" : "Incoming audio call";
7245
+ color = "#54D62C";
7246
+ } else {
7247
+ if (enderId === myUserId) {
7248
+ text = "You cancel audio call";
7249
+ } else {
7250
+ text = "You missed audio call";
7251
+ }
7252
+ color = "#FF4842";
7253
+ }
7254
+ callType = CallType.AUDIO;
7255
+ break;
7256
+ case 4:
7257
+ text = isMe ? "Calling..." : "Incoming video call...";
7258
+ callType = CallType.VIDEO;
7259
+ color = "#54D62C";
7260
+ break;
7261
+ case 5:
7262
+ text = isMe ? "Outgoing video call" : "You missed video call";
7263
+ callType = CallType.VIDEO;
7264
+ color = "#FF4842";
7265
+ break;
7266
+ case 6:
7267
+ if (duration) {
7268
+ text = isMe ? "Outgoing video call" : "Incoming video call";
7269
+ color = "#54D62C";
7270
+ } else {
7271
+ if (enderId === myUserId) {
7272
+ text = "You cancel video call";
7273
+ } else {
7274
+ text = "You missed video call";
7275
+ }
7276
+ color = "#FF4842";
7277
+ }
7278
+ callType = CallType.VIDEO;
7279
+ break;
7280
+ case 7:
7281
+ text = isMe ? "Recipient rejected audio call" : "You rejected audio call";
7282
+ callType = CallType.AUDIO;
7283
+ color = "#FF4842";
7284
+ break;
7285
+ case 8:
7286
+ text = isMe ? "Recipient rejected video call" : "You rejected video call";
7287
+ callType = CallType.VIDEO;
7288
+ color = "#FF4842";
7289
+ break;
7290
+ case 9:
7291
+ text = isMe ? "Recipient was busy" : "You missed audio call";
7292
+ callType = CallType.AUDIO;
7293
+ color = "#FF4842";
7294
+ break;
7295
+ case 10:
7296
+ text = isMe ? "Recipient was busy" : "You missed video call";
7297
+ callType = CallType.VIDEO;
7298
+ color = "#FF4842";
7299
+ break;
7226
7300
  default:
7227
- return trimmed;
7301
+ text = trimmed;
7302
+ callType = "";
7303
+ color = "";
7228
7304
  }
7305
+ return { text, duration: formatDuration(duration), callType, color };
7229
7306
  }
7230
7307
  export {
7231
7308
  CallAction,
7232
7309
  CallStatus,
7310
+ CallType,
7233
7311
  Channel,
7234
7312
  ChannelState,
7235
7313
  ClientState,