@cometchat/chat-uikit-react-native 5.5.4 → 5.6.0
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/android/src/main/java/com/reactnativecometchatuikit/FileManager.java +78 -3
- package/android/src/main/java/com/reactnativecometchatuikit/SafeFileName.java +78 -0
- package/ios/CCLocalFileGuard.h +19 -0
- package/ios/CCLocalFileGuard.m +34 -0
- package/ios/FileManager.m +70 -6
- package/ios/VideoManager.m +1 -1
- package/package.json +18 -3
- package/src/CometChatCompactMessageComposer/CometChatCompactMessageComposer.tsx +124 -55
- package/src/CometChatConversations/CometChatConversations.tsx +240 -117
- package/src/CometChatGroupMembers/CometChatGroupMembers.tsx +4 -0
- package/src/CometChatGroups/CometChatGroups.tsx +61 -52
- package/src/CometChatMessageComposer/CometChatMessageComposer.tsx +146 -66
- package/src/CometChatMessageHeader/CometChatMessageHeader.tsx +7 -3
- package/src/CometChatMessageHeader/listners.ts +76 -56
- package/src/CometChatMessageList/CometChatMessageList.tsx +1371 -188
- package/src/CometChatNotificationFeed/CometChatNotificationFeed.tsx +71 -7
- package/src/CometChatPinnedMessages/CometChatPinnedMessages.tsx +74 -2
- package/src/CometChatSavedMessages/CometChatSavedMessages.tsx +69 -4
- package/src/CometChatSearch/CometChatSearch.tsx +353 -101
- package/src/CometChatThreadHeader/CometChatThreadHeader.tsx +23 -1
- package/src/CometChatUsers/CometChatUsers.tsx +2 -0
- package/src/calls/CometChatCallButtons/CometChatCallButtons.tsx +19 -0
- package/src/extensions/Stickers/StickersExtensionDecorator.tsx +2 -0
- package/src/index.ts +6 -0
- package/src/shared/CometChatUiKit/CometChatUIKit.ts +85 -2
- package/src/shared/CometChatUiKit/UIKitSettings.ts +20 -0
- package/src/shared/events/CometChatUIEventHandler/Listener.ts +10 -0
- package/src/shared/events/index.ts +4 -0
- package/src/shared/events/offline.ts +28 -0
- package/src/shared/formatters/CometChatMentionsFormatter/CometChatMentionsFormatter.tsx +5 -3
- package/src/shared/framework/MessageDataSource.tsx +34 -14
- package/src/shared/index.ts +2 -0
- package/src/shared/offline/OfflineHub.ts +651 -0
- package/src/shared/offline/OfflineMediaBubble.tsx +49 -0
- package/src/shared/offline/OfflineViews.tsx +218 -0
- package/src/shared/offline/conversationPins.ts +137 -0
- package/src/shared/offline/conversations.ts +325 -0
- package/src/shared/offline/frameProbe.ts +24 -0
- package/src/shared/offline/gate.ts +94 -0
- package/src/shared/offline/groupActions.ts +81 -0
- package/src/shared/offline/identity.ts +162 -0
- package/src/shared/offline/index.ts +76 -0
- package/src/shared/offline/media.ts +247 -0
- package/src/shared/offline/notices.ts +90 -0
- package/src/shared/offline/reconcile.ts +296 -0
- package/src/shared/offline/searchOutcome.ts +62 -0
- package/src/shared/offline/send.ts +430 -0
- package/src/shared/offline/stateContract.ts +85 -0
- package/src/shared/offline/threadReplies.ts +125 -0
- package/src/shared/offline/twoPhase.ts +372 -0
- package/src/shared/offline/unsentActions.ts +168 -0
- package/src/shared/offline/useOfflineMediaUri.ts +87 -0
- package/src/shared/offline/useOfflineReconnect.ts +25 -0
- package/src/shared/offline/useOfflineSessionEnded.ts +22 -0
- package/src/shared/offline/useOfflineUploadTray.ts +76 -0
- package/src/shared/offline/usePresenceHidden.ts +33 -0
- package/src/shared/resources/CometChatLocalizeNew/resources/de/translation.json +24 -1
- package/src/shared/resources/CometChatLocalizeNew/resources/en/translation.json +24 -1
- package/src/shared/resources/CometChatLocalizeNew/resources/es/translation.json +24 -1
- package/src/shared/resources/CometChatLocalizeNew/resources/fr/translation.json +24 -1
- package/src/shared/resources/CometChatLocalizeNew/resources/hi/translation.json +24 -1
- package/src/shared/resources/CometChatLocalizeNew/resources/hu/translation.json +24 -1
- package/src/shared/resources/CometChatLocalizeNew/resources/it/translation.json +24 -1
- package/src/shared/resources/CometChatLocalizeNew/resources/ja/translation.json +24 -1
- package/src/shared/resources/CometChatLocalizeNew/resources/ko/translation.json +24 -1
- package/src/shared/resources/CometChatLocalizeNew/resources/lt/translation.json +24 -1
- package/src/shared/resources/CometChatLocalizeNew/resources/ms/translation.json +24 -1
- package/src/shared/resources/CometChatLocalizeNew/resources/nl/translation.json +24 -1
- package/src/shared/resources/CometChatLocalizeNew/resources/pt/translation.json +24 -1
- package/src/shared/resources/CometChatLocalizeNew/resources/ru/translation.json +24 -1
- package/src/shared/resources/CometChatLocalizeNew/resources/sv/translation.json +24 -1
- package/src/shared/resources/CometChatLocalizeNew/resources/tr/translation.json +24 -1
- package/src/shared/resources/CometChatLocalizeNew/resources/zh/translation.json +24 -1
- package/src/shared/resources/CometChatLocalizeNew/resources/zh-tw/translation.json +24 -1
- package/src/shared/services/UploadQueueService.ts +106 -1
- package/src/shared/utils/MessageReceiptUtils.ts +8 -0
- package/src/shared/utils/SavedMessagesHelper.ts +18 -1
- package/src/shared/utils/catchUpCursor.ts +36 -0
- package/src/shared/utils/failedMessageRetry.ts +73 -0
- package/src/shared/utils/messageIdentity.ts +38 -0
- package/src/shared/views/CometChatAttachmentViewer/CometChatAttachmentViewer.tsx +34 -5
- package/src/shared/views/CometChatAudiosBubble/CometChatAudiosBubble.tsx +10 -2
- package/src/shared/views/CometChatFileBubble/CometChatFileBubble.tsx +13 -27
- package/src/shared/views/CometChatFilesBubble/CometChatFilesBubble.tsx +60 -11
- package/src/shared/views/CometChatImagesBubble/CometChatImagesBubble.tsx +27 -2
- package/src/shared/views/CometChatList/CometChatList.tsx +468 -7
- package/src/shared/views/CometChatMediaViewer/CometChatMediaViewer.tsx +27 -5
- package/src/shared/views/CometChatMessageInput/CometChatMessageInput.tsx +1 -0
- package/src/shared/views/CometChatReactionList/CometChatReactionList.tsx +56 -10
- package/src/shared/views/CometChatStatusIndicator/CometChatStatusIndicator.tsx +4 -0
- package/src/shared/views/CometChatVideosBubble/CometChatVideosBubble.tsx +36 -2
- package/src/shared/views/CometChatVoiceNoteBubble/CometChatVoiceNoteBubble.tsx +24 -12
- package/src/theme/type.ts +17 -0
- package/android/.gradle/7.4/checksums/checksums.lock +0 -0
- package/android/.gradle/7.4/fileChanges/last-build.bin +0 -0
- package/android/.gradle/7.4/fileHashes/fileHashes.lock +0 -0
- package/android/.gradle/7.4/gc.properties +0 -0
- package/android/.gradle/vcs-1/gc.properties +0 -0
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Offline (ON path) wrappers for media bubbles (README §3.8, UIK-MEDIA-01).
|
|
3
|
+
*
|
|
4
|
+
* {@link MediaOutOfReachNote} is the "Available when online" line on media the device has no copy of
|
|
5
|
+
* while it cannot reach the server. {@link OfflineMediaBubble} serves the bubbles that take their URL
|
|
6
|
+
* as a prop (the single-attachment bubbles, the voice note): the source comes from the SDK's cached
|
|
7
|
+
* copy, and a tap online asks the SDK to download one. Mounted only on the ON path, so with offline
|
|
8
|
+
* off a bubble's view tree is today's.
|
|
9
|
+
*
|
|
10
|
+
* @internal
|
|
11
|
+
*/
|
|
12
|
+
import React, { JSX } from "react";
|
|
13
|
+
import { View } from "react-native";
|
|
14
|
+
import { useTheme } from "../../theme";
|
|
15
|
+
import { useCometChatTranslation } from "../resources/CometChatLocalizeNew";
|
|
16
|
+
import { requestMediaDownload } from "./media";
|
|
17
|
+
import { MediaOutOfReachLabel } from "./OfflineViews";
|
|
18
|
+
import { useMediaOutOfReach, useOfflineMediaUri } from "./useOfflineMediaUri";
|
|
19
|
+
|
|
20
|
+
export const MediaOutOfReachNote = ({ attachment, color }: { attachment: any; color?: any }) => {
|
|
21
|
+
const outOfReach = useMediaOutOfReach(attachment);
|
|
22
|
+
const theme = useTheme();
|
|
23
|
+
const { t } = useCometChatTranslation();
|
|
24
|
+
if (!outOfReach) return null;
|
|
25
|
+
const tokens = theme.messageListStyles?.offlineStyles?.mediaPlaceholderStyle;
|
|
26
|
+
return (
|
|
27
|
+
<MediaOutOfReachLabel
|
|
28
|
+
t={t}
|
|
29
|
+
textStyle={[{ color: color ?? theme.color.textSecondary }, tokens?.textStyle]}
|
|
30
|
+
containerStyle={tokens?.containerStyle}
|
|
31
|
+
/>
|
|
32
|
+
);
|
|
33
|
+
};
|
|
34
|
+
|
|
35
|
+
export interface OfflineMediaBubbleProps {
|
|
36
|
+
attachment: any;
|
|
37
|
+
playable?: boolean;
|
|
38
|
+
children(uri: string): JSX.Element | null;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
export const OfflineMediaBubble = ({ attachment, playable, children }: OfflineMediaBubbleProps) => {
|
|
42
|
+
const uri = useOfflineMediaUri(attachment, { playable });
|
|
43
|
+
return (
|
|
44
|
+
<View onTouchStart={() => requestMediaDownload(attachment)}>
|
|
45
|
+
{children(uri)}
|
|
46
|
+
<MediaOutOfReachNote attachment={attachment} />
|
|
47
|
+
</View>
|
|
48
|
+
);
|
|
49
|
+
};
|
|
@@ -0,0 +1,218 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Views for the offline (ON) path: the one-line notice (README §3.4/§3.7, with Copy for a removed
|
|
3
|
+
* unsent text), the "Connect to load more" footer (UIK-READ-03) and the "Connect to load ..." /
|
|
4
|
+
* "Connect to search" state of a list that has nothing on screen (D84). Rendered only when the
|
|
5
|
+
* offline path produced something to show, so with offline off nothing here mounts.
|
|
6
|
+
*
|
|
7
|
+
* @internal
|
|
8
|
+
*/
|
|
9
|
+
import React, { useEffect, useRef, useState } from "react";
|
|
10
|
+
import { Pressable, StyleProp, StyleSheet, Text, TextStyle, View, ViewStyle } from "react-native";
|
|
11
|
+
import Clipboard from "@react-native-clipboard/clipboard";
|
|
12
|
+
import { OfflineHub, OfflineNotice } from "./OfflineHub";
|
|
13
|
+
import { noticeCopyText, OFFLINE_STRING_KEYS, offlineNoticeText, Translate } from "./notices";
|
|
14
|
+
import { CONTRACT_IDS } from "./stateContract";
|
|
15
|
+
import { useTheme } from "../../theme";
|
|
16
|
+
import { Icon } from "../icons/Icon";
|
|
17
|
+
|
|
18
|
+
const NOTICE_MS = 5000;
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* How long the short "Sending… try again in a moment" notice stays (R162). It answers one tap, so
|
|
22
|
+
* it goes by itself sooner than the others; a newer one replaces it (one at a time).
|
|
23
|
+
*/
|
|
24
|
+
export const SEND_IN_FLIGHT_NOTICE_MS = 4000;
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* Subscribe to the hub's notices that belong to this screen and hold the latest one for a few
|
|
28
|
+
* seconds. Disabled (`enabled = false`) it never subscribes.
|
|
29
|
+
*/
|
|
30
|
+
export function useOfflineNotice(enabled: boolean, belongsHere: (notice: OfflineNotice) => boolean) {
|
|
31
|
+
const [notice, setNotice] = useState<OfflineNotice | null>(null);
|
|
32
|
+
const belongsRef = useRef(belongsHere);
|
|
33
|
+
belongsRef.current = belongsHere;
|
|
34
|
+
const timer = useRef<ReturnType<typeof setTimeout> | null>(null);
|
|
35
|
+
|
|
36
|
+
useEffect(() => {
|
|
37
|
+
if (!enabled) return;
|
|
38
|
+
const hub = OfflineHub.getInstance();
|
|
39
|
+
// Notices that fired before this screen existed show one after the other.
|
|
40
|
+
let waiting: OfflineNotice[] = [];
|
|
41
|
+
const show = (next: OfflineNotice) => {
|
|
42
|
+
setNotice(next);
|
|
43
|
+
if (timer.current) clearTimeout(timer.current);
|
|
44
|
+
timer.current = setTimeout(() => {
|
|
45
|
+
const following = waiting.shift();
|
|
46
|
+
if (following) show(following);
|
|
47
|
+
else setNotice(null);
|
|
48
|
+
}, next.kind === "send_in_flight" ? SEND_IN_FLIGHT_NOTICE_MS : NOTICE_MS);
|
|
49
|
+
};
|
|
50
|
+
const subscription = hub.notice$.subscribe((next) => {
|
|
51
|
+
if (next.gen !== hub.gen || !belongsRef.current(next)) return;
|
|
52
|
+
hub.claimNotice(next);
|
|
53
|
+
show(next);
|
|
54
|
+
});
|
|
55
|
+
waiting = hub.takeUndeliveredNotices(belongsRef.current);
|
|
56
|
+
const first = waiting.shift();
|
|
57
|
+
if (first) show(first);
|
|
58
|
+
return () => {
|
|
59
|
+
subscription.unsubscribe();
|
|
60
|
+
if (timer.current) clearTimeout(timer.current);
|
|
61
|
+
timer.current = null;
|
|
62
|
+
};
|
|
63
|
+
}, [enabled]);
|
|
64
|
+
|
|
65
|
+
return { notice, dismiss: () => setNotice(null) };
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
export interface OfflineNoticeViewProps {
|
|
69
|
+
notice: OfflineNotice;
|
|
70
|
+
t: Translate;
|
|
71
|
+
onDismiss: () => void;
|
|
72
|
+
backgroundColor?: string;
|
|
73
|
+
textColor?: string;
|
|
74
|
+
actionColor?: string;
|
|
75
|
+
/** Theme tokens (`messageListStyles.offlineStyles.noticeStyle`). */
|
|
76
|
+
containerStyle?: StyleProp<ViewStyle>;
|
|
77
|
+
textStyle?: StyleProp<TextStyle>;
|
|
78
|
+
actionTextStyle?: StyleProp<TextStyle>;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
export const OfflineNoticeView = ({ notice, t, onDismiss, backgroundColor, textColor, actionColor, containerStyle, textStyle, actionTextStyle }: OfflineNoticeViewProps) => {
|
|
82
|
+
const text = offlineNoticeText(notice, t);
|
|
83
|
+
const copyText = noticeCopyText(notice);
|
|
84
|
+
// The kit's inverse surface and its text (as in CometChatSearch's active filter), and the
|
|
85
|
+
// highlight text for the action, so the notice follows the light / dark theme.
|
|
86
|
+
const { color } = useTheme();
|
|
87
|
+
if (!text) return null;
|
|
88
|
+
return (
|
|
89
|
+
<View
|
|
90
|
+
testID={CONTRACT_IDS.notice(notice.kind)}
|
|
91
|
+
accessibilityRole="alert"
|
|
92
|
+
accessibilityLiveRegion="polite"
|
|
93
|
+
style={[styles.notice, { backgroundColor: backgroundColor || color.neutral900 }, containerStyle]}
|
|
94
|
+
>
|
|
95
|
+
<Text style={[styles.noticeText, { color: textColor || color.textWhite }, textStyle]} numberOfLines={2}>
|
|
96
|
+
{text}
|
|
97
|
+
</Text>
|
|
98
|
+
{copyText ? (
|
|
99
|
+
<Pressable
|
|
100
|
+
testID={CONTRACT_IDS.noticeCopy}
|
|
101
|
+
accessibilityRole="button"
|
|
102
|
+
accessibilityLabel={t("COPY")}
|
|
103
|
+
hitSlop={8}
|
|
104
|
+
onPress={() => {
|
|
105
|
+
try {
|
|
106
|
+
Clipboard.setString(copyText);
|
|
107
|
+
} catch {
|
|
108
|
+
/* clipboard unavailable */
|
|
109
|
+
}
|
|
110
|
+
onDismiss();
|
|
111
|
+
}}
|
|
112
|
+
>
|
|
113
|
+
<Text style={[styles.noticeAction, { color: actionColor || color.textHighlight }, actionTextStyle]}>{t("COPY")}</Text>
|
|
114
|
+
</Pressable>
|
|
115
|
+
) : null}
|
|
116
|
+
</View>
|
|
117
|
+
);
|
|
118
|
+
};
|
|
119
|
+
|
|
120
|
+
export interface ConnectToLoadMoreProps {
|
|
121
|
+
t: Translate;
|
|
122
|
+
onRetry: () => void;
|
|
123
|
+
textColor?: string;
|
|
124
|
+
/** Theme tokens (`messageListStyles.offlineStyles.footerStyle`). */
|
|
125
|
+
containerStyle?: StyleProp<ViewStyle>;
|
|
126
|
+
textStyle?: StyleProp<TextStyle>;
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
/** Footer for an offline read that could not go past the cache: tap to try again. */
|
|
130
|
+
export const ConnectToLoadMore = ({ t, onRetry, textColor, containerStyle, textStyle }: ConnectToLoadMoreProps) => {
|
|
131
|
+
const { color } = useTheme();
|
|
132
|
+
return (
|
|
133
|
+
<Pressable
|
|
134
|
+
testID={CONTRACT_IDS.footerConnectToLoadMore}
|
|
135
|
+
accessibilityRole="button"
|
|
136
|
+
accessibilityLabel={t(OFFLINE_STRING_KEYS.connectToLoadMore)}
|
|
137
|
+
onPress={onRetry}
|
|
138
|
+
style={[styles.footer, containerStyle]}
|
|
139
|
+
>
|
|
140
|
+
<Text style={[styles.footerText, { color: textColor || color.neutral600 }, textStyle]}>
|
|
141
|
+
{t(OFFLINE_STRING_KEYS.connectToLoadMore)}
|
|
142
|
+
</Text>
|
|
143
|
+
</Pressable>
|
|
144
|
+
);
|
|
145
|
+
};
|
|
146
|
+
|
|
147
|
+
export interface ConnectStateViewProps {
|
|
148
|
+
/** "Connect to load users", "Connect to search", ... */
|
|
149
|
+
title: string;
|
|
150
|
+
/** Second line, only on "Connect to search" ("Search needs an internet connection."). */
|
|
151
|
+
subtitle?: string;
|
|
152
|
+
testID: string;
|
|
153
|
+
/** The host list's error-state tokens, so the state sits where its error view would. */
|
|
154
|
+
containerStyle?: StyleProp<ViewStyle>;
|
|
155
|
+
titleStyle?: StyleProp<TextStyle>;
|
|
156
|
+
subtitleStyle?: StyleProp<TextStyle>;
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
/**
|
|
160
|
+
* What a list shows when it has nothing on screen and could not reach the server because the device
|
|
161
|
+
* is offline (D84 rule 3 and 7): an icon and the "Connect to ..." text. No Retry button: the list
|
|
162
|
+
* loads by itself once the connection is back.
|
|
163
|
+
*/
|
|
164
|
+
export const ConnectStateView = ({ title, subtitle, testID, containerStyle, titleStyle, subtitleStyle }: ConnectStateViewProps) => {
|
|
165
|
+
const { color, spacing, typography } = useTheme();
|
|
166
|
+
return (
|
|
167
|
+
<View testID={testID} accessibilityRole='text' style={[styles.connectState, containerStyle]}>
|
|
168
|
+
<Icon name='wifi' size={spacing.spacing.s15 << 1} color={color.iconSecondary} containerStyle={{ marginBottom: spacing.margin.m5 }} />
|
|
169
|
+
<Text style={[{ color: color.textPrimary, ...typography.heading3.bold, marginBottom: spacing.margin.m1 }, styles.connectStateText, titleStyle]}>{title}</Text>
|
|
170
|
+
{subtitle ? <Text style={[{ color: color.textSecondary, ...typography.body.regular }, styles.connectStateText, subtitleStyle]}>{subtitle}</Text> : null}
|
|
171
|
+
</View>
|
|
172
|
+
);
|
|
173
|
+
};
|
|
174
|
+
|
|
175
|
+
export interface MediaOutOfReachLabelProps {
|
|
176
|
+
t: Translate;
|
|
177
|
+
textStyle?: StyleProp<TextStyle>;
|
|
178
|
+
containerStyle?: StyleProp<ViewStyle>;
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
/**
|
|
182
|
+
* "Available when online", on media the device never downloaded (AC-12.3).
|
|
183
|
+
*
|
|
184
|
+
* `messageListStyles.offlineStyles.mediaPlaceholderStyle` is applied last, so an integrator can
|
|
185
|
+
* restyle this label wherever it appears. The caller still passes the base look for its own
|
|
186
|
+
* surface (a dark full-screen viewer needs light text), and the theme token overrides it.
|
|
187
|
+
*/
|
|
188
|
+
export const MediaOutOfReachLabel = ({ t, textStyle, containerStyle }: MediaOutOfReachLabelProps) => {
|
|
189
|
+
const tokens = useTheme().messageListStyles?.offlineStyles?.mediaPlaceholderStyle;
|
|
190
|
+
return (
|
|
191
|
+
<View testID={CONTRACT_IDS.mediaOutOfReach} style={[containerStyle, tokens?.containerStyle]}>
|
|
192
|
+
<Text numberOfLines={2} style={[styles.mediaLabel, textStyle, tokens?.textStyle]}>
|
|
193
|
+
{t(OFFLINE_STRING_KEYS.mediaAvailableWhenOnline)}
|
|
194
|
+
</Text>
|
|
195
|
+
</View>
|
|
196
|
+
);
|
|
197
|
+
};
|
|
198
|
+
|
|
199
|
+
const styles = StyleSheet.create({
|
|
200
|
+
mediaLabel: { fontSize: 11, marginTop: 4 },
|
|
201
|
+
notice: {
|
|
202
|
+
position: "absolute",
|
|
203
|
+
left: 12,
|
|
204
|
+
right: 12,
|
|
205
|
+
bottom: 12,
|
|
206
|
+
flexDirection: "row",
|
|
207
|
+
alignItems: "center",
|
|
208
|
+
borderRadius: 8,
|
|
209
|
+
paddingHorizontal: 12,
|
|
210
|
+
paddingVertical: 10,
|
|
211
|
+
},
|
|
212
|
+
noticeText: { flex: 1, fontSize: 14 },
|
|
213
|
+
noticeAction: { marginStart: 12, fontSize: 14, fontWeight: "600" },
|
|
214
|
+
connectState: { flex: 1, justifyContent: "center", alignItems: "center", paddingHorizontal: "10%", flexDirection: "column" },
|
|
215
|
+
connectStateText: { textAlign: "center" },
|
|
216
|
+
footer: { paddingVertical: 12, alignItems: "center" },
|
|
217
|
+
footerText: { fontSize: 13 },
|
|
218
|
+
});
|
|
@@ -0,0 +1,137 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Remote conversation pins on the conversation list's offline (ON) path (UIK-CONV-01,
|
|
3
|
+
* BEH-CPIN-09): a pin or unpin made on the user's other device or by an admin, delivered by the
|
|
4
|
+
* SDK's `ConversationListener` through the hub.
|
|
5
|
+
*
|
|
6
|
+
* - Matched to a row by (conversation type, uid/guid), case-insensitive.
|
|
7
|
+
* - A 1:1 event whose peer is the logged-in user is ignored (it names no chat on this list).
|
|
8
|
+
* - A row on screen keeps everything it has (the payload may be compact: no last message, no
|
|
9
|
+
* unread count) and takes only the pin attributes, then moves to its ordered place.
|
|
10
|
+
* - A pinned chat not on screen is fetched (`getConversation`) and inserted; a failed fetch is
|
|
11
|
+
* ignored. A filtered, searched or `pinnedBy` list only updates the rows it shows.
|
|
12
|
+
* - Own pins never come here: the acting device learns from the pin call's `onSuccess`.
|
|
13
|
+
*
|
|
14
|
+
* Nothing here runs with offline off: the caller reads the gate per event.
|
|
15
|
+
*
|
|
16
|
+
* @internal
|
|
17
|
+
*/
|
|
18
|
+
import { isOfflineOn } from "./gate";
|
|
19
|
+
import { OfflineHub } from "./OfflineHub";
|
|
20
|
+
|
|
21
|
+
export interface RemotePinTarget {
|
|
22
|
+
type: string;
|
|
23
|
+
with: string;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
export interface RemotePinOps {
|
|
27
|
+
/** The rows the list shows now. */
|
|
28
|
+
rows(): readonly any[];
|
|
29
|
+
/** Put a row at its ordered place (insert, move or update in place). */
|
|
30
|
+
place(row: any): void;
|
|
31
|
+
/** A copy of a row that keeps its prototype (the kit's `CommonUtils.clone`). */
|
|
32
|
+
clone(row: any): any;
|
|
33
|
+
loggedInUid(): string | undefined;
|
|
34
|
+
/** A filtered, searched or `pinnedBy` list: never inserts a chat it did not fetch itself. */
|
|
35
|
+
filtered(): boolean;
|
|
36
|
+
fetchConversation(withId: string, type: string): Promise<any>;
|
|
37
|
+
/** The session or list changed since the event arrived: drop a late fetch. */
|
|
38
|
+
stale(): boolean;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
function call(target: any, name: string): any {
|
|
42
|
+
try {
|
|
43
|
+
const fn = target?.[name];
|
|
44
|
+
return typeof fn === "function" ? fn.call(target) : undefined;
|
|
45
|
+
} catch {
|
|
46
|
+
return undefined;
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/** (type, uid/guid) of a conversation, both lower-cased; `null` when it names no chat. */
|
|
51
|
+
export function remotePinTargetOf(conversation: any): RemotePinTarget | null {
|
|
52
|
+
const type = call(conversation, "getConversationType") ?? conversation?.conversationType;
|
|
53
|
+
const other = call(conversation, "getConversationWith") ?? conversation?.conversationWith;
|
|
54
|
+
if (typeof type !== "string" || !type || !other) return null;
|
|
55
|
+
const lowerType = type.toLowerCase();
|
|
56
|
+
const raw = lowerType === "group" ? call(other, "getGuid") ?? other.guid : call(other, "getUid") ?? other.uid;
|
|
57
|
+
if (raw === undefined || raw === null || raw === "") return null;
|
|
58
|
+
return { type: lowerType, with: String(raw).toLowerCase() };
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
function sameChat(a: RemotePinTarget | null, b: RemotePinTarget | null): boolean {
|
|
62
|
+
return !!a && !!b && a.type === b.type && a.with === b.with;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
function findRow(rows: readonly any[], target: RemotePinTarget): any {
|
|
66
|
+
return rows.find((row) => sameChat(remotePinTargetOf(row), target));
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/** Copy the event's pin attributes onto `row` (a clone). */
|
|
70
|
+
function withPin(row: any, event: any, pinned: boolean): any {
|
|
71
|
+
if (pinned) {
|
|
72
|
+
const pinnedAt = call(event, "getPinnedAt") ?? Math.floor(Date.now() / 1000);
|
|
73
|
+
row.setPinnedAt?.(pinnedAt);
|
|
74
|
+
row.setPinnedBy?.(call(event, "getPinnedBy"));
|
|
75
|
+
} else {
|
|
76
|
+
row.setPinnedAt?.(undefined);
|
|
77
|
+
row.setPinnedBy?.(undefined);
|
|
78
|
+
}
|
|
79
|
+
return row;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
/**
|
|
83
|
+
* Apply one remote pin / unpin. Returns the fetch's Promise when a chat not on screen is fetched
|
|
84
|
+
* (it never rejects), else `undefined`.
|
|
85
|
+
*/
|
|
86
|
+
export function applyRemoteConversationPin(event: any, pinned: boolean, ops: RemotePinOps): Promise<void> | undefined {
|
|
87
|
+
const target = remotePinTargetOf(event);
|
|
88
|
+
if (!target) return undefined;
|
|
89
|
+
const me = ops.loggedInUid();
|
|
90
|
+
if (target.type === "user" && me && target.with === String(me).toLowerCase()) return undefined;
|
|
91
|
+
|
|
92
|
+
const shown = findRow(ops.rows(), target);
|
|
93
|
+
if (shown) {
|
|
94
|
+
ops.place(withPin(ops.clone(shown), event, pinned));
|
|
95
|
+
return undefined;
|
|
96
|
+
}
|
|
97
|
+
// Not on screen. An unpinned chat that is not shown has nowhere it must appear; a filtered
|
|
98
|
+
// list only updates rows it shows.
|
|
99
|
+
if (!pinned || ops.filtered()) return undefined;
|
|
100
|
+
const original = call(call(event, "getConversationWith"), target.type === "group" ? "getGuid" : "getUid");
|
|
101
|
+
let fetched: Promise<any>;
|
|
102
|
+
try {
|
|
103
|
+
fetched = ops.fetchConversation(String(original ?? target.with), target.type);
|
|
104
|
+
} catch {
|
|
105
|
+
return undefined;
|
|
106
|
+
}
|
|
107
|
+
if (!fetched || typeof fetched.then !== "function") return undefined;
|
|
108
|
+
return fetched.then(
|
|
109
|
+
(conversation: any) => {
|
|
110
|
+
if (ops.stale() || !conversation) return;
|
|
111
|
+
// It may have arrived on screen meanwhile (a message, a sync page): merge into that row.
|
|
112
|
+
// Otherwise the server's copy is authoritative (it may have been unpinned since).
|
|
113
|
+
const nowShown = findRow(ops.rows(), target);
|
|
114
|
+
ops.place(nowShown ? withPin(ops.clone(nowShown), event, true) : conversation);
|
|
115
|
+
},
|
|
116
|
+
() => undefined
|
|
117
|
+
);
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
/**
|
|
121
|
+
* The conversation list's handler for one hub `conversationPin$` event: drops an event of an
|
|
122
|
+
* older session, reads the gate once for it (offline off: ignored, as the release before it did)
|
|
123
|
+
* and applies it. Returns what {@link applyRemoteConversationPin} returns.
|
|
124
|
+
*/
|
|
125
|
+
export function onRemotePinEvent(
|
|
126
|
+
event: { gen: number; pinned: boolean; conversation: any },
|
|
127
|
+
ops: Omit<RemotePinOps, "stale">
|
|
128
|
+
): Promise<void> | undefined {
|
|
129
|
+
const hub = OfflineHub.getInstance();
|
|
130
|
+
if (event.gen !== hub.gen) return undefined;
|
|
131
|
+
if (!isOfflineOn()) return undefined;
|
|
132
|
+
const startGen = hub.gen;
|
|
133
|
+
return applyRemoteConversationPin(event.conversation, event.pinned, {
|
|
134
|
+
...ops,
|
|
135
|
+
stale: () => OfflineHub.getInstance().gen !== startGen,
|
|
136
|
+
});
|
|
137
|
+
}
|