Skip to content

Commit

Permalink
Update adapter for new matrix sdk version
Browse files Browse the repository at this point in the history
  • Loading branch information
Laupetin committed Nov 3, 2023
1 parent 6c14f4a commit 9bd5f58
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 34 deletions.
38 changes: 13 additions & 25 deletions src/matrix.js
Original file line number Diff line number Diff line change
@@ -1,25 +1,17 @@
let Adapter, localStorage, Robot, TextMessage, User;
try {
({Robot, Adapter, TextMessage, User} = require('hubot/es2015'));
} catch (e) {
try {
({Robot, Adapter, TextMessage, User} = require('hubot'));
} catch (error) {
let prequire = require('parent-require');
({Robot, Adapter, TextMessage, User} = prequire('hubot'));
}
}
const { Robot, Adapter, TextMessage, User } = require.main.require('hubot/es2015');

let sdk = require("matrix-js-sdk");

let request = require('request');
let sizeOf = require('image-size');
let MatrixSession = require('./session.js');

let localStorage;
if (localStorage == null) {
let {LocalStorage} = require('node-localstorage');
localStorage = new LocalStorage('./hubot-matrix.localStorage');
}

module.exports.use = (robot) => {

let that;
Expand All @@ -34,12 +26,11 @@ module.exports.use = (robot) => {
let that = this;
return (() => {
let result = [];
for (var stranger in err.devices) {
var devices = err.devices[stranger];
for (const stranger in err.devices) {
const devices = err.devices[stranger];
result.push((() => {
let result1 = [];
for (let device in devices) {
let _ = devices[device];
that.robot.logger.info(`Acknowledging ${stranger}'s device ${device}`);
result1.push(that.robot.matrixClient.setDeviceKnown(stranger, device));
}
Expand All @@ -53,7 +44,7 @@ module.exports.use = (robot) => {
send(envelope, ...strings) {
return (() => {
let result = [];
for (var str of Array.from(strings)) {
for (const str of Array.from(strings)) {
that.robot.logger.info(`Sending to ${envelope.room}: ${str}`);
if (/^(f|ht)tps?:\/\//i.test(str)) {
result.push(that.sendURL(envelope, str));
Expand Down Expand Up @@ -108,14 +99,12 @@ module.exports.use = (robot) => {
info = {mimetype: `image/${dims.type}`, h: dims.height, w: dims.width, size: body.length};
return that.robot.matrixClient.uploadContent(body, {
name: url,
type: info.mimetype,
rawResponse: false,
onlyContentUri: true
}).done(content_uri => {
return that.robot.matrixClient.sendImageMessage(envelope.room, content_uri, info, url).catch(err => {
type: info.mimetype
}).then(response => {
return that.robot.matrixClient.sendImageMessage(envelope.room, response.content_uri, info, url).catch(err => {
if (err.name === 'UnknownDeviceError') {
that.handleUnknownDevices(err);
return that.robot.matrixClient.sendImageMessage(envelope.room, content_uri, info, url);
return that.robot.matrixClient.sendImageMessage(envelope.room, response.content_uri, info, url);
}
});
});
Expand Down Expand Up @@ -154,15 +143,14 @@ module.exports.use = (robot) => {
const currentDisplayName = that.robot.matrixClient.getUser(userId).displayName;
if (that.robot.name !== currentDisplayName) {
that.robot.logger.info(`Setting display name to ${that.robot.name}`);
that.robot.matrixClient.setDisplayName(that.robot.name, () => {
});
that.robot.matrixClient.setDisplayName(that.robot.name);
}
return that.emit('connected');
}
});
that.robot.matrixClient.on('Room.timeline', (event, room, toStartOfTimeline) => {
if ((event.getType() === 'm.room.message') && (toStartOfTimeline === false)) {
that.robot.matrixClient.setPresence("online");
that.robot.matrixClient.setPresence({ presence: "online" });
let message = event.getContent();
let name = event.getSender();
let user = that.robot.brain.userForId(name);
Expand All @@ -182,7 +170,7 @@ module.exports.use = (robot) => {
that.robot.matrixClient.on('RoomMember.membership', (event, member) => {
let userId = that.robot.matrixClient.getUserId();
if ((member.membership === 'invite') && (member.userId === userId)) {
return that.robot.matrixClient.joinRoom(member.roomId).done(() => {
return that.robot.matrixClient.joinRoom(member.roomId).then(() => {
return that.robot.logger.info(`Auto-joined ${member.roomId}`);
});
}
Expand Down
17 changes: 8 additions & 9 deletions src/session.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,25 +32,21 @@ class MatrixSession {
baseUrl: this.matrixServer || 'https://matrix.org',
accessToken: accessToken,
userId: userId,
deviceId: deviceId,
deviceId: deviceId
});

cb(null, this.client)
}

login(cb) {
let that = this;
this.client = sdk.createClient(this.matrixServer || 'https://matrix.org');
this.client = sdk.createClient({
baseUrl: this.matrixServer || 'https://matrix.org'
});
this.client.login('m.login.password', {
user: this.matrixUser || this.botName,
password: this.matrixPassword
}, async (err, data) => {

if (err) {
that.logger.error(err);
cb(err, null)
}

}).then((data) => {
that.logger.info(`Logged in ${data.user_id} on device ${data.device_id}`);
that.client = sdk.createClient({
baseUrl: that.matrixServer || 'https://matrix.org',
Expand All @@ -65,6 +61,9 @@ class MatrixSession {
that.localStorage.setItem("user_id", data.user_id)
that.localStorage.setItem("device_id", data.device_id)
cb(null, that.client)
}).catch((err) => {
that.logger.error(err);
cb(err, null)
});
}

Expand Down

0 comments on commit 9bd5f58

Please sign in to comment.