diff --git a/package.json b/package.json index ee53999..3fe980e 100644 --- a/package.json +++ b/package.json @@ -9,8 +9,9 @@ "author": "David A Roberts", "license": "MIT", "dependencies": { - "parent-require": "^1.0.0", - "matrix-js-sdk": "*" + "matrix-js-sdk": "*", + "node-localstorage": "^1.3.0", + "parent-require": "^1.0.0" }, "peerDependencies": { "hubot": ">=2.0" diff --git a/src/matrix.coffee b/src/matrix.coffee index 278a90e..fb22ef0 100644 --- a/src/matrix.coffee +++ b/src/matrix.coffee @@ -7,6 +7,10 @@ catch sdk = require 'matrix-js-sdk' request = require 'request' +unless localStorage? + {LocalStorage} = require('node-localstorage') + localStorage = new LocalStorage('./hubot-matrix.localStorage') + class Matrix extends Adapter constructor: -> super @@ -18,10 +22,13 @@ class Matrix extends Adapter if /^(f|ht)tps?:\/\//i.test(str) @sendImage envelope, str else - @client.sendMessage envelope.room, { - msgtype: "m.notice", - body: str - } + @client.sendNotice(envelope.room, str).catch (err) => + if err.name == 'UnknownDeviceError' + for stranger, devices of err.devices + for device, _ of devices + @robot.logger.info "Acknowledging #{stranger}'s device #{device}" + @client.setDeviceKnown(stranger, device) + @client.sendNotice(envelope.room, str) emote: (envelope, strings...) -> for str in strings @@ -60,11 +67,14 @@ class Matrix extends Adapter return @user_id = data.user_id @access_token = data.access_token - @robot.logger.info "Logged in #{@user_id}" + @device_id = data.device_id + @robot.logger.info "Logged in #{@user_id} on device #{@device_id}" @client = sdk.createClient baseUrl: process.env.HUBOT_MATRIX_HOST_SERVER || 'https://matrix.org' accessToken: @access_token userId: @user_id + deviceId: @device_id + sessionStore: new sdk.WebStorageSessionStore(localStorage) @client.on 'sync', (state, prevState, data) => switch state when "PREPARED"