Skip to content

Commit

Permalink
Handes visibility preference setting for dm/gm (#6078)
Browse files Browse the repository at this point in the history
* Handes visibility preference setting for dm/gm

* Feedback

Co-authored-by: Elias Nahum <nahumhbl@gmail.com>
  • Loading branch information
shaz-r and enahum authored Mar 23, 2022
1 parent 7c642b1 commit 8093047
Show file tree
Hide file tree
Showing 8 changed files with 64 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ describe('components/channel_list/categories/body', () => {
category={category}
locale={DEFAULT_LOCALE}
currentChannelId={''}
currentUserId={''}
/>,
{database},
);
Expand Down
16 changes: 12 additions & 4 deletions app/components/channel_list/categories/body/category_body.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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(() => {
let filteredIds = sortedIds;

// Remove all closed gm/dms
if (hiddenChannelIds.length) {
filteredIds = sortedIds.filter((id) => !hiddenChannelIds.includes(id));
}

if (category.type === 'direct_messages' && limit > 0) {
return sortedIds.slice(0, limit - 1);
return filteredIds.slice(0, limit - 1);
}
return sortedIds;
}, [category.type, limit]);
return filteredIds;
}, [category.type, limit, hiddenChannelIds]);

const ChannelItem = useCallback(({item}: {item: string}) => {
return (
Expand Down
40 changes: 35 additions & 5 deletions app/components/channel_list/categories/body/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,17 @@ import {combineLatest, of as of$} from 'rxjs';
import {switchMap} from 'rxjs/operators';

import {General, Preferences} from '@constants';
import {queryMyChannelSettingsByIds} from '@queries/servers/channel';
import {queryChannelsByNames, queryMyChannelSettingsByIds} from '@queries/servers/channel';
import {queryPreferencesByCategoryAndName} from '@queries/servers/preference';
import {WithDatabaseArgs} from '@typings/database/database';
import {getDirectChannelName} from '@utils/channel';

import CategoryBody from './category_body';

import type CategoryModel from '@typings/database/models/servers/category';
import type ChannelModel from '@typings/database/models/servers/channel';
import type MyChannelSettingsModel from '@typings/database/models/servers/my_channel_settings';
import type PreferenceModel from '@typings/database/models/servers/preference';

type ChannelData = Pick<ChannelModel, 'id' | 'displayName'> & {
isMuted: boolean;
Expand Down Expand Up @@ -77,23 +79,51 @@ 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));

type EnhanceProps = {category: CategoryModel; locale: string; currentUserId: string} & WithDatabaseArgs

const enhance = withObservables(['category'], ({category, locale, database, currentUserId}: EnhanceProps) => {
const observedCategory = category.observe();
const sortedIds = observedCategory.pipe(
switchMap((c) => getSortedIds(database, c, locale)),
);

let limit = of$(0);
const dmMap = (p: PreferenceModel) => getDirectChannelName(p.name, currentUserId);

const hiddenDmIds = queryPreferencesByCategoryAndName(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 = queryPreferencesByCategoryAndName(database, Preferences.CATEGORY_GROUP_CHANNEL_SHOW, undefined, 'false').
observe().pipe(switchMap(mapPrefName));

let limit = of$(Preferences.CHANNEL_SIDEBAR_LIMIT_DMS_DEFAULT);
if (category.type === 'direct_messages') {
limit = queryPreferencesByCategoryAndName(database, Preferences.CATEGORY_SIDEBAR_SETTINGS, 'limit_visible_dms_gms').observe().pipe(
limit = queryPreferencesByCategoryAndName(database, Preferences.CATEGORY_SIDEBAR_SETTINGS, Preferences.CHANNEL_SIDEBAR_LIMIT_DMS).observe().pipe(
switchMap((val) => {
return val[0] ? of$(parseInt(val[0].value, 10)) : of$(0);
return val[0] ? of$(parseInt(val[0].value, 10)) : of$(Preferences.CHANNEL_SIDEBAR_LIMIT_DMS_DEFAULT);
}),
);
}

const hiddenChannelIds = combineLatest([hiddenDmIds, hiddenGmIds]).pipe(switchMap(
([a, b]) => of$(a.concat(b)),
));

return {
limit,
hiddenChannelIds,
sortedIds,
category: observedCategory,
};
Expand Down
5 changes: 4 additions & 1 deletion app/components/channel_list/categories/categories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import type {CategoryModel} from '@database/models/server';
type Props = {
categories: CategoryModel[];
currentChannelId: string;
currentUserId: string;
}

const styles = StyleSheet.create({
Expand All @@ -24,15 +25,17 @@ 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 (
<>
<CategoryHeader category={data.item}/>
<CategoryBody
category={data.item}
currentChannelId={currentChannelId}
currentUserId={currentUserId}
locale={intl.locale}
/>
</>
Expand Down
4 changes: 3 additions & 1 deletion app/components/channel_list/categories/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {withDatabase} from '@nozbe/watermelondb/DatabaseProvider';
import withObservables from '@nozbe/with-observables';

import {queryCategoriesByTeamIds} from '@queries/servers/categories';
import {observeCurrentChannelId} from '@queries/servers/system';
import {observeCurrentChannelId, observeCurrentUserId} from '@queries/servers/system';

import Categories from './categories';

Expand All @@ -17,11 +17,13 @@ const enhanced = withObservables(
['currentTeamId'],
({currentTeamId, database}: WithDatabaseProps) => {
const currentChannelId = observeCurrentChannelId(database);
const currentUserId = observeCurrentUserId(database);
const categories = queryCategoriesByTeamIds(database, [currentTeamId]).observeWithColumns(['sort_order']);

return {
currentChannelId,
categories,
currentUserId,
};
});

Expand Down
3 changes: 2 additions & 1 deletion app/constants/preferences.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ const Preferences: Record<string, any> = {
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',
CHANNEL_SIDEBAR_LIMIT_DMS_DEFAULT: 20,
AUTOCLOSE_DMS_ENABLED: 'after_seven_days',
CATEGORY_ADVANCED_SETTINGS: 'advanced_settings',
ADVANCED_FILTER_JOIN_LEAVE: 'join_leave',
Expand Down
4 changes: 4 additions & 0 deletions app/queries/servers/channel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -303,3 +303,7 @@ export const queryMyChannelSettingsByIds = (database: Database, ids: string[]) =
Q.where('id', Q.oneOf(ids)),
);
};

export const queryChannelsByNames = (database: Database, names: string[]) => {
return database.get<ChannelModel>(CHANNEL).query(Q.where('name', Q.oneOf(names)));
};
4 changes: 3 additions & 1 deletion app/queries/servers/preference.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import {getCurrentTeamId} 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({
Expand All @@ -32,7 +34,7 @@ export const queryPreferencesByCategoryAndName = (database: Database, category:
if (value != null) {
clauses.push(Q.where('value', value));
}
return database.get<PreferenceModel>(MM_TABLES.SERVER.PREFERENCE).query(...clauses);
return database.get<PreferenceModel>(PREFERENCE).query(...clauses);
};

export const getThemeForCurrentTeam = async (database: Database) => {
Expand Down

0 comments on commit 8093047

Please sign in to comment.