Commit 85428dac40b63d45d1787f7a14b4a3f2cdc2e4fd

Authored by Adhidarma Hadiwinoto
1 parent 98cfca3add
Exists in master

return if Exception

Showing 1 changed file with 2 additions and 0 deletions Inline Diff

1 'use strict'; 1 'use strict';
2 2
3 var moment = require('moment'); 3 var moment = require('moment');
4 var momentFormat = 'YYYY-MM-DD HH:mm:ss'; 4 var momentFormat = 'YYYY-MM-DD HH:mm:ss';
5 5
6 module.exports = MatrixUtil; 6 module.exports = MatrixUtil;
7 7
8 function _cleanPartnerId(partnerId) { 8 function _cleanPartnerId(partnerId) {
9 let cleaned = partnerId; 9 let cleaned = partnerId;
10 10
11 try { 11 try {
12 cleaned = cleaned.toLocaleString(); 12 cleaned = cleaned.toLocaleString();
13 cleaned = cleaned.trim().toLowerCase(); 13 cleaned = cleaned.trim().toLowerCase();
14 } catch(e) { 14 } catch(e) {
15 return partnerId; 15 return partnerId;
16 } 16 }
17 17
18 return cleaned; 18 return cleaned;
19 } 19 }
20 20
21 function MatrixUtil(options) { 21 function MatrixUtil(options) {
22 if (!options) { 22 if (!options) {
23 console.trace('Undefined options'); 23 console.trace('Undefined options');
24 process.exit(1); 24 process.exit(1);
25 } 25 }
26 26
27 this.matrix = options.matrix; 27 this.matrix = options.matrix;
28 if (!this.matrix) { 28 if (!this.matrix) {
29 console.trace("Matrix not set"); 29 console.trace("Matrix not set");
30 process.exit(1); 30 process.exit(1);
31 } 31 }
32 32
33 this.logger = options.logger; 33 this.logger = options.logger;
34 if (!this.logger) { 34 if (!this.logger) {
35 console.trace("Logger not set"); 35 console.trace("Logger not set");
36 process.exit(1); 36 process.exit(1);
37 } 37 }
38 } 38 }
39 39
40 MatrixUtil.prototype.updateBuddyState = function(jid, state, statusText, resource) { 40 MatrixUtil.prototype.updateBuddyState = function(jid, state, statusText, resource) {
41 if (!jid) {return; } 41 if (!jid) {return; }
42 if (jid == 'undefined') {return; } 42 if (jid == 'undefined') {return; }
43 43
44 jid = _cleanPartnerId(jid); 44 jid = _cleanPartnerId(jid);
45 45
46 if (!resource) { 46 if (!resource) {
47 resource = 'undefined'; 47 resource = 'undefined';
48 } 48 }
49 49
50 let logger = this.logger; 50 let logger = this.logger;
51 let matrix = this.matrix; 51 let matrix = this.matrix;
52 52
53 logger.verbose('Buddy state change', {jid: jid, state: state, statusText: statusText, resource: resource}); 53 logger.verbose('Buddy state change', {jid: jid, state: state, statusText: statusText, resource: resource});
54 54
55 if (!matrix) { 55 if (!matrix) {
56 return; 56 return;
57 } 57 }
58 58
59 if (!matrix.buddies) { 59 if (!matrix.buddies) {
60 matrix.buddies = {}; 60 matrix.buddies = {};
61 } 61 }
62 62
63 if (!matrix.buddies[jid]) { 63 if (!matrix.buddies[jid]) {
64 matrix.buddies[jid] = {resources: {}}; 64 matrix.buddies[jid] = {resources: {}};
65 } 65 }
66 66
67 try { 67 try {
68 matrix.buddies[jid]['resources'][resource] = { 68 matrix.buddies[jid]['resources'][resource] = {
69 state: state, 69 state: state,
70 statusText: statusText, 70 statusText: statusText,
71 last_update: moment().format(momentFormat) 71 last_update: moment().format(momentFormat)
72 } 72 }
73 } 73 }
74 catch(e) { 74 catch(e) {
75 logger.warn('MatrixUtil: Exception on update resources on matrix', {jid: jid, state: state, statusText: statusText, resource: resource}); 75 logger.warn('MatrixUtil: Exception on update resources on matrix', {jid: jid, state: state, statusText: statusText, resource: resource});
76 } 76 }
77 77
78 if (resource != 'undefined' && matrix.buddies[jid].resources['undefined']) { 78 if (resource != 'undefined' && matrix.buddies[jid].resources['undefined']) {
79 try { 79 try {
80 delete matrix.buddies[jid].resources['undefined']; 80 delete matrix.buddies[jid].resources['undefined'];
81 } 81 }
82 catch(e) {}; 82 catch(e) {};
83 } 83 }
84 } 84 }
85 85
86 MatrixUtil.prototype.isAFriend = function(jid) { 86 MatrixUtil.prototype.isAFriend = function(jid) {
87 if (!jid) { return; } 87 if (!jid) { return; }
88 88
89 jid = _cleanPartnerId(jid); 89 jid = _cleanPartnerId(jid);
90 90
91 let matrix = this.matrix; 91 let matrix = this.matrix;
92 92
93 if (!matrix) { return false; }; 93 if (!matrix) { return false; };
94 if (!matrix.buddies) { return false; } 94 if (!matrix.buddies) { return false; }
95 if (!matrix.buddies[jid]) { return false; } 95 if (!matrix.buddies[jid]) { return false; }
96 96
97 return true; 97 return true;
98 } 98 }
99 99
100 MatrixUtil.prototype.isPartnerOffline = function(partner) { 100 MatrixUtil.prototype.isPartnerOffline = function(partner) {
101 if (!partner) { return; } 101 if (!partner) { return; }
102 102
103 partner = _cleanPartnerId(partner); 103 partner = _cleanPartnerId(partner);
104 104
105 let matrix = this.matrix; 105 let matrix = this.matrix;
106 let logger = this.logger; 106 let logger = this.logger;
107 107
108 if (!matrix) { return false; } 108 if (!matrix) { return false; }
109 109
110 if (!matrix.buddies[partner]) { return false; } 110 if (!matrix.buddies[partner]) { return false; }
111 if (!matrix.buddies[partner].resources) { return false; }; 111 if (!matrix.buddies[partner].resources) { return false; };
112 112
113 let resources = matrix.buddies[partner].resources; 113 let resources = matrix.buddies[partner].resources;
114 for (let key in resources) { 114 for (let key in resources) {
115 if (resources.hasOwnProperty(key)) { 115 if (resources.hasOwnProperty(key)) {
116 let resource = resources[key]; 116 let resource = resources[key];
117 if (resources[key].state == 'online') { 117 if (resources[key].state == 'online') {
118 return false; 118 return false;
119 } 119 }
120 } 120 }
121 } 121 }
122 logger.verbose('Offline partner detected: ' + partner); 122 logger.verbose('Offline partner detected: ' + partner);
123 return true; 123 return true;
124 } 124 }
125 125
126 MatrixUtil.prototype._updateLastResponseTime = function(partner) { 126 MatrixUtil.prototype._updateLastResponseTime = function(partner) {
127 let matrix = this.matrix; 127 let matrix = this.matrix;
128 let logger = this.logger; 128 let logger = this.logger;
129 129
130 try { 130 try {
131 if (!matrix.buddies[partner]['last_outgoing']) { 131 if (!matrix.buddies[partner]['last_outgoing']) {
132 logger.verbose('No outgoing yet, skip updateLastResponseTime'); 132 logger.verbose('No outgoing yet, skip updateLastResponseTime');
133 return; 133 return;
134 } 134 }
135 135
136 if (!matrix.buddies[partner]['last_outgoing']['last_update_ts']) { 136 if (!matrix.buddies[partner]['last_outgoing']['last_update_ts']) {
137 logger.verbose('No outgoing timestamp yet, skip updateLastResponseTime'); 137 logger.verbose('No outgoing timestamp yet, skip updateLastResponseTime');
138 return; 138 return;
139 } 139 }
140 } 140 }
141 catch(e) { 141 catch(e) {
142 logger.warn('Exception when preparing updateLastResponseTime', {err: e}); 142 logger.warn('Exception when preparing updateLastResponseTime', {err: e});
143 return;
143 } 144 }
144 145
145 try { 146 try {
146 if ( 147 if (
147 matrix.buddies[partner]['last_incoming'] 148 matrix.buddies[partner]['last_incoming']
148 && (Number(matrix.buddies[partner]['last_incoming']['last_update_ts']) > Number(matrix.buddies[partner]['last_outgoing']['last_update_ts'])) 149 && (Number(matrix.buddies[partner]['last_incoming']['last_update_ts']) > Number(matrix.buddies[partner]['last_outgoing']['last_update_ts']))
149 ) { 150 ) {
150 return; 151 return;
151 } 152 }
152 } 153 }
153 catch(e) { 154 catch(e) {
154 logger.warn('Exception when checking if last_incoming > last_outgoing', {err: e}); 155 logger.warn('Exception when checking if last_incoming > last_outgoing', {err: e});
156 return;
155 } 157 }
156 158
157 159
158 try { 160 try {
159 let delta = Date.now() - Number(matrix.buddies[partner]['last_outgoing']['last_update_ts']); 161 let delta = Date.now() - Number(matrix.buddies[partner]['last_outgoing']['last_update_ts']);
160 logger.verbose('Response time in ' + delta + 'ms'); 162 logger.verbose('Response time in ' + delta + 'ms');
161 matrix.buddies[partner]['last_response_time'] = Math.round(delta/1000); 163 matrix.buddies[partner]['last_response_time'] = Math.round(delta/1000);
162 } 164 }
163 catch(e) { 165 catch(e) {
164 logger.warn('Exception when calculating last_response_time', {err: e}); 166 logger.warn('Exception when calculating last_response_time', {err: e});
165 } 167 }
166 168
167 } 169 }
168 170
169 MatrixUtil.prototype._updateLastMessage = function(partner, msg, direction) { 171 MatrixUtil.prototype._updateLastMessage = function(partner, msg, direction) {
170 if (!partner) { return; } 172 if (!partner) { return; }
171 partner = _cleanPartnerId(partner); 173 partner = _cleanPartnerId(partner);
172 174
173 let matrix = this.matrix; 175 let matrix = this.matrix;
174 let logger = this.logger; 176 let logger = this.logger;
175 177
176 if (!matrix) { 178 if (!matrix) {
177 return; 179 return;
178 } 180 }
179 181
180 if (!matrix.buddies) { 182 if (!matrix.buddies) {
181 matrix.buddies = {}; 183 matrix.buddies = {};
182 } 184 }
183 185
184 if (!matrix.buddies[partner]) { 186 if (!matrix.buddies[partner]) {
185 matrix.buddies[partner] = {}; 187 matrix.buddies[partner] = {};
186 } 188 }
187 189
188 if (direction == 'incoming') { 190 if (direction == 'incoming') {
189 try { 191 try {
190 _updateLastResponseTime(partner); 192 _updateLastResponseTime(partner);
191 } 193 }
192 catch(e) { 194 catch(e) {
193 logger.warn('Exception when updateLastResponseTime', {err: e}); 195 logger.warn('Exception when updateLastResponseTime', {err: e});
194 } 196 }
195 } 197 }
196 198
197 matrix.buddies[partner]['last_' + direction] = { 199 matrix.buddies[partner]['last_' + direction] = {
198 msg: msg, 200 msg: msg,
199 last_update: moment().format(momentFormat), 201 last_update: moment().format(momentFormat),
200 last_update_ts: Date.now() 202 last_update_ts: Date.now()
201 } 203 }
202 204
203 if (direction == 'outgoing') { 205 if (direction == 'outgoing') {
204 return; 206 return;
205 } 207 }
206 208
207 209
208 } 210 }
209 211
210 212
211 MatrixUtil.prototype.updateLastIncoming = function(partner, msg) { 213 MatrixUtil.prototype.updateLastIncoming = function(partner, msg) {
212 this._updateLastMessage(partner, msg, 'incoming'); 214 this._updateLastMessage(partner, msg, 'incoming');
213 } 215 }
214 216
215 MatrixUtil.prototype.updateLastOutgoing = function(partner, msg) { 217 MatrixUtil.prototype.updateLastOutgoing = function(partner, msg) {
216 this._updateLastMessage(partner, msg, 'outgoing'); 218 this._updateLastMessage(partner, msg, 'outgoing');
217 } 219 }
218 220