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",
"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"

View File

@ -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"