diff --git a/plans/apps-upsert-old-item-updates.md b/plans/apps-upsert-old-item-updates.md index 49c83fa004..e6ba8315ac 100644 --- a/plans/apps-upsert-old-item-updates.md +++ b/plans/apps-upsert-old-item-updates.md @@ -33,7 +33,7 @@ So when `upsertChatItem` does not add an item that is not loaded, it keeps the u - `addChatItem` adds the kept item instead of the item it got when the kept item's `updatedAt` is not earlier. The core reports an updated item with the `updatedAt` it had before the change: status changes and deletions write a new `updated_at` to the database only, and edits and file status changes do not change it. `addChatItem` gets the item as it was created (send and forward responses, `NewChatItems`), and every update of it comes later, so the kept item's `updatedAt` is that of the created item or, after an earlier status change or deletion, later, and the kept item is the newer state. Otherwise (only when the clock went back) it adds the item it got with the kept status when `shouldKeepOldSndCIStatus` prefers it. So the send response does not replace a kept status (`SndSent`, `SndRcvd`) or file status with `SndNew` and the initial file status, and on Android and desktop `NewChatItems` does not replace an edit, moderation or deletion mark that came first. - Kept updates are not applied to pages: an update that arrives while `ChatItemsLoader` reads a page that contains its item is applied to neither the loaded items nor the page, so the page shows the earlier state until the item's next update or until the chat is reopened, as before (see Open questions). -- A kept update is removed when `addChatItem` gets its item, whether or not it adds it, when an update of another chat is kept, by the limit below, or when kept updates are cleared. It stays when `upsertChatItem` adds its item, when a page contains its item, or when a chat is shown from items that are already loaded (Kotlin `openLoadedChat`). That is harmless: only `addChatItem` applies a kept update, only to the item with the same ID, and only to an item as it was created, which no kept update is older than. Android and desktop do not change an item that is already loaded when it is added. iOS updates it in place, as before, now with the kept item instead of the created one: the status is kept by `shouldKeepOldSndCIStatus`, and the rest is at least as new as what was applied before. +- A kept update is removed when `addChatItem` gets its item while its chat is open (in scope), whether or not it adds it, when an update of another chat is kept, by the limit below, or when kept updates are cleared. It stays when `upsertChatItem` adds its item, when a page contains its item, or when a chat is shown from items that are already loaded (Kotlin `openLoadedChat`). That is harmless: only `addChatItem` applies a kept update, only to the item with the same ID, and only to an item as it was created, which no kept update is older than. Android and desktop do not change an item that is already loaded when it is added. iOS updates it in place, as before, now with the kept item instead of the created one: the status is kept by `shouldKeepOldSndCIStatus`, and the rest is at least as new as what was applied before. The kept updates are of one chat only: they are cleared when an update of another chat is kept (on Android and desktop, also of the same chat of another remote host). Member support chats and reports have their own (`secondaryChatsContext`, `secondaryIM`). Kept updates of a chat that is no longer open stay until an update of another chat is kept, and they are applied only to the chat and database they were kept for: `addChatItem` applies them only to items of the same chat (and remote host), and they are cleared when the database is deleted (also by the self-destruct passcode and migration) or imported, and on Android and desktop when the remote host is switched (`ChatModel.clearDroppedUpdates`). At most 100 updates are kept; above that, the one with the lowest ID is removed, because the items that are still to be added are the newest. Most kept updates are of items above the loaded part of the chat, for example receipts or edits of old messages; `addChatItem` never gets those items, so they are never applied. @@ -58,5 +58,5 @@ Not changed: - Page loads, both platforms: an update of an item of a page that arrives while the page is read is applied to neither the loaded items nor the page (above). Applying kept updates to a merged page cannot tell whether the page already has the update, because the reported `updatedAt` does not change with it. A fix needs either an event sequence counter in the apps, to apply only updates kept after the page read started, or the core reporting a new `updatedAt` for edits, status changes and deletions. - Items the core creates from updates (`ChatItemUpdated` of a message the user never received) are not added and do not notify while the newest items of the chat are not loaded, or after the user sent a message before the update arrived; they are shown, without a notification, when the newest items are loaded. - Groups: items are ordered by timestamp, so an update of a message received late, placed above more than a page of messages but with a higher ID than every loaded item and the preview, is still added at the bottom. -- Kotlin: when the bottommost loaded item is removed (`ChatItemsMerger` `itemsRemoved`), its split is dropped, because the split is moved to the item below the removed one and there is none, so the newest items count as loaded although they are not. In the main chat the preview's item ID still bounds the IDs, except where it does not (groups, a deleted last item, above); member support chats and reports are bounded only by the loaded items. +- Both platforms: when the bottommost loaded item is removed (`ChatItemsMerger` `itemsRemoved`), its split is dropped, because the split is moved to the item below the removed one and there is none, so the newest items count as loaded although they are not. In the main chat the preview's item ID still bounds the IDs, except where it does not (groups, a deleted last item, above); member support chats and reports are bounded only by the loaded items. - Core alternative: the core reporting items it creates from updates as new items (`NewChatItems`) would let the apps never add an item from an update, and would remove the bottom-of-chat check, the ID rule and the group timestamp case above.