This commit is contained in:
David A Roberts 2017-02-09 08:44:30 +00:00
parent 4ae4c229b8
commit 14c6b86e84
2 changed files with 18 additions and 7 deletions

View File

@ -9,8 +9,9 @@
"author": "David A Roberts", "author": "David A Roberts",
"license": "MIT", "license": "MIT",
"dependencies": { "dependencies": {
"parent-require": "^1.0.0", "matrix-js-sdk": "*",
"matrix-js-sdk": "*" "node-localstorage": "^1.3.0",
"parent-require": "^1.0.0"
}, },
"peerDependencies": { "peerDependencies": {
"hubot": ">=2.0" "hubot": ">=2.0"

View File

@ -7,6 +7,10 @@ catch
sdk = require 'matrix-js-sdk' sdk = require 'matrix-js-sdk'
request = require 'request' request = require 'request'
unless localStorage?
{LocalStorage} = require('node-localstorage')
localStorage = new LocalStorage('./hubot-matrix.localStorage')
class Matrix extends Adapter class Matrix extends Adapter
constructor: -> constructor: ->
super super
@ -18,10 +22,13 @@ class Matrix extends Adapter
if /^(f|ht)tps?:\/\//i.test(str) if /^(f|ht)tps?:\/\//i.test(str)
@sendImage envelope, str @sendImage envelope, str
else else
@client.sendMessage envelope.room, { @client.sendNotice(envelope.room, str).catch (err) =>
msgtype: "m.notice", if err.name == 'UnknownDeviceError'
body: str 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...) -> emote: (envelope, strings...) ->
for str in strings for str in strings
@ -60,11 +67,14 @@ class Matrix extends Adapter
return return
@user_id = data.user_id @user_id = data.user_id
@access_token = data.access_token @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 @client = sdk.createClient
baseUrl: process.env.HUBOT_MATRIX_HOST_SERVER || 'https://matrix.org' baseUrl: process.env.HUBOT_MATRIX_HOST_SERVER || 'https://matrix.org'
accessToken: @access_token accessToken: @access_token
userId: @user_id userId: @user_id
deviceId: @device_id
sessionStore: new sdk.WebStorageSessionStore(localStorage)
@client.on 'sync', (state, prevState, data) => @client.on 'sync', (state, prevState, data) =>
switch state switch state
when "PREPARED" when "PREPARED"