Skip to content

Commit

Permalink
Merge pull request #369 from matrix-org/hs/encryption-refactore
Browse files Browse the repository at this point in the history
Refactor encryption code to use bot-sdk
  • Loading branch information
Half-Shot authored Nov 18, 2021
2 parents b57a3f2 + 159b2b4 commit 91f709e
Show file tree
Hide file tree
Showing 14 changed files with 1,269 additions and 5,039 deletions.
3 changes: 3 additions & 0 deletions changelog.d/369.misc
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Refactor Encryption broker code to use the bot-sdk, and generally be more race-resistant when starting new sessions.

**Breaking**: This change drops support for ephemeral events via the encryption broker, so ephemeral events MUST be enabled within the appservice registration to work.
4,546 changes: 0 additions & 4,546 deletions examples/encryption/package-lock.json

This file was deleted.

7 changes: 3 additions & 4 deletions examples/encryption/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,10 @@
"author": "Matrix.org",
"license": "Apache-2.0",
"dependencies": {
"@types/node": "^14",
"@types/request": "^2.48.5",
"matrix-appservice-bridge": "file:../.."
"@types/express": "^4.17.13",
"@types/node": "^14"
},
"devDependencies": {
"typescript": "^4.0.2"
"typescript": "^4.4.4"
}
}
29 changes: 18 additions & 11 deletions examples/encryption/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ limitations under the License.
// Usage:
// node index.js -r -u "http://localhost:9000" # remember to add the registration!
// node index.js -p 9000
import { Cli, Bridge, AppServiceRegistration, ClientEncryptionSession, ClientEncryptionStore, Logging} from 'matrix-appservice-bridge';
import { Cli, Bridge, AppServiceRegistration, ClientEncryptionSession, ClientEncryptionStore, Logging} from '../../..';

Logging.configure({
console: "debug",
Expand All @@ -31,7 +31,10 @@ const encryptionStore: ClientEncryptionStore = {
async setStoredSession(session: ClientEncryptionSession) {
log.info("Set session", session.userId, session.deviceId);
encMap.set(session.userId, session);
}
},
async updateSyncToken() {
// No-op
},
};

new Cli({
Expand All @@ -44,34 +47,35 @@ new Cli({
reg.addRegexPattern("users", "@enc_.*", true);
callback(reg);
},
run: function (port, config) {
run: function (port, _config, registration) {
let bridge: Bridge;
bridge = new Bridge({
homeserverUrl: "http://localhost:8008",
domain: "halfyxps",
domain: process.env.MATRIX_DOMAIN,
registration: "enc-registration.yaml",
bridgeEncryption: {
homeserverUrl: "http://localhost:8009",
homeserverUrl: "http://localhost:8004",
store: encryptionStore,
},
controller: {
onUserQuery: function (queriedUser) {
onUserQuery: function () {
return {}; // auto-provision users with no additonal data
},

onEvent: async function (request, context) {
const event = request.getData();
const bot = bridge.getBot();
const intent = bridge.getIntentFromLocalpart(`enc_${context.senders.matrix.localpart}`);
console.log(event, bot.getUserId());
console.log(request, bot.getUserId());
if (event.type === "m.room.member" &&
event.content.membership === "invite" &&
event.state_key === "@encbot:halfyxps") {
event.state_key === bot.getUserId()) {
console.log("Joining the room!");
try {
await intent.join(event.room_id);
console.log("Joined the room!");
} catch (ex) {
}
catch (ex) {
console.log("Err joining room:", ex);
}
return;
Expand All @@ -90,7 +94,10 @@ new Cli({
}
}
});
log.info("Matrix-side listening on port %s", port);
bridge.run(port, config);
const splitUrl = registration.getAppServiceUrl().split(":");
const urlPort = parseInt(splitUrl[splitUrl.length-1]);
port = port || urlPort || 8000;
bridge.run(port);
log.info(`Matrix-side listening on port ${port}`);
}
}).run();
Loading

0 comments on commit 91f709e

Please sign in to comment.