From c2aaef2e14db019b93257dc089514c6d75a440bd Mon Sep 17 00:00:00 2001 From: Elias Nahum Date: Fri, 6 Jan 2023 11:38:40 +0200 Subject: [PATCH] Preserve new message line on WS reconnect (#6934) --- app/actions/local/channel.ts | 10 ++++++---- app/actions/websocket/channel.ts | 2 +- app/actions/websocket/index.ts | 2 +- app/actions/websocket/posts.ts | 2 +- app/init/push_notifications.ts | 2 +- 5 files changed, 10 insertions(+), 8 deletions(-) diff --git a/app/actions/local/channel.ts b/app/actions/local/channel.ts index 03ab9403fc..be4c2e2194 100644 --- a/app/actions/local/channel.ts +++ b/app/actions/local/channel.ts @@ -76,7 +76,7 @@ export async function switchToChannel(serverUrl: string, channelId: string, team } models = (await Promise.all(modelPromises)).flat(); - const {member: viewedAt} = await markChannelAsViewed(serverUrl, channelId, true); + const {member: viewedAt} = await markChannelAsViewed(serverUrl, channelId, false, true); if (viewedAt) { models.push(viewedAt); } @@ -160,7 +160,7 @@ export async function selectAllMyChannelIds(serverUrl: string) { } } -export async function markChannelAsViewed(serverUrl: string, channelId: string, prepareRecordsOnly = false) { +export async function markChannelAsViewed(serverUrl: string, channelId: string, onlyCounts = false, prepareRecordsOnly = false) { try { const {database, operator} = DatabaseManager.getServerDatabaseAndOperator(serverUrl); const member = await getMyChannel(database, channelId); @@ -172,8 +172,10 @@ export async function markChannelAsViewed(serverUrl: string, channelId: string, m.isUnread = false; m.mentionsCount = 0; m.manuallyUnread = false; - m.viewedAt = member.lastViewedAt; - m.lastViewedAt = Date.now(); + if (!onlyCounts) { + m.viewedAt = member.lastViewedAt; + m.lastViewedAt = Date.now(); + } }); PushNotifications.removeChannelNotifications(serverUrl, channelId); if (!prepareRecordsOnly) { diff --git a/app/actions/websocket/channel.ts b/app/actions/websocket/channel.ts index e765f17e06..189c7f7d12 100644 --- a/app/actions/websocket/channel.ts +++ b/app/actions/websocket/channel.ts @@ -128,7 +128,7 @@ export async function handleChannelViewedEvent(serverUrl: string, msg: any) { const currentChannelId = await getCurrentChannelId(database); if (activeServerUrl !== serverUrl || (currentChannelId !== channelId && !EphemeralStore.isSwitchingToChannel(channelId))) { - await markChannelAsViewed(serverUrl, channelId, false); + await markChannelAsViewed(serverUrl, channelId); } } catch { // do nothing diff --git a/app/actions/websocket/index.ts b/app/actions/websocket/index.ts index 88d3145f15..6fc0f370ec 100644 --- a/app/actions/websocket/index.ts +++ b/app/actions/websocket/index.ts @@ -444,7 +444,7 @@ async function fetchPostDataIfNeeded(serverUrl: string) { await fetchPostsForChannel(serverUrl, currentChannelId); markChannelAsRead(serverUrl, currentChannelId); if (!EphemeralStore.wasNotificationTapped()) { - markChannelAsViewed(serverUrl, currentChannelId); + markChannelAsViewed(serverUrl, currentChannelId, true); } EphemeralStore.setNotificationTapped(false); } diff --git a/app/actions/websocket/posts.ts b/app/actions/websocket/posts.ts index 88e5ea87cc..9123790cf6 100644 --- a/app/actions/websocket/posts.ts +++ b/app/actions/websocket/posts.ts @@ -143,7 +143,7 @@ export async function handleNewPostEvent(serverUrl: string, msg: WebSocketMessag markChannelAsRead(serverUrl, post.channel_id); } else if (markAsViewed) { preparedMyChannelHack(myChannel); - const {member: viewedAt} = await markChannelAsViewed(serverUrl, post.channel_id, true); + const {member: viewedAt} = await markChannelAsViewed(serverUrl, post.channel_id, false, true); if (viewedAt) { models.push(viewedAt); } diff --git a/app/init/push_notifications.ts b/app/init/push_notifications.ts index 14aac23155..13df0a194c 100644 --- a/app/init/push_notifications.ts +++ b/app/init/push_notifications.ts @@ -96,7 +96,7 @@ class PushNotifications { markThreadAsRead(serverUrl, payload.team_id, payload.post_id); } } else { - markChannelAsViewed(serverUrl, payload.channel_id, false); + markChannelAsViewed(serverUrl, payload.channel_id); } } }