Commit b748823ddfa1b2b9a3161c46fbefed8c88ccde41

Authored by Adhidarma Hadiwinoto
1 parent 94a22ccdaa
Exists in master

delta

Showing 1 changed file with 5 additions and 1 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 129
129 if (!matrix.buddies[partner]['last_outgoing']) { 130 if (!matrix.buddies[partner]['last_outgoing']) {
131 logger.verbose('No outgoing yet, skip updateLastResponseTime');
130 return; 132 return;
131 } 133 }
132 134
133 if (!matrix.buddies[partner]['last_outgoing'].last_update_ts) { 135 if (!matrix.buddies[partner]['last_outgoing'].last_update_ts) {
136 logger.verbose('No outgoing timestamp yet, skip updateLastResponseTime');
134 return; 137 return;
135 } 138 }
136 139
137 if ( 140 if (
138 matrix.buddies[partner]['last_incoming'] 141 matrix.buddies[partner]['last_incoming']
139 && (matrix.buddies[partner]['last_incoming']['last_update_ts'] > matrix.buddies[partner]['last_outgoing']['last_update_ts']) 142 && (matrix.buddies[partner]['last_incoming']['last_update_ts'] > matrix.buddies[partner]['last_outgoing']['last_update_ts'])
140 ) { 143 ) {
141 return; 144 return;
142 } 145 }
143 146
144 matrix.buddies[partner]['last_response_time'] = Math.round((Date.now() - matrix.buddies[partner]['last_outgoing']['last_update_ts'])/1000); 147 let delta = Date.now() - matrix.buddies[partner]['last_outgoing']['last_update_ts'];
148 matrix.buddies[partner]['last_response_time'] = Math.round(delta/1000);
145 } 149 }
146 150
147 MatrixUtil.prototype._updateLastMessage = function(partner, msg, direction) { 151 MatrixUtil.prototype._updateLastMessage = function(partner, msg, direction) {
148 if (!partner) { return; } 152 if (!partner) { return; }
149 partner = _cleanPartnerId(partner); 153 partner = _cleanPartnerId(partner);
150 154
151 let matrix = this.matrix; 155 let matrix = this.matrix;
152 let logger = this.logger; 156 let logger = this.logger;
153 157
154 if (!matrix) { 158 if (!matrix) {
155 return; 159 return;
156 } 160 }
157 161
158 if (!matrix.buddies) { 162 if (!matrix.buddies) {
159 matrix.buddies = {}; 163 matrix.buddies = {};
160 } 164 }
161 165
162 if (!matrix.buddies[partner]) { 166 if (!matrix.buddies[partner]) {
163 matrix.buddies[partner] = {}; 167 matrix.buddies[partner] = {};
164 } 168 }
165 169
166 if (direction == 'incoming') { 170 if (direction == 'incoming') {
167 try { 171 try {
168 _updateLastResponseTime(partner); 172 _updateLastResponseTime(partner);
169 } 173 }
170 catch(e) { 174 catch(e) {
171 logger.warn('Exception when updateLastResponseTime', {err: e}); 175 logger.warn('Exception when updateLastResponseTime', {err: e});
172 } 176 }
173 } 177 }
174 178
175 matrix.buddies[partner]['last_' + direction] = { 179 matrix.buddies[partner]['last_' + direction] = {
176 msg: msg, 180 msg: msg,
177 last_update: moment().format(momentFormat), 181 last_update: moment().format(momentFormat),
178 last_update_ts: Date.now() 182 last_update_ts: Date.now()
179 } 183 }
180 184
181 if (direction == 'outgoing') { 185 if (direction == 'outgoing') {
182 return; 186 return;
183 } 187 }
184 188
185 189
186 } 190 }
187 191
188 192
189 MatrixUtil.prototype.updateLastIncoming = function(partner, msg) { 193 MatrixUtil.prototype.updateLastIncoming = function(partner, msg) {
190 this._updateLastMessage(partner, msg, 'incoming'); 194 this._updateLastMessage(partner, msg, 'incoming');
191 } 195 }
192 196
193 MatrixUtil.prototype.updateLastOutgoing = function(partner, msg) { 197 MatrixUtil.prototype.updateLastOutgoing = function(partner, msg) {
194 this._updateLastMessage(partner, msg, 'outgoing'); 198 this._updateLastMessage(partner, msg, 'outgoing');
195 } 199 }
196 200