Skip to content

Commit

Permalink
Extend url template variables with device_id
Browse files Browse the repository at this point in the history
I mentioned the need for this parameter in the related [MSC 3891 ](matrix-org/matrix-spec-proposals#3819) which implements to device messages. They are relying on the device id to be available, but there is currently no way to access a device id from within a widget (Element Call does this but is cheating :P). Therefore [I propose to include the device id in the available parameters](matrix-org/matrix-spec-proposals#3819 (comment)).

Signed-off-by: Oliver Sand <[email protected]>
  • Loading branch information
Fox32 committed Feb 21, 2023
1 parent 1f378c7 commit cddc092
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/templating/url-template.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ export interface ITemplateParams {
clientId?: string;
clientTheme?: string;
clientLanguage?: string;
deviceId?: string;
}

export function runTemplate(url: string, widget: IWidget, params: ITemplateParams): string {
Expand All @@ -39,6 +40,9 @@ export function runTemplate(url: string, widget: IWidget, params: ITemplateParam
'org.matrix.msc2873.client_id': params.clientId || "",
'org.matrix.msc2873.client_theme': params.clientTheme || "",
'org.matrix.msc2873.client_language': params.clientLanguage || "",

// TODO: Convert to stable (https://github.com/matrix-org/matrix-spec-proposals/pull/3819)
'org.matrix.msc3819.device_id': params.deviceId || "",
});
let result = url;
for (const key of Object.keys(variables)) {
Expand Down
38 changes: 38 additions & 0 deletions test/url-template-test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
* Copyright 2023 Nordeck IT + Consulting GmbH.
*
* 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 { runTemplate } from "../src";

describe("runTemplate", () => {
it("should replace device id template in url", () => {
const url = "https://localhost/?my-query#device_id=$org.matrix.msc3819.device_id";
const replacedUrl = runTemplate(
url,
{
id: "widget-id",
creatorUserId: '@user-id',
type: 'type',
url,
},
{
deviceId: "my-device-id",
currentUserId: '@user-id',
},
);

expect(replacedUrl).toBe("https://localhost/?my-query#device_id=my-device-id");
});
});

0 comments on commit cddc092

Please sign in to comment.