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