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