Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Properly unset the active channel in the server #7919

Merged
merged 1 commit into from
May 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
11 changes: 11 additions & 0 deletions app/actions/remote/channel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
9 changes: 8 additions & 1 deletion app/actions/websocket/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -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);
Expand Down
21 changes: 18 additions & 3 deletions app/screens/channel/channel_post_list/channel_post_list.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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}/>);

Expand Down