Properly unset the active channel in the server (#7919)

This commit is contained in:
Daniel Espino García 2024-05-06 10:46:20 +02:00 committed by GitHub
parent 105073ce76
commit a6d043670a
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 37 additions and 4 deletions

View file

@ -667,6 +667,17 @@ export async function markChannelAsRead(serverUrl: string, channelId: string, up
}
}
export async function unsetActiveChannelOnServer(serverUrl: string) {
try {
const client = NetworkManager.getClient(serverUrl);
await client.viewMyChannel('');
return {};
} catch (error) {
logDebug('error on markChannelAsRead', getFullErrorMessage(error));
return {error};
}
}
export async function switchToChannelByName(serverUrl: string, channelName: string, teamName: string, errorHandler: (intl: IntlShape) => void, intl: IntlShape) {
const onError = (joinedTeam: boolean, teamId?: string) => {
errorHandler(intl);

View file

@ -41,6 +41,7 @@ import {Screens, WebsocketEvents} from '@constants';
import {SYSTEM_IDENTIFIERS} from '@constants/database';
import DatabaseManager from '@database/manager';
import AppsManager from '@managers/apps_manager';
import {getActiveServerUrl} from '@queries/app/servers';
import {getLastPostInThread} from '@queries/servers/post';
import {
getConfig,
@ -171,7 +172,8 @@ async function doReconnect(serverUrl: string) {
}
const tabletDevice = isTablet();
if (tabletDevice && initialChannelId === currentChannelId) {
const isActiveServer = (await getActiveServerUrl()) === serverUrl;
if (isActiveServer && tabletDevice && initialChannelId === currentChannelId) {
await markChannelAsRead(serverUrl, initialChannelId);
markChannelAsViewed(serverUrl, initialChannelId);
}
@ -483,6 +485,11 @@ export async function handleEvent(serverUrl: string, msg: WebSocketMessage) {
async function fetchPostDataIfNeeded(serverUrl: string) {
try {
const isActiveServer = (await getActiveServerUrl()) === serverUrl;
if (!isActiveServer) {
return;
}
const {database} = DatabaseManager.getServerDatabaseAndOperator(serverUrl);
const currentChannelId = await getCurrentChannelId(database);
const isCRTEnabled = await getIsCRTEnabled(database);

View file

@ -5,7 +5,7 @@ import React, {useCallback, useEffect, useRef, useState} from 'react';
import {type StyleProp, StyleSheet, type ViewStyle, DeviceEventEmitter} from 'react-native';
import {type Edge, SafeAreaView} from 'react-native-safe-area-context';
import {markChannelAsRead} from '@actions/remote/channel';
import {markChannelAsRead, unsetActiveChannelOnServer} from '@actions/remote/channel';
import {fetchPosts, fetchPostsBefore} from '@actions/remote/post';
import {PER_PAGE_DEFAULT} from '@client/rest/constants';
import PostList from '@components/post_list';
@ -84,12 +84,27 @@ const ChannelPostList = ({
}
}, [fetchingPosts, posts]);
useEffect(() => {
useDidUpdate(() => {
if (oldPostsCount.current < posts.length && appState === 'active') {
oldPostsCount.current = posts.length;
markChannelAsRead(serverUrl, channelId, true);
}
}, [isCRTEnabled, posts, channelId, serverUrl, appState === 'active']);
}, [posts.length]);
useDidUpdate(() => {
if (appState === 'active') {
markChannelAsRead(serverUrl, channelId, true);
}
if (appState !== 'active') {
unsetActiveChannelOnServer(serverUrl);
}
}, [appState === 'active']);
useEffect(() => {
return () => {
unsetActiveChannelOnServer(serverUrl);
};
}, []);
const intro = (<Intro channelId={channelId}/>);