Commit 7d5f671709bd2b82ef06ff3d9d88650ecca52056

Authored by Adhidarma Hadiwinoto
1 parent 9d69d361fc
Exists in master

updateLastResponseTime

Showing 1 changed file with 37 additions and 0 deletions Side-by-side Diff

... ... @@ -123,11 +123,33 @@ MatrixUtil.prototype.isPartnerOffline = function(partner) {
123 123 return true;
124 124 }
125 125  
  126 +MatrixUtil.prototype._updateLastResponseTime(partner) {
  127 + let matrix = this.matrix;
  128 +
  129 + if (!matrix.buddies[partner]['last_outgoing']) {
  130 + return;
  131 + }
  132 +
  133 + if (!matrix.buddies[partner]['last_outgoing'].last_update_ts) {
  134 + return;
  135 + }
  136 +
  137 + if (
  138 + matrix.buddies[partner]['last_incoming']
  139 + && (matrix.buddies[partner]['last_incoming']['last_update_ts'] > matrix.buddies[partner]['last_outgoing']['last_update_ts'])
  140 + ) {
  141 + return;
  142 + }
  143 +
  144 + matrix.buddies[partner]['last_response_time'] = Math.round((Date.now() - matrix.buddies[partner]['last_outgoing']['last_update_ts'])/1000);
  145 +}
  146 +
126 147 MatrixUtil.prototype._updateLastMessage = function(partner, msg, direction) {
127 148 if (!partner) { return; }
128 149 partner = _cleanPartnerId(partner);
129 150  
130 151 let matrix = this.matrix;
  152 + let logger = this.logger;
131 153  
132 154 if (!matrix) {
133 155 return;
... ... @@ -141,11 +163,26 @@ MatrixUtil.prototype._updateLastMessage = function(partner, msg, direction) {
141 163 matrix.buddies[partner] = {};
142 164 }
143 165  
  166 + if (direction == 'incoming') {
  167 + try {
  168 + _updateLastResponseTime(partner);
  169 + }
  170 + catch(e) {
  171 + logger.warn('Exception when updateLastResponseTime', {err: e});
  172 + }
  173 + }
  174 +
144 175 matrix.buddies[partner]['last_' + direction] = {
145 176 msg: msg,
146 177 last_update: moment().format(momentFormat),
147 178 last_update_ts: Date.now()
148 179 }
  180 +
  181 + if (direction == 'outgoing') {
  182 + return;
  183 + }
  184 +
  185 +
149 186 }
150 187  
151 188