@droppii-org/chat-mobile 0.2.81 → 0.2.82

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.
Files changed (151) hide show
  1. package/lib/module/components/ThreadCard/MentionBadge.js +24 -0
  2. package/lib/module/components/ThreadCard/MentionBadge.js.map +1 -0
  3. package/lib/module/components/ThreadCard/ThreadCard.js +5 -1
  4. package/lib/module/components/ThreadCard/ThreadCard.js.map +1 -1
  5. package/lib/module/components/messages/linkMessage/LinkPreviewCard.js +67 -0
  6. package/lib/module/components/messages/linkMessage/LinkPreviewCard.js.map +1 -0
  7. package/lib/module/components/messages/linkMessage/index.js +21 -68
  8. package/lib/module/components/messages/linkMessage/index.js.map +1 -1
  9. package/lib/module/components/messages/quotedMessage/QuotedMessagePreviewStrip.js +152 -0
  10. package/lib/module/components/messages/quotedMessage/QuotedMessagePreviewStrip.js.map +1 -0
  11. package/lib/module/components/messages/textMessage/index.js +102 -8
  12. package/lib/module/components/messages/textMessage/index.js.map +1 -1
  13. package/lib/module/core/useChatListener.js +1 -1
  14. package/lib/module/core/useChatListener.js.map +1 -1
  15. package/lib/module/hooks/message/useSendMessage.js +64 -3
  16. package/lib/module/hooks/message/useSendMessage.js.map +1 -1
  17. package/lib/module/hooks/query-keys.js +4 -0
  18. package/lib/module/hooks/query-keys.js.map +1 -1
  19. package/lib/module/hooks/useChatCompose.js +3 -2
  20. package/lib/module/hooks/useChatCompose.js.map +1 -1
  21. package/lib/module/screens/chat-detail/ChatComposer.js +170 -48
  22. package/lib/module/screens/chat-detail/ChatComposer.js.map +1 -1
  23. package/lib/module/screens/chat-detail/ChatListLegend.js +1 -0
  24. package/lib/module/screens/chat-detail/ChatListLegend.js.map +1 -1
  25. package/lib/module/screens/chat-detail/components/ChatMessageInput.js +23 -2
  26. package/lib/module/screens/chat-detail/components/ChatMessageInput.js.map +1 -1
  27. package/lib/module/screens/chat-detail/components/MentionHighlightAdapter.js +51 -0
  28. package/lib/module/screens/chat-detail/components/MentionHighlightAdapter.js.map +1 -0
  29. package/lib/module/screens/chat-detail/components/MentionSuggestionItem.js +49 -0
  30. package/lib/module/screens/chat-detail/components/MentionSuggestionItem.js.map +1 -0
  31. package/lib/module/screens/chat-detail/components/MentionSuggestionList.js +114 -0
  32. package/lib/module/screens/chat-detail/components/MentionSuggestionList.js.map +1 -0
  33. package/lib/module/screens/chat-detail/hooks/useChatComposerState.js +8 -0
  34. package/lib/module/screens/chat-detail/hooks/useChatComposerState.js.map +1 -1
  35. package/lib/module/screens/chat-detail/hooks/useMentionComposer.js +251 -0
  36. package/lib/module/screens/chat-detail/hooks/useMentionComposer.js.map +1 -0
  37. package/lib/module/screens/chat-detail/legend/LegendChatMessage.js +5 -1
  38. package/lib/module/screens/chat-detail/legend/LegendChatMessage.js.map +1 -1
  39. package/lib/module/services/mentionSearch.js +49 -0
  40. package/lib/module/services/mentionSearch.js.map +1 -0
  41. package/lib/module/services/message.js +6 -1
  42. package/lib/module/services/message.js.map +1 -1
  43. package/lib/module/translation/resources/i18n.js +5 -0
  44. package/lib/module/translation/resources/i18n.js.map +1 -1
  45. package/lib/module/types/chat.js.map +1 -1
  46. package/lib/module/types/mention.js +2 -0
  47. package/lib/module/types/mention.js.map +1 -0
  48. package/lib/module/utils/mentionSerializer.js +113 -0
  49. package/lib/module/utils/mentionSerializer.js.map +1 -0
  50. package/lib/module/utils/mentions/index.js +7 -0
  51. package/lib/module/utils/mentions/index.js.map +1 -0
  52. package/lib/module/utils/mentions/mentionDetector.js +49 -0
  53. package/lib/module/utils/mentions/mentionDetector.js.map +1 -0
  54. package/lib/module/utils/mentions/mentionDiff.js +66 -0
  55. package/lib/module/utils/mentions/mentionDiff.js.map +1 -0
  56. package/lib/module/utils/mentions/mentionRange.js +188 -0
  57. package/lib/module/utils/mentions/mentionRange.js.map +1 -0
  58. package/lib/module/utils/mentions/mentionTokenize.js +50 -0
  59. package/lib/module/utils/mentions/mentionTokenize.js.map +1 -0
  60. package/lib/module/utils/messageUtils.js +6 -1
  61. package/lib/module/utils/messageUtils.js.map +1 -1
  62. package/lib/module/utils/url.js +33 -0
  63. package/lib/module/utils/url.js.map +1 -1
  64. package/lib/typescript/src/components/ThreadCard/MentionBadge.d.ts +7 -0
  65. package/lib/typescript/src/components/ThreadCard/MentionBadge.d.ts.map +1 -0
  66. package/lib/typescript/src/components/ThreadCard/ThreadCard.d.ts.map +1 -1
  67. package/lib/typescript/src/components/messages/linkMessage/LinkPreviewCard.d.ts +16 -0
  68. package/lib/typescript/src/components/messages/linkMessage/LinkPreviewCard.d.ts.map +1 -0
  69. package/lib/typescript/src/components/messages/linkMessage/index.d.ts.map +1 -1
  70. package/lib/typescript/src/components/messages/quotedMessage/QuotedMessagePreviewStrip.d.ts +13 -0
  71. package/lib/typescript/src/components/messages/quotedMessage/QuotedMessagePreviewStrip.d.ts.map +1 -0
  72. package/lib/typescript/src/components/messages/textMessage/index.d.ts +3 -1
  73. package/lib/typescript/src/components/messages/textMessage/index.d.ts.map +1 -1
  74. package/lib/typescript/src/hooks/message/useSendMessage.d.ts +5 -2
  75. package/lib/typescript/src/hooks/message/useSendMessage.d.ts.map +1 -1
  76. package/lib/typescript/src/hooks/query-keys.d.ts +4 -0
  77. package/lib/typescript/src/hooks/query-keys.d.ts.map +1 -1
  78. package/lib/typescript/src/hooks/useChatCompose.d.ts +2 -1
  79. package/lib/typescript/src/hooks/useChatCompose.d.ts.map +1 -1
  80. package/lib/typescript/src/screens/chat-detail/ChatComposer.d.ts.map +1 -1
  81. package/lib/typescript/src/screens/chat-detail/ChatListLegend.d.ts.map +1 -1
  82. package/lib/typescript/src/screens/chat-detail/components/ChatMessageInput.d.ts +10 -2
  83. package/lib/typescript/src/screens/chat-detail/components/ChatMessageInput.d.ts.map +1 -1
  84. package/lib/typescript/src/screens/chat-detail/components/MentionHighlightAdapter.d.ts +29 -0
  85. package/lib/typescript/src/screens/chat-detail/components/MentionHighlightAdapter.d.ts.map +1 -0
  86. package/lib/typescript/src/screens/chat-detail/components/MentionSuggestionItem.d.ts +8 -0
  87. package/lib/typescript/src/screens/chat-detail/components/MentionSuggestionItem.d.ts.map +1 -0
  88. package/lib/typescript/src/screens/chat-detail/components/MentionSuggestionList.d.ts +12 -0
  89. package/lib/typescript/src/screens/chat-detail/components/MentionSuggestionList.d.ts.map +1 -0
  90. package/lib/typescript/src/screens/chat-detail/hooks/useChatComposerState.d.ts +1 -1
  91. package/lib/typescript/src/screens/chat-detail/hooks/useChatComposerState.d.ts.map +1 -1
  92. package/lib/typescript/src/screens/chat-detail/hooks/useMentionComposer.d.ts +48 -0
  93. package/lib/typescript/src/screens/chat-detail/hooks/useMentionComposer.d.ts.map +1 -0
  94. package/lib/typescript/src/screens/chat-detail/legend/LegendChatMessage.d.ts.map +1 -1
  95. package/lib/typescript/src/services/mentionSearch.d.ts +15 -0
  96. package/lib/typescript/src/services/mentionSearch.d.ts.map +1 -0
  97. package/lib/typescript/src/services/message.d.ts +1 -1
  98. package/lib/typescript/src/services/message.d.ts.map +1 -1
  99. package/lib/typescript/src/translation/resources/i18n.d.ts.map +1 -1
  100. package/lib/typescript/src/types/chat.d.ts +23 -0
  101. package/lib/typescript/src/types/chat.d.ts.map +1 -1
  102. package/lib/typescript/src/types/mention.d.ts +65 -0
  103. package/lib/typescript/src/types/mention.d.ts.map +1 -0
  104. package/lib/typescript/src/utils/mentionSerializer.d.ts +78 -0
  105. package/lib/typescript/src/utils/mentionSerializer.d.ts.map +1 -0
  106. package/lib/typescript/src/utils/mentions/index.d.ts +5 -0
  107. package/lib/typescript/src/utils/mentions/index.d.ts.map +1 -0
  108. package/lib/typescript/src/utils/mentions/mentionDetector.d.ts +18 -0
  109. package/lib/typescript/src/utils/mentions/mentionDetector.d.ts.map +1 -0
  110. package/lib/typescript/src/utils/mentions/mentionDiff.d.ts +20 -0
  111. package/lib/typescript/src/utils/mentions/mentionDiff.d.ts.map +1 -0
  112. package/lib/typescript/src/utils/mentions/mentionRange.d.ts +86 -0
  113. package/lib/typescript/src/utils/mentions/mentionRange.d.ts.map +1 -0
  114. package/lib/typescript/src/utils/mentions/mentionTokenize.d.ts +29 -0
  115. package/lib/typescript/src/utils/mentions/mentionTokenize.d.ts.map +1 -0
  116. package/lib/typescript/src/utils/messageUtils.d.ts.map +1 -1
  117. package/lib/typescript/src/utils/url.d.ts +12 -0
  118. package/lib/typescript/src/utils/url.d.ts.map +1 -1
  119. package/package.json +1 -1
  120. package/src/components/ThreadCard/MentionBadge.tsx +28 -0
  121. package/src/components/ThreadCard/ThreadCard.tsx +3 -0
  122. package/src/components/messages/linkMessage/LinkPreviewCard.tsx +92 -0
  123. package/src/components/messages/linkMessage/index.tsx +25 -89
  124. package/src/components/messages/quotedMessage/QuotedMessagePreviewStrip.tsx +168 -0
  125. package/src/components/messages/textMessage/index.tsx +124 -4
  126. package/src/core/useChatListener.ts +1 -1
  127. package/src/hooks/message/useSendMessage.ts +60 -1
  128. package/src/hooks/query-keys.ts +6 -0
  129. package/src/hooks/useChatCompose.ts +115 -107
  130. package/src/screens/chat-detail/ChatComposer.tsx +201 -67
  131. package/src/screens/chat-detail/ChatListLegend.tsx +1 -0
  132. package/src/screens/chat-detail/components/ChatMessageInput.tsx +35 -2
  133. package/src/screens/chat-detail/components/MentionHighlightAdapter.tsx +57 -0
  134. package/src/screens/chat-detail/components/MentionSuggestionItem.tsx +53 -0
  135. package/src/screens/chat-detail/components/MentionSuggestionList.tsx +133 -0
  136. package/src/screens/chat-detail/hooks/useChatComposerState.ts +7 -1
  137. package/src/screens/chat-detail/hooks/useMentionComposer.ts +344 -0
  138. package/src/screens/chat-detail/legend/LegendChatMessage.tsx +7 -1
  139. package/src/services/mentionSearch.ts +57 -0
  140. package/src/services/message.ts +9 -1
  141. package/src/translation/resources/i18n.ts +6 -0
  142. package/src/types/chat.ts +22 -0
  143. package/src/types/mention.ts +70 -0
  144. package/src/utils/mentionSerializer.ts +155 -0
  145. package/src/utils/mentions/index.ts +16 -0
  146. package/src/utils/mentions/mentionDetector.ts +58 -0
  147. package/src/utils/mentions/mentionDiff.ts +95 -0
  148. package/src/utils/mentions/mentionRange.ts +207 -0
  149. package/src/utils/mentions/mentionTokenize.ts +53 -0
  150. package/src/utils/messageUtils.ts +6 -1
  151. package/src/utils/url.ts +30 -0
@@ -137,8 +137,14 @@ export const LegendChatMessage = memo(
137
137
 
138
138
  const hasMediaPress =
139
139
  MEDIA_TYPES.includes(message?.contentType) && onMediaPress;
140
+ // AtTextMessage included: mention+URL combo attaches its link-preview
141
+ // card via ex.urlMetadata on an AtTextMessage (see TextMessage's doc
142
+ // comment) instead of a dedicated UrlTextMessage — the card still needs
143
+ // a tap handler.
140
144
  const hasUrlPress =
141
- onPressUrl && message?.contentType === MessageType.UrlTextMessage;
145
+ onPressUrl &&
146
+ (message?.contentType === MessageType.UrlTextMessage ||
147
+ message?.contentType === MessageType.AtTextMessage);
142
148
 
143
149
  return (
144
150
  <KContainer.View style={messageStyles.wrapper} row alignItems>
@@ -0,0 +1,57 @@
1
+ import OpenIMSDK, {
2
+ GroupMemberFilter,
3
+ type GroupMemberItem,
4
+ } from '@droppii/openim-rn-client-sdk';
5
+ import type {
6
+ MentionSearchResult,
7
+ MentionSearchSource,
8
+ MentionSuggestion,
9
+ } from '../types/mention';
10
+
11
+ const toSuggestion = (member: GroupMemberItem): MentionSuggestion => ({
12
+ userId: member.userID,
13
+ displayName: member.nickname,
14
+ avatar: member.faceURL,
15
+ });
16
+
17
+ /**
18
+ * Concrete MentionSearchSource implementation for Part A's mention
19
+ * composer, wrapping the OpenIM SDK's group-member primitives — 0 call
20
+ * sites anywhere else in this repo before this feature. Empty keyword ->
21
+ * the full/initial member list (getGroupMemberList); non-empty keyword ->
22
+ * server-backed search (searchGroupMembers), purpose-built for this case.
23
+ *
24
+ * Neither SDK call returns a total count, so `hasMore` is inferred from
25
+ * page size (a full page *might* mean there's another one) — the same
26
+ * heuristic every offset/count-paginated list in this codebase already
27
+ * relies on, since OpenIM doesn't expose a cheaper alternative here.
28
+ */
29
+ export function createOpenIMMentionSearchSource(
30
+ groupID: string
31
+ ): MentionSearchSource {
32
+ return {
33
+ async search({ keyword, limit, offset }): Promise<MentionSearchResult> {
34
+ const members =
35
+ keyword === ''
36
+ ? await OpenIMSDK.getGroupMemberList({
37
+ groupID,
38
+ filter: GroupMemberFilter.All,
39
+ offset,
40
+ count: limit,
41
+ })
42
+ : await OpenIMSDK.searchGroupMembers({
43
+ groupID,
44
+ keywordList: [keyword],
45
+ isSearchUserID: false,
46
+ isSearchMemberNickname: true,
47
+ offset,
48
+ count: limit,
49
+ });
50
+
51
+ return {
52
+ items: members.map(toSuggestion),
53
+ hasMore: members.length === limit,
54
+ };
55
+ },
56
+ };
57
+ }
@@ -67,7 +67,15 @@ export namespace ChatMessageAPI {
67
67
  }
68
68
 
69
69
  export async function markConversationAsRead(conversationID: string) {
70
- return OpenIMSDK.markConversationMessageAsRead(conversationID);
70
+ // resetConversationGroupAtType clears the "@ mention" flag
71
+ // (ConversationItem.groupAtType) — the core SDK never resets it on its
72
+ // own (confirmed: markConversationMessageAsRead only zeroes
73
+ // unreadCount), so it has to be requested explicitly at the same
74
+ // "user is caught up on this conversation" moment as the read marker.
75
+ return Promise.all([
76
+ OpenIMSDK.markConversationMessageAsRead(conversationID),
77
+ OpenIMSDK.resetConversationGroupAtType(conversationID),
78
+ ]);
71
79
  }
72
80
 
73
81
  export async function getCurrentUserId() {
@@ -215,6 +215,12 @@ const i18nKey: Record<string, string> = {
215
215
  'group:dismiss_confirm_primary': 'Giải tán',
216
216
  'group:dismiss_confirm_secondary': 'Quay lại',
217
217
  'group:dismiss_toast_success': 'Đã giải tán nhóm',
218
+ 'group:tag_title': 'Nhắc đến',
219
+ 'group:tag_all': 'Tất cả',
220
+ 'group:tag_all_desc': 'Nhắc đến tất cả thành viên',
221
+ 'group:tag_no_member_found': 'Không tìm thấy thành viên nào',
222
+ 'group:tag_limit_exceeded':
223
+ 'Chỉ {{max}} người được nhắc đến đầu tiên sẽ nhận được thông báo',
218
224
  'group:access_blocked_title': 'Không thể xem cuộc trò chuyện này',
219
225
  'group:access_blocked_desc':
220
226
  'Bạn hiện không có quyền truy cập vào cuộc trò chuyện hoặc tin nhắn này.',
package/src/types/chat.ts CHANGED
@@ -160,6 +160,12 @@ export interface DConversationQueryParams {
160
160
  pageSize: number;
161
161
  }
162
162
 
163
+ export interface ISerializedMentionEx {
164
+ userID: string;
165
+ offset: number;
166
+ length: number;
167
+ }
168
+
163
169
  export interface IMessageItemEx {
164
170
  applicationType: DChatApplicationType;
165
171
  sessionId?: string;
@@ -171,6 +177,22 @@ export interface IMessageItemEx {
171
177
  urlMetadata?: IUrlMetadata;
172
178
  flow?: IRunConversationFlowPayload;
173
179
  stickerId?: string;
180
+ /**
181
+ * @mention ranges, riding in this app-defined `ex` channel because
182
+ * OpenIM's own AtTextElem/AtMsgParams carry no per-mention offset/length
183
+ * — just a flat atUserIDList/atUsersInfo. Render logic prefers this
184
+ * (exact) and falls back to searching AtTextElem.text for
185
+ * "@{groupNickname}" per atUsersInfo entry when absent (interop safety
186
+ * net — see mentionSerializer.ts deserializeMentionRanges).
187
+ *
188
+ * Wrapped under `offsets` (not a bare array) to match the wire shape
189
+ * already sent by other Droppii clients (confirmed against a real
190
+ * message from the web/CRM client, `applicationType: "OBEFE"`:
191
+ * `ex.mentions = { offsets: [{userID, offset, length}, ...] }`). Offset
192
+ * units matched too (UTF-16 code units) — verified against a real
193
+ * Vietnamese-diacritic payload, no cross-client encoding mismatch.
194
+ */
195
+ mentions?: { offsets: ISerializedMentionEx[] };
174
196
  }
175
197
 
176
198
  export interface IGetMessagesParams {
@@ -0,0 +1,70 @@
1
+ /**
2
+ * @mention composer types.
3
+ *
4
+ * All offsets throughout this module are raw JS string indices (UTF-16 code
5
+ * units) — the same unit RN's TextInput selection/cursor API uses on both
6
+ * platforms. This is a deliberate choice, not a default: it means offsets
7
+ * never need conversion at the RN boundary, at the cost of requiring
8
+ * surrogate-pair-aware handling wherever text is diffed (see mentionDiff.ts).
9
+ * AT Protocol-style byte offsets are unnecessary here because this data
10
+ * never crosses a non-JS boundary — see mentionSerializer.ts.
11
+ */
12
+
13
+ export interface Selection {
14
+ start: number;
15
+ end: number;
16
+ }
17
+
18
+ /**
19
+ * A committed mention: created only by explicit picker selection
20
+ * (insertMention), never inferred from text content. [start, end) excludes
21
+ * the trailing separator space inserted after the display name — see
22
+ * insertMention's doc comment for why.
23
+ */
24
+ export interface Mention {
25
+ id: string;
26
+ userId: string;
27
+ displayName: string;
28
+ start: number;
29
+ end: number;
30
+ }
31
+
32
+ /**
33
+ * The in-progress, uncommitted "@query" at the cursor. Freely editable
34
+ * character-by-character — never atomic, never promoted to a Mention except
35
+ * through explicit selection from the suggestion list.
36
+ */
37
+ export interface ActiveMention {
38
+ trigger: '@';
39
+ start: number;
40
+ end: number;
41
+ keyword: string;
42
+ }
43
+
44
+ export interface MentionSuggestion {
45
+ userId: string;
46
+ displayName: string;
47
+ avatar?: string;
48
+ }
49
+
50
+ /**
51
+ * The boundary between Part A (composer, no concrete OpenIM dependency) and
52
+ * Part B (OpenIM SDK integration). Part A depends only on this type,
53
+ * injected; Part B's `openIMMentionSearchSource` (src/services/mentionSearch.ts)
54
+ * implements it against the real SDK. Defined here, in Part A's own types
55
+ * file, rather than in the Part B service file, so the dependency direction
56
+ * stays correct: Part A defines the contract, Part B implements it.
57
+ */
58
+ export interface MentionSearchResult {
59
+ items: MentionSuggestion[];
60
+ /** True when `items.length === limit` — a same-size next page may exist. */
61
+ hasMore: boolean;
62
+ }
63
+
64
+ export interface MentionSearchSource {
65
+ search(params: {
66
+ keyword: string;
67
+ limit: number;
68
+ offset: number;
69
+ }): Promise<MentionSearchResult>;
70
+ }
@@ -0,0 +1,155 @@
1
+ import type { AtUsersInfoItem } from '@droppii/openim-rn-client-sdk';
2
+ import type { ISerializedMentionEx } from '../types/chat';
3
+ import type { Mention } from '../types/mention';
4
+
5
+ /** Composer-internal marker for the "Tất cả" suggestion row / a committed
6
+ * "tag everyone" Mention — never sent on the wire, see AT_ALL_TAG below. */
7
+ export const ALL_MEMBERS_USER_ID = 'ALL';
8
+
9
+ /**
10
+ * OpenIM's own protocol sentinel for "everyone in the group", used inside
11
+ * atUserIDList in place of enumerating every member. Confirmed against
12
+ * openimsdk-core (pkg/constant/constant.go:242 — `AtAllString = "AtAllTag"`,
13
+ * exposed client-side as `GetAtAllTag()`) and openim-server's group-message
14
+ * send path (internal/rpc/msg/send.go's `setConversationAtInfo`), which
15
+ * special-cases exactly this string and does the "who is everyone right
16
+ * now" resolution itself, server-side.
17
+ *
18
+ * This is why "Tất cả" must serialize to this one token instead of
19
+ * expanding to every resolved member's userID client-side (the previous
20
+ * approach): the Go core's CreateTextAtMessage hard-rejects any
21
+ * atUserIDList longer than 10 entries regardless of group size
22
+ * (openimsdk-core/internal/conversation_msg/create_message.go:80-82) — a
23
+ * >10-member group's "Tất cả" silently failed to send at all. Sending just
24
+ * `[AT_ALL_TAG]` keeps atUserIDList small no matter the group size, and is
25
+ * also the behavior the protocol actually expects.
26
+ */
27
+ export const AT_ALL_TAG = 'AtAllTag';
28
+
29
+ /**
30
+ * OpenIM's Go core hard-rejects any AtMsgParams.atUserIDList longer than
31
+ * this (openimsdk-core/internal/conversation_msg/create_message.go:80-82,
32
+ * `if len(userIDList) > 10 { return nil, sdkerrs.ErrArgs }`) — not
33
+ * adjustable client-side. "Tất cả" no longer risks this (see AT_ALL_TAG),
34
+ * but tagging more than 10 *individual* members in one message still
35
+ * would, so serializeMentionsForSend caps atUserIDList/atUsersInfo at this
36
+ * count (keeping the first MAX_AT_USER_COUNT, in mention order) rather
37
+ * than letting the send fail outright the way "Tất cả" used to.
38
+ */
39
+ export const MAX_AT_USER_COUNT = 10;
40
+
41
+ export interface SerializedMentions {
42
+ atUserIDList: string[];
43
+ atUsersInfo: AtUsersInfoItem[];
44
+ exMentions: ISerializedMentionEx[];
45
+ /**
46
+ * True when more than MAX_AT_USER_COUNT distinct users were mentioned —
47
+ * atUserIDList/atUsersInfo were capped to the first MAX_AT_USER_COUNT (in
48
+ * mention order) so the send doesn't hit the Go core's hard rejection.
49
+ * `exMentions` is deliberately NOT capped: every mention still renders
50
+ * highlighted in the sent text (the sender sees what they typed), only
51
+ * native tag/notify is limited to the first MAX_AT_USER_COUNT. Callers
52
+ * use this flag to warn the user, not to block the send.
53
+ */
54
+ truncated: boolean;
55
+ }
56
+
57
+ /**
58
+ * Translates the composer's Mention[] (client-side ranges) into OpenIM's
59
+ * wire shapes for createTextAtMessage, plus our own ex.mentions channel. No
60
+ * group-member resolution needed here (or by the caller) any more — see
61
+ * AT_ALL_TAG's doc comment for why a "Tất cả" mention maps to that single
62
+ * sentinel rather than a per-member list.
63
+ */
64
+ export function serializeMentionsForSend(
65
+ mentions: Mention[]
66
+ ): SerializedMentions {
67
+ const wireUserId = (m: Mention) =>
68
+ m.userId === ALL_MEMBERS_USER_ID ? AT_ALL_TAG : m.userId;
69
+
70
+ const exMentions: ISerializedMentionEx[] = mentions.map((m) => ({
71
+ userID: wireUserId(m),
72
+ offset: m.start,
73
+ length: m.end - m.start,
74
+ }));
75
+
76
+ // Map dedupes by userID — e.g. "Tất cả" and an individually-tagged member
77
+ // both present in the same message only produce one atUserIDList entry
78
+ // each, not duplicates. Insertion order == mention order, which is what
79
+ // makes "keep the first MAX_AT_USER_COUNT" below well-defined.
80
+ const nicknameByUserId = new Map<string, string>();
81
+ for (const m of mentions) {
82
+ nicknameByUserId.set(wireUserId(m), m.displayName);
83
+ }
84
+
85
+ const allEntries = [...nicknameByUserId.entries()];
86
+ const truncated = allEntries.length > MAX_AT_USER_COUNT;
87
+ const cappedEntries = truncated
88
+ ? allEntries.slice(0, MAX_AT_USER_COUNT)
89
+ : allEntries;
90
+
91
+ return {
92
+ atUserIDList: cappedEntries.map(([atUserID]) => atUserID),
93
+ atUsersInfo: cappedEntries.map(([atUserID, groupNickname]) => ({
94
+ atUserID,
95
+ groupNickname,
96
+ })),
97
+ exMentions,
98
+ truncated,
99
+ };
100
+ }
101
+
102
+ interface ReceivedAtTextElem {
103
+ text: string;
104
+ atUserList: string[];
105
+ atUsersInfo?: AtUsersInfoItem[];
106
+ }
107
+
108
+ /**
109
+ * Reverse of serializeMentionsForSend, for rendering a received/sent
110
+ * AtTextMessage (Phase B3). Prefers `exMentions` (exact, our own channel);
111
+ * falls back to searching `atTextElem.text` for "@{groupNickname}" per
112
+ * atUsersInfo entry when ex is absent — an interop safety net, not the
113
+ * primary path. The fallback is inherently ambiguous if a member's
114
+ * nickname coincidentally appears elsewhere in the message as plain text;
115
+ * accepted as a known, documented limitation (see the mention plan's
116
+ * finding #3) rather than something client code can fully resolve without
117
+ * position data from the protocol.
118
+ */
119
+ export function deserializeMentionRanges(
120
+ atTextElem: ReceivedAtTextElem,
121
+ exMentions?: ISerializedMentionEx[]
122
+ ): Mention[] {
123
+ if (exMentions && exMentions.length > 0) {
124
+ return exMentions.map((e, index) => ({
125
+ id: `${e.userID}-${e.offset}-${index}`,
126
+ userId: e.userID,
127
+ displayName: atTextElem.text
128
+ .slice(e.offset, e.offset + e.length)
129
+ .replace(/^@/, ''),
130
+ start: e.offset,
131
+ end: e.offset + e.length,
132
+ }));
133
+ }
134
+
135
+ const nicknameByUserId = new Map(
136
+ (atTextElem.atUsersInfo ?? []).map((i) => [i.atUserID, i.groupNickname])
137
+ );
138
+
139
+ const mentions: Mention[] = [];
140
+ for (const userId of atTextElem.atUserList) {
141
+ const nickname = nicknameByUserId.get(userId);
142
+ if (!nickname) continue;
143
+ const needle = `@${nickname}`;
144
+ const start = atTextElem.text.indexOf(needle);
145
+ if (start === -1) continue;
146
+ mentions.push({
147
+ id: `${userId}-${start}`,
148
+ userId,
149
+ displayName: nickname,
150
+ start,
151
+ end: start + needle.length,
152
+ });
153
+ }
154
+ return mentions;
155
+ }
@@ -0,0 +1,16 @@
1
+ export { findActiveMention } from './mentionDetector';
2
+ export {
3
+ diffTextChange,
4
+ snapBoundary,
5
+ type TextChangeDiff,
6
+ } from './mentionDiff';
7
+ export {
8
+ insertMention,
9
+ reconcileMentions,
10
+ normalizeDeleteRange,
11
+ applyTextChange,
12
+ type InsertMentionResult,
13
+ type ReconcileMentionsResult,
14
+ type ApplyTextChangeResult,
15
+ } from './mentionRange';
16
+ export { tokenize, type Segment } from './mentionTokenize';
@@ -0,0 +1,58 @@
1
+ import type { ActiveMention } from '../../types/mention';
2
+
3
+ // No realistic @query is longer than this — caps the backward scan so the
4
+ // common case (cursor nowhere near an '@') bails in O(word length), not
5
+ // O(text length).
6
+ const MAX_ACTIVE_MENTION_SCAN = 64;
7
+
8
+ const isBoundaryChar = (ch: string) => /\s/.test(ch);
9
+
10
+ /**
11
+ * Scans backward from `cursor` for an unclosed "@query" — the in-progress
12
+ * trigger that drives the suggestion list, not a committed mention.
13
+ *
14
+ * Only called for a collapsed cursor (selection.start === selection.end);
15
+ * the caller must resolve a range selection to `null` before calling this —
16
+ * a live "@query" only makes sense at a single insertion point.
17
+ *
18
+ * "@" itself counts as a boundary: the scan stops there whether or not it's
19
+ * a valid trigger, which is what prevents re-scanning back through an
20
+ * already-committed mention's "@Name " when the user starts typing a second
21
+ * "@" right after it (insertMention always appends a trailing space, a
22
+ * boundary char, so the previous mention's "@" is never reachable from the
23
+ * new query).
24
+ */
25
+ export function findActiveMention(
26
+ text: string,
27
+ cursor: number
28
+ ): ActiveMention | null {
29
+ if (cursor <= 0 || cursor > text.length) return null;
30
+
31
+ let i = cursor;
32
+ let scanned = 0;
33
+
34
+ while (i > 0 && scanned < MAX_ACTIVE_MENTION_SCAN) {
35
+ const ch = text[i - 1] as string;
36
+
37
+ if (ch === '@') {
38
+ const before = i - 2 >= 0 ? text[i - 2] : undefined;
39
+ if (before === undefined || isBoundaryChar(before)) {
40
+ return {
41
+ trigger: '@',
42
+ start: i - 1,
43
+ end: cursor,
44
+ keyword: text.slice(i, cursor),
45
+ };
46
+ }
47
+ // '@' present but not at a valid boundary (e.g. "user@domain") — not a trigger.
48
+ return null;
49
+ }
50
+
51
+ if (isBoundaryChar(ch)) return null;
52
+
53
+ i--;
54
+ scanned++;
55
+ }
56
+
57
+ return null;
58
+ }
@@ -0,0 +1,95 @@
1
+ // RN's onChangeText hands over only the new full string — no delta, no
2
+ // info about what was inserted/deleted/replaced. A common-prefix +
3
+ // common-suffix scan is the only viable way to localize the changed region,
4
+ // and it's IME-safe for free: a Vietnamese diacritic recomposition (e.g.
5
+ // "tie" + accent key -> "tiế") is just "two different strings" to this
6
+ // algorithm, not a keystroke sequence it has to track.
7
+ //
8
+ // Assumes exactly one contiguous edit happened between the two strings —
9
+ // true for every realistic single-field mutation (typing, backspace,
10
+ // paste, autocorrect, IME composition). If that assumption is ever wrong
11
+ // (some exotic keyboard rewriting two disjoint spans in one event), the
12
+ // failure mode is safe-by-default: the computed region is a superset of
13
+ // the true edits, so callers may over-invalidate mention ranges but never
14
+ // corrupt them.
15
+
16
+ const isHighSurrogate = (code: number) => code >= 0xd800 && code <= 0xdbff;
17
+ const isLowSurrogate = (code: number) => code >= 0xdc00 && code <= 0xdfff;
18
+
19
+ /**
20
+ * Snaps `index` so it never sits between a surrogate pair in `str` —
21
+ * without this, diffing e.g. "😀" vs "😁" (which differ only in their low
22
+ * surrogate) would compute a boundary that bisects the emoji. Snapping
23
+ * always shrinks the "common" region (treats one more code point as
24
+ * "changed"), which is always safe: being more conservative about what
25
+ * counts as touched only ever produces more mention-invalidation upstream,
26
+ * never corruption.
27
+ */
28
+ export function snapBoundary(
29
+ str: string,
30
+ index: number,
31
+ direction: 1 | -1
32
+ ): number {
33
+ if (
34
+ index > 0 &&
35
+ index < str.length &&
36
+ isHighSurrogate(str.charCodeAt(index - 1)) &&
37
+ isLowSurrogate(str.charCodeAt(index))
38
+ ) {
39
+ return index + direction;
40
+ }
41
+ return index;
42
+ }
43
+
44
+ export interface TextChangeDiff {
45
+ /** [removedStart, removedEnd) in previousText coords — what was deleted. */
46
+ removedStart: number;
47
+ removedEnd: number;
48
+ /** [insertedStart, insertedEnd) in nextText coords — what was inserted. insertedStart === removedStart. */
49
+ insertedStart: number;
50
+ insertedEnd: number;
51
+ }
52
+
53
+ export function diffTextChange(
54
+ previousText: string,
55
+ nextText: string
56
+ ): TextChangeDiff {
57
+ const prevLen = previousText.length;
58
+ const nextLen = nextText.length;
59
+ const maxCommon = Math.min(prevLen, nextLen);
60
+
61
+ let prefixLen = 0;
62
+ while (
63
+ prefixLen < maxCommon &&
64
+ previousText.charCodeAt(prefixLen) === nextText.charCodeAt(prefixLen)
65
+ ) {
66
+ prefixLen++;
67
+ }
68
+ prefixLen = snapBoundary(previousText, prefixLen, -1);
69
+
70
+ // Clamp the suffix scan budget against what's left after the prefix —
71
+ // without this, a naive independent prefix+suffix scan can overlap on
72
+ // repeated-character edits (e.g. "aaa" -> "aaaa") and produce a
73
+ // negative-length "changed" region.
74
+ const maxSuffix = maxCommon - prefixLen;
75
+ let suffixLen = 0;
76
+ while (
77
+ suffixLen < maxSuffix &&
78
+ previousText.charCodeAt(prevLen - 1 - suffixLen) ===
79
+ nextText.charCodeAt(nextLen - 1 - suffixLen)
80
+ ) {
81
+ suffixLen++;
82
+ }
83
+
84
+ let removedEnd = prevLen - suffixLen;
85
+ let insertedEnd = nextLen - suffixLen;
86
+ removedEnd = snapBoundary(previousText, removedEnd, 1);
87
+ insertedEnd = snapBoundary(nextText, insertedEnd, 1);
88
+
89
+ return {
90
+ removedStart: prefixLen,
91
+ removedEnd,
92
+ insertedStart: prefixLen,
93
+ insertedEnd,
94
+ };
95
+ }