Commit 9d69d361fceb8b3d55866f97669cb48ea40cf468
1 parent
b56e7b1fa4
Exists in
master
momentFormat
Showing 1 changed file with 4 additions and 2 deletions Inline Diff
matrix-util.js
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 | 5 | ||
5 | module.exports = MatrixUtil; | 6 | module.exports = MatrixUtil; |
6 | 7 | ||
7 | function _cleanPartnerId(partnerId) { | 8 | function _cleanPartnerId(partnerId) { |
8 | let cleaned = partnerId; | 9 | let cleaned = partnerId; |
9 | 10 | ||
10 | try { | 11 | try { |
11 | cleaned = cleaned.toLocaleString(); | 12 | cleaned = cleaned.toLocaleString(); |
12 | cleaned = cleaned.trim().toLowerCase(); | 13 | cleaned = cleaned.trim().toLowerCase(); |
13 | } catch(e) { | 14 | } catch(e) { |
14 | return partnerId; | 15 | return partnerId; |
15 | } | 16 | } |
16 | 17 | ||
17 | return cleaned; | 18 | return cleaned; |
18 | } | 19 | } |
19 | 20 | ||
20 | function MatrixUtil(options) { | 21 | function MatrixUtil(options) { |
21 | if (!options) { | 22 | if (!options) { |
22 | console.trace('Undefined options'); | 23 | console.trace('Undefined options'); |
23 | process.exit(1); | 24 | process.exit(1); |
24 | } | 25 | } |
25 | 26 | ||
26 | this.matrix = options.matrix; | 27 | this.matrix = options.matrix; |
27 | if (!this.matrix) { | 28 | if (!this.matrix) { |
28 | console.trace("Matrix not set"); | 29 | console.trace("Matrix not set"); |
29 | process.exit(1); | 30 | process.exit(1); |
30 | } | 31 | } |
31 | 32 | ||
32 | this.logger = options.logger; | 33 | this.logger = options.logger; |
33 | if (!this.logger) { | 34 | if (!this.logger) { |
34 | console.trace("Logger not set"); | 35 | console.trace("Logger not set"); |
35 | process.exit(1); | 36 | process.exit(1); |
36 | } | 37 | } |
37 | } | 38 | } |
38 | 39 | ||
39 | MatrixUtil.prototype.updateBuddyState = function(jid, state, statusText, resource) { | 40 | MatrixUtil.prototype.updateBuddyState = function(jid, state, statusText, resource) { |
40 | if (!jid) {return; } | 41 | if (!jid) {return; } |
41 | if (jid == 'undefined') {return; } | 42 | if (jid == 'undefined') {return; } |
42 | 43 | ||
43 | jid = _cleanPartnerId(jid); | 44 | jid = _cleanPartnerId(jid); |
44 | 45 | ||
45 | if (!resource) { | 46 | if (!resource) { |
46 | resource = 'undefined'; | 47 | resource = 'undefined'; |
47 | } | 48 | } |
48 | 49 | ||
49 | let logger = this.logger; | 50 | let logger = this.logger; |
50 | let matrix = this.matrix; | 51 | let matrix = this.matrix; |
51 | 52 | ||
52 | 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}); |
53 | 54 | ||
54 | if (!matrix) { | 55 | if (!matrix) { |
55 | return; | 56 | return; |
56 | } | 57 | } |
57 | 58 | ||
58 | if (!matrix.buddies) { | 59 | if (!matrix.buddies) { |
59 | matrix.buddies = {}; | 60 | matrix.buddies = {}; |
60 | } | 61 | } |
61 | 62 | ||
62 | if (!matrix.buddies[jid]) { | 63 | if (!matrix.buddies[jid]) { |
63 | matrix.buddies[jid] = {resources: {}}; | 64 | matrix.buddies[jid] = {resources: {}}; |
64 | } | 65 | } |
65 | 66 | ||
66 | try { | 67 | try { |
67 | matrix.buddies[jid]['resources'][resource] = { | 68 | matrix.buddies[jid]['resources'][resource] = { |
68 | state: state, | 69 | state: state, |
69 | statusText: statusText, | 70 | statusText: statusText, |
70 | last_update: moment().format('YYYY-MM-DD HH:mm:ss') | 71 | last_update: moment().format(momentFormat) |
71 | } | 72 | } |
72 | } | 73 | } |
73 | catch(e) { | 74 | catch(e) { |
74 | 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}); |
75 | } | 76 | } |
76 | 77 | ||
77 | if (resource != 'undefined' && matrix.buddies[jid].resources['undefined']) { | 78 | if (resource != 'undefined' && matrix.buddies[jid].resources['undefined']) { |
78 | try { | 79 | try { |
79 | delete matrix.buddies[jid].resources['undefined']; | 80 | delete matrix.buddies[jid].resources['undefined']; |
80 | } | 81 | } |
81 | catch(e) {}; | 82 | catch(e) {}; |
82 | } | 83 | } |
83 | } | 84 | } |
84 | 85 | ||
85 | MatrixUtil.prototype.isAFriend = function(jid) { | 86 | MatrixUtil.prototype.isAFriend = function(jid) { |
86 | if (!jid) { return; } | 87 | if (!jid) { return; } |
87 | 88 | ||
88 | jid = _cleanPartnerId(jid); | 89 | jid = _cleanPartnerId(jid); |
89 | 90 | ||
90 | let matrix = this.matrix; | 91 | let matrix = this.matrix; |
91 | 92 | ||
92 | if (!matrix) { return false; }; | 93 | if (!matrix) { return false; }; |
93 | if (!matrix.buddies) { return false; } | 94 | if (!matrix.buddies) { return false; } |
94 | if (!matrix.buddies[jid]) { return false; } | 95 | if (!matrix.buddies[jid]) { return false; } |
95 | 96 | ||
96 | return true; | 97 | return true; |
97 | } | 98 | } |
98 | 99 | ||
99 | MatrixUtil.prototype.isPartnerOffline = function(partner) { | 100 | MatrixUtil.prototype.isPartnerOffline = function(partner) { |
100 | if (!partner) { return; } | 101 | if (!partner) { return; } |
101 | 102 | ||
102 | partner = _cleanPartnerId(partner); | 103 | partner = _cleanPartnerId(partner); |
103 | 104 | ||
104 | let matrix = this.matrix; | 105 | let matrix = this.matrix; |
105 | let logger = this.logger; | 106 | let logger = this.logger; |
106 | 107 | ||
107 | if (!matrix) { return false; } | 108 | if (!matrix) { return false; } |
108 | 109 | ||
109 | if (!matrix.buddies[partner]) { return false; } | 110 | if (!matrix.buddies[partner]) { return false; } |
110 | if (!matrix.buddies[partner].resources) { return false; }; | 111 | if (!matrix.buddies[partner].resources) { return false; }; |
111 | 112 | ||
112 | let resources = matrix.buddies[partner].resources; | 113 | let resources = matrix.buddies[partner].resources; |
113 | for (let key in resources) { | 114 | for (let key in resources) { |
114 | if (resources.hasOwnProperty(key)) { | 115 | if (resources.hasOwnProperty(key)) { |
115 | let resource = resources[key]; | 116 | let resource = resources[key]; |
116 | if (resources[key].state == 'online') { | 117 | if (resources[key].state == 'online') { |
117 | return false; | 118 | return false; |
118 | } | 119 | } |
119 | } | 120 | } |
120 | } | 121 | } |
121 | logger.verbose('Offline partner detected: ' + partner); | 122 | logger.verbose('Offline partner detected: ' + partner); |
122 | return true; | 123 | return true; |
123 | } | 124 | } |
124 | 125 | ||
125 | MatrixUtil.prototype._updateLastMessage = function(partner, msg, direction) { | 126 | MatrixUtil.prototype._updateLastMessage = function(partner, msg, direction) { |
126 | if (!partner) { return; } | 127 | if (!partner) { return; } |
127 | partner = _cleanPartnerId(partner); | 128 | partner = _cleanPartnerId(partner); |
128 | 129 | ||
129 | let matrix = this.matrix; | 130 | let matrix = this.matrix; |
130 | 131 | ||
131 | if (!matrix) { | 132 | if (!matrix) { |
132 | return; | 133 | return; |
133 | } | 134 | } |
134 | 135 | ||
135 | if (!matrix.buddies) { | 136 | if (!matrix.buddies) { |
136 | matrix.buddies = {}; | 137 | matrix.buddies = {}; |
137 | } | 138 | } |
138 | 139 | ||
139 | if (!matrix.buddies[partner]) { | 140 | if (!matrix.buddies[partner]) { |
140 | matrix.buddies[partner] = {}; | 141 | matrix.buddies[partner] = {}; |
141 | } | 142 | } |
142 | 143 | ||
143 | matrix.buddies[partner]['last_' + direction] = { | 144 | matrix.buddies[partner]['last_' + direction] = { |
144 | msg: msg, | 145 | msg: msg, |
145 | last_update: moment().format('YYYY-MM-DD HH:mm:ss') | 146 | last_update: moment().format(momentFormat), |
147 | last_update_ts: Date.now() | ||
146 | } | 148 | } |
147 | } | 149 | } |
148 | 150 | ||
149 | 151 | ||
150 | MatrixUtil.prototype.updateLastIncoming = function(partner, msg) { | 152 | MatrixUtil.prototype.updateLastIncoming = function(partner, msg) { |
151 | this._updateLastMessage(partner, msg, 'incoming'); | 153 | this._updateLastMessage(partner, msg, 'incoming'); |
152 | } | 154 | } |
153 | 155 | ||
154 | MatrixUtil.prototype.updateLastOutgoing = function(partner, msg) { | 156 | MatrixUtil.prototype.updateLastOutgoing = function(partner, msg) { |
155 | this._updateLastMessage(partner, msg, 'outgoing'); | 157 | this._updateLastMessage(partner, msg, 'outgoing'); |
156 | } | 158 | } |
157 | 159 |