Skip to content

Commit dabf660

Browse files
Old local changes
1 parent c4dddcf commit dabf660

File tree

2 files changed

+38
-24
lines changed

2 files changed

+38
-24
lines changed

src/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@ import { XAPI, connect } from "jsxapi";
22
import { run } from "./macro";
33
require("dotenv").config();
44

5-
const clientAuth = {
5+
// @ts-ignore
6+
global.clientAuth = {
67
"user_id": process.env.ROOMKIT_MATRIX_USER_ID,
78
"device_id": process.env.ROOMKIT_MATRIX_DEVICE_ID,
89
"access_token": process.env.ROOMKIT_MATRIX_ACCESS_TOKEN,

src/macro.ts

Lines changed: 36 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -8,25 +8,14 @@ export async function run(deviceApi: XAPI) {
88
const elementCallDomain = "ec.rory.gay";
99
const displayName = "Room Kit Mini";
1010

11-
// Obtain from client devtools -> Storage -> Local Storage -> "matrix-auth-store"
12-
// const clientAuth = {
13-
// "user_id": ...,
14-
// "device_id": ...,
15-
// "access_token": ...,
16-
// "passwordlessUser": true,
17-
// "tempPassword": ...
18-
// };
11+
// FOR MACRO: Obtain from client devtools -> Storage -> Local Storage -> "matrix-auth-store"
12+
//@ts-ignore
13+
const clientAuth: MatrixClientAuth = global.clientAuth;
14+
// Optionally: hardcode value if needed
15+
const matrixHomeserverBaseUrl = await getMatrixServerDomain(elementCallDomain);
1916
//#endregion
2017

2118
//#region Constants
22-
const KEYBOARD_TYPES = {
23-
NUMERIC: "Numeric",
24-
SINGLELINE: "SingleLine",
25-
PASSWORD: "Password",
26-
PIN: "PIN",
27-
};
28-
29-
const MEETING_ID = "meetingID";
3019

3120
/* This will be the Panel/Widget ID you are using in the UI Extension */
3221
const INROOMCONTROL_AUDIOCONTROL_PANELID = "ElementCallButton";
@@ -84,7 +73,7 @@ export async function run(deviceApi: XAPI) {
8473
const roomId = await getText({
8574
Title: "Enter room ID",
8675
Description: "... or press Next is a value is already there",
87-
DefaultValue: await tryResolveRoomAlias(`#${alias}:${serverDomain}`)
76+
DefaultValue: await tryResolveRoomAlias(`#${alias}:${serverDomain}`) ?? undefined
8877
});
8978
const roomEncryptionEnabled = await checkEncryptionEnabled(roomId);
9079
const password = await getText({
@@ -161,6 +150,11 @@ export async function run(deviceApi: XAPI) {
161150
//#endregion
162151

163152
//#region Matrix API
153+
async function mxHttpGet(uri: string){
154+
//const server = await getMatrixServerDomain(elementCallDomain);
155+
return await httpGet(`${matrixHomeserverBaseUrl}${uri}`, [`Authorization: Bearer ${clientAuth.access_token}`])
156+
}
157+
164158
async function getMatrixServerDomain(ecInstance: string) {
165159
const ecConfig = await httpGet(`https://${ecInstance}/config.json`);
166160
const serverDomain = ecConfig.default_server_config["m.homeserver"].base_url;
@@ -172,35 +166,46 @@ export async function run(deviceApi: XAPI) {
172166

173167
}
174168

175-
async function tryResolveRoomAlias(alias: string) {
176-
const server = await getMatrixServerDomain(elementCallDomain);
169+
async function tryResolveRoomAlias(alias: string): Promise<string | null> {
177170
const encodedAlias = encodeURIComponent(alias);
178171
try {
179172
// const res = await httpGet("https://matrix.rory.gay/_matrix/client/v3/directory/room/" + encodeURIComponent("#proxmox:envs.net"));
180-
const res = await httpGet(`${server}/_matrix/client/v3/directory/room/${encodedAlias}`);
173+
const res = await mxHttpGet(`/_matrix/client/v3/directory/room/${encodedAlias}`);
181174
log(res);
182175
return res.room_id;
183176
} catch (e: any) {
184177
log(e);
178+
return null;
185179
}
186180
}
187181

188182
async function checkEncryptionEnabled(roomId: string) {
189-
const server = await getMatrixServerDomain(elementCallDomain);
190183
const encodedRoomId = encodeURIComponent(roomId);
191184
try {
192-
const res = await httpGet(`${server}/_matrix/client/v3/room/${encodedRoomId}/state/m.room.encryption/`);
185+
const res = await mxHttpGet(`/_matrix/client/v3/room/${encodedRoomId}/state/m.room.encryption/`);
193186
return true;
194187
} catch (e) {
195188
return false;
196189
}
197190
}
198191

192+
async function checkMatrixAuth(){
193+
try {
194+
const res = await mxHttpGet(`/_matrix/client/v3/account/whoami`);
195+
log(res);
196+
return !!res.user_id;
197+
}
198+
catch {
199+
return false;
200+
}
201+
}
202+
199203
//#endregion
200204

201205

202206
await registerHomeButton();
203-
await tryResolveRoomAlias("#cisco:call.ems.host");
207+
//await tryResolveRoomAlias("#cisco:call.ems.host");
208+
console.log("Matrix auth valid:", await checkMatrixAuth());
204209
await deviceApi.Command.UserInterface.WebView.Clear();
205210

206211
}
@@ -223,6 +228,14 @@ enum KeyboardTypes {
223228
PIN = "PIN",
224229
}
225230

231+
interface MatrixClientAuth {
232+
user_id: string,
233+
device_id: string,
234+
access_token: string,
235+
passwordlessUser: boolean,
236+
tempPassword: string
237+
}
238+
226239
//#endregion
227240

228241
// FOR MACRO: -- NOTE: no top level await!

0 commit comments

Comments
 (0)