stream-chat 10.0.0-rc.8 → 10.0.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/README.md +7 -6
- package/dist/cjs/index.browser.js +22095 -22290
- package/dist/cjs/index.browser.js.map +4 -4
- package/dist/cjs/index.node.js +22143 -22298
- package/dist/cjs/index.node.js.map +4 -4
- package/dist/esm/index.mjs +22093 -22288
- package/dist/esm/index.mjs.map +4 -4
- package/dist/types/ChannelManager.d.ts +216 -135
- package/dist/types/CooldownTimer.d.ts +16 -27
- package/dist/types/EventHandlerPipeline.d.ts +18 -38
- package/dist/types/LiveLocationManager.d.ts +25 -47
- package/dist/types/api-client.d.ts +17 -5
- package/dist/types/channel.d.ts +435 -500
- package/dist/types/channel_state.d.ts +72 -112
- package/dist/types/client.d.ts +323 -440
- package/dist/types/client_state.d.ts +1 -3
- package/dist/types/configuration/ConfigController.d.ts +31 -125
- package/dist/types/configuration/InstanceConfigurationRegistry.d.ts +32 -124
- package/dist/types/configuration/keys.d.ts +5 -19
- package/dist/types/configuration/shape.d.ts +12 -29
- package/dist/types/configuration/types.d.ts +64 -79
- package/dist/types/configuration/utils/applyInstanceConfiguration.d.ts +14 -36
- package/dist/types/configuration/utils/copyConfigPatch.d.ts +8 -0
- package/dist/types/configuration/utils/declarativeSlices.d.ts +5 -16
- package/dist/types/configuration/utils/deepFreezeConfig.d.ts +3 -17
- package/dist/types/configuration/utils/serverAuthority.d.ts +9 -29
- package/dist/types/connection/ConnectionIdManager.d.ts +45 -0
- package/dist/types/connection/ConnectionRecoveryManager.d.ts +84 -0
- package/dist/types/connection/index.d.ts +4 -0
- package/dist/types/connection/networkConnection/NetworkConnectionObserver.d.ts +82 -0
- package/dist/types/connection/networkConnection/index.d.ts +3 -0
- package/dist/types/connection/networkConnection/reporters.d.ts +20 -0
- package/dist/types/connection/networkConnection/types.d.ts +40 -0
- package/dist/types/connection/wsConnection/StableWSConnection.d.ts +137 -0
- package/dist/types/connection/wsConnection/WSConnection.d.ts +105 -0
- package/dist/types/connection/wsConnection/WSConnectionFallback.d.ts +47 -0
- package/dist/types/connection/wsConnection/config.d.ts +31 -0
- package/dist/types/connection/wsConnection/index.d.ts +4 -0
- package/dist/types/connection/wsConnection/types.d.ts +46 -0
- package/dist/types/constants.d.ts +3 -1
- package/dist/types/custom_types.d.ts +0 -2
- package/dist/types/entityStore/EntityStore.d.ts +226 -65
- package/dist/types/entityStore/StoreBackedItemIndex.d.ts +19 -33
- package/dist/types/entityStore/index.d.ts +0 -1
- package/dist/types/errors.d.ts +16 -19
- package/dist/types/formatMessageMemo.d.ts +5 -0
- package/dist/types/gen/chat/ChannelApi.d.ts +5 -1
- package/dist/types/gen/chat/ChatApi.d.ts +149 -105
- package/dist/types/gen/models/index.d.ts +2540 -698
- package/dist/types/gen/models/timestamp-guard.d.ts +22 -0
- package/dist/types/gen/moderation/ModerationApi.d.ts +37 -14
- package/dist/types/index.d.ts +10 -5
- package/dist/types/{i18n/languageNames.d.ts → languageNames.d.ts} +9 -24
- package/dist/types/logger.d.ts +1 -3
- package/dist/types/messageComposer/CustomDataManager.d.ts +7 -1
- package/dist/types/messageComposer/LocationComposer.d.ts +8 -2
- package/dist/types/messageComposer/attachmentIdentity.d.ts +24 -0
- package/dist/types/messageComposer/attachmentManager.d.ts +52 -24
- package/dist/types/messageComposer/attachmentUtils.d.ts +23 -0
- package/dist/types/messageComposer/configuration/configuration.d.ts +2 -5
- package/dist/types/messageComposer/configuration/types.d.ts +38 -50
- package/dist/types/messageComposer/index.d.ts +1 -0
- package/dist/types/messageComposer/linkPreviewsManager.d.ts +12 -9
- package/dist/types/messageComposer/messageComposer.d.ts +60 -123
- package/dist/types/messageComposer/middleware/attachmentManager/postUpload/attachmentEnrichment.d.ts +10 -1
- package/dist/types/messageComposer/middleware/messageComposer/attachments.d.ts +4 -0
- package/dist/types/messageComposer/middleware/messageComposer/commandInjection.d.ts +5 -0
- package/dist/types/messageComposer/middleware/messageComposer/index.d.ts +2 -0
- package/dist/types/messageComposer/middleware/messageComposer/pollOnly.d.ts +6 -0
- package/dist/types/messageComposer/middleware/messageComposer/userDataInjection.d.ts +5 -0
- package/dist/types/messageComposer/middleware/pollComposer/types.d.ts +2 -4
- package/dist/types/messageComposer/middleware/pollComposer/validation.d.ts +7 -19
- package/dist/types/messageComposer/middleware/textComposer/commandUtils.d.ts +4 -0
- package/dist/types/messageComposer/middleware/textComposer/commands.d.ts +11 -2
- package/dist/types/messageComposer/middleware/textComposer/mentions.d.ts +20 -19
- package/dist/types/messageComposer/middleware/textComposer/textMiddlewareUtils.d.ts +9 -3
- package/dist/types/messageComposer/middleware/textComposer/types.d.ts +1 -6
- package/dist/types/messageComposer/pollComposer.d.ts +15 -5
- package/dist/types/messageComposer/textComposer.d.ts +14 -7
- package/dist/types/messageComposer/types.d.ts +27 -7
- package/dist/types/messageDelivery/MessageDeliveryReporter.d.ts +27 -80
- package/dist/types/messageDelivery/MessageReceiptsTracker.d.ts +43 -73
- package/dist/types/messageOperations/FailedSendCache.d.ts +29 -0
- package/dist/types/messageOperations/MessageOperationStatePolicy.d.ts +16 -22
- package/dist/types/messageOperations/MessageOperations.d.ts +63 -34
- package/dist/types/messageOperations/applyReactionLocally.d.ts +15 -0
- package/dist/types/messageOperations/createMessageOperations.d.ts +9 -0
- package/dist/types/messageOperations/index.d.ts +6 -4
- package/dist/types/messageOperations/reflectReactionEvent.d.ts +16 -0
- package/dist/types/messageOperations/sendOrdering.d.ts +14 -0
- package/dist/types/messageOperations/settlePendingAttachmentUploads.d.ts +20 -0
- package/dist/types/messageOperations/types.d.ts +43 -14
- package/dist/types/middleware.d.ts +10 -0
- package/dist/types/moderation.d.ts +21 -14
- package/dist/types/notifications/NotificationManager.d.ts +10 -23
- package/dist/types/notifications/types.d.ts +21 -57
- package/dist/types/offline-support/offline_support_api.d.ts +150 -528
- package/dist/types/offline-support/offline_sync_manager.d.ts +21 -42
- package/dist/types/offline-support/queueableOperations.d.ts +35 -37
- package/dist/types/offline-support/types.d.ts +40 -185
- package/dist/types/pagination/FilterBuilder.d.ts +1 -18
- package/dist/types/pagination/ItemIndex.d.ts +4 -12
- package/dist/types/pagination/cursorDerivation/aroundPaginationFlags.d.ts +13 -0
- package/dist/types/pagination/cursorDerivation/createdAtAroundPaginationFlags.d.ts +4 -5
- package/dist/types/pagination/cursorDerivation/linearPaginationFlags.d.ts +19 -5
- package/dist/types/pagination/index.d.ts +1 -0
- package/dist/types/pagination/paginators/BasePaginator.d.ts +604 -550
- package/dist/types/pagination/paginators/ChannelPaginator.d.ts +123 -106
- package/dist/types/pagination/paginators/MessageIntervalPaginator.d.ts +576 -327
- package/dist/types/pagination/paginators/MessagePaginator.d.ts +237 -131
- package/dist/types/pagination/paginators/PinnedMessagePaginator.d.ts +15 -26
- package/dist/types/pagination/paginators/ReminderPaginator.d.ts +4 -3
- package/dist/types/pagination/paginators/ThreadPaginator.d.ts +44 -0
- package/dist/types/pagination/paginators/UserGroupPaginator.d.ts +5 -8
- package/dist/types/pagination/paginators/index.d.ts +1 -0
- package/dist/types/pagination/sortCompiler.d.ts +11 -22
- package/dist/types/pagination/utility.normalization.d.ts +8 -13
- package/dist/types/pagination/utility.search.d.ts +3 -8
- package/dist/types/poll.d.ts +35 -8
- package/dist/types/poll_manager.d.ts +12 -2
- package/dist/types/poll_optimistic_votes.d.ts +57 -0
- package/dist/types/reminders/Reminder.d.ts +17 -7
- package/dist/types/reminders/ReminderManager.d.ts +13 -25
- package/dist/types/search/BaseSearchSource.d.ts +10 -8
- package/dist/types/search/ChannelSearchSource.d.ts +16 -0
- package/dist/types/search/MessageSearchSource.d.ts +17 -0
- package/dist/types/search/SearchController.d.ts +26 -28
- package/dist/types/search/types.d.ts +6 -10
- package/dist/types/signing.d.ts +12 -3
- package/dist/types/thread.d.ts +67 -129
- package/dist/types/thread_manager.d.ts +95 -66
- package/dist/types/token_manager.d.ts +8 -13
- package/dist/types/types.d.ts +81 -197
- package/dist/types/types.utility.d.ts +3 -3
- package/dist/types/upload-utils.d.ts +5 -10
- package/dist/types/uploadManager.d.ts +31 -11
- package/dist/types/utils/FixedSizeQueueCache.d.ts +8 -11
- package/dist/types/utils/WithSubscriptions.d.ts +6 -16
- package/dist/types/utils/abort.d.ts +17 -0
- package/dist/types/utils/concurrency.d.ts +12 -20
- package/dist/types/utils/mergeWith/mergeWith.d.ts +8 -11
- package/dist/types/utils/mergeWith/mergeWithCore.d.ts +5 -13
- package/dist/types/utils/mergeWith/mergeWithDiff.d.ts +22 -0
- package/dist/types/utils/objectPath.d.ts +6 -41
- package/dist/types/utils/retryable.d.ts +8 -12
- package/dist/types/utils/throttling/throttle.d.ts +8 -23
- package/dist/types/utils/time.d.ts +45 -0
- package/dist/types/utils.d.ts +74 -93
- package/package.json +14 -40
- package/src/ChannelManager.ts +494 -303
- package/src/CooldownTimer.ts +46 -59
- package/src/EventHandlerPipeline.ts +22 -45
- package/src/LiveLocationManager.ts +99 -91
- package/src/api-client.ts +113 -50
- package/src/campaign.ts +1 -1
- package/src/channel.ts +1098 -1351
- package/src/channel_batch_updater.ts +1 -1
- package/src/channel_state.ts +81 -174
- package/src/client.ts +755 -970
- package/src/client_state.ts +3 -8
- package/src/configuration/ConfigController.ts +42 -158
- package/src/configuration/InstanceConfigurationRegistry.ts +43 -172
- package/src/configuration/index.ts +1 -1
- package/src/configuration/keys.ts +13 -32
- package/src/configuration/shape.ts +169 -59
- package/src/configuration/types.ts +69 -92
- package/src/configuration/utils/applyInstanceConfiguration.ts +22 -54
- package/src/configuration/utils/copyConfigPatch.ts +11 -32
- package/src/configuration/utils/declarativeSlices.ts +9 -39
- package/src/configuration/utils/deepFreezeConfig.ts +3 -17
- package/src/configuration/utils/index.ts +2 -4
- package/src/configuration/utils/serverAuthority.ts +20 -68
- package/src/connection/ConnectionIdManager.ts +111 -0
- package/src/connection/ConnectionRecoveryManager.ts +257 -0
- package/src/connection/index.ts +4 -0
- package/src/connection/networkConnection/NetworkConnectionObserver.ts +204 -0
- package/src/connection/networkConnection/index.ts +3 -0
- package/src/connection/networkConnection/reporters.ts +58 -0
- package/src/connection/networkConnection/types.ts +45 -0
- package/src/{connection.ts → connection/wsConnection/StableWSConnection.ts} +230 -231
- package/src/connection/wsConnection/WSConnection.ts +270 -0
- package/src/connection/wsConnection/WSConnectionFallback.ts +250 -0
- package/src/connection/wsConnection/config.ts +73 -0
- package/src/connection/wsConnection/index.ts +4 -0
- package/src/connection/wsConnection/types.ts +48 -0
- package/src/constants.ts +7 -5
- package/src/custom_types.ts +0 -1
- package/src/entityStore/EntityStore.ts +415 -79
- package/src/entityStore/StoreBackedItemIndex.ts +37 -41
- package/src/entityStore/index.ts +0 -1
- package/src/errors.ts +39 -20
- package/src/formatMessageMemo.ts +17 -0
- package/src/gen/chat/ChannelApi.ts +122 -36
- package/src/gen/chat/ChatApi.ts +578 -1752
- package/src/gen/models/index.ts +2566 -3440
- package/src/gen/models/timestamp-guard.ts +27 -0
- package/src/gen/moderation/ModerationApi.ts +183 -406
- package/src/index.ts +32 -10
- package/src/languageNames.ts +88 -0
- package/src/logger.ts +1 -3
- package/src/messageComposer/CustomDataManager.ts +19 -6
- package/src/messageComposer/LocationComposer.ts +49 -9
- package/src/messageComposer/MessageComposerEffectHandlers.ts +1 -2
- package/src/messageComposer/attachmentIdentity.ts +54 -0
- package/src/messageComposer/attachmentManager.ts +170 -185
- package/src/messageComposer/attachmentUtils.ts +44 -0
- package/src/messageComposer/configuration/configuration.ts +15 -8
- package/src/messageComposer/configuration/types.ts +38 -50
- package/src/messageComposer/fileUtils.ts +1 -1
- package/src/messageComposer/index.ts +1 -0
- package/src/messageComposer/linkPreviewsManager.ts +18 -21
- package/src/messageComposer/messageComposer.ts +234 -179
- package/src/messageComposer/middleware/attachmentManager/postUpload/AttachmentPostUploadMiddlewareExecutor.ts +1 -1
- package/src/messageComposer/middleware/attachmentManager/postUpload/attachmentEnrichment.ts +46 -33
- package/src/messageComposer/middleware/attachmentManager/postUpload/uploadErrorHandler.ts +4 -1
- package/src/messageComposer/middleware/attachmentManager/preUpload/blockedUploadNotification.ts +1 -1
- package/src/messageComposer/middleware/messageComposer/MessageComposerMiddlewareExecutor.ts +6 -0
- package/src/messageComposer/middleware/messageComposer/attachments.ts +76 -5
- package/src/messageComposer/middleware/messageComposer/commandInjection.ts +10 -4
- package/src/messageComposer/middleware/messageComposer/customData.ts +17 -5
- package/src/messageComposer/middleware/messageComposer/index.ts +2 -0
- package/src/messageComposer/middleware/messageComposer/linkPreviews.ts +22 -12
- package/src/messageComposer/middleware/messageComposer/pollOnly.ts +9 -1
- package/src/messageComposer/middleware/messageComposer/sharedLocation.ts +7 -2
- package/src/messageComposer/middleware/messageComposer/textComposer.ts +1 -1
- package/src/messageComposer/middleware/messageComposer/userDataInjection.ts +12 -12
- package/src/messageComposer/middleware/pollComposer/state.ts +2 -2
- package/src/messageComposer/middleware/pollComposer/types.ts +2 -4
- package/src/messageComposer/middleware/pollComposer/validation.ts +8 -26
- package/src/messageComposer/middleware/textComposer/TextComposerMiddlewareExecutor.ts +8 -6
- package/src/messageComposer/middleware/textComposer/commandUtils.ts +10 -3
- package/src/messageComposer/middleware/textComposer/commands.ts +11 -16
- package/src/messageComposer/middleware/textComposer/mentions.ts +32 -70
- package/src/messageComposer/middleware/textComposer/textMiddlewareUtils.ts +10 -4
- package/src/messageComposer/middleware/textComposer/types.ts +1 -6
- package/src/messageComposer/pollComposer.ts +21 -9
- package/src/messageComposer/textComposer.ts +37 -57
- package/src/messageComposer/types.ts +39 -11
- package/src/messageDelivery/MessageDeliveryReporter.ts +86 -124
- package/src/messageDelivery/MessageReceiptsTracker.ts +122 -139
- package/src/messageOperations/FailedSendCache.ts +85 -0
- package/src/messageOperations/MessageOperationStatePolicy.ts +124 -116
- package/src/messageOperations/MessageOperations.ts +297 -175
- package/src/{entityStore → messageOperations}/applyReactionLocally.ts +13 -25
- package/src/messageOperations/createMessageOperations.ts +176 -0
- package/src/messageOperations/index.ts +6 -14
- package/src/messageOperations/reflectReactionEvent.ts +43 -0
- package/src/messageOperations/sendOrdering.ts +25 -0
- package/src/messageOperations/settlePendingAttachmentUploads.ts +98 -0
- package/src/messageOperations/types.ts +51 -13
- package/src/middleware.ts +13 -5
- package/src/moderation.ts +25 -21
- package/src/notifications/NotificationManager.ts +19 -28
- package/src/notifications/types.ts +21 -57
- package/src/offline-support/offline_support_api.ts +231 -681
- package/src/offline-support/offline_sync_manager.ts +51 -90
- package/src/offline-support/queueableOperations.ts +98 -73
- package/src/offline-support/types.ts +43 -185
- package/src/pagination/FilterBuilder.ts +1 -22
- package/src/pagination/ItemIndex.ts +4 -12
- package/src/pagination/cursorDerivation/aroundPaginationFlags.ts +29 -0
- package/src/pagination/cursorDerivation/createdAtAroundPaginationFlags.ts +17 -62
- package/src/pagination/cursorDerivation/idAroundPaginationFlags.ts +11 -39
- package/src/pagination/cursorDerivation/linearPaginationFlags.ts +53 -33
- package/src/pagination/filterCompiler.ts +11 -25
- package/src/pagination/index.ts +1 -0
- package/src/pagination/paginators/BasePaginator.ts +1415 -1041
- package/src/pagination/paginators/ChannelPaginator.ts +481 -219
- package/src/pagination/paginators/MessageIntervalPaginator.ts +1362 -775
- package/src/pagination/paginators/MessagePaginator.ts +521 -240
- package/src/pagination/paginators/PinnedMessagePaginator.ts +32 -40
- package/src/pagination/paginators/ReminderPaginator.ts +21 -16
- package/src/pagination/paginators/ThreadPaginator.ts +170 -0
- package/src/pagination/paginators/UserGroupPaginator.ts +18 -15
- package/src/pagination/paginators/index.ts +1 -0
- package/src/pagination/paginators/stateThrottling.ts +5 -10
- package/src/pagination/sortCompiler.ts +23 -50
- package/src/pagination/utility.normalization.ts +17 -24
- package/src/pagination/utility.search.ts +4 -11
- package/src/poll.ts +183 -102
- package/src/poll_manager.ts +24 -22
- package/src/poll_optimistic_votes.ts +335 -0
- package/src/reminders/Reminder.ts +33 -16
- package/src/reminders/ReminderManager.ts +23 -56
- package/src/reminders/ReminderTimer.ts +6 -6
- package/src/search/BaseSearchSource.ts +19 -25
- package/src/search/ChannelSearchSource.ts +43 -1
- package/src/search/MessageSearchSource.ts +77 -13
- package/src/search/SearchController.ts +39 -36
- package/src/search/types.ts +6 -10
- package/src/segment.ts +1 -1
- package/src/signing.ts +23 -3
- package/src/thread.ts +430 -712
- package/src/thread_manager.ts +300 -234
- package/src/token_manager.ts +12 -22
- package/src/types.ts +102 -214
- package/src/types.utility.ts +3 -3
- package/src/upload-utils.ts +5 -10
- package/src/uploadManager.ts +64 -18
- package/src/utils/FixedSizeQueueCache.ts +18 -21
- package/src/utils/WithSubscriptions.ts +6 -16
- package/src/utils/abort.ts +45 -0
- package/src/utils/concurrency.ts +16 -55
- package/src/utils/mergeWith/mergeWith.ts +10 -13
- package/src/utils/mergeWith/mergeWithCore.ts +24 -81
- package/src/utils/mergeWith/mergeWithDiff.ts +15 -33
- package/src/utils/objectPath.ts +6 -41
- package/src/utils/retryable.ts +10 -14
- package/src/utils/throttling/throttle.ts +9 -25
- package/src/utils/time.ts +64 -0
- package/src/utils.ts +227 -174
- package/codegen/i18n/callSites.ts +0 -126
- package/codegen/i18n/generate.ts +0 -226
- package/codegen/i18n/guards.ts +0 -178
- package/codegen/i18n/index.ts +0 -29
- package/codegen/i18n/stringMaps.ts +0 -91
- package/codegen/i18n/types.ts +0 -100
- package/dist/cjs/i18n.browser.js +0 -1131
- package/dist/cjs/i18n.browser.js.map +0 -7
- package/dist/cjs/i18n.node.js +0 -1160
- package/dist/cjs/i18n.node.js.map +0 -7
- package/dist/esm/i18n-codegen.mjs +0 -409
- package/dist/esm/i18n-codegen.mjs.map +0 -7
- package/dist/esm/i18n.mjs +0 -1098
- package/dist/esm/i18n.mjs.map +0 -7
- package/dist/types/ConnectionRecoveryManager.d.ts +0 -156
- package/dist/types/connection.d.ts +0 -139
- package/dist/types/entityStore/applyReactionLocally.d.ts +0 -26
- package/dist/types/gen/model-decoders/decoders.d.ts +0 -3
- package/dist/types/gen/model-decoders/event-decoder-mapping.d.ts +0 -6
- package/dist/types/i18n/Streami18n.d.ts +0 -168
- package/dist/types/i18n/TranslationBuilder.d.ts +0 -70
- package/dist/types/i18n/TranslationStore.d.ts +0 -57
- package/dist/types/i18n/dayjs.d.ts +0 -104
- package/dist/types/i18n/formatters.d.ts +0 -91
- package/dist/types/i18n/index.d.ts +0 -15
- package/dist/types/i18n/translator.d.ts +0 -29
- package/dist/types/i18n/types.d.ts +0 -262
- package/dist/types/i18n-codegen/callSites.d.ts +0 -10
- package/dist/types/i18n-codegen/generate.d.ts +0 -15
- package/dist/types/i18n-codegen/guards.d.ts +0 -61
- package/dist/types/i18n-codegen/index.d.ts +0 -29
- package/dist/types/i18n-codegen/stringMaps.d.ts +0 -14
- package/dist/types/i18n-codegen/types.d.ts +0 -94
- package/dist/types/insights.d.ts +0 -56
- package/dist/types/messageOperations/optimistic.d.ts +0 -118
- package/dist/types/messageOperations/persistence.d.ts +0 -21
- package/dist/types/pagination/utility.queryChannel.d.ts +0 -26
- package/dist/types/store.d.ts +0 -125
- package/src/ConnectionRecoveryManager.ts +0 -293
- package/src/gen/model-decoders/decoders.ts +0 -2710
- package/src/gen/model-decoders/event-decoder-mapping.ts +0 -198
- package/src/i18n/Streami18n.ts +0 -520
- package/src/i18n/TranslationBuilder.ts +0 -176
- package/src/i18n/TranslationStore.ts +0 -96
- package/src/i18n/dayjs.ts +0 -228
- package/src/i18n/formatters.ts +0 -461
- package/src/i18n/index.ts +0 -15
- package/src/i18n/languageNames.ts +0 -103
- package/src/i18n/translator.ts +0 -76
- package/src/i18n/types.ts +0 -375
- package/src/insights.ts +0 -82
- package/src/messageOperations/optimistic.ts +0 -249
- package/src/messageOperations/persistence.ts +0 -40
- package/src/pagination/utility.queryChannel.ts +0 -84
- package/src/store.ts +0 -304
|
@@ -1,13 +1,14 @@
|
|
|
1
|
-
import type { ReadStateResponse, UserResponse } from '../types';
|
|
2
|
-
import { StateStore } from '
|
|
1
|
+
import type { ReadStateResponse, TimestampNS, UserResponse } from '../types';
|
|
2
|
+
import { StateStore } from '@stream-io/state-store';
|
|
3
3
|
import type { Channel } from '../channel';
|
|
4
|
+
import { asTimestampNS } from '../utils/time';
|
|
4
5
|
import { WithSubscriptions } from '../utils/WithSubscriptions';
|
|
5
6
|
|
|
6
7
|
type UserId = string;
|
|
7
8
|
type MessageId = string;
|
|
8
|
-
export type MsgRef = {
|
|
9
|
+
export type MsgRef = { timestamp: TimestampNS; msgId: MessageId };
|
|
9
10
|
export type OwnMessageReceiptsTrackerMessageLocator = (
|
|
10
|
-
|
|
11
|
+
timestamp: TimestampNS,
|
|
11
12
|
) => MsgRef | null;
|
|
12
13
|
export type UserProgress = {
|
|
13
14
|
user: UserResponse;
|
|
@@ -16,39 +17,46 @@ export type UserProgress = {
|
|
|
16
17
|
};
|
|
17
18
|
export type MessageReceiptsSnapshot = {
|
|
18
19
|
revision: number;
|
|
20
|
+
/**
|
|
21
|
+
* Users whose read cursor sits on each message: one entry per user, not everyone who has read it.
|
|
22
|
+
* For "who has read X" use {@link MessageReceiptsTracker.readersForMessage}.
|
|
23
|
+
*/
|
|
19
24
|
readersByMessageId: Record<MessageId, UserResponse[]>;
|
|
25
|
+
/** As {@link readersByMessageId}, for delivery. */
|
|
20
26
|
deliveredByMessageId: Record<MessageId, UserResponse[]>;
|
|
27
|
+
/** Furthest delivered ref of any other member, or `null`. A message at or before it is delivered. */
|
|
28
|
+
lastDeliveredRefByOthers: MsgRef | null;
|
|
29
|
+
/** As {@link lastDeliveredRefByOthers}, for reads. */
|
|
30
|
+
lastReadRefByOthers: MsgRef | null;
|
|
21
31
|
};
|
|
22
32
|
export type ReadStoreReconcileMeta = {
|
|
23
33
|
changedUserIds?: string[];
|
|
24
34
|
removedUserIds?: string[];
|
|
25
35
|
};
|
|
26
36
|
type ReadStoreUserState = {
|
|
27
|
-
last_read?:
|
|
37
|
+
last_read?: TimestampNS;
|
|
28
38
|
unread_messages?: number;
|
|
29
39
|
user?: UserResponse;
|
|
30
40
|
first_unread_message_id?: string;
|
|
31
41
|
last_read_message_id?: string;
|
|
32
|
-
last_delivered_at?:
|
|
42
|
+
last_delivered_at?: TimestampNS;
|
|
33
43
|
last_delivered_message_id?: string;
|
|
34
44
|
};
|
|
35
45
|
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
const toTimestampMs = (value: Date | string) =>
|
|
41
|
-
value instanceof Date ? value.getTime() : new Date(value).getTime();
|
|
46
|
+
const MIN_REF: MsgRef = {
|
|
47
|
+
timestamp: asTimestampNS(Number.NEGATIVE_INFINITY),
|
|
48
|
+
msgId: '',
|
|
49
|
+
} as const;
|
|
42
50
|
|
|
43
51
|
const isValidReadState = (
|
|
44
52
|
readState: ReadStoreUserState | undefined,
|
|
45
53
|
): readState is ReadStoreUserState & {
|
|
46
|
-
last_read:
|
|
54
|
+
last_read: TimestampNS;
|
|
47
55
|
user: UserResponse;
|
|
48
|
-
} => !!readState?.user &&
|
|
56
|
+
} => !!readState?.user && Number.isFinite(readState.last_read);
|
|
49
57
|
|
|
50
58
|
const compareRefsAsc = (a: MsgRef, b: MsgRef) =>
|
|
51
|
-
a.
|
|
59
|
+
a.timestamp !== b.timestamp ? a.timestamp - b.timestamp : 0;
|
|
52
60
|
|
|
53
61
|
const findIndex = <T>(arr: T[], target: MsgRef, keyOf: (x: T) => MsgRef): number => {
|
|
54
62
|
let lo = 0,
|
|
@@ -61,15 +69,7 @@ const findIndex = <T>(arr: T[], target: MsgRef, keyOf: (x: T) => MsgRef): number
|
|
|
61
69
|
return lo;
|
|
62
70
|
};
|
|
63
71
|
|
|
64
|
-
/**
|
|
65
|
-
* Finds the insertion index after the last equal item. E.g. when array `[a]` exists and `b` is
|
|
66
|
-
* being inserted we want `[a, b]`, not `[b, a]`.
|
|
67
|
-
*
|
|
68
|
-
* @param arr - The sorted array to search.
|
|
69
|
-
* @param target - The reference value to compare against.
|
|
70
|
-
* @param keyOf - Accessor that maps an item to its comparable reference.
|
|
71
|
-
* @returns The insertion index in `arr`.
|
|
72
|
-
*/
|
|
72
|
+
/** Insertion index after the last equal item, so ties keep arrival order. */
|
|
73
73
|
const findUpperIndex = <T>(arr: T[], target: MsgRef, keyOf: (x: T) => MsgRef): number => {
|
|
74
74
|
let lo = 0,
|
|
75
75
|
hi = arr.length;
|
|
@@ -93,7 +93,7 @@ const removeByOldKey = (
|
|
|
93
93
|
oldKey: MsgRef,
|
|
94
94
|
keyOf: (x: UserProgress) => MsgRef,
|
|
95
95
|
) => {
|
|
96
|
-
// Find the plateau for oldKey, scan to match by user
|
|
96
|
+
// Find the plateau for oldKey, scan to match by user ID
|
|
97
97
|
let i = findIndex(arr, oldKey, keyOf);
|
|
98
98
|
while (i < arr.length && compareRefsAsc(keyOf(arr[i]), oldKey) === 0) {
|
|
99
99
|
if (arr[i].user.id === item.user.id) {
|
|
@@ -110,62 +110,13 @@ export type OwnMessageReceiptsTrackerOptions = {
|
|
|
110
110
|
};
|
|
111
111
|
|
|
112
112
|
/**
|
|
113
|
-
*
|
|
114
|
-
*
|
|
115
|
-
*
|
|
116
|
-
*
|
|
117
|
-
*
|
|
118
|
-
* How it works
|
|
119
|
-
* ------------
|
|
120
|
-
* - Each user has a compact progress record:
|
|
121
|
-
* - `lastReadRef`: latest message they have **read**
|
|
122
|
-
* - `lastDeliveredRef`: latest message they have **received** (always `>= lastReadRef`)
|
|
123
|
-
* - Internally keeps two arrays sorted **ascending by timestamp**:
|
|
124
|
-
* - `readSorted` (by `lastReadRef`)
|
|
125
|
-
* - `deliveredSorted` (by `lastDeliveredRef`)
|
|
126
|
-
* - Queries like “who read message M?” become a **binary search + suffix slice**.
|
|
127
|
-
*
|
|
128
|
-
* Construction
|
|
129
|
-
* ------------
|
|
130
|
-
* `new MessageReceiptsTracker({ channel, locateMessage? })`
|
|
131
|
-
* - By default, message references are read through `channel.state.findMessageByTimestamp`.
|
|
132
|
-
* - `locateMessage` can override this lookup strategy.
|
|
133
|
-
* If a message cannot be resolved locally, the event is ignored.
|
|
113
|
+
* Tracks other participants' delivery and read progress toward own messages in one timeline
|
|
114
|
+
* (channel or thread). Per user it keeps `lastReadRef` and `lastDeliveredRef` (always
|
|
115
|
+
* `>= lastReadRef`) in two arrays sorted by timestamp, so "who read message M?" is a binary search
|
|
116
|
+
* plus a suffix slice.
|
|
134
117
|
*
|
|
135
|
-
*
|
|
136
|
-
*
|
|
137
|
-
* - `ingestInitial(rows: ReadStateResponse[])`: Builds initial state from server snapshot.
|
|
138
|
-
* If a user’s `last_read` is ahead of `last_delivered_at`, the tracker enforces
|
|
139
|
-
* the invariant `lastDeliveredRef >= lastReadRef`.
|
|
140
|
-
* - `onMessageRead(user, readAtISO)`:
|
|
141
|
-
* Advances the user’s read; also bumps delivered to match if needed.
|
|
142
|
-
* - `onMessageDelivered(user, deliveredAtISO)`:
|
|
143
|
-
* Advances the user’s delivered to `max(currentRead, deliveredAt)`.
|
|
144
|
-
*
|
|
145
|
-
* Queries
|
|
146
|
-
* -------
|
|
147
|
-
* - `readersForMessage(msgRef) : UserResponse[]` → users with `lastReadRef >= msgRef`
|
|
148
|
-
* - `deliveredForMessage(msgRef) : UserResponse[]` → users with `lastDeliveredRef >= msgRef`
|
|
149
|
-
* - `deliveredNotReadForMessage(msgRef): UserResponse[]` → delivered but `lastReadRef < msgRef`
|
|
150
|
-
* - `usersWhoseLastReadIs : UserResponse[]` → users for whom `msgRef` is their *last read* (exact match)
|
|
151
|
-
* - `usersWhoseLastDeliveredIs : UserResponse[]` → users for whom `msgRef` is their *last delivered* (exact match)
|
|
152
|
-
* - `groupUsersByLastReadMessage : Record<MsgId, UserResponse[]> → mapping of messages to their readers
|
|
153
|
-
* - `groupUsersByLastDeliveredMessage : Record<MsgId, UserResponse[]> → mapping of messages to their receivers
|
|
154
|
-
* - `hasUserRead(msgRef, userId) : boolean`
|
|
155
|
-
* - `hasUserDelivered(msgRef, userId) : boolean`
|
|
156
|
-
*
|
|
157
|
-
* Complexity
|
|
158
|
-
* ----------
|
|
159
|
-
* - Update on read/delivered: **O(log U)** (binary search + one splice) per event, where U is count of users stored by tracker.
|
|
160
|
-
* - Query lists: **O(log U + K)** where `K` is the number of returned users (suffix length).
|
|
161
|
-
* - Memory: **O(U)** - tracker’s memory grows linearly with the number of users in the channel/thread and does not depend on the number of messages.
|
|
162
|
-
*
|
|
163
|
-
* Scope & notes
|
|
164
|
-
* -------------
|
|
165
|
-
* - One tracker instance is **scoped to a single timeline**. Instantiate per channel/thread.
|
|
166
|
-
* - Ordering is by **ascending timestamp**; ties are kept stable by inserting at the end of the
|
|
167
|
-
* equal-timestamp plateau (upper-bound insertion), preserving intuitive arrival order.
|
|
168
|
-
* - This tracker models **others’ progress toward own messages**;
|
|
118
|
+
* Messages resolve through `channel.messagePaginator.findItemByTimestamp` unless `locateMessage` is
|
|
119
|
+
* passed. Events for messages that cannot be resolved are ignored.
|
|
169
120
|
*/
|
|
170
121
|
export class MessageReceiptsTracker extends WithSubscriptions {
|
|
171
122
|
private byUser = new Map<UserId, UserProgress>();
|
|
@@ -178,6 +129,8 @@ export class MessageReceiptsTracker extends WithSubscriptions {
|
|
|
178
129
|
revision: 0,
|
|
179
130
|
readersByMessageId: {},
|
|
180
131
|
deliveredByMessageId: {},
|
|
132
|
+
lastDeliveredRefByOthers: null,
|
|
133
|
+
lastReadRefByOthers: null,
|
|
181
134
|
});
|
|
182
135
|
|
|
183
136
|
constructor({ channel, locateMessage }: OwnMessageReceiptsTrackerOptions) {
|
|
@@ -185,9 +138,9 @@ export class MessageReceiptsTracker extends WithSubscriptions {
|
|
|
185
138
|
this.channel = channel;
|
|
186
139
|
this.locateMessage =
|
|
187
140
|
locateMessage ??
|
|
188
|
-
((
|
|
189
|
-
const message = this.channel.messagePaginator.findItemByTimestamp(
|
|
190
|
-
return message ? {
|
|
141
|
+
((timestamp: TimestampNS) => {
|
|
142
|
+
const message = this.channel.messagePaginator.findItemByTimestamp(timestamp);
|
|
143
|
+
return message ? { timestamp, msgId: message.id } : null;
|
|
191
144
|
});
|
|
192
145
|
}
|
|
193
146
|
|
|
@@ -196,8 +149,7 @@ export class MessageReceiptsTracker extends WithSubscriptions {
|
|
|
196
149
|
if (this.hasSubscriptions) return;
|
|
197
150
|
|
|
198
151
|
this.addUnsubscribeFunction(
|
|
199
|
-
//
|
|
200
|
-
// read changes, not on every unrelated channel-state write (typing/members/…).
|
|
152
|
+
// Only the `read` slice, to skip unrelated channel-state writes.
|
|
201
153
|
this.channel.state.subscribeWithSelector(
|
|
202
154
|
(currentState) => ({ read: currentState.read }),
|
|
203
155
|
(next, prev) => {
|
|
@@ -235,7 +187,7 @@ export class MessageReceiptsTracker extends WithSubscriptions {
|
|
|
235
187
|
return;
|
|
236
188
|
}
|
|
237
189
|
|
|
238
|
-
//
|
|
190
|
+
// After the initial bootstrap, only updates that carry patch metadata are applied.
|
|
239
191
|
if (!meta) return;
|
|
240
192
|
|
|
241
193
|
const removedUserIds = new Set(meta?.removedUserIds ?? []);
|
|
@@ -268,22 +220,22 @@ export class MessageReceiptsTracker extends WithSubscriptions {
|
|
|
268
220
|
}
|
|
269
221
|
}
|
|
270
222
|
|
|
271
|
-
/**
|
|
223
|
+
/** Replaces the tracker's state with the given server read states. */
|
|
272
224
|
ingestInitial(responses: ReadStateResponse[]) {
|
|
273
225
|
this.byUser.clear();
|
|
274
226
|
this.readSorted = [];
|
|
275
227
|
this.deliveredSorted = [];
|
|
276
228
|
for (const r of responses) {
|
|
277
|
-
const lastReadTimestamp = r.last_read
|
|
278
|
-
const lastDeliveredTimestamp = r.last_delivered_at
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
229
|
+
const lastReadTimestamp = r.last_read ?? null;
|
|
230
|
+
const lastDeliveredTimestamp = r.last_delivered_at ?? null;
|
|
231
|
+
const lastReadRef =
|
|
232
|
+
lastReadTimestamp != null
|
|
233
|
+
? (this.locateMessage(lastReadTimestamp) ?? MIN_REF)
|
|
234
|
+
: MIN_REF;
|
|
235
|
+
let lastDeliveredRef =
|
|
236
|
+
lastDeliveredTimestamp != null
|
|
237
|
+
? (this.locateMessage(lastDeliveredTimestamp) ?? MIN_REF)
|
|
238
|
+
: MIN_REF;
|
|
287
239
|
const isReadAfterDelivered = compareRefsAsc(lastDeliveredRef, lastReadRef) < 0;
|
|
288
240
|
if (isReadAfterDelivered) lastDeliveredRef = lastReadRef;
|
|
289
241
|
|
|
@@ -304,20 +256,21 @@ export class MessageReceiptsTracker extends WithSubscriptions {
|
|
|
304
256
|
this.emitSnapshot();
|
|
305
257
|
}
|
|
306
258
|
|
|
307
|
-
/** message.delivered
|
|
259
|
+
/** `message.delivered`: delivery confirmed up to and including the message. */
|
|
308
260
|
onMessageDelivered({
|
|
309
261
|
user,
|
|
310
262
|
deliveredAt,
|
|
311
263
|
lastDeliveredMessageId,
|
|
312
264
|
}: {
|
|
313
265
|
user: UserResponse;
|
|
314
|
-
|
|
266
|
+
/** Unix nanoseconds, as the API sends it. */
|
|
267
|
+
deliveredAt: TimestampNS;
|
|
315
268
|
lastDeliveredMessageId?: string;
|
|
316
269
|
}) {
|
|
317
|
-
const
|
|
270
|
+
const timestamp = deliveredAt;
|
|
318
271
|
const msgRef = lastDeliveredMessageId
|
|
319
|
-
? {
|
|
320
|
-
: this.locateMessage(deliveredAt
|
|
272
|
+
? { timestamp, msgId: lastDeliveredMessageId }
|
|
273
|
+
: this.locateMessage(deliveredAt);
|
|
321
274
|
if (!msgRef) return;
|
|
322
275
|
const userProgress = this.ensureUser(user);
|
|
323
276
|
|
|
@@ -325,7 +278,6 @@ export class MessageReceiptsTracker extends WithSubscriptions {
|
|
|
325
278
|
compareRefsAsc(msgRef, userProgress.lastReadRef) < 0
|
|
326
279
|
? userProgress.lastReadRef
|
|
327
280
|
: msgRef; // max(read, loc)
|
|
328
|
-
// newly announced delivered is older than or equal what is already registered
|
|
329
281
|
if (compareRefsAsc(newDelivered, userProgress.lastDeliveredRef) <= 0) return;
|
|
330
282
|
|
|
331
283
|
removeByOldKey(
|
|
@@ -339,26 +291,25 @@ export class MessageReceiptsTracker extends WithSubscriptions {
|
|
|
339
291
|
this.emitSnapshot();
|
|
340
292
|
}
|
|
341
293
|
|
|
342
|
-
/** message.read
|
|
294
|
+
/** `message.read`: read up to and including the message. */
|
|
343
295
|
onMessageRead({
|
|
344
296
|
user,
|
|
345
297
|
readAt,
|
|
346
298
|
lastReadMessageId,
|
|
347
299
|
}: {
|
|
348
300
|
user: UserResponse;
|
|
349
|
-
|
|
301
|
+
/** Unix nanoseconds, as the API sends it. */
|
|
302
|
+
readAt: TimestampNS;
|
|
350
303
|
lastReadMessageId?: string;
|
|
351
304
|
}) {
|
|
352
|
-
const
|
|
305
|
+
const timestamp = readAt;
|
|
353
306
|
const msgRef = lastReadMessageId
|
|
354
|
-
? {
|
|
355
|
-
: this.locateMessage(
|
|
307
|
+
? { timestamp, msgId: lastReadMessageId }
|
|
308
|
+
: this.locateMessage(timestamp);
|
|
356
309
|
if (!msgRef) return;
|
|
357
310
|
const userProgress = this.ensureUser(user);
|
|
358
|
-
// newly announced read message is older than or equal the already recorded last read message
|
|
359
311
|
if (compareRefsAsc(msgRef, userProgress.lastReadRef) <= 0) return;
|
|
360
312
|
|
|
361
|
-
// move in readSorted
|
|
362
313
|
removeByOldKey(
|
|
363
314
|
this.readSorted,
|
|
364
315
|
userProgress,
|
|
@@ -383,28 +334,25 @@ export class MessageReceiptsTracker extends WithSubscriptions {
|
|
|
383
334
|
this.emitSnapshot();
|
|
384
335
|
}
|
|
385
336
|
|
|
386
|
-
/** notification.mark_unread
|
|
387
|
-
* Sets lastReadRef to the event’s last_read_* values. Delivery never moves backward.
|
|
388
|
-
* The event is sent only to the user that triggered the action (own user), so we will never adjust read ref
|
|
389
|
-
* for other users - we will not see changes in the UI for other users. However, this implementation does not
|
|
390
|
-
* take into consideration this fact and is ready to handle the mark-unread event for any user.
|
|
391
|
-
*/
|
|
337
|
+
/** `notification.mark_unread`: sets `lastReadRef` from `last_read_*`; delivery never moves back. */
|
|
392
338
|
onNotificationMarkUnread({
|
|
393
339
|
user,
|
|
394
340
|
lastReadAt,
|
|
395
341
|
lastReadMessageId,
|
|
396
342
|
}: {
|
|
397
343
|
user: UserResponse;
|
|
398
|
-
|
|
344
|
+
/** Unix nanoseconds, as the API sends it. */
|
|
345
|
+
lastReadAt?: TimestampNS;
|
|
399
346
|
lastReadMessageId?: string;
|
|
400
347
|
}) {
|
|
401
348
|
const userProgress = this.ensureUser(user);
|
|
402
349
|
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
350
|
+
// A missing or non-finite `lastReadAt` means "never read".
|
|
351
|
+
const newReadRef: MsgRef =
|
|
352
|
+
lastReadAt != null && Number.isFinite(lastReadAt)
|
|
353
|
+
? { timestamp: lastReadAt, msgId: lastReadMessageId ?? '' }
|
|
354
|
+
: { ...MIN_REF };
|
|
406
355
|
|
|
407
|
-
// If no change, exit early.
|
|
408
356
|
if (
|
|
409
357
|
compareRefsAsc(newReadRef, userProgress.lastReadRef) === 0 &&
|
|
410
358
|
newReadRef.msgId === userProgress.lastReadRef.msgId
|
|
@@ -452,6 +400,21 @@ export class MessageReceiptsTracker extends WithSubscriptions {
|
|
|
452
400
|
return this.deliveredSorted.slice(pos).map((x) => x.user);
|
|
453
401
|
}
|
|
454
402
|
|
|
403
|
+
/**
|
|
404
|
+
* Whether any other member has this message delivered. Uses the furthest cursor, so it is monotonic
|
|
405
|
+
* across messages.
|
|
406
|
+
*/
|
|
407
|
+
isDeliveredToOthers(msgRef: MsgRef): boolean {
|
|
408
|
+
const ref = this.snapshotStore.getLatestValue().lastDeliveredRefByOthers;
|
|
409
|
+
return !!ref && compareRefsAsc(msgRef, ref) <= 0;
|
|
410
|
+
}
|
|
411
|
+
|
|
412
|
+
/** As {@link isDeliveredToOthers}, for reads. */
|
|
413
|
+
isReadByOthers(msgRef: MsgRef): boolean {
|
|
414
|
+
const ref = this.snapshotStore.getLatestValue().lastReadRefByOthers;
|
|
415
|
+
return !!ref && compareRefsAsc(msgRef, ref) <= 0;
|
|
416
|
+
}
|
|
417
|
+
|
|
455
418
|
/** Users who delivered but have NOT read. */
|
|
456
419
|
deliveredNotReadForMessage(msgRef: MsgRef): UserResponse[] {
|
|
457
420
|
const pos = findIndex(
|
|
@@ -494,8 +457,6 @@ export class MessageReceiptsTracker extends WithSubscriptions {
|
|
|
494
457
|
return users;
|
|
495
458
|
}
|
|
496
459
|
|
|
497
|
-
// ---- queries: per-user status ----
|
|
498
|
-
|
|
499
460
|
hasUserRead(msgRef: MsgRef, userId: string): boolean {
|
|
500
461
|
const up = this.byUser.get(userId);
|
|
501
462
|
return !!up && compareRefsAsc(up.lastReadRef, msgRef) >= 0;
|
|
@@ -590,7 +551,8 @@ export class MessageReceiptsTracker extends WithSubscriptions {
|
|
|
590
551
|
) === 0 &&
|
|
591
552
|
existingUserProgress.lastDeliveredRef.msgId ===
|
|
592
553
|
nextUserProgress.lastDeliveredRef.msgId;
|
|
593
|
-
|
|
554
|
+
// by reference: an updated user (a new name or image) arrives as a new object with the same ID
|
|
555
|
+
const hasSameUser = existingUserProgress.user === nextUserProgress.user;
|
|
594
556
|
|
|
595
557
|
if (hasSameReadRef && hasSameDeliveredRef && hasSameUser) {
|
|
596
558
|
return false;
|
|
@@ -620,27 +582,25 @@ export class MessageReceiptsTracker extends WithSubscriptions {
|
|
|
620
582
|
}
|
|
621
583
|
|
|
622
584
|
private readStateToUserProgress(readState: {
|
|
623
|
-
last_read:
|
|
585
|
+
last_read: TimestampNS;
|
|
624
586
|
unread_messages?: number;
|
|
625
587
|
user: UserResponse;
|
|
626
588
|
first_unread_message_id?: string;
|
|
627
589
|
last_read_message_id?: string;
|
|
628
|
-
last_delivered_at?:
|
|
590
|
+
last_delivered_at?: TimestampNS;
|
|
629
591
|
last_delivered_message_id?: string;
|
|
630
592
|
}): UserProgress {
|
|
631
|
-
const lastReadTimestamp =
|
|
632
|
-
const lastDeliveredTimestamp = readState.last_delivered_at
|
|
633
|
-
? toTimestampMs(readState.last_delivered_at)
|
|
634
|
-
: null;
|
|
593
|
+
const lastReadTimestamp = readState.last_read;
|
|
594
|
+
const lastDeliveredTimestamp = readState.last_delivered_at ?? null;
|
|
635
595
|
const lastReadRef = readState.last_read_message_id
|
|
636
|
-
? {
|
|
596
|
+
? { timestamp: lastReadTimestamp, msgId: readState.last_read_message_id }
|
|
637
597
|
: (this.locateMessage(lastReadTimestamp) ?? MIN_REF);
|
|
638
598
|
let lastDeliveredRef = readState.last_delivered_message_id
|
|
639
599
|
? {
|
|
640
|
-
|
|
600
|
+
timestamp: lastDeliveredTimestamp ?? lastReadTimestamp,
|
|
641
601
|
msgId: readState.last_delivered_message_id,
|
|
642
602
|
}
|
|
643
|
-
: lastDeliveredTimestamp
|
|
603
|
+
: lastDeliveredTimestamp != null && Number.isFinite(lastDeliveredTimestamp)
|
|
644
604
|
? (this.locateMessage(lastDeliveredTimestamp) ?? MIN_REF)
|
|
645
605
|
: MIN_REF;
|
|
646
606
|
|
|
@@ -661,17 +621,13 @@ export class MessageReceiptsTracker extends WithSubscriptions {
|
|
|
661
621
|
return Object.values(readState).reduce<ReadStateResponse[]>(
|
|
662
622
|
(responses, userReadState) => {
|
|
663
623
|
if (!isValidReadState(userReadState)) return responses;
|
|
664
|
-
const lastReadDate = new Date(userReadState.last_read);
|
|
665
|
-
if (Number.isNaN(lastReadDate.getTime())) return responses;
|
|
666
624
|
|
|
667
625
|
responses.push({
|
|
668
|
-
last_read:
|
|
626
|
+
last_read: userReadState.last_read,
|
|
669
627
|
user: userReadState.user,
|
|
670
628
|
last_read_message_id: userReadState.last_read_message_id,
|
|
671
629
|
unread_messages: userReadState.unread_messages ?? 0,
|
|
672
|
-
last_delivered_at: userReadState.last_delivered_at
|
|
673
|
-
? new Date(userReadState.last_delivered_at)
|
|
674
|
-
: undefined,
|
|
630
|
+
last_delivered_at: userReadState.last_delivered_at,
|
|
675
631
|
last_delivered_message_id: userReadState.last_delivered_message_id,
|
|
676
632
|
});
|
|
677
633
|
|
|
@@ -681,6 +637,25 @@ export class MessageReceiptsTracker extends WithSubscriptions {
|
|
|
681
637
|
);
|
|
682
638
|
}
|
|
683
639
|
|
|
640
|
+
/**
|
|
641
|
+
* The furthest ref in an ascending-by-cursor array, ignoring this user's own progress - your own
|
|
642
|
+
* cursor says nothing about whether anyone received the message.
|
|
643
|
+
*/
|
|
644
|
+
private furthestRefByOthers(
|
|
645
|
+
sorted: UserProgress[],
|
|
646
|
+
refOf: (progress: UserProgress) => MsgRef,
|
|
647
|
+
): MsgRef | null {
|
|
648
|
+
const ownUserId = this.channel.getClient().userId;
|
|
649
|
+
|
|
650
|
+
for (let i = sorted.length - 1; i >= 0; i -= 1) {
|
|
651
|
+
if (sorted[i].user.id === ownUserId) continue;
|
|
652
|
+
const ref = refOf(sorted[i]);
|
|
653
|
+
return Number.isFinite(ref.timestamp) ? ref : null;
|
|
654
|
+
}
|
|
655
|
+
|
|
656
|
+
return null;
|
|
657
|
+
}
|
|
658
|
+
|
|
684
659
|
private emitSnapshot() {
|
|
685
660
|
const readersByMessageId = this.groupUsersByLastReadMessage();
|
|
686
661
|
const deliveredByMessageId = this.groupUsersByLastDeliveredMessage();
|
|
@@ -690,6 +665,14 @@ export class MessageReceiptsTracker extends WithSubscriptions {
|
|
|
690
665
|
revision: currentSnapshot.revision + 1,
|
|
691
666
|
readersByMessageId,
|
|
692
667
|
deliveredByMessageId,
|
|
668
|
+
lastDeliveredRefByOthers: this.furthestRefByOthers(
|
|
669
|
+
this.deliveredSorted,
|
|
670
|
+
({ lastDeliveredRef }) => lastDeliveredRef,
|
|
671
|
+
),
|
|
672
|
+
lastReadRefByOthers: this.furthestRefByOthers(
|
|
673
|
+
this.readSorted,
|
|
674
|
+
({ lastReadRef }) => lastReadRef,
|
|
675
|
+
),
|
|
693
676
|
});
|
|
694
677
|
}
|
|
695
678
|
}
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
import { localMessageToNewMessagePayload } from '../utils';
|
|
2
|
+
import type { LocalMessage, MessageRequest } from '../types';
|
|
3
|
+
import type { MessageOperationsConfig } from './MessageOperations';
|
|
4
|
+
import type { OperationParams } from './types';
|
|
5
|
+
|
|
6
|
+
type FailedSendCacheEntry = {
|
|
7
|
+
message: MessageRequest;
|
|
8
|
+
options?: OperationParams<'send'>['options'];
|
|
9
|
+
cachedAt: number;
|
|
10
|
+
};
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* Payloads of failed sends, kept so a retry sends exactly what failed. Entries expire after
|
|
14
|
+
* `failedSendCacheTtlMs`, and past `failedSendCacheMaxSize` the oldest is dropped. Both limits are read
|
|
15
|
+
* from the owner's config on every call.
|
|
16
|
+
*/
|
|
17
|
+
export class FailedSendCache {
|
|
18
|
+
private readonly entries = new Map<string, FailedSendCacheEntry>();
|
|
19
|
+
|
|
20
|
+
constructor(private readonly getConfig: () => Readonly<MessageOperationsConfig>) {}
|
|
21
|
+
|
|
22
|
+
add({
|
|
23
|
+
message,
|
|
24
|
+
messageId,
|
|
25
|
+
options,
|
|
26
|
+
}: {
|
|
27
|
+
messageId: string;
|
|
28
|
+
message: MessageRequest;
|
|
29
|
+
options?: OperationParams<'send'>['options'];
|
|
30
|
+
}) {
|
|
31
|
+
this.pruneExpired();
|
|
32
|
+
|
|
33
|
+
if (
|
|
34
|
+
!this.entries.has(messageId) &&
|
|
35
|
+
this.entries.size >= this.getConfig().failedSendCacheMaxSize
|
|
36
|
+
) {
|
|
37
|
+
const oldestMessageId = this.entries.keys().next().value;
|
|
38
|
+
if (oldestMessageId) {
|
|
39
|
+
this.clear(oldestMessageId);
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
this.entries.set(messageId, { cachedAt: Date.now(), message, options });
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
get(messageId: string) {
|
|
47
|
+
const cached = this.entries.get(messageId);
|
|
48
|
+
if (!cached) return;
|
|
49
|
+
|
|
50
|
+
if (Date.now() - cached.cachedAt > this.getConfig().failedSendCacheTtlMs) {
|
|
51
|
+
this.clear(messageId);
|
|
52
|
+
return;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
return cached;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
clear(messageId: string) {
|
|
59
|
+
this.entries.delete(messageId);
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/** Folds an edit into the cached payload, so a retry resends the message as it is now. */
|
|
63
|
+
rewriteWithEdit(localMessage: LocalMessage) {
|
|
64
|
+
const cached = this.get(localMessage.id);
|
|
65
|
+
if (!cached) return;
|
|
66
|
+
|
|
67
|
+
this.entries.set(localMessage.id, {
|
|
68
|
+
...cached,
|
|
69
|
+
message: {
|
|
70
|
+
...cached.message,
|
|
71
|
+
...localMessageToNewMessagePayload(localMessage),
|
|
72
|
+
},
|
|
73
|
+
});
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
private pruneExpired() {
|
|
77
|
+
const now = Date.now();
|
|
78
|
+
|
|
79
|
+
for (const [messageId, entry] of this.entries) {
|
|
80
|
+
if (now - entry.cachedAt > this.getConfig().failedSendCacheTtlMs) {
|
|
81
|
+
this.clear(messageId);
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
}
|