Skip to content

Commit

Permalink
Fix build failure in node.js example (#4394)
Browse files Browse the repository at this point in the history
* Fix node.js example

Relates to: element-hq/element-web#26922
Signed-off-by: Johannes Marbach <[email protected]>

* Update examples/node/app.js

Co-authored-by: Richard van der Hoff <[email protected]>

* Update examples/node/package.json

Co-authored-by: Richard van der Hoff <[email protected]>

* Move imports to the top of the file

---------

Co-authored-by: Richard van der Hoff <[email protected]>
  • Loading branch information
Johennes and richvdh authored Sep 13, 2024
1 parent 8cf5df7 commit c8403f3
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 12 deletions.
27 changes: 16 additions & 11 deletions examples/node/app.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
import clc from "cli-color";
import fs from "fs";
import readline from "readline";
import sdk, { ClientEvent, EventType, MsgType, RoomEvent } from "matrix-js-sdk";
import { KnownMembership } from "matrix-js-sdk/lib/@types/membership.js";

var myHomeServer = "http://localhost:8008";
var myUserId = "@example:localhost";
var myAccessToken = "QGV4YW1wbGU6bG9jYWxob3N0.qPEvLuYfNBjxikiCjP";
var sdk = require("matrix-js-sdk");
var clc = require("cli-color");

var matrixClient = sdk.createClient({
baseUrl: "http://localhost:8008",
baseUrl: myHomeServer,
accessToken: myAccessToken,
userId: myUserId,
});
Expand All @@ -15,7 +21,6 @@ var numMessagesToShow = 20;

// Reading from stdin
var CLEAR_CONSOLE = "\x1B[2J";
var readline = require("readline");
var rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
Expand Down Expand Up @@ -97,7 +102,7 @@ rl.on("line", function (line) {
})
.then(function (url) {
var content = {
msgtype: "m.file",
msgtype: MsgType.File,
body: filename,
url: JSON.parse(url).content_uri,
};
Expand Down Expand Up @@ -138,7 +143,7 @@ rl.on("line", function (line) {
// ==== END User input

// show the room list after syncing.
matrixClient.on("sync", function (state, prevState, data) {
matrixClient.on(ClientEvent.Sync, function (state, prevState, data) {
switch (state) {
case "PREPARED":
setRoomList();
Expand All @@ -149,7 +154,7 @@ matrixClient.on("sync", function (state, prevState, data) {
}
});

matrixClient.on("Room", function () {
matrixClient.on(ClientEvent.Room, function () {
setRoomList();
if (!viewingRoom) {
printRoomList();
Expand All @@ -158,7 +163,7 @@ matrixClient.on("Room", function () {
});

// print incoming messages.
matrixClient.on("Room.timeline", function (event, room, toStartOfTimeline) {
matrixClient.on(RoomEvent.Timeline, function (event, room, toStartOfTimeline) {
if (toStartOfTimeline) {
return; // don't print paginated results
}
Expand Down Expand Up @@ -305,7 +310,7 @@ function printRoomInfo(room) {
print(eTypeHeader + sendHeader + contentHeader);
print(new Array(100).join("-"));
eventMap.keys().forEach(function (eventType) {
if (eventType === "m.room.member") {
if (eventType === EventType.RoomMember) {
return;
} // use /members instead.
var eventEventMap = eventMap.get(eventType);
Expand Down Expand Up @@ -343,7 +348,7 @@ function printLine(event) {
name = name.slice(0, maxNameWidth - 1) + "\u2026";
}

if (event.getType() === "m.room.message") {
if (event.getType() === EventType.RoomMessage) {
body = event.getContent().body;
} else if (event.isState()) {
var stateName = event.getType();
Expand Down Expand Up @@ -394,4 +399,4 @@ function fixWidth(str, len) {
return str;
}

matrixClient.startClient(numMessagesToShow); // messages for each room.
matrixClient.startClient({ initialSyncLimit: numMessagesToShow });
3 changes: 2 additions & 1 deletion examples/node/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@
"main": "app.js",
"author": "",
"license": "Apache 2.0",
"type": "module",
"dependencies": {
"cli-color": "^1.0.0",
"matrix-js-sdk": "^32.0.0"
"matrix-js-sdk": "^34.5.0"
}
}

0 comments on commit c8403f3

Please sign in to comment.