mattermost-mobile/app/screens/browse_channels/index.ts
Daniel Espino García 1aa4188f8e
Move config to its own database table (#6744)
* Move config to its own database table

* Address feedback

* Fix test

* Revert minimum version related changes
2022-11-11 21:20:42 +04:00

46 lines
1.8 KiB
TypeScript

// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.
import {withDatabase} from '@nozbe/watermelondb/DatabaseProvider';
import withObservables from '@nozbe/with-observables';
import {of as of$} from 'rxjs';
import {switchMap} from 'rxjs/operators';
import {Permissions} from '@constants';
import {queryAllMyChannel} from '@queries/servers/channel';
import {queryRolesByNames} from '@queries/servers/role';
import {observeConfigBooleanValue, observeCurrentTeamId, observeCurrentUserId} from '@queries/servers/system';
import {observeUser} from '@queries/servers/user';
import {hasPermission} from '@utils/role';
import SearchHandler from './search_handler';
import type {WithDatabaseArgs} from '@typings/database/database';
const enhanced = withObservables([], ({database}: WithDatabaseArgs) => {
const sharedChannelsEnabled = observeConfigBooleanValue(database, 'ExperimentalSharedChannels');
const canShowArchivedChannels = observeConfigBooleanValue(database, 'ExperimentalViewArchivedChannels');
const currentTeamId = observeCurrentTeamId(database);
const currentUserId = observeCurrentUserId(database);
const joinedChannels = queryAllMyChannel(database).observe();
const roles = currentUserId.pipe(
switchMap((id) => observeUser(database, id)),
switchMap((u) => (u ? of$(u.roles.split(' ')) : of$([]))),
switchMap((values) => queryRolesByNames(database, values).observeWithColumns(['permissions'])),
);
const canCreateChannels = roles.pipe(switchMap((r) => of$(hasPermission(r, Permissions.CREATE_PUBLIC_CHANNEL))));
return {
canCreateChannels,
currentTeamId,
joinedChannels,
sharedChannelsEnabled,
canShowArchivedChannels,
};
});
export default withDatabase(enhanced(SearchHandler));