Add a custom widget API action for viewing a different room

This commit is contained in:
Travis Ralston 2020-11-02 15:17:05 -07:00
parent 294876f062
commit e15041bd53
5 changed files with 89 additions and 7 deletions

View file

@ -14,17 +14,40 @@
* limitations under the License.
*/
import { Capability, WidgetDriver } from "matrix-widget-api";
import { Capability, WidgetDriver, WidgetType } from "matrix-widget-api";
import { iterableUnion } from "../../utils/iterables";
import { MatrixClientPeg } from "../../MatrixClientPeg";
import { arrayFastClone } from "../../utils/arrays";
import { ElementWidgetCapabilities } from "./ElementWidgetCapabilities";
// TODO: Purge this from the universe
export class StopGapWidgetDriver extends WidgetDriver {
constructor(private allowedCapabilities: Capability[]) {
constructor(private allowedCapabilities: Capability[], private forType: WidgetType) {
super();
}
public async validateCapabilities(requested: Set<Capability>): Promise<Set<Capability>> {
return new Set(iterableUnion(requested, this.allowedCapabilities));
// TODO: All of this should be a capabilities prompt.
// See https://github.com/vector-im/element-web/issues/13111
// Note: None of this well-known widget permissions stuff is documented intentionally. We
// do not want to encourage people relying on this, but need to be able to support it at
// the moment.
//
// If you're a widget developer and seeing this message, please ask the Element team if
// it is safe for you to use this permissions system before trying to use it - it might
// not be here in the future.
const wkPerms = (MatrixClientPeg.get().getClientWellKnown() || {})['io.element.widget_permissions'];
const allowedCaps = arrayFastClone(this.allowedCapabilities);
if (wkPerms) {
if (Array.isArray(wkPerms["view_room_action"])) {
if (wkPerms["view_room_action"].includes(this.forType)) {
allowedCaps.push(ElementWidgetCapabilities.CanChangeViewedRoom);
}
}
}
return new Set(iterableUnion(requested, allowedCaps));
}
}