From 0a0dd5297c596578497d9880dbae583f322e7a28 Mon Sep 17 00:00:00 2001 From: Adhidarma Hadiwinoto <me@adhisimon.org> Date: Mon, 31 Oct 2016 20:09:01 +0700 Subject: [PATCH] _cleanPartnerId --- matrix-util.js | 48 +++++++++++++++++++++++++++++------------------- 1 file changed, 29 insertions(+), 19 deletions(-) diff --git a/matrix-util.js b/matrix-util.js index cd77763..98cf323 100644 --- a/matrix-util.js +++ b/matrix-util.js @@ -4,6 +4,19 @@ var moment = require('moment'); module.exports = MatrixUtil; +function _cleanPartnerId(partnerId) { + let cleaned = partnerId; + + try { + cleaned = cleaned.toLocaleString(); + cleaned = cleaned.trim().toLowerCase(); + } catch(e) { + return partnerId; + } + + return cleaned; +} + function MatrixUtil(options) { if (!options) { console.trace('Undefined options'); @@ -27,10 +40,7 @@ MatrixUtil.prototype.updateBuddyState = function(jid, state, statusText, resourc if (!jid) {return; } if (jid == 'undefined') {return; } - try { - jid = jid.toLowerCase(); - jid = jid.toLocaleString(); - } catch(e) { } + jid = _cleanPartnerId(jid); if (!resource) { resource = 'undefined'; @@ -64,8 +74,6 @@ MatrixUtil.prototype.updateBuddyState = function(jid, state, statusText, resourc logger.warn('MatrixUtil: Exception on update resources on matrix', {jid: jid, state: state, statusText: statusText, resource: resource}); } - - if (resource != 'undefined' && matrix.buddies[jid].resources['undefined']) { try { delete matrix.buddies[jid].resources['undefined']; @@ -76,7 +84,8 @@ MatrixUtil.prototype.updateBuddyState = function(jid, state, statusText, resourc MatrixUtil.prototype.isAFriend = function(jid) { if (!jid) { return; } - jid = jid.toLowerCase(); + + jid = _cleanPartnerId(jid); let matrix = this.matrix; @@ -89,7 +98,8 @@ MatrixUtil.prototype.isAFriend = function(jid) { MatrixUtil.prototype.isPartnerOffline = function(partner) { if (!partner) { return; } - partner = partner.toLowerCase(); + + partner = _cleanPartnerId(partner); let matrix = this.matrix; let logger = this.logger; @@ -112,9 +122,9 @@ MatrixUtil.prototype.isPartnerOffline = function(partner) { return true; } -MatrixUtil.prototype.updateLastIncoming = function(sender, msg) { - if (!sender) { return; } - sender = sender.toLowerCase(); +MatrixUtil.prototype.updateLastIncoming = function(partner, msg) { + if (!partner) { return; } + partner = _cleanPartnerId(partner); let matrix = this.matrix; @@ -126,19 +136,19 @@ MatrixUtil.prototype.updateLastIncoming = function(sender, msg) { matrix.buddies = {}; } - if (!matrix.buddies[sender]) { - matrix.buddies[sender] = {}; + if (!matrix.buddies[partner]) { + matrix.buddies[partner] = {}; } - matrix.buddies[sender].lastIncoming = { + matrix.buddies[partner].lastIncoming = { msg: msg, lastUpdate: moment().format('YYYY-MM-DD HH:mm:ss') } } -MatrixUtil.prototype.updateLastOutgoing = function(destination, msg) { - if (!destination) { return; } - destination = destination.toLowerCase(); +MatrixUtil.prototype.updateLastOutgoing = function(partner, msg) { + if (!partner) { return; } + partner = _cleanPartnerId(partner); let matrix = this.matrix; @@ -150,8 +160,8 @@ MatrixUtil.prototype.updateLastOutgoing = function(destination, msg) { matrix.buddies = {}; } - if (!matrix.buddies[destination]) { - matrix.buddies[destination] = {}; + if (!matrix.buddies[partner]) { + matrix.buddies[partner] = {}; } matrix.buddies[destination].lastOutgoing = { -- 1.9.0