From 368f6aa08d30cfa99705290d71400bf9341f1d9c Mon Sep 17 00:00:00 2001 From: Shaz MJ Date: Tue, 22 Mar 2022 09:42:47 +1100 Subject: [PATCH] Handes visibility preference setting for dm/gm --- .../categories/body/category_body.test.tsx | 1 + .../categories/body/category_body.tsx | 18 ++++++--- .../channel_list/categories/body/index.ts | 40 +++++++++++++++---- .../channel_list/categories/categories.tsx | 5 ++- .../channel_list/categories/index.ts | 6 ++- app/constants/preferences.ts | 3 +- app/queries/servers/channel.ts | 4 ++ app/queries/servers/preference.ts | 16 ++++++++ 8 files changed, 78 insertions(+), 15 deletions(-) diff --git a/app/components/channel_list/categories/body/category_body.test.tsx b/app/components/channel_list/categories/body/category_body.test.tsx index 220ac79ae..3a32e3bf2 100644 --- a/app/components/channel_list/categories/body/category_body.test.tsx +++ b/app/components/channel_list/categories/body/category_body.test.tsx @@ -36,6 +36,7 @@ describe('components/channel_list/categories/body', () => { category={category} locale={DEFAULT_LOCALE} currentChannelId={''} + currentUserId={''} />, {database}, ); diff --git a/app/components/channel_list/categories/body/category_body.tsx b/app/components/channel_list/categories/body/category_body.tsx index 38f3787a9..c27f2ac00 100644 --- a/app/components/channel_list/categories/body/category_body.tsx +++ b/app/components/channel_list/categories/body/category_body.tsx @@ -11,19 +11,27 @@ import type CategoryModel from '@typings/database/models/servers/category'; type Props = { currentChannelId: string; sortedIds: string[]; + hiddenChannelIds: string[]; category: CategoryModel; limit: number; }; const extractKey = (item: string) => item; -const CategoryBody = ({currentChannelId, sortedIds, category, limit}: Props) => { +const CategoryBody = ({currentChannelId, sortedIds, category, hiddenChannelIds, limit}: Props) => { const ids = useMemo(() => { - if (category.type === 'direct_messages' && limit > 0) { - return sortedIds.slice(0, limit - 1); + let filteredIds = sortedIds; + + // Remove all closed gm/dms + if (hiddenChannelIds.length) { + filteredIds = sortedIds.filter((id) => !hiddenChannelIds.includes(id)); } - return sortedIds; - }, [category.type, limit]); + + if (category.type === 'direct_messages' && limit > 0) { + return filteredIds.slice(0, limit - 1); + } + return filteredIds; + }, [category.type, limit, hiddenChannelIds]); const ChannelItem = useCallback(({item}: {item: string}) => { return ( diff --git a/app/components/channel_list/categories/body/index.ts b/app/components/channel_list/categories/body/index.ts index 9acf7d37e..3ca076ef4 100644 --- a/app/components/channel_list/categories/body/index.ts +++ b/app/components/channel_list/categories/body/index.ts @@ -8,6 +8,9 @@ import {combineLatest, of as of$} from 'rxjs'; import {switchMap} from 'rxjs/operators'; import {MM_TABLES} from '@app/constants/database'; +import {queryChannelsByNames} from '@app/queries/servers/channel'; +import {queryPreferencesByCategory} from '@app/queries/servers/preference'; +import {getDirectChannelName} from '@app/utils/channel'; import {General, Preferences} from '@constants'; import {WithDatabaseArgs} from '@typings/database/database'; @@ -22,7 +25,7 @@ type ChannelData = Pick & { isMuted: boolean; }; -const {SERVER: {MY_CHANNEL_SETTINGS, PREFERENCE}} = MM_TABLES; +const {SERVER: {MY_CHANNEL_SETTINGS}} = MM_TABLES; const sortAlpha = (locale: string, a: ChannelData, b: ChannelData) => { if (a.isMuted && !b.isMuted) { @@ -82,19 +85,37 @@ const getSortedIds = (database: Database, category: CategoryModel, locale: strin } }; -const enhance = withObservables(['category'], ({category, locale, database}: {category: CategoryModel; locale: string} & WithDatabaseArgs) => { +const mapPrefName = (prefs: PreferenceModel[]) => of$(prefs.map((p) => p.name)); + +const mapChannelIds = (channels: ChannelModel[]) => of$(channels.map((c) => c.id)); + +const enhance = withObservables(['category'], ({category, locale, database, currentUserId}: {category: CategoryModel; locale: string; currentUserId: string} & WithDatabaseArgs) => { const observedCategory = category.observe(); const sortedIds = observedCategory.pipe( switchMap((c) => getSortedIds(database, c, locale)), ); + const dmMap = (p: PreferenceModel) => getDirectChannelName(p.name, currentUserId); + + const hiddenDmIds = queryPreferencesByCategory(database, Preferences.CATEGORY_DIRECT_CHANNEL_SHOW, undefined, 'false'). + observe().pipe( + switchMap((prefs: PreferenceModel[]) => { + const names = prefs.map(dmMap); + const channels = queryChannelsByNames(database, names).observe(); + + return channels.pipe( + switchMap(mapChannelIds), + ); + }), + ); + + const hiddenGmIds = queryPreferencesByCategory(database, Preferences.CATEGORY_GROUP_CHANNEL_SHOW, undefined, 'false'). + observe().pipe(switchMap(mapPrefName)); + let limit = of$(0); if (category.type === 'direct_messages') { - limit = database.get(PREFERENCE). - query( - Q.where('category', Preferences.CATEGORY_SIDEBAR_SETTINGS), - Q.where('name', 'limit_visible_dms_gms'), - ).observe().pipe( + limit = queryPreferencesByCategory(database, Preferences.CATEGORY_SIDEBAR_SETTINGS, Preferences.CHANNEL_SIDEBAR_LIMIT_DMS). + observe().pipe( switchMap( (val) => { if (val[0]) { @@ -107,8 +128,13 @@ const enhance = withObservables(['category'], ({category, locale, database}: {ca ); } + const hiddenChannelIds = combineLatest([hiddenDmIds, hiddenGmIds]).pipe(switchMap( + ([a, b]) => of$(a.concat(b)), + )); + return { limit, + hiddenChannelIds, sortedIds, category: observedCategory, }; diff --git a/app/components/channel_list/categories/categories.tsx b/app/components/channel_list/categories/categories.tsx index 406b12294..5718fd42b 100644 --- a/app/components/channel_list/categories/categories.tsx +++ b/app/components/channel_list/categories/categories.tsx @@ -14,6 +14,7 @@ import type {CategoryModel} from '@database/models/server'; type Props = { categories: CategoryModel[]; currentChannelId: string; + currentUserId: string; } const styles = StyleSheet.create({ @@ -24,8 +25,9 @@ const styles = StyleSheet.create({ const extractKey = (item: CategoryModel) => item.id; -const Categories = ({categories, currentChannelId}: Props) => { +const Categories = ({categories, currentChannelId, currentUserId}: Props) => { const intl = useIntl(); + const renderCategory = useCallback((data: {item: CategoryModel}) => { return ( <> @@ -33,6 +35,7 @@ const Categories = ({categories, currentChannelId}: Props) => { diff --git a/app/components/channel_list/categories/index.ts b/app/components/channel_list/categories/index.ts index cf6010965..7de7c61cb 100644 --- a/app/components/channel_list/categories/index.ts +++ b/app/components/channel_list/categories/index.ts @@ -16,7 +16,7 @@ import type CategoryModel from '@typings/database/models/servers/category'; import type SystemModel from '@typings/database/models/servers/system'; const {SERVER: {CATEGORY, SYSTEM}} = MM_TABLES; -const {CURRENT_CHANNEL_ID} = SYSTEM_IDENTIFIERS; +const {CURRENT_CHANNEL_ID, CURRENT_USER_ID} = SYSTEM_IDENTIFIERS; type WithDatabaseProps = {currentTeamId: string } & WithDatabaseArgs @@ -26,6 +26,9 @@ const enhanced = withObservables( const currentChannelId = database.get(SYSTEM).findAndObserve(CURRENT_CHANNEL_ID).pipe( switchMap(({value}) => of$(value)), ); + const currentUserId = database.get(SYSTEM).findAndObserve(CURRENT_USER_ID).pipe( + switchMap(({value}) => of$(value)), + ); const categories = database.get(CATEGORY).query( Q.where('team_id', currentTeamId), ).observeWithColumns(['sort_order']); @@ -33,6 +36,7 @@ const enhanced = withObservables( return { currentChannelId, categories, + currentUserId, }; }); diff --git a/app/constants/preferences.ts b/app/constants/preferences.ts index 0a962329a..c76a1d17b 100644 --- a/app/constants/preferences.ts +++ b/app/constants/preferences.ts @@ -11,6 +11,7 @@ const Preferences: Record = { CATEGORY_FAVORITE_CHANNEL: 'favorite_channel', CATEGORY_AUTO_RESET_MANUAL_STATUS: 'auto_reset_manual_status', CATEGORY_NOTIFICATIONS: 'notifications', + COMMENTS: 'comments', COMMENTS_ANY: 'any', COMMENTS_ROOT: 'root', @@ -35,7 +36,7 @@ const Preferences: Record = { USE_MILITARY_TIME: 'use_military_time', CATEGORY_SIDEBAR_SETTINGS: 'sidebar_settings', CHANNEL_SIDEBAR_ORGANIZATION: 'channel_sidebar_organization', - CHANNEL_SIDEBAR_AUTOCLOSE_DMS: 'close_unused_direct_messages', + CHANNEL_SIDEBAR_LIMIT_DMS: 'limit_visible_dms_gms', AUTOCLOSE_DMS_ENABLED: 'after_seven_days', CATEGORY_ADVANCED_SETTINGS: 'advanced_settings', ADVANCED_FILTER_JOIN_LEAVE: 'join_leave', diff --git a/app/queries/servers/channel.ts b/app/queries/servers/channel.ts index 48a9c0308..2683db156 100644 --- a/app/queries/servers/channel.ts +++ b/app/queries/servers/channel.ts @@ -247,3 +247,7 @@ export const addChannelMembership = async (operator: ServerDataOperator, userId: export const queryAllMyChannelMembershipIds = async (database: Database, userId: string) => { return database.get(CHANNEL_MEMBERSHIP).query(Q.where('user_id', Q.eq(userId))).fetchIds(); }; + +export const queryChannelsByNames = (database: Database, names: string[]) => { + return database.get(CHANNEL).query(Q.where('name', Q.oneOf(names))); +}; diff --git a/app/queries/servers/preference.ts b/app/queries/servers/preference.ts index 3290f04dc..d4fd7acc0 100644 --- a/app/queries/servers/preference.ts +++ b/app/queries/servers/preference.ts @@ -12,6 +12,8 @@ import {queryCurrentTeamId} from './system'; import type ServerDataOperator from '@database/operator/server_data_operator'; import type PreferenceModel from '@typings/database/models/servers/preference'; +const {SERVER: {PREFERENCE}} = MM_TABLES; + export const prepareMyPreferences = (operator: ServerDataOperator, preferences: PreferenceType[], sync = false) => { try { return operator.handlePreferences({ @@ -65,3 +67,17 @@ export const deletePreferences = async (database: ServerDatabase, preferences: P return false; } }; + +export const queryPreferencesByCategory = (database: Database, category: string, name?: string, value?: string) => { + const clauses = [Q.where('category', category)]; + + if (typeof name === 'string') { + clauses.push(Q.where('name', name)); + } + + if (typeof value === 'string') { + clauses.push(Q.where('value', value)); + } + + return database.get(PREFERENCE).query(...clauses); +};