Skip to content

Commit

Permalink
Handes visibility preference setting for dm/gm
Browse files Browse the repository at this point in the history
  • Loading branch information
shaz-r committed Mar 21, 2022
1 parent 4cedbdf commit 368f6aa
Show file tree
Hide file tree
Showing 8 changed files with 77 additions and 14 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: 33 additions & 7 deletions app/components/channel_list/categories/body/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand All @@ -22,7 +25,7 @@ type ChannelData = Pick<ChannelModel, 'id' | 'displayName'> & {
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) {
Expand Down Expand Up @@ -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<PreferenceModel>(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]) {
Expand All @@ -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,
};
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
6 changes: 5 additions & 1 deletion app/components/channel_list/categories/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -26,13 +26,17 @@ const enhanced = withObservables(
const currentChannelId = database.get<SystemModel>(SYSTEM).findAndObserve(CURRENT_CHANNEL_ID).pipe(
switchMap(({value}) => of$(value)),
);
const currentUserId = database.get<SystemModel>(SYSTEM).findAndObserve(CURRENT_USER_ID).pipe(
switchMap(({value}) => of$(value)),
);
const categories = database.get<CategoryModel>(CATEGORY).query(
Q.where('team_id', 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 @@ -11,6 +11,7 @@ const Preferences: Record<string, any> = {
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',
Expand All @@ -35,7 +36,7 @@ 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',
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 @@ -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<ChannelModel>(CHANNEL).query(Q.where('name', Q.oneOf(names)));
};
16 changes: 16 additions & 0 deletions app/queries/servers/preference.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand Down Expand Up @@ -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<PreferenceModel>(PREFERENCE).query(...clauses);
};

0 comments on commit 368f6aa

Please sign in to comment.