Skip to content
This repository has been archived by the owner on Sep 11, 2024. It is now read-only.

Commit

Permalink
Merge pull request #6685 from matrix-org/travis/fix-widget-remember
Browse files Browse the repository at this point in the history
Fix client forgetting which capabilities a widget was approved for
  • Loading branch information
turt2live authored Aug 26, 2021
2 parents a1a7de0 + 76cc5bf commit 22a78cf
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 16 deletions.
13 changes: 1 addition & 12 deletions src/components/views/dialogs/WidgetCapabilitiesPromptDialog.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,6 @@ import LabelledToggleSwitch from "../elements/LabelledToggleSwitch";
import { CapabilityText } from "../../../widgets/CapabilityText";
import { replaceableComponent } from "../../../utils/replaceableComponent";

export function getRememberedCapabilitiesForWidget(widget: Widget): Capability[] {
return JSON.parse(localStorage.getItem(`widget_${widget.id}_approved_caps`) || "[]");
}

function setRememberedCapabilitiesForWidget(widget: Widget, caps: Capability[]) {
localStorage.setItem(`widget_${widget.id}_approved_caps`, JSON.stringify(caps));
}

interface IProps extends IDialogProps {
requestedCapabilities: Set<Capability>;
widget: Widget;
Expand Down Expand Up @@ -95,10 +87,7 @@ export default class WidgetCapabilitiesPromptDialog extends React.PureComponent<
};

private closeAndTryRemember(approved: Capability[]) {
if (this.state.rememberSelection) {
setRememberedCapabilitiesForWidget(this.props.widget, approved);
}
this.props.onFinished({ approved });
this.props.onFinished({ approved, remember: this.state.rememberSelection });
}

public render() {
Expand Down
22 changes: 18 additions & 4 deletions src/stores/widgets/StopGapWidgetDriver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,7 @@ import { MatrixClientPeg } from "../../MatrixClientPeg";
import ActiveRoomObserver from "../../ActiveRoomObserver";
import Modal from "../../Modal";
import WidgetOpenIDPermissionsDialog from "../../components/views/dialogs/WidgetOpenIDPermissionsDialog";
import WidgetCapabilitiesPromptDialog, {
getRememberedCapabilitiesForWidget,
} from "../../components/views/dialogs/WidgetCapabilitiesPromptDialog";
import WidgetCapabilitiesPromptDialog from "../../components/views/dialogs/WidgetCapabilitiesPromptDialog";
import { WidgetPermissionCustomisations } from "../../customisations/WidgetPermissions";
import { OIDCState, WidgetPermissionStore } from "./WidgetPermissionStore";
import { WidgetType } from "../../widgets/WidgetType";
Expand All @@ -48,6 +46,14 @@ import { MatrixEvent } from "matrix-js-sdk/src/models/event";

// TODO: Purge this from the universe

function getRememberedCapabilitiesForWidget(widget: Widget): Capability[] {
return JSON.parse(localStorage.getItem(`widget_${widget.id}_approved_caps`) || "[]");
}

function setRememberedCapabilitiesForWidget(widget: Widget, caps: Capability[]) {
localStorage.setItem(`widget_${widget.id}_approved_caps`, JSON.stringify(caps));
}

export class StopGapWidgetDriver extends WidgetDriver {
private allowedCapabilities: Set<Capability>;

Expand Down Expand Up @@ -100,6 +106,7 @@ export class StopGapWidgetDriver extends WidgetDriver {
}
}
// TODO: Do something when the widget requests new capabilities not yet asked for
let rememberApproved = false;
if (missing.size > 0) {
try {
const [result] = await Modal.createTrackedDialog(
Expand All @@ -111,12 +118,19 @@ export class StopGapWidgetDriver extends WidgetDriver {
widgetKind: this.forWidgetKind,
}).finished;
(result.approved || []).forEach(cap => allowedSoFar.add(cap));
rememberApproved = result.remember;
} catch (e) {
console.error("Non-fatal error getting capabilities: ", e);
}
}

return new Set(iterableUnion(allowedSoFar, requested));
const allAllowed = new Set(iterableUnion(allowedSoFar, requested));

if (rememberApproved) {
setRememberedCapabilitiesForWidget(this.forWidget, Array.from(allAllowed));
}

return allAllowed;
}

public async sendEvent(eventType: string, content: any, stateKey: string = null): Promise<ISendEventDetails> {
Expand Down

0 comments on commit 22a78cf

Please sign in to comment.