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