Commit 0a0dd5297c596578497d9880dbae583f322e7a28

Authored by Adhidarma Hadiwinoto
1 parent 637ced97f0
Exists in master

_cleanPartnerId

Showing 1 changed file with 29 additions and 19 deletions Side-by-side Diff

... ... @@ -4,6 +4,19 @@ var moment = require('moment');
4 4  
5 5 module.exports = MatrixUtil;
6 6  
  7 +function _cleanPartnerId(partnerId) {
  8 + let cleaned = partnerId;
  9 +
  10 + try {
  11 + cleaned = cleaned.toLocaleString();
  12 + cleaned = cleaned.trim().toLowerCase();
  13 + } catch(e) {
  14 + return partnerId;
  15 + }
  16 +
  17 + return cleaned;
  18 +}
  19 +
7 20 function MatrixUtil(options) {
8 21 if (!options) {
9 22 console.trace('Undefined options');
... ... @@ -27,10 +40,7 @@ MatrixUtil.prototype.updateBuddyState = function(jid, state, statusText, resourc
27 40 if (!jid) {return; }
28 41 if (jid == 'undefined') {return; }
29 42  
30   - try {
31   - jid = jid.toLowerCase();
32   - jid = jid.toLocaleString();
33   - } catch(e) { }
  43 + jid = _cleanPartnerId(jid);
34 44  
35 45 if (!resource) {
36 46 resource = 'undefined';
... ... @@ -64,8 +74,6 @@ MatrixUtil.prototype.updateBuddyState = function(jid, state, statusText, resourc
64 74 logger.warn('MatrixUtil: Exception on update resources on matrix', {jid: jid, state: state, statusText: statusText, resource: resource});
65 75 }
66 76  
67   -
68   -
69 77 if (resource != 'undefined' && matrix.buddies[jid].resources['undefined']) {
70 78 try {
71 79 delete matrix.buddies[jid].resources['undefined'];
... ... @@ -76,7 +84,8 @@ MatrixUtil.prototype.updateBuddyState = function(jid, state, statusText, resourc
76 84  
77 85 MatrixUtil.prototype.isAFriend = function(jid) {
78 86 if (!jid) { return; }
79   - jid = jid.toLowerCase();
  87 +
  88 + jid = _cleanPartnerId(jid);
80 89  
81 90 let matrix = this.matrix;
82 91  
... ... @@ -89,7 +98,8 @@ MatrixUtil.prototype.isAFriend = function(jid) {
89 98  
90 99 MatrixUtil.prototype.isPartnerOffline = function(partner) {
91 100 if (!partner) { return; }
92   - partner = partner.toLowerCase();
  101 +
  102 + partner = _cleanPartnerId(partner);
93 103  
94 104 let matrix = this.matrix;
95 105 let logger = this.logger;
... ... @@ -112,9 +122,9 @@ MatrixUtil.prototype.isPartnerOffline = function(partner) {
112 122 return true;
113 123 }
114 124  
115   -MatrixUtil.prototype.updateLastIncoming = function(sender, msg) {
116   - if (!sender) { return; }
117   - sender = sender.toLowerCase();
  125 +MatrixUtil.prototype.updateLastIncoming = function(partner, msg) {
  126 + if (!partner) { return; }
  127 + partner = _cleanPartnerId(partner);
118 128  
119 129 let matrix = this.matrix;
120 130  
... ... @@ -126,19 +136,19 @@ MatrixUtil.prototype.updateLastIncoming = function(sender, msg) {
126 136 matrix.buddies = {};
127 137 }
128 138  
129   - if (!matrix.buddies[sender]) {
130   - matrix.buddies[sender] = {};
  139 + if (!matrix.buddies[partner]) {
  140 + matrix.buddies[partner] = {};
131 141 }
132 142  
133   - matrix.buddies[sender].lastIncoming = {
  143 + matrix.buddies[partner].lastIncoming = {
134 144 msg: msg,
135 145 lastUpdate: moment().format('YYYY-MM-DD HH:mm:ss')
136 146 }
137 147 }
138 148  
139   -MatrixUtil.prototype.updateLastOutgoing = function(destination, msg) {
140   - if (!destination) { return; }
141   - destination = destination.toLowerCase();
  149 +MatrixUtil.prototype.updateLastOutgoing = function(partner, msg) {
  150 + if (!partner) { return; }
  151 + partner = _cleanPartnerId(partner);
142 152  
143 153 let matrix = this.matrix;
144 154  
... ... @@ -150,8 +160,8 @@ MatrixUtil.prototype.updateLastOutgoing = function(destination, msg) {
150 160 matrix.buddies = {};
151 161 }
152 162  
153   - if (!matrix.buddies[destination]) {
154   - matrix.buddies[destination] = {};
  163 + if (!matrix.buddies[partner]) {
  164 + matrix.buddies[partner] = {};
155 165 }
156 166  
157 167 matrix.buddies[destination].lastOutgoing = {