@ai-matrx/messaging 0.1.0 → 0.1.1

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.
package/dist/react.cjs CHANGED
@@ -1062,6 +1062,29 @@ function createMessagingStore() {
1062
1062
  });
1063
1063
  emit();
1064
1064
  },
1065
+ applyLastMessage(message, args = {}) {
1066
+ const existing = conversations.find(
1067
+ (item) => item.conversation.id === message.conversationId
1068
+ );
1069
+ if (existing === void 0) return;
1070
+ if (message.deliveryState === "sending" || message.deletedAt !== null) return;
1071
+ if (existing.lastMessageAt !== null && message.createdAt < existing.lastMessageAt) return;
1072
+ const isActive = activeConversationId === message.conversationId;
1073
+ const shouldCount = args.incrementUnread === true && !isActive;
1074
+ conversations = normalizeConversations(
1075
+ conversations.map(
1076
+ (item) => item.conversation.id === message.conversationId ? {
1077
+ ...item,
1078
+ lastMessageContent: message.content,
1079
+ lastMessageSenderId: message.senderId,
1080
+ lastMessageAt: message.createdAt,
1081
+ sortAt: message.createdAt,
1082
+ unreadCount: shouldCount ? item.unreadCount + 1 : item.unreadCount
1083
+ } : item
1084
+ )
1085
+ );
1086
+ emit();
1087
+ },
1065
1088
  markConversationRead(id) {
1066
1089
  conversations = conversations.map(
1067
1090
  (item) => item.conversation.id === id ? { ...item, unreadCount: 0 } : item
@@ -1132,7 +1155,7 @@ function createMessagingEngine(options) {
1132
1155
  onSent: (entry, message) => {
1133
1156
  store.ingest(message);
1134
1157
  broadcastMessage(message);
1135
- void refreshConversationRow(message.conversationId);
1158
+ store.applyLastMessage(message);
1136
1159
  void entry;
1137
1160
  },
1138
1161
  onChange: (entries) => {
@@ -1158,15 +1181,6 @@ function createMessagingEngine(options) {
1158
1181
  function broadcastMessage(message) {
1159
1182
  openChannels.get(message.conversationId)?.send(MESSAGING_EVENTS.message, message);
1160
1183
  }
1161
- async function refreshConversationRow(id) {
1162
- try {
1163
- const page = await repository.listConversations({ limit: conversationPageSize });
1164
- const row = page.items.find((item) => item.conversation.id === id);
1165
- if (row !== void 0) store.upsertConversation(row);
1166
- } catch (error) {
1167
- reportError(error, "refreshConversationRow");
1168
- }
1169
- }
1170
1184
  async function reloadInbox() {
1171
1185
  const page = await repository.listConversations({ limit: conversationPageSize });
1172
1186
  conversationCursor = page.nextCursor;
@@ -1217,10 +1231,9 @@ function createMessagingEngine(options) {
1217
1231
  return;
1218
1232
  }
1219
1233
  store.ingest(message);
1220
- if (message.senderId !== identity.userId) {
1221
- options.onIncoming?.(message);
1222
- }
1223
- void refreshConversationRow(message.conversationId);
1234
+ const isMine = message.senderId === identity.userId;
1235
+ store.applyLastMessage(message, { incrementUnread: !isMine });
1236
+ if (!isMine) options.onIncoming?.(message);
1224
1237
  }
1225
1238
  },
1226
1239
  {