From f2b4176db3d7c7fcb5a21f17591cc6f66a82d2a7 Mon Sep 17 00:00:00 2001
From: Kristina <kristin.fefelova@gmail.com>
Date: Wed, 13 Mar 2024 19:05:26 +0400
Subject: [PATCH] Chat fixes (#4947)

Signed-off-by: Kristina Fefelova <kristin.fefelova@gmail.com>
---
 .../src/components/ChannelScrollView.svelte   | 46 ++++++++++++++++---
 .../src/inboxNotificationsClient.ts           | 14 ++++++
 .../src/components/Chat.svelte                |  4 +-
 3 files changed, 55 insertions(+), 9 deletions(-)

diff --git a/plugins/chunter-resources/src/components/ChannelScrollView.svelte b/plugins/chunter-resources/src/components/ChannelScrollView.svelte
index 2bcb5e074c..2bcabe1720 100644
--- a/plugins/chunter-resources/src/components/ChannelScrollView.svelte
+++ b/plugins/chunter-resources/src/components/ChannelScrollView.svelte
@@ -52,7 +52,7 @@
   export let isAsideOpened = false
 
   const dateSelectorHeight = 30
-  const headerHeight = 50
+  const headerHeight = 52
   const minMsgHeightRem = 4.375
 
   const client = getClient()
@@ -316,10 +316,9 @@
       return
     }
 
-    const reversedMessages = [...displayMessages].reverse()
     const reversedDates = [...get(datesStore)].reverse()
 
-    for (const message of reversedMessages) {
+    for (const message of displayMessages) {
       const msgElement = messagesElements?.[message._id as any]
 
       if (!msgElement) {
@@ -332,12 +331,37 @@
         continue
       }
 
-      if (messageInView(msgElement, containerRect)) {
+      const messageRect = msgElement.getBoundingClientRect()
+
+      const isInView =
+        messageRect.top > 0 &&
+        messageRect.top < containerRect.bottom &&
+        messageRect.bottom - headerHeight - 2 * dateSelectorHeight > 0 &&
+        messageRect.bottom <= containerRect.bottom
+
+      if (isInView) {
         selectedDate = reversedDates.find((date) => date <= createdOn)
 
         break
       }
     }
+
+    if (selectedDate) {
+      const day = getDay(selectedDate)
+      const dateElement = document.getElementById(day.toString())
+
+      let isElementVisible = false
+
+      if (dateElement) {
+        const elementRect = dateElement.getBoundingClientRect()
+        isElementVisible =
+          elementRect.top + dateSelectorHeight / 2 >= containerRect.top && elementRect.bottom <= containerRect.bottom
+      }
+
+      if (isElementVisible) {
+        selectedDate = undefined
+      }
+    }
   }
 
   $: void initializeScroll($isLoadingStore, separatorElement, $newTimestampStore)
@@ -379,9 +403,10 @@
       shouldWaitAndRead = false
       onComplete?.()
     } else if (shouldWaitAndRead && messages.length > 0) {
+      shouldWaitAndRead = false
       setTimeout(() => {
         waitLastMessageRenderAndRead(onComplete)
-      }, 50)
+      }, 500)
     } else {
       onComplete?.()
     }
@@ -514,8 +539,8 @@
     {#if startFromBottom}
       <div class="grower" />
     {/if}
-    {#if withDates && displayMessages.length > 0}
-      <div class="ml-2 pr-2">
+    {#if withDates && displayMessages.length > 0 && selectedDate}
+      <div class="selectedDate">
         <JumpToDateSelector {selectedDate} fixed on:jumpToDate={jumpToDate} />
       </div>
     {/if}
@@ -606,4 +631,11 @@
     align-items: center;
     justify-content: center;
   }
+
+  .selectedDate {
+    position: absolute;
+    top: 0;
+    left: 0;
+    right: 0;
+  }
 </style>
diff --git a/plugins/notification-resources/src/inboxNotificationsClient.ts b/plugins/notification-resources/src/inboxNotificationsClient.ts
index 3e93e3221f..b774223b8a 100644
--- a/plugins/notification-resources/src/inboxNotificationsClient.ts
+++ b/plugins/notification-resources/src/inboxNotificationsClient.ts
@@ -258,9 +258,14 @@ export class InboxNotificationsClientImpl implements InboxNotificationsClient {
 
     try {
       const inboxNotifications = get(this.inboxNotifications) ?? []
+      const contexts = get(this.docNotifyContexts) ?? []
       for (const notification of inboxNotifications) {
         await ops.remove(notification)
       }
+
+      for (const context of contexts) {
+        await ops.update(context, { lastViewedTimestamp: Date.now() })
+      }
     } finally {
       await ops.commit()
       await doneOp()
@@ -273,11 +278,15 @@ export class InboxNotificationsClientImpl implements InboxNotificationsClient {
 
     try {
       const inboxNotifications = get(this.inboxNotifications) ?? []
+      const contexts = get(this.docNotifyContexts) ?? []
       for (const notification of inboxNotifications) {
         if (!notification.isViewed) {
           await ops.update(notification, { isViewed: true })
         }
       }
+      for (const context of contexts) {
+        await ops.update(context, { lastViewedTimestamp: Date.now() })
+      }
     } finally {
       await ops.commit()
       await doneOp()
@@ -290,11 +299,16 @@ export class InboxNotificationsClientImpl implements InboxNotificationsClient {
 
     try {
       const inboxNotifications = get(this.inboxNotifications) ?? []
+      const contexts = get(this.docNotifyContexts) ?? []
+
       for (const notification of inboxNotifications) {
         if (notification.isViewed) {
           await ops.update(notification, { isViewed: false })
         }
       }
+      for (const context of contexts) {
+        await ops.update(context, { lastViewedTimestamp: 0 })
+      }
     } finally {
       await ops.commit()
       await doneOp()
diff --git a/plugins/telegram-resources/src/components/Chat.svelte b/plugins/telegram-resources/src/components/Chat.svelte
index a762422842..8fc1eb61bd 100644
--- a/plugins/telegram-resources/src/components/Chat.svelte
+++ b/plugins/telegram-resources/src/components/Chat.svelte
@@ -88,7 +88,7 @@
       (res) => {
         messages = res.reverse()
         if (channel !== undefined) {
-          inboxClient.forceReadDoc(channel._id, channel._class)
+          inboxClient.forceReadDoc(client, channel._id, channel._class)
         }
       },
       {
@@ -149,7 +149,7 @@
       }
     )
     if (channel !== undefined) {
-      await inboxClient.forceReadDoc(channel._id, channel._class)
+      await inboxClient.forceReadDoc(client, channel._id, channel._class)
     }
     clear()
   }