Skip to content

Commit

Permalink
Properly unset the active channel in the server (#7919)
Browse files Browse the repository at this point in the history
  • Loading branch information
larkox committed May 6, 2024
1 parent 105073c commit a6d0436
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 4 deletions.
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

0 comments on commit a6d0436

Please sign in to comment.