diff --git a/app/queries/servers/channel.ts b/app/queries/servers/channel.ts index ab917c6ae..25666bfd7 100644 --- a/app/queries/servers/channel.ts +++ b/app/queries/servers/channel.ts @@ -469,22 +469,16 @@ export function observeMyChannelMentionCount(database: Database, teamId?: string ); } -export function queryMyChannelsByUnread(database: Database, isUnread: boolean, sortBy: 'last_viewed_at' | 'last_post_at', take: number, excludeIds?: string[]) { - const clause: Q.Clause[] = [Q.where('is_unread', Q.eq(isUnread))]; +export function queryMyRecentChannels(database: Database, take: number) { const count: Q.Clause[] = []; - if (excludeIds?.length) { - clause.push(Q.where('id', Q.notIn(excludeIds))); - } - if (take > 0) { count.push(Q.take(take)); } return queryAllMyChannel(database).extend( Q.on(CHANNEL, Q.where('delete_at', Q.eq(0))), - ...clause, - Q.sortBy(sortBy, Q.desc), + Q.sortBy('last_viewed_at', Q.desc), ...count, ); } diff --git a/app/screens/find_channels/unfiltered_list/index.ts b/app/screens/find_channels/unfiltered_list/index.ts index 4fcbcdf3f..e094d6df3 100644 --- a/app/screens/find_channels/unfiltered_list/index.ts +++ b/app/screens/find_channels/unfiltered_list/index.ts @@ -1,57 +1,32 @@ // Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved. // See LICENSE.txt for license information. -import {Database} from '@nozbe/watermelondb'; import {withDatabase} from '@nozbe/watermelondb/DatabaseProvider'; import withObservables from '@nozbe/with-observables'; import {of as of$} from 'rxjs'; -import {combineLatestWith, map, switchMap} from 'rxjs/operators'; +import {switchMap} from 'rxjs/operators'; -import {filterAndSortMyChannels, makeChannelsMap} from '@helpers/database'; -import {observeNotifyPropsByChannels, queryMyChannelsByUnread} from '@queries/servers/channel'; +import {queryMyRecentChannels} from '@queries/servers/channel'; import {queryJoinedTeams} from '@queries/servers/team'; import {retrieveChannels} from '@screens/find_channels/utils'; import UnfilteredList from './unfiltered_list'; import type {WithDatabaseArgs} from '@typings/database/database'; -import type ChannelModel from '@typings/database/models/servers/channel'; -const MAX_UNREAD_CHANNELS = 10; const MAX_CHANNELS = 20; -const observeRecentChannels = (database: Database, unreads: ChannelModel[]) => { - const count = MAX_CHANNELS - unreads.length; - const unreadIds = unreads.map((u) => u.id); - return queryMyChannelsByUnread(database, false, 'last_viewed_at', count, unreadIds).observe().pipe( - switchMap((myChannels) => retrieveChannels(database, myChannels, true)), - ); -}; - const enhanced = withObservables([], ({database}: WithDatabaseArgs) => { const teamsCount = queryJoinedTeams(database).observeCount(); - const myUnreadChannels = queryMyChannelsByUnread(database, true, 'last_post_at', 0). - observeWithColumns(['last_post_at']); - const notifyProps = myUnreadChannels.pipe(switchMap((cs) => observeNotifyPropsByChannels(database, cs))); - const channels = myUnreadChannels.pipe( - switchMap((myChannels) => retrieveChannels(database, myChannels)), - ); - const channelsMap = channels.pipe(switchMap((cs) => of$(makeChannelsMap(cs)))); - const unreadChannels = myUnreadChannels.pipe( - combineLatestWith(channelsMap, notifyProps), - map(filterAndSortMyChannels), - switchMap((cs) => of$(cs.slice(0, MAX_UNREAD_CHANNELS))), - ); - - const recentChannels = unreadChannels.pipe( - switchMap((unreads) => observeRecentChannels(database, unreads)), - ); + const recentChannels = queryMyRecentChannels(database, MAX_CHANNELS). + observeWithColumns(['last_viewed_at']).pipe( + switchMap((myChannels) => retrieveChannels(database, myChannels, true)), + ); return { recentChannels, showTeamName: teamsCount.pipe(switchMap((count) => of$(count > 1))), - unreadChannels, }; }); diff --git a/app/screens/find_channels/unfiltered_list/unfiltered_list.tsx b/app/screens/find_channels/unfiltered_list/unfiltered_list.tsx index f335cb9c2..fc0ea767a 100644 --- a/app/screens/find_channels/unfiltered_list/unfiltered_list.tsx +++ b/app/screens/find_channels/unfiltered_list/unfiltered_list.tsx @@ -20,15 +20,10 @@ type Props = { keyboardHeight: number; recentChannels: ChannelModel[]; showTeamName: boolean; - unreadChannels: ChannelModel[]; testID?: string; } const sectionNames = { - unreads: { - id: t('mobile.channel_list.unreads'), - defaultMessage: 'Unreads', - }, recent: { id: t('mobile.channel_list.recent'), defaultMessage: 'Recent', @@ -39,15 +34,8 @@ const style = StyleSheet.create({ flex: {flex: 1}, }); -const buildSections = (unreadChannels: ChannelModel[], recentChannels: ChannelModel[]) => { +const buildSections = (recentChannels: ChannelModel[]) => { const sections = []; - if (unreadChannels.length) { - sections.push({ - ...sectionNames.unreads, - data: unreadChannels, - }); - } - if (recentChannels.length) { sections.push({ ...sectionNames.recent, @@ -58,10 +46,10 @@ const buildSections = (unreadChannels: ChannelModel[], recentChannels: ChannelMo return sections; }; -const UnfilteredList = ({close, keyboardHeight, recentChannels, showTeamName, unreadChannels, testID}: Props) => { +const UnfilteredList = ({close, keyboardHeight, recentChannels, showTeamName, testID}: Props) => { const intl = useIntl(); const serverUrl = useServerUrl(); - const [sections, setSections] = useState(buildSections(unreadChannels, recentChannels)); + const [sections, setSections] = useState(buildSections(recentChannels)); const sectionListStyle = useMemo(() => ({paddingBottom: keyboardHeight}), [keyboardHeight]); const onPress = useCallback(async (channelId: string) => { @@ -86,8 +74,8 @@ const UnfilteredList = ({close, keyboardHeight, recentChannels, showTeamName, un }, [onPress, showTeamName]); useEffect(() => { - setSections(buildSections(unreadChannels, recentChannels)); - }, [unreadChannels, recentChannels]); + setSections(buildSections(recentChannels)); + }, [recentChannels]); return (