From 70b9ea66ba642c0b33ceeddbe2419152b9f0879d Mon Sep 17 00:00:00 2001 From: Adhidarma Hadiwinoto <me@adhisimon.org> Date: Fri, 28 Oct 2016 12:27:55 +0700 Subject: [PATCH] coba matrix-util --- adaptor-xmpp.js | 38 ++++++++------------------------------ matrix-util.js | 56 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 64 insertions(+), 30 deletions(-) create mode 100644 matrix-util.js diff --git a/adaptor-xmpp.js b/adaptor-xmpp.js index 08d8e65..bbc113c 100644 --- a/adaptor-xmpp.js +++ b/adaptor-xmpp.js @@ -1,11 +1,13 @@ var xmpp = require('simple-xmpp'); var moment = require('moment'); +var MatrixUtil = require('./matrix-util'); var username; var password; var callbacks; var matrix; +var matrixUtil; function onOnline(data) { logger.info('XMPP login successful', {data: data}); @@ -60,34 +62,7 @@ function onUnsubscribe(sender) { } function onBuddy(jid, state, statusText, resource) { - if (jid == 'undefined') {return; } - - logger.verbose('Buddy state change', {jid: jid, state: state, statusText: statusText, resource: resource}); - - if (!matrix) { - return; - } - - if (!matrix.buddies) { - matrix.buddies = {}; - } - - if (!matrix.buddies[jid]) { - matrix.buddies[jid] = {resources: {}}; - } - - matrix.buddies[jid].resources[resource] = { - state: state, - statusText: statusText, - lastUpdate: moment().format('YYYY-MM-DD HH:mm:ss') - } - - if (resource != 'undefined' && matrix.buddies[jid].resources.undefined) { - try { - delete matrix.buddies[jid].resources.undefined; - } - catch(e) {}; - } + matrixUtil.updateBuddyState(jid, state, statusText, resource); } function isPartnerOffline(partner) { @@ -136,9 +111,12 @@ function init(_username, _password, _logger, _callbacks) { } function setOptions(options) { - if (options.matrix) { - matrix = options.matrix; + if (!options.matrix) { + return; } + + matrix = options.matrix; + matrixUtil = new MatrixUtil({matrix: matrix, logger: logger}); } function sendMessage(destination, msg) { diff --git a/matrix-util.js b/matrix-util.js new file mode 100644 index 0000000..d0b2264 --- /dev/null +++ b/matrix-util.js @@ -0,0 +1,56 @@ +var moment = require('moment'); + +module.exports = MatrixUtil; + +function MatrixUtil(options) { + if (!options) { + console.trace('Undefined options'); + process.exit(1); + } + + this.matrix = options.matrix; + if (!this.matrix) { + console.trace("Matrix not set"); + process.exit(1); + } + + this.logger = options.logger; + if (!this.logger) { + console.trace("Logger not set"); + process.exit(1); + } +} + +MatrixUtil.prototype.updateBuddyState = function(jid, state, statusText, resource) { + if (jid == 'undefined') {return; } + + var logger = this.logger; + var matrix = this.matrix; + + logger.verbose('Buddy state change', {jid: jid, state: state, statusText: statusText, resource: resource}); + + if (!matrix) { + return; + } + + if (!matrix.buddies) { + matrix.buddies = {}; + } + + if (!matrix.buddies[jid]) { + matrix.buddies[jid] = {resources: {}}; + } + + matrix.buddies[jid].resources[resource] = { + state: state, + statusText: statusText, + lastUpdate: moment().format('YYYY-MM-DD HH:mm:ss') + } + + if (resource != 'undefined' && matrix.buddies[jid].resources.undefined) { + try { + delete matrix.buddies[jid].resources.undefined; + } + catch(e) {}; + } +} -- 1.9.0