-
-
Notifications
You must be signed in to change notification settings - Fork 20
Add an implementation of MSC2762: send/receive events #9
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 |
|---|---|---|
|
|
@@ -24,7 +24,7 @@ import { IContentLoadedActionRequest } from "./interfaces/ContentLoadedAction"; | |
| import { WidgetApiFromWidgetAction, WidgetApiToWidgetAction } from "./interfaces/WidgetApiAction"; | ||
| import { IWidgetApiErrorResponseData } from "./interfaces/IWidgetApiErrorResponse"; | ||
| import { Capability } from "./interfaces/Capabilities"; | ||
| import { WidgetDriver } from "./driver/WidgetDriver"; | ||
| import { ISendEventDetails, WidgetDriver } from "./driver/WidgetDriver"; | ||
| import { ICapabilitiesActionResponseData } from "./interfaces/CapabilitiesAction"; | ||
| import { | ||
| ISupportedVersionsActionRequest, | ||
|
|
@@ -40,6 +40,13 @@ import { | |
| IModalWidgetOpenRequestDataButton, | ||
| IModalWidgetReturnData, | ||
| } from "./interfaces/ModalWidgetActions"; | ||
| import { | ||
| ISendEventFromWidgetActionRequest, | ||
| ISendEventFromWidgetResponseData, | ||
| ISendEventToWidgetRequestData, | ||
| } from "./interfaces/SendEventAction"; | ||
| import { EventDirection, WidgetEventCapability } from "./models/WidgetEventCapability"; | ||
| import { IRoomEvent } from "./interfaces/IRoomEvent"; | ||
|
|
||
| /** | ||
| * API handler for the client side of widgets. This raises events | ||
|
|
@@ -70,6 +77,7 @@ export class ClientWidgetApi extends EventEmitter { | |
|
|
||
| private capabilitiesFinished = false; | ||
| private allowedCapabilities = new Set<Capability>(); | ||
| private allowedEvents: WidgetEventCapability[] = []; | ||
| private isStopped = false; | ||
|
|
||
| /** | ||
|
|
@@ -115,6 +123,26 @@ export class ClientWidgetApi extends EventEmitter { | |
| return this.allowedCapabilities.has(capability); | ||
| } | ||
|
|
||
| public canSendRoomEvent(eventType: string, msgtype: string = null): boolean { | ||
| return this.allowedEvents.some(e => | ||
| e.matchesAsRoomEvent(eventType, msgtype) && e.direction === EventDirection.Send); | ||
| } | ||
|
|
||
| public canSendStateEvent(eventType: string, stateKey: string): boolean { | ||
| return this.allowedEvents.some(e => | ||
| e.matchesAsStateEvent(eventType, stateKey) && e.direction === EventDirection.Send); | ||
| } | ||
|
|
||
| public canReceiveRoomEvent(eventType: string, msgtype: string = null): boolean { | ||
| return this.allowedEvents.some(e => | ||
| e.matchesAsRoomEvent(eventType, msgtype) && e.direction === EventDirection.Receive); | ||
| } | ||
|
|
||
| public canReceiveStateEvent(eventType: string, stateKey: string): boolean { | ||
| return this.allowedEvents.some(e => | ||
| e.matchesAsStateEvent(eventType, stateKey) && e.direction === EventDirection.Receive); | ||
| } | ||
|
|
||
| public stop() { | ||
| this.isStopped = true; | ||
| this.transport.stop(); | ||
|
|
@@ -140,7 +168,9 @@ export class ClientWidgetApi extends EventEmitter { | |
| ).then(caps => { | ||
| return this.driver.validateCapabilities(new Set(caps.capabilities)); | ||
| }).then(allowedCaps => { | ||
| console.log(`Widget ${this.widget.id} is allowed capabilities:`, Array.from(allowedCaps)); | ||
| this.allowedCapabilities = allowedCaps; | ||
| this.allowedEvents = Array.from(new Set(WidgetEventCapability.findEventCapabilities(allowedCaps))); | ||
| this.capabilitiesFinished = true; | ||
| this.emit("ready"); | ||
| }); | ||
|
|
@@ -165,6 +195,63 @@ export class ClientWidgetApi extends EventEmitter { | |
| }); | ||
| } | ||
|
|
||
| private async handleSendEvent(request: ISendEventFromWidgetActionRequest) { | ||
| if (!request.data.type) { | ||
| return this.transport.reply<IWidgetApiErrorResponseData>(request, { | ||
| error: {message: "Invalid request - missing event type"}, | ||
| }); | ||
| } | ||
|
|
||
| const isState = request.data.state_key !== null && request.data.state_key !== undefined; | ||
| let sentEvent: ISendEventDetails; | ||
| if (isState) { | ||
| if (!this.canSendStateEvent(request.data.type, request.data.state_key)) { | ||
| return this.transport.reply<IWidgetApiErrorResponseData>(request, { | ||
| error: {message: "Cannot send state events of this type"}, | ||
| }); | ||
| } | ||
|
|
||
| try { | ||
| sentEvent = await this.driver.sendEvent( | ||
| request.data.type, | ||
| request.data.content || {}, | ||
| request.data.state_key, | ||
| ); | ||
| } catch (e) { | ||
| console.error("error sending event: ", e); | ||
| return this.transport.reply<IWidgetApiErrorResponseData>(request, { | ||
| error: {message: "Error sending event"}, | ||
| }); | ||
| } | ||
| } else { | ||
| const content = request.data.content || {}; | ||
| const msgtype = content['msgtype']; | ||
| if (!this.canSendRoomEvent(request.data.type, msgtype)) { | ||
| return this.transport.reply<IWidgetApiErrorResponseData>(request, { | ||
| error: {message: "Cannot send room events of this type"}, | ||
| }); | ||
| } | ||
|
|
||
| try { | ||
| sentEvent = await this.driver.sendEvent( | ||
| request.data.type, | ||
| content, | ||
| null, // not sending a state event | ||
| ); | ||
| } catch (e) { | ||
| console.error("error sending event: ", e); | ||
| return this.transport.reply<IWidgetApiErrorResponseData>(request, { | ||
| error: {message: "Error sending event"}, | ||
| }); | ||
| } | ||
| } | ||
|
|
||
| return this.transport.reply<ISendEventFromWidgetResponseData>(request, { | ||
| room_id: sentEvent.roomId, | ||
| event_id: sentEvent.eventId, | ||
| }); | ||
| } | ||
|
|
||
| private handleMessage(ev: CustomEvent<IWidgetApiRequest>) { | ||
| if (this.isStopped) return; | ||
| const actionEv = new CustomEvent(`action:${ev.detail.action}`, { | ||
|
|
@@ -178,6 +265,8 @@ export class ClientWidgetApi extends EventEmitter { | |
| return this.handleContentLoadedAction(<IContentLoadedActionRequest>ev.detail); | ||
| case WidgetApiFromWidgetAction.SupportedApiVersions: | ||
| return this.replyVersions(<ISupportedVersionsActionRequest>ev.detail); | ||
| case WidgetApiFromWidgetAction.SendEvent: | ||
| return this.handleSendEvent(<ISendEventFromWidgetActionRequest>ev.detail); | ||
| default: | ||
| return this.transport.reply(ev.detail, <IWidgetApiErrorResponseData>{ | ||
| error: { | ||
|
|
@@ -223,4 +312,31 @@ export class ClientWidgetApi extends EventEmitter { | |
| WidgetApiToWidgetAction.CloseModalWidget, data, | ||
| ).then(); | ||
| } | ||
|
|
||
| /** | ||
| * Feeds an event to the widget. If the widget is not able to accept the event due to | ||
| * permissions, this will no-op and return calmly. If the widget failed to handle the | ||
| * event, this will raise an error. | ||
| * @param {IRoomEvent} rawEvent The event to (try to) send to the widget. | ||
| * @returns {Promise<void>} Resolves when complete, rejects if there was an error sending. | ||
| */ | ||
| public feedEvent(rawEvent: IRoomEvent): Promise<void> { | ||
| if (rawEvent.state_key !== undefined && rawEvent.state_key !== null) { | ||
| // state event | ||
| if (!this.canReceiveStateEvent(rawEvent.type, rawEvent.state_key)) { | ||
| return Promise.resolve(); // no-op | ||
| } | ||
| } else { | ||
| // message event | ||
| if (!this.canReceiveRoomEvent(rawEvent.type, (rawEvent.content || {})['msgtype'])) { | ||
| return Promise.resolve(); // no-op | ||
| } | ||
| } | ||
|
|
||
| // Feed the event into the widget | ||
| return this.transport.send<ISendEventToWidgetRequestData>( | ||
| WidgetApiToWidgetAction.SendEvent, | ||
| rawEvent as ISendEventToWidgetRequestData, // it's compatible, but missing the index signature | ||
| ).then(); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. unintentional? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's a conversion because using |
||
| } | ||
| } | ||
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,25 @@ | ||
| /* | ||
| * Copyright 2020 The Matrix.org Foundation C.I.C. | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| export interface IRoomEvent { | ||
| type: string; | ||
| sender: string; | ||
| event_id: string; // eslint-disable-line camelcase | ||
| state_key?: string; // eslint-disable-line camelcase | ||
| origin_server_ts: number; // eslint-disable-line camelcase | ||
| content: unknown; | ||
| unsigned: unknown; | ||
| } |
This file contains hidden or 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,56 @@ | ||
| /* | ||
| * Copyright 2020 The Matrix.org Foundation C.I.C. | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| import { IWidgetApiRequest, IWidgetApiRequestData } from "./IWidgetApiRequest"; | ||
| import { WidgetApiFromWidgetAction, WidgetApiToWidgetAction } from "./WidgetApiAction"; | ||
| import { IWidgetApiResponseData } from "./IWidgetApiResponse"; | ||
| import { IRoomEvent } from "./IRoomEvent"; | ||
|
|
||
| export interface ISendEventFromWidgetRequestData extends IWidgetApiRequestData { | ||
| state_key?: string; // eslint-disable-line camelcase | ||
| type: string; | ||
| content: unknown; | ||
| } | ||
|
|
||
| export interface ISendEventFromWidgetActionRequest extends IWidgetApiRequest { | ||
| action: WidgetApiFromWidgetAction.SendEvent; | ||
| data: ISendEventFromWidgetRequestData; | ||
| } | ||
|
|
||
| export interface ISendEventFromWidgetResponseData extends IWidgetApiResponseData { | ||
| room_id: string; // eslint-disable-line camelcase | ||
| event_id: string; // eslint-disable-line camelcase | ||
| } | ||
|
|
||
| export interface ISendEventFromWidgetActionResponse extends ISendEventFromWidgetActionRequest { | ||
| response: ISendEventFromWidgetResponseData; | ||
| } | ||
|
|
||
| export interface ISendEventToWidgetRequestData extends IWidgetApiRequestData, IRoomEvent { | ||
| } | ||
|
|
||
| export interface ISendEventToWidgetActionRequest extends IWidgetApiRequest { | ||
| action: WidgetApiToWidgetAction.SendEvent; | ||
| data: ISendEventToWidgetRequestData; | ||
| } | ||
|
|
||
| export interface ISendEventToWidgetResponseData extends IWidgetApiResponseData { | ||
| // nothing | ||
| } | ||
|
|
||
| export interface ISendEventToWidgetActionResponse extends ISendEventToWidgetActionRequest { | ||
| response: ISendEventToWidgetResponseData; | ||
| } |
This file contains hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.