Skip to content

Commit

Permalink
feat: Add new permissions to teams (#5943)
Browse files Browse the repository at this point in the history
  • Loading branch information
diegolmello authored Oct 30, 2024
1 parent cb1868c commit b8f9873
Show file tree
Hide file tree
Showing 6 changed files with 158 additions and 59 deletions.
7 changes: 6 additions & 1 deletion app/lib/methods/getPermissions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,12 @@ export const SUPPORTED_PERMISSIONS = [
'mobile-upload-file',
'delete-own-message',
'call-management',
'test-push-notifications'
'test-push-notifications',
'move-room-to-team',
'create-team-channel',
'create-team-group',
'delete-team-channel',
'delete-team-group'
] as const;

export async function setPermissions(): Promise<void> {
Expand Down
2 changes: 2 additions & 0 deletions app/stacks/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,8 @@ export type ChatsStackParamList = {
};
AddChannelTeamView: {
teamId: string;
rid: string;
t: 'c' | 'p';
};
AddExistingChannelView: {
teamId: string;
Expand Down
90 changes: 64 additions & 26 deletions app/views/AddChannelTeamView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ import SafeAreaView from '../containers/SafeAreaView';
import I18n from '../i18n';
import { ChatsStackParamList, DrawerParamList, NewMessageStackParamList } from '../stacks/types';
import { IApplicationState } from '../definitions';
import { usePermissions } from '../lib/hooks';
import { compareServerVersion } from '../lib/methods/helpers';
import { TSupportedPermissions } from '../reducers/permissions';

type TRoute = RouteProp<ChatsStackParamList, 'AddChannelTeamView'>;

Expand All @@ -18,13 +21,40 @@ type TNavigation = CompositeNavigationProp<
CompositeNavigationProp<NativeStackNavigationProp<NewMessageStackParamList>, NativeStackNavigationProp<DrawerParamList>>
>;

const useCreateNewPermission = (rid: string, t: 'c' | 'p') => {
const permissions: TSupportedPermissions[] = t === 'c' ? ['create-c'] : ['create-p'];

const serverVersion = useSelector((state: IApplicationState) => state.server.version);
if (compareServerVersion(serverVersion, 'greaterThanOrEqualTo', '7.0.0')) {
permissions.push(t === 'c' ? 'create-team-channel' : 'create-team-group');
}

const result = usePermissions(permissions, rid);
return result.some(Boolean);
};

const useAddExistingPermission = (rid: string) => {
let permissions: TSupportedPermissions[] = ['add-team-channel'];

const serverVersion = useSelector((state: IApplicationState) => state.server.version);
if (compareServerVersion(serverVersion, 'greaterThanOrEqualTo', '7.0.0')) {
permissions = ['move-room-to-team'];
}

const result = usePermissions(permissions, rid);
return result[0];
};

const AddChannelTeamView = () => {
const navigation = useNavigation<TNavigation>();
const isMasterDetail = useSelector((state: IApplicationState) => state.app.isMasterDetail);
const {
params: { teamId }
params: { teamId, rid, t }
} = useRoute<TRoute>();

const canCreateNew = useCreateNewPermission(rid, t);
const canAddExisting = useAddExistingPermission(rid);

useLayoutEffect(() => {
navigation.setOptions({ title: I18n.t('Add_Channel_to_Team') });
}, [navigation]);
Expand All @@ -34,31 +64,39 @@ const AddChannelTeamView = () => {
<StatusBar />
<List.Container>
<List.Separator />
<List.Item
title='Create_New'
onPress={() =>
isMasterDetail
? navigation.navigate('SelectedUsersViewCreateChannel', {
nextAction: () => navigation.navigate('CreateChannelView', { teamId })
})
: navigation.navigate('SelectedUsersView', {
nextAction: () =>
navigation.navigate('ChatsStackNavigator', { screen: 'CreateChannelView', params: { teamId } })
})
}
testID='add-channel-team-view-create-channel'
left={() => <List.Icon name='team' />}
right={() => <List.Icon name='chevron-right' />}
/>
<List.Separator />
<List.Item
title='Add_Existing'
onPress={() => navigation.navigate('AddExistingChannelView', { teamId })}
testID='add-channel-team-view-add-existing'
left={() => <List.Icon name='channel-public' />}
right={() => <List.Icon name='chevron-right' />}
/>
<List.Separator />
{canCreateNew ? (
<>
<List.Item
title='Create_New'
onPress={() =>
isMasterDetail
? navigation.navigate('SelectedUsersViewCreateChannel', {
nextAction: () => navigation.navigate('CreateChannelView', { teamId })
})
: navigation.navigate('SelectedUsersView', {
nextAction: () =>
navigation.navigate('ChatsStackNavigator', { screen: 'CreateChannelView', params: { teamId } })
})
}
testID='add-channel-team-view-create-channel'
left={() => <List.Icon name='team' />}
right={() => <List.Icon name='chevron-right' />}
/>
<List.Separator />
</>
) : null}
{canAddExisting ? (
<>
<List.Item
title='Add_Existing'
onPress={() => navigation.navigate('AddExistingChannelView', { teamId })}
testID='add-channel-team-view-add-existing'
left={() => <List.Icon name='channel-public' />}
right={() => <List.Icon name='chevron-right' />}
/>
<List.Separator />
</>
) : null}
</List.Container>
</SafeAreaView>
);
Expand Down
24 changes: 16 additions & 8 deletions app/views/AddExistingChannelView/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import { animateNextTransition } from '../../lib/methods/helpers/layoutAnimation
import { showErrorAlert } from '../../lib/methods/helpers/info';
import { ChatsStackParamList } from '../../stacks/types';
import { TSubscriptionModel, SubscriptionType } from '../../definitions';
import { getRoomTitle, hasPermission, useDebounce } from '../../lib/methods/helpers';
import { compareServerVersion, getRoomTitle, hasPermission, useDebounce } from '../../lib/methods/helpers';
import { Services } from '../../lib/services';
import { useAppSelector } from '../../lib/hooks';

Expand All @@ -38,9 +38,11 @@ const AddExistingChannelView = () => {
params: { teamId }
} = useRoute<TRoute>();

const { addTeamChannelPermission, isMasterDetail } = useAppSelector(state => ({
const { serverVersion, addTeamChannelPermission, isMasterDetail, moveRoomToTeamPermission } = useAppSelector(state => ({
serverVersion: state.server.version,
isMasterDetail: state.app.isMasterDetail,
addTeamChannelPermission: state.permissions['add-team-channel']
addTeamChannelPermission: state.permissions['add-team-channel'],
moveRoomToTeamPermission: state.permissions['move-room-to-team']
}));

useLayoutEffect(() => {
Expand Down Expand Up @@ -70,6 +72,15 @@ const AddExistingChannelView = () => {
navigation.setOptions(options);
};

const hasCreatePermission = async (id: string) => {
if (compareServerVersion(serverVersion, 'greaterThanOrEqualTo', '7.0.0')) {
const result = await hasPermission([moveRoomToTeamPermission], id);
return result[0];
}
const result = await hasPermission([addTeamChannelPermission], id);
return result[0];
};

const query = async (stringToSearch = '') => {
try {
const db = database.active;
Expand All @@ -90,11 +101,8 @@ const AddExistingChannelView = () => {
if (channel.prid) {
return false;
}
const permissions = await hasPermission([addTeamChannelPermission], channel.rid);
if (!permissions[0]) {
return false;
}
return true;
const result = await hasCreatePermission(channel.rid);
return result;
})
);

Expand Down
28 changes: 13 additions & 15 deletions app/views/RoomActionsView/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ interface IRoomActionsViewProps extends IActionSheetProvider, IBaseScreen<StackT
viewBroadcastMemberListPermission?: string[];
createTeamPermission?: string[];
addTeamChannelPermission?: string[];
moveRoomToTeamPermission?: string[];
convertTeamPermission?: string[];
viewCannedResponsesPermission?: string[];
livechatAllowManualOnHold?: boolean;
Expand Down Expand Up @@ -222,7 +223,7 @@ class RoomActionsView extends React.Component<IRoomActionsViewProps, IRoomAction
const canToggleEncryption = await this.canToggleEncryption();
const canViewMembers = await this.canViewMembers();
const canCreateTeam = await this.canCreateTeam();
const canAddChannelToTeam = await this.canAddChannelToTeam();
const canAddChannelToTeam = await this.hasMoveToTeamPermission(room.rid);
const canConvertTeam = await this.canConvertTeam();
const hasE2EEWarning = EncryptionUtils.hasE2EEWarning({
encryptionEnabled,
Expand Down Expand Up @@ -313,14 +314,14 @@ class RoomActionsView extends React.Component<IRoomActionsViewProps, IRoomAction
return canCreateTeam;
};

canAddChannelToTeam = async () => {
const { room } = this.state;
const { addTeamChannelPermission } = this.props;
const { rid } = room;
const permissions = await hasPermission([addTeamChannelPermission], rid);

const canAddChannelToTeam = permissions[0];
return canAddChannelToTeam;
hasMoveToTeamPermission = async (rid: string) => {
const { addTeamChannelPermission, moveRoomToTeamPermission, serverVersion } = this.props;
if (compareServerVersion(serverVersion, 'greaterThanOrEqualTo', '7.0.0')) {
const result = await hasPermission([moveRoomToTeamPermission], rid);
return result[0];
}
const result = await hasPermission([addTeamChannelPermission], rid);
return result[0];
};

canConvertTeam = async () => {
Expand Down Expand Up @@ -693,7 +694,6 @@ class RoomActionsView extends React.Component<IRoomActionsViewProps, IRoomAction
searchTeam = async (onChangeText: string) => {
logEvent(events.RA_SEARCH_TEAM);
try {
const { addTeamChannelPermission, createTeamPermission } = this.props;
const QUERY_SIZE = 50;
const db = database.active;
const teams = await db
Expand All @@ -709,11 +709,8 @@ class RoomActionsView extends React.Component<IRoomActionsViewProps, IRoomAction
const asyncFilter = async (teamArray: TSubscriptionModel[]) => {
const results = await Promise.all(
teamArray.map(async team => {
const permissions = await hasPermission([addTeamChannelPermission, createTeamPermission], team.rid);
if (!permissions[0]) {
return false;
}
return true;
const result = await this.hasMoveToTeamPermission(team.rid);
return result;
})
);

Expand Down Expand Up @@ -1290,6 +1287,7 @@ const mapStateToProps = (state: IApplicationState) => ({
viewBroadcastMemberListPermission: state.permissions['view-broadcast-member-list'],
createTeamPermission: state.permissions['create-team'],
addTeamChannelPermission: state.permissions['add-team-channel'],
moveRoomToTeamPermission: state.permissions['move-room-to-team'],
convertTeamPermission: state.permissions['convert-team'],
viewCannedResponsesPermission: state.permissions['view-canned-responses'],
livechatAllowManualOnHold: state.settings.Livechat_allow_manual_on_hold as boolean,
Expand Down
66 changes: 57 additions & 9 deletions app/views/TeamChannelsView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,12 @@ import I18n from '../i18n';
import database from '../lib/database';
import { CustomIcon } from '../containers/CustomIcon';
import RoomItem, { ROW_HEIGHT } from '../containers/RoomItem';
import { getUserSelector } from '../selectors/login';
import { ChatsStackParamList } from '../stacks/types';
import { withTheme } from '../theme';
import { goRoom } from '../lib/methods/helpers/goRoom';
import { showErrorAlert } from '../lib/methods/helpers/info';
import log, { events, logEvent } from '../lib/methods/helpers/log';
import { getRoomAvatar, getRoomTitle, hasPermission, debounce, isIOS } from '../lib/methods/helpers';
import { getRoomAvatar, getRoomTitle, hasPermission, debounce, isIOS, compareServerVersion } from '../lib/methods/helpers';
import { Services } from '../lib/services';

const API_FETCH_COUNT = 25;
Expand Down Expand Up @@ -71,14 +70,22 @@ interface ITeamChannelsViewState {
}

interface ITeamChannelsViewProps extends IBaseScreen<ChatsStackParamList, 'TeamChannelsView'> {
serverVersion: string;
useRealName: boolean;
width: number;
StoreLastMessage: boolean;
addTeamChannelPermission: string[];
moveRoomToTeamPermission: string[];
editTeamChannelPermission: string[];
removeTeamChannelPermission: string[];
createCPermission: string[];
createTeamChannelPermission: string[];
createPPermission: string[];
createTeamGroupPermission: string[];
deleteCPermission: string[];
deletePPermission: string[];
deleteTeamChannelPermission: string[];
deleteTeamGroupPermission: string[];
showActionSheet: (options: TActionSheetOptions) => void;
showAvatar: boolean;
displayMode: DisplayMode;
Expand Down Expand Up @@ -113,8 +120,28 @@ class TeamChannelsView extends React.Component<ITeamChannelsViewProps, ITeamChan
this.load();
}

hasCreatePermission = async () => {
const {
addTeamChannelPermission,
moveRoomToTeamPermission,
serverVersion,
createCPermission,
createPPermission,
createTeamChannelPermission,
createTeamGroupPermission
} = this.props;
if (compareServerVersion(serverVersion, 'greaterThanOrEqualTo', '7.0.0')) {
const createPermissions =
this.team.t === 'c' ? [createCPermission, createTeamChannelPermission] : [createPPermission, createTeamGroupPermission];
const result = await hasPermission([moveRoomToTeamPermission, ...createPermissions], this.team.rid);
return result.some(Boolean);
}
const createPermissions = this.team.t === 'c' ? [createCPermission] : [createPPermission];
const result = await hasPermission([addTeamChannelPermission, ...createPermissions], this.team.rid);
return result.some(Boolean);
};

loadTeam = async () => {
const { addTeamChannelPermission } = this.props;
const { loading, data } = this.state;

const db = database.active;
Expand All @@ -128,8 +155,8 @@ class TeamChannelsView extends React.Component<ITeamChannelsViewProps, ITeamChan
throw new Error();
}

const permissions = await hasPermission([addTeamChannelPermission], this.team.rid);
if (permissions[0]) {
const hasCreatePermission = await this.hasCreatePermission();
if (hasCreatePermission) {
this.setState({ showCreate: true }, () => this.setHeader());
}

Expand Down Expand Up @@ -219,7 +246,9 @@ class TeamChannelsView extends React.Component<ITeamChannelsViewProps, ITeamChan
<HeaderButton.Item
iconName='create'
testID='team-channels-view-create'
onPress={() => navigation.navigate('AddChannelTeamView', { teamId: this.teamId })}
onPress={() =>
navigation.navigate('AddChannelTeamView', { teamId: this.teamId, rid: this.team.rid, t: this.team.t as any })
}
/>
) : null}
<HeaderButton.Item iconName='search' testID='team-channels-view-search' onPress={this.onSearchPress} />
Expand Down Expand Up @@ -405,6 +434,20 @@ class TeamChannelsView extends React.Component<ITeamChannelsViewProps, ITeamChan
);
};

hasDeletePermission = async (rid: string, t: 'c' | 'p') => {
const { serverVersion, deleteCPermission, deletePPermission, deleteTeamChannelPermission, deleteTeamGroupPermission } =
this.props;
if (compareServerVersion(serverVersion, 'greaterThanOrEqualTo', '7.0.0')) {
const permissions =
t === 'c' ? [deleteTeamChannelPermission, deleteTeamGroupPermission] : [deletePPermission, deletePPermission];
const result = await hasPermission(permissions, rid);
return result[0] && result[1];
}

const result = await hasPermission([t === 'c' ? deleteCPermission : deletePPermission], rid);
return result[0];
};

showChannelActions = async (item: IItem) => {
logEvent(events.ROOM_SHOW_BOX_ACTIONS);
const {
Expand Down Expand Up @@ -535,16 +578,21 @@ class TeamChannelsView extends React.Component<ITeamChannelsViewProps, ITeamChan
}

const mapStateToProps = (state: IApplicationState) => ({
baseUrl: state.server.server,
user: getUserSelector(state),
serverVersion: state.server.version,
useRealName: state.settings.UI_Use_Real_Name,
isMasterDetail: state.app.isMasterDetail,
StoreLastMessage: state.settings.Store_Last_Message,
addTeamChannelPermission: state.permissions['add-team-channel'],
moveRoomToTeamPermission: state.permissions['move-room-to-team'],
editTeamChannelPermission: state.permissions['edit-team-channel'],
removeTeamChannelPermission: state.permissions['remove-team-channel'],
deleteCPermission: state.permissions['delete-c'],
createCPermission: state.permissions['create-c'],
createTeamChannelPermission: state.permissions['create-team-channel'],
createPPermission: state.permissions['create-p'],
createTeamGroupPermission: state.permissions['create-team-group'],
deleteTeamChannelPermission: state.permissions['delete-team-channel'],
deletePPermission: state.permissions['delete-p'],
deleteTeamGroupPermission: state.permissions['delete-team-group'],
showAvatar: state.sortPreferences.showAvatar,
displayMode: state.sortPreferences.displayMode
});
Expand Down

0 comments on commit b8f9873

Please sign in to comment.