Merge pull request #7584 from mattermost/MM-53857

This commit is contained in:
Caleb Roseland 2023-10-06 17:48:49 -05:00 committed by GitHub
commit feb521c754
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
9 changed files with 60 additions and 15 deletions

View file

@ -249,13 +249,14 @@ export async function createChannel(serverUrl: string, displayName: string, purp
}
}
export async function patchChannel(serverUrl: string, channelPatch: Partial<Channel> & {id: string}) {
export async function patchChannel(serverUrl: string, channelId: string, channelPatch: ChannelPatch) {
try {
const client = NetworkManager.getClient(serverUrl);
const {database, operator} = DatabaseManager.getServerDatabaseAndOperator(serverUrl);
const channelData = await client.patchChannel(channelPatch.id, channelPatch);
const channelData = await client.patchChannel(channelId, channelPatch);
const models = [];
const channelInfo = (await getChannelInfo(database, channelData.id));
if (channelInfo && (channelInfo.purpose !== channelData.purpose || channelInfo.header !== channelData.header)) {
channelInfo.prepareUpdate((v) => {
@ -264,10 +265,14 @@ export async function patchChannel(serverUrl: string, channelPatch: Partial<Chan
});
models.push(channelInfo);
}
const channel = await getChannelById(database, channelData.id);
if (channel && (channel.displayName !== channelData.display_name || channel.type !== channelData.type)) {
channel.prepareUpdate((v) => {
v.displayName = channelData.display_name;
// DM and GM display names cannot be patched and are formatted client-side; do not overwrite
if (channelData.type !== General.DM_CHANNEL && channelData.type !== General.GM_CHANNEL) {
v.displayName = channelData.display_name;
}
v.type = channelData.type;
});
models.push(channel);

View file

@ -128,7 +128,7 @@ const ClientChannels = <TBase extends Constructor<ClientBase>>(superclass: TBase
);
};
patchChannel = async (channelId: string, channelPatch: Partial<Channel>) => {
patchChannel = async (channelId: string, channelPatch: ChannelPatch) => {
this.analytics?.trackAPI('api_channels_patch', {channel_id: channelId});
return this.doFetch(

View file

@ -101,3 +101,17 @@ export function observeCanManageChannelMembers(database: Database, channelId: st
distinctUntilChanged(),
);
}
export function observeCanManageChannelSettings(database: Database, channelId: string, user: UserModel) {
return observeChannel(database, channelId).pipe(
switchMap((c) => {
if (!c || c.deleteAt !== 0 || isDMorGM(c)) {
return of$(false);
}
const permission = c.type === General.OPEN_CHANNEL ? Permissions.MANAGE_PUBLIC_CHANNEL_PROPERTIES : Permissions.MANAGE_PRIVATE_CHANNEL_PROPERTIES;
return observePermissionForChannel(database, c, user, permission, true);
}),
distinctUntilChanged(),
);
}

View file

@ -74,14 +74,20 @@ const PublicOrPrivateChannel = ({channel, creator, roles, theme}: Props) => {
}, []);
const canManagePeople = useMemo(() => {
if (channel.deleteAt !== 0) {
return false;
}
const permission = channel.type === General.OPEN_CHANNEL ? Permissions.MANAGE_PUBLIC_CHANNEL_MEMBERS : Permissions.MANAGE_PRIVATE_CHANNEL_MEMBERS;
return hasPermission(roles, permission);
}, [channel.type, roles]);
}, [channel.type, roles, channel.deleteAt]);
const canSetHeader = useMemo(() => {
if (channel.deleteAt !== 0) {
return false;
}
const permission = channel.type === General.OPEN_CHANNEL ? Permissions.MANAGE_PUBLIC_CHANNEL_PROPERTIES : Permissions.MANAGE_PRIVATE_CHANNEL_PROPERTIES;
return hasPermission(roles, permission);
}, [channel.type, roles]);
}, [channel.type, roles, channel.deleteAt]);
const createdBy = useMemo(() => {
const id = channel.type === General.OPEN_CHANNEL ? t('intro.public_channel') : t('intro.private_channel');

View file

@ -30,6 +30,7 @@ type Props = {
canEnableDisableCalls: boolean;
isCallsEnabledInChannel: boolean;
canManageMembers: boolean;
canManageSettings: boolean;
}
const edges: Edge[] = ['bottom', 'left', 'right'];
@ -57,6 +58,7 @@ const ChannelInfo = ({
canEnableDisableCalls,
isCallsEnabledInChannel,
canManageMembers,
canManageSettings,
}: Props) => {
const theme = useTheme();
const serverUrl = useServerUrl();
@ -103,6 +105,7 @@ const ChannelInfo = ({
type={type}
callsEnabled={callsAvailable}
canManageMembers={canManageMembers}
canManageSettings={canManageSettings}
/>
<View style={styles.separator}/>
{canEnableDisableCalls &&

View file

@ -10,7 +10,7 @@ import {observeIsCallsEnabledInChannel} from '@calls/observers';
import {observeCallsConfig} from '@calls/state';
import {withServerUrl} from '@context/server';
import {observeCurrentChannel} from '@queries/servers/channel';
import {observeCanManageChannelMembers} from '@queries/servers/role';
import {observeCanManageChannelMembers, observeCanManageChannelSettings} from '@queries/servers/role';
import {
observeConfigValue,
observeCurrentChannelId,
@ -105,11 +105,19 @@ const enhanced = withObservables([], ({serverUrl, database}: Props) => {
switchMap(([u, cId]) => (u ? observeCanManageChannelMembers(database, cId, u) : of$(false))),
distinctUntilChanged(),
);
const canManageSettings = currentUser.pipe(
combineLatestWith(channelId),
switchMap(([u, cId]) => (u ? observeCanManageChannelSettings(database, cId, u) : of$(false))),
distinctUntilChanged(),
);
return {
type,
canEnableDisableCalls,
isCallsEnabledInChannel,
canManageMembers,
canManageSettings,
};
});

View file

@ -20,6 +20,7 @@ type Props = {
type?: ChannelType;
callsEnabled: boolean;
canManageMembers: boolean;
canManageSettings: boolean;
}
const Options = ({
@ -27,6 +28,7 @@ const Options = ({
type,
callsEnabled,
canManageMembers,
canManageSettings,
}: Props) => {
const isDMorGM = isTypeDMorGM(type);
@ -50,7 +52,7 @@ const Options = ({
testID='channel_info.options.copy_channel_link.option'
/>
}
{type !== General.DM_CHANNEL && type !== General.GM_CHANNEL &&
{canManageSettings &&
<EditChannel channelId={channelId}/>
}
</>

View file

@ -202,16 +202,16 @@ const CreateOrEditChannel = ({
return;
}
const patchChannel = {
id: channel.id,
type: channel.type,
display_name: isDirect(channel) ? channel.displayName : displayName,
purpose,
const patchChannel: ChannelPatch = {
header,
} as Channel;
...!isDirect(channel) && {
display_name: displayName,
purpose,
},
};
setCanSave(false);
const patchedChannel = await handlePatchChannel(serverUrl, patchChannel);
const patchedChannel = await handlePatchChannel(serverUrl, channel.id, patchChannel);
if (patchedChannel.error) {
dispatch({
type: RequestActions.FAILURE,

View file

@ -44,6 +44,13 @@ type Channel = {
group_constrained: boolean|null;
shared: boolean;
};
type ChannelPatch = {
name?: string;
display_name?: string;
header?: string;
purpose?: string;
group_constrained?: boolean|null;
};
type ChannelWithTeamData = Channel & {
team_display_name: string;
team_name: string;