Mercurial > psgxs
comparison storage.js @ 26:f2e9365bc8ec
Can use differents backends for node storage. Writing directory-based backend in top of file-based backend.
author | Emmanuel Gil Peyrot <linkmauve@linkmauve.fr> |
---|---|
date | Mon, 01 Nov 2010 02:39:58 +0100 |
parents | 9a6b8b3357c6 |
children | 62cbb1c49bc5 |
comparison
equal
deleted
inserted
replaced
25:c774f2ffb271 | 26:f2e9365bc8ec |
---|---|
15 * | 15 * |
16 * You should have received a copy of the GNU Affero General Public License | 16 * You should have received a copy of the GNU Affero General Public License |
17 * along with PSĜS. If not, see <http://www.gnu.org/licenses/>. | 17 * along with PSĜS. If not, see <http://www.gnu.org/licenses/>. |
18 */ | 18 */ |
19 | 19 |
20 var fs = require('fs'); | |
20 var sha1hex = require('sha1').hex; | 21 var sha1hex = require('sha1').hex; |
21 require('./iso8601'); | 22 require('./iso8601'); |
22 var errors = require('./errors'); | 23 var errors = require('./errors'); |
23 var config = require('./configuration'); | 24 var config = require('./configuration'); |
24 var service_configuration = config.service_configuration; | 25 var service_configuration = config.service_configuration; |
28 | 29 |
29 var nodes = require('./nodes'); | 30 var nodes = require('./nodes'); |
30 var Node = nodes.Node; | 31 var Node = nodes.Node; |
31 var Item = nodes.Item; | 32 var Item = nodes.Item; |
32 | 33 |
33 var fs = require('fs'); | |
34 | |
35 var list; | |
36 | |
37 var storage = exports; | 34 var storage = exports; |
35 | |
36 var backend = require('./backends/' + config.backend); | |
37 storage.load = backend.load; | |
38 storage.save = backend.save; | |
39 | |
38 storage.createNode = function(nodeID, params) { | 40 storage.createNode = function(nodeID, params) { |
39 for (var i in list) | 41 for (var i in backend.list) |
40 if (i == nodeID) | 42 if (i == nodeID) |
41 return errors.owner.create.nodeid_already_exists.n; | 43 return errors.owner.create.nodeid_already_exists.n; |
42 | 44 |
43 list[nodeID] = new Node(params); | 45 backend.list[nodeID] = new Node(params); |
44 storage.save(); | 46 storage.save(); |
45 return errors.success; | 47 return errors.success; |
46 }; | 48 }; |
47 | 49 |
48 storage.getMetadata = function(nodeID) { | 50 storage.getMetadata = function(nodeID) { |
117 var n; | 119 var n; |
118 var items = {}; | 120 var items = {}; |
119 if (node) | 121 if (node) |
120 n = storage.getNode(node).items; | 122 n = storage.getNode(node).items; |
121 else | 123 else |
122 n = list; | 124 n = backend.list; |
123 | 125 |
124 for (var i in n) { | 126 for (var i in n) { |
125 var type; | 127 var type; |
126 if (n[i].content) | 128 if (n[i].content) |
127 type = 'item'; | 129 type = 'item'; |
131 } | 133 } |
132 return items; | 134 return items; |
133 }; | 135 }; |
134 | 136 |
135 storage.getNode = function(nodeID) { | 137 storage.getNode = function(nodeID) { |
136 if (list[nodeID]) | 138 if (backend.list[nodeID]) |
137 return list[nodeID]; | 139 return backend.list[nodeID]; |
138 return errors.node_does_not_exist.n; | 140 return errors.node_does_not_exist.n; |
139 }; | 141 }; |
140 | 142 |
141 storage.existsNode = function(nodeID) { | 143 storage.existsNode = function(nodeID) { |
142 if (list[nodeID]) | 144 if (backend.list[nodeID]) |
143 return true; | 145 return true; |
144 return false; | 146 return false; |
145 }; | 147 }; |
146 | 148 |
147 storage.purgeNode = function(nodeID) { | 149 storage.purgeNode = function(nodeID) { |
167 storage.deleteNodeWithRedirect = function(nodeID, uri) { | 169 storage.deleteNodeWithRedirect = function(nodeID, uri) { |
168 var del = storage.deleteNode(nodeID); | 170 var del = storage.deleteNode(nodeID); |
169 if (typeof del == 'number') | 171 if (typeof del == 'number') |
170 return del; | 172 return del; |
171 | 173 |
172 list[nodeID] = uri; | 174 backend.list[nodeID] = uri; |
173 storage.save(); | 175 storage.save(); |
174 return errors.success; | 176 return errors.success; |
175 }; | 177 }; |
176 | 178 |
177 storage.deleteNode = function(nodeID) { | 179 storage.deleteNode = function(nodeID) { |
189 notifs[node.publisher[i]] = {}; | 191 notifs[node.publisher[i]] = {}; |
190 if (config.enabled('publish-only-affiliation')) | 192 if (config.enabled('publish-only-affiliation')) |
191 for (var i in node.publishOnly) | 193 for (var i in node.publishOnly) |
192 notifs[node.publishOnly[i]] = {}; | 194 notifs[node.publishOnly[i]] = {}; |
193 | 195 |
194 delete list[nodeID]; | 196 delete backend.list[nodeID]; |
195 storage.save(); | 197 storage.save(); |
196 return notifs; | 198 return notifs; |
197 }; | 199 }; |
198 | 200 |
199 storage.setItem = function(nodeID, itemID, content) { | 201 storage.setItem = function(nodeID, itemID, content) { |
303 var node = storage.getNode(nodeID); | 305 var node = storage.getNode(nodeID); |
304 for (var sub in node.subscribers) | 306 for (var sub in node.subscribers) |
305 if (toBareJID(sub) == jid) | 307 if (toBareJID(sub) == jid) |
306 return node.subscribers[sub] | 308 return node.subscribers[sub] |
307 } else { | 309 } else { |
308 for (var node in list) { | 310 for (var node in backend.list) { |
309 for (var sub in list[node].subscribers) { | 311 for (var sub in backend.list[node].subscribers) { |
310 if (toBareJID(sub) == jid) | 312 if (toBareJID(sub) == jid) |
311 subs[node] = list[node].subscribers[sub]; | 313 subs[node] = backend.list[node].subscribers[sub]; |
312 } | 314 } |
313 } | 315 } |
314 } | 316 } |
315 return subs; | 317 return subs; |
316 }; | 318 }; |
411 return 'none'; | 413 return 'none'; |
412 }; | 414 }; |
413 | 415 |
414 storage.getAffiliationsFromJID = function(jid) { | 416 storage.getAffiliationsFromJID = function(jid) { |
415 var affils = {}; | 417 var affils = {}; |
416 for (var nodeID in list) { | 418 for (var nodeID in backend.list) { |
417 var node = list[nodeID]; | 419 var node = backend.list[nodeID]; |
418 for (var affil in config.owner) | 420 for (var affil in config.owner) |
419 if (typeof affil == 'string' && config.owner[affil] == jid) { | 421 if (typeof affil == 'string' && config.owner[affil] == jid) { |
420 affils[nodeID] = 'super-owner'; | 422 affils[nodeID] = 'super-owner'; |
421 break; | 423 break; |
422 } | 424 } |
486 if (typeof jid == 'string') | 488 if (typeof jid == 'string') |
487 affils[node.outcast[jid]] = 'outcast'; | 489 affils[node.outcast[jid]] = 'outcast'; |
488 return affils; | 490 return affils; |
489 }; | 491 }; |
490 | 492 |
491 storage.save = function(file) { | |
492 function sanitize(o) { | |
493 var n = {}; | |
494 for (var i in o) { | |
495 if (i == 'content' || o[i].setISO8601) | |
496 n[i] = o[i].toString(); | |
497 else if (o[i] instanceof Array) | |
498 n[i] = o[i]; | |
499 else if (typeof o[i] == 'object') | |
500 n[i] = sanitize(o[i]); | |
501 else if (typeof o[i] == 'function') | |
502 ; | |
503 else | |
504 n[i] = o[i]; | |
505 } | |
506 return n; | |
507 } | |
508 | |
509 var data = sanitize(list); | |
510 | |
511 if (!file) | |
512 file = 'save.json'; | |
513 | |
514 fs.writeFile(file, require('sys').inspect(data, null, null)); | |
515 } | |
516 | |
517 storage.load = function(file) { | |
518 var xmpp = require('xmpp'); | |
519 function parseStanza(path, content) { | |
520 var stanza = null; | |
521 var stream = new xmpp.Stream({ | |
522 stanza: function (stanza) { | |
523 path[content] = stanza; | |
524 } | |
525 }); | |
526 stream.opened = true; | |
527 stream.data(path[content]); | |
528 } | |
529 | |
530 function endParsing(o) { | |
531 var regexp = /\d{4}-\d\d-\d\dT\d\d:\d\d:\d\dZ/; | |
532 for (var i in o) { | |
533 if (typeof o[i] == 'string' && i == 'content') | |
534 parseStanza(o, i); | |
535 else if (typeof o[i] == 'string' && regexp(o[i])) { | |
536 var today = new Date(); | |
537 today.setISO8601(o[i]); | |
538 o[i] = today; | |
539 } else if (typeof o[i] == 'object') | |
540 endParsing(o[i]); | |
541 } | |
542 return o; | |
543 } | |
544 | |
545 if (!file) | |
546 file = 'save.json'; | |
547 | |
548 var data = fs.readFileSync(file); | |
549 var obj = eval('('+data+')'); | |
550 list = endParsing(obj); | |
551 | |
552 for (var i in list) | |
553 for (var j in Node.prototype) | |
554 list[i][j] = Node.prototype[j]; | |
555 } | |
556 | |
557 storage.debug = function() { | 493 storage.debug = function() { |
558 console.log('\033[1;33m' + require('sys').inspect(list, null, null) + '\033[0m'); | 494 console.log('\033[1;33m' + require('sys').inspect(backend.list, null, null) + '\033[0m'); |
559 }; | 495 }; |