-
Notifications
You must be signed in to change notification settings - Fork 10.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Show buttons in contexts * Change tabbar filter * Improve room type filter * Remove testing code and refactor IRoom auxiliary functions * Add startup loadButtons * Trigger new UI button interactions (#23798) * Show buttons in contexts * Change tabbar filter * Handle action button interactions in endpoint * Trigger new interaction type on UI buttons
- Loading branch information
Showing
8 changed files
with
193 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
49 changes: 49 additions & 0 deletions
49
app/ui-message/client/actionButtons/lib/applyButtonFilters.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
/* Style disabled as having some arrow functions in one-line hurts readability */ | ||
/* eslint-disable arrow-body-style */ | ||
|
||
import { Meteor } from 'meteor/meteor'; | ||
import { IUIActionButton, TemporaryRoomTypeFilter } from '@rocket.chat/apps-engine/definition/ui'; | ||
|
||
import { hasAtLeastOnePermission, hasPermission, hasRole } from '../../../../authorization/client'; | ||
import { | ||
IRoom, | ||
isDirectMessageRoom, | ||
isMultipleDirectMessageRoom, | ||
isOmnichannelRoom, | ||
isPrivateDiscussion, | ||
isPrivateTeamRoom, | ||
isPublicDiscussion, | ||
isPublicTeamRoom, | ||
} from '../../../../../definition/IRoom'; | ||
|
||
export const applyAuthFilter = (button: IUIActionButton, room?: IRoom): boolean => { | ||
const { hasAllPermissions, hasOnePermission, hasAllRoles, hasOneRole } = button.when || {}; | ||
|
||
const hasAllPermissionsResult = hasAllPermissions ? hasPermission(hasAllPermissions) : true; | ||
const hasOnePermissionResult = hasOnePermission ? hasAtLeastOnePermission(hasOnePermission) : true; | ||
const hasAllRolesResult = hasAllRoles ? hasAllRoles.every((role) => hasRole(Meteor.userId(), role, room?._id)) : true; | ||
const hasOneRoleResult = hasOneRole ? hasRole(Meteor.userId(), hasOneRole, room?._id) : true; | ||
|
||
return hasAllPermissionsResult && hasOnePermissionResult && hasAllRolesResult && hasOneRoleResult; | ||
}; | ||
|
||
const enumToFilter: {[k in TemporaryRoomTypeFilter]: (room: IRoom) => boolean} = { | ||
[TemporaryRoomTypeFilter.PUBLIC_CHANNEL]: (room) => room.t === 'c', | ||
[TemporaryRoomTypeFilter.PRIVATE_CHANNEL]: (room) => room.t === 'p', | ||
[TemporaryRoomTypeFilter.PUBLIC_TEAM]: isPublicTeamRoom, | ||
[TemporaryRoomTypeFilter.PRIVATE_TEAM]: isPrivateTeamRoom, | ||
[TemporaryRoomTypeFilter.PUBLIC_DISCUSSION]: isPublicDiscussion, | ||
[TemporaryRoomTypeFilter.PRIVATE_DISCUSSION]: isPrivateDiscussion, | ||
[TemporaryRoomTypeFilter.DIRECT]: isDirectMessageRoom, | ||
[TemporaryRoomTypeFilter.DIRECT_MULTIPLE]: isMultipleDirectMessageRoom, | ||
[TemporaryRoomTypeFilter.LIVE_CHAT]: isOmnichannelRoom, | ||
}; | ||
|
||
export const applyRoomFilter = (button: IUIActionButton, room: IRoom): boolean => { | ||
const { roomTypes } = button.when || {}; | ||
return !roomTypes || roomTypes.some((filter): boolean => enumToFilter[filter]?.(room)); | ||
}; | ||
|
||
export const applyButtonFilters = (button: IUIActionButton, room?: IRoom): boolean => { | ||
return applyAuthFilter(button, room) && (!room || applyRoomFilter(button, room)); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import { IUIActionButton } from '@rocket.chat/apps-engine/definition/ui'; | ||
|
||
import { MessageAction, messageArgs } from '../../../ui-utils/client'; | ||
import { triggerActionButtonAction } from '../ActionManager'; | ||
import { applyButtonFilters } from './lib/applyButtonFilters'; | ||
|
||
const getIdForActionButton = ({ appId, actionId }: IUIActionButton): string => `${ appId }/${ actionId }`; | ||
|
||
// eslint-disable-next-line no-void | ||
export const onAdded = (button: IUIActionButton): void => void MessageAction.addButton({ | ||
id: getIdForActionButton(button), | ||
icon: button.icon || '', | ||
label: button.nameI18n, | ||
context: button.when?.messageActionContext || ['message', 'message-mobile', 'threads', 'starred'], | ||
condition({ room }: any) { | ||
return applyButtonFilters(button, room); | ||
}, | ||
async action() { | ||
const { msg } = messageArgs(this); | ||
triggerActionButtonAction({ | ||
rid: msg.rid, | ||
mid: msg._id, | ||
actionId: button.actionId, | ||
appId: button.appId, | ||
payload: { context: button.context }, | ||
}); | ||
}, | ||
}); | ||
|
||
export const onRemoved = (button: IUIActionButton): void => MessageAction.removeButton(getIdForActionButton(button)); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import { Session } from 'meteor/session'; | ||
import { IUIActionButton } from '@rocket.chat/apps-engine/definition/ui'; | ||
|
||
import { Rooms } from '../../../models/client'; | ||
import { messageBox } from '../../../ui-utils/client'; | ||
import { applyButtonFilters } from './lib/applyButtonFilters'; | ||
import { triggerActionButtonAction } from '../ActionManager'; | ||
|
||
const getIdForActionButton = ({ appId, actionId }: IUIActionButton): string => `${ appId }/${ actionId }`; | ||
|
||
const APP_GROUP = 'Create_new'; | ||
|
||
// eslint-disable-next-line no-void | ||
export const onAdded = (button: IUIActionButton): void => void messageBox.actions.add(APP_GROUP, button.nameI18n, { | ||
id: getIdForActionButton(button), | ||
icon: button.icon || '', | ||
condition() { | ||
return applyButtonFilters(button, Rooms.findOne(Session.get('openedRoom'))); | ||
}, | ||
action() { | ||
triggerActionButtonAction({ | ||
rid: Session.get('openedRoom'), | ||
actionId: button.actionId, | ||
appId: button.appId, | ||
payload: { context: button.context }, | ||
}); | ||
}, | ||
}); | ||
|
||
// eslint-disable-next-line no-void | ||
export const onRemoved = (button: IUIActionButton): void => void messageBox.actions.remove(APP_GROUP, new RegExp(getIdForActionButton(button))); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,25 @@ | ||
import { IUIActionButton } from '@rocket.chat/apps-engine/definition/ui'; | ||
|
||
import { addAction, deleteAction } from '../../../../client/views/room/lib/Toolbox'; | ||
import { triggerActionButtonAction } from '../ActionManager'; | ||
import { applyButtonFilters } from './lib/applyButtonFilters'; | ||
|
||
const getIdForActionButton = ({ appId, actionId }: IUIActionButton): string => `${ appId }/${ actionId }`; | ||
|
||
// eslint-disable-next-line no-void | ||
export const onAdded = (button: IUIActionButton): void => void addAction(getIdForActionButton(button), { | ||
export const onAdded = (button: IUIActionButton): void => void addAction(getIdForActionButton(button), ({ room }) => (applyButtonFilters(button, room) ? { | ||
id: button.actionId, | ||
icon: '', | ||
icon: button.icon || '', | ||
title: button.nameI18n as any, | ||
// Introduce a mapper from Apps-engine's RoomTypes to these | ||
// Determine what 'group' and 'team' are | ||
// Filters were applied in the applyButtonFilters function | ||
// if the code made it this far, the button should be shown | ||
groups: ['group', 'channel', 'live', 'team', 'direct', 'direct_multiple'], | ||
}); | ||
action: (): Promise<any> => triggerActionButtonAction({ | ||
rid: room._id, | ||
actionId: button.actionId, | ||
appId: button.appId, | ||
payload: { context: button.context }, | ||
}), | ||
} : null)); | ||
|
||
export const onRemoved = (button: IUIActionButton): boolean => deleteAction(getIdForActionButton(button)); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters