/usr/share/buddycloud-server/local/model_postgres.js is in buddycloud-server 0.3.5-2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 | // Generated by CoffeeScript 1.3.3
(function() {
var LOST_TRANSACTION_TIMEOUT, Transaction, async, connectDB, dbIsAvailable, debugDB, errors, logger, ltx, parseEl, pg, pool, queue, withNextDb,
__slice = [].slice,
__hasProp = {}.hasOwnProperty;
logger = require('../logger').makeLogger('local/model_postgres');
pg = require("pg");
ltx = require("ltx");
async = require("async");
errors = require("../errors");
pool = [];
queue = [];
debugDB = function(db) {
var oldQuery;
oldQuery = db.query;
return db.query = function(sql, params) {
logger.trace("query " + sql + " " + (JSON.stringify(params)));
return oldQuery.apply(this, arguments);
};
};
connectDB = function(config) {
var db;
db = new pg.Client(config);
debugDB(db);
db.connect();
db.on("error", function(err) {
logger.error("Postgres: " + err.message);
setTimeout(function() {
return connectDB(config);
}, Math.ceil(Math.random() * 5000));
try {
return db.end();
} catch (e) {
}
});
return db.connection.once("readyForQuery", function() {
return dbIsAvailable(db);
});
};
dbIsAvailable = function(db) {
var cb;
if ((cb = queue.shift())) {
return cb(db);
} else {
return pool.push(db);
}
};
withNextDb = function(cb) {
var db;
if ((db = pool.shift())) {
return cb(db);
} else {
return queue.push(function(db) {
return cb(db);
});
}
};
exports.start = function(config) {
var i, _i, _ref, _results;
_results = [];
for (i = _i = 0, _ref = config.poolSize || 4; 0 <= _ref ? _i <= _ref : _i >= _ref; i = 0 <= _ref ? ++_i : --_i) {
_results.push(connectDB(config));
}
return _results;
};
exports.transaction = function(cb) {
return withNextDb(function(db) {
return new Transaction(db, cb);
});
};
exports.isListeningToNode = function(node, listenerJids, cb) {
var conditions, i;
i = 1;
conditions = listenerJids.map(function(listenerJid) {
i++;
return "listener = $" + i;
}).join(" OR ");
if (!conditions) {
return cb(null, false);
}
return withNextDb(function(db) {
return db.query("SELECT listener FROM subscriptions WHERE node = $1 AND (" + conditions + ") LIMIT 1", [node].concat(__slice.call(listenerJids)), function(err, res) {
var _ref;
process.nextTick(function() {
return dbIsAvailable(db);
});
return cb(err, ((res != null ? (_ref = res.rows) != null ? _ref[0] : void 0 : void 0) != null));
});
});
};
exports.nodeExists = function(node, cb) {
return withNextDb(function(db) {
return db.query("SELECT node FROM nodes WHERE node=$1", [node], function(err, res) {
var _ref;
process.nextTick(function() {
return dbIsAvailable(db);
});
if (err) {
return cb(err);
} else {
return cb(null, (res != null ? (_ref = res.rows) != null ? _ref[0] : void 0 : void 0) != null);
}
});
});
};
exports.forListeners = function(iter) {
return withNextDb(function(db) {
return db.query("SELECT DISTINCT listener FROM subscriptions WHERE listener IS NOT NULL", function(err, res) {
var _ref;
process.nextTick(function() {
return dbIsAvailable(db);
});
if (err) {
logger.error(err);
return;
}
return res != null ? (_ref = res.rows) != null ? _ref.forEach(function(row) {
return iter(row.listener);
}) : void 0 : void 0;
});
});
};
exports.getAllNodes = function(cb) {
return withNextDb(function(db) {
return db.query("SELECT node FROM nodes", function(err, res) {
var nodes, _ref;
process.nextTick(function() {
return dbIsAvailable(db);
});
if (err) {
return cb(err);
}
nodes = res != null ? (_ref = res.rows) != null ? _ref.map(function(row) {
return row.node;
}) : void 0 : void 0;
return cb(null, nodes);
});
});
};
exports.getListenerNodes = function(listener, cb) {
return db.query("SELECT DISTINCT node FROM subscriptions WHERE listener=$1", [listener], function(err, res) {
var _ref;
return cb(err, res != null ? (_ref = res.rows) != null ? _ref.map(function(row) {
return row.node;
}) : void 0 : void 0);
});
};
LOST_TRANSACTION_TIMEOUT = 60 * 1000;
Transaction = (function() {
function Transaction(db, cb) {
var timeout,
_this = this;
this.db = db;
db.query("BEGIN", [], function(err, res) {
return cb(err, _this);
});
timeout = setTimeout(function() {
logger.error("Danger: lost transaction, rolling back!");
timeout = void 0;
return _this.rollback();
}, LOST_TRANSACTION_TIMEOUT);
this.rmTimeout = function() {
if (timeout) {
clearTimeout(timeout);
return timeout = void 0;
}
};
}
Transaction.prototype.commit = function(cb) {
var _this = this;
return this.db.query("COMMIT", [], function(err, res) {
_this.rmTimeout();
process.nextTick(function() {
return dbIsAvailable(_this.db);
});
return typeof cb === "function" ? cb(err) : void 0;
});
};
Transaction.prototype.rollback = function(cb) {
var _this = this;
return this.db.query("ROLLBACK", [], function(err, res) {
_this.rmTimeout();
process.nextTick(function() {
return dbIsAvailable(_this.db);
});
return typeof cb === "function" ? cb(err) : void 0;
});
};
Transaction.prototype.validateNode = function(node) {
var db;
db = this.db;
return function(cb) {
return db.query("SELECT node FROM nodes WHERE node=$1", [node], function(err, res) {
var _ref;
if (err) {
return cb(err);
} else if ((res != null ? (_ref = res.rows) != null ? _ref[0] : void 0 : void 0) != null) {
return cb(null);
} else {
logger.warn("" + node + " does not exist!");
return cb(new errors.NotFound("Node does not exist"));
}
});
};
};
Transaction.prototype.nodeExists = function(node, cb) {
return this.db.query("SELECT node FROM nodes WHERE node=$1", [node], function(err, res) {
var exists, _ref;
if (err) {
return cb(err);
} else {
exists = (res != null ? (_ref = res.rows) != null ? _ref[0] : void 0 : void 0) != null;
if (!exists) {
logger.warn("" + node + " does not exist");
}
return cb(null, exists);
}
});
};
Transaction.prototype.createNode = function(node, cb) {
var db;
db = this.db;
return async.waterfall([
function(cb2) {
return db.query("SELECT node FROM nodes WHERE node=$1", [node], cb2);
}, function(res, cb2) {
var _ref;
if (res != null ? (_ref = res.rows) != null ? _ref[0] : void 0 : void 0) {
return cb2(null, false);
} else {
return db.query("INSERT INTO nodes (node) VALUES ($1)", [node], function(err) {
return cb2(err, true);
});
}
}
], cb);
};
Transaction.prototype.purgeNode = function(node, cb) {
var db, q;
db = this.db;
q = function(sql) {
return function(cb2) {
return db.query(sql, [node], cb2);
};
};
return async.series([q("DELETE FROM items WHERE node=$1"), q("DELETE FROM subscriptions WHERE node=$1"), q("DELETE FROM affiliations WHERE node=$1"), q("DELETE FROM node_config WHERE node=$1"), q("DELETE FROM nodes WHERE node=$1")], function(err) {
logger.info("Purged all data of node " + node);
return cb(err);
});
};
Transaction.prototype.listNodes = function(cb) {
var db;
db = this.db;
return async.waterfall([
function(cb2) {
return db.query("SELECT node FROM nodes\nWHERE node IN (SELECT node FROM open_nodes)\nORDER BY node ASC", cb2);
}, function(res, cb2) {
var nodes;
nodes = res.rows.map(function(row) {
return {
node: row.node,
title: void 0
};
});
return cb2(null, nodes);
}
], cb);
};
Transaction.prototype.getSubscription = function(node, user, cb) {
if (!node) {
return cb(new Error("No node"));
}
if (!user) {
return cb(new Error("No user"));
}
return this.db.query("SELECT subscription FROM subscriptions WHERE node=$1 AND \"user\"=$2", [node, user], function(err, res) {
var _ref, _ref1;
return cb(err, (res != null ? (_ref = res.rows) != null ? (_ref1 = _ref[0]) != null ? _ref1.subscription : void 0 : void 0 : void 0) || "none");
});
};
Transaction.prototype.getSubscriptionListener = function(node, user, cb) {
return this.db.query("SELECT listener FROM subscriptions WHERE node=$1 AND \"user\"=$2", [node, user], function(err, res) {
var _ref, _ref1;
return cb(err, (res != null ? (_ref = res.rows) != null ? (_ref1 = _ref[0]) != null ? _ref1.listener : void 0 : void 0 : void 0) || "none");
});
};
Transaction.prototype.setSubscription = function(node, user, listener, subscription, cb) {
var db, toDelete;
if (!node) {
return cb(new Error("No node"));
}
if (!user) {
return cb(new Error("No user"));
}
db = this.db;
toDelete = !subscription || subscription === "none";
return async.waterfall([
this.validateNode(node), function(cb2) {
return db.query("SELECT subscription FROM subscriptions WHERE node=$1 AND \"user\"=$2", [node, user], cb2);
}, function(res, cb2) {
var isSet, _ref;
isSet = (res != null ? (_ref = res.rows) != null ? _ref[0] : void 0 : void 0) != null;
logger.debug("setSubscription " + node + " " + user + " isSet=" + isSet + " toDelete=" + toDelete);
if (isSet && !toDelete) {
if (listener) {
return db.query("UPDATE subscriptions SET listener=$1, subscription=$2, updated=CURRENT_TIMESTAMP WHERE node=$3 AND \"user\"=$4", [listener, subscription, node, user], cb2);
} else {
return db.query("UPDATE subscriptions SET subscription=$1, updated=CURRENT_TIMESTAMP WHERE node=$2 AND \"user\"=$3", [subscription, node, user], cb2);
}
} else if (!isSet && !toDelete) {
if (listener) {
return db.query("INSERT INTO subscriptions (node, \"user\", listener, subscription, updated) VALUES ($1, $2, $3, $4, CURRENT_TIMESTAMP)", [node, user, listener, subscription], cb2);
} else {
return db.query("INSERT INTO subscriptions (node, \"user\", subscription, updated) VALUES ($1, $2, $3, CURRENT_TIMESTAMP)", [node, user, subscription], cb2);
}
} else if (isSet && toDelete) {
return db.query("DELETE FROM subscriptions WHERE node=$1 AND \"user\"=$2", [node, user], cb2);
} else if (!isSet && toDelete) {
return cb2(null);
} else {
return cb2(new Error('Invalid subscription transition'));
}
}
], function(err) {
return cb(err);
});
};
Transaction.prototype.getSubscribers = function(node, cb) {
var db;
if (!node) {
return cb(new Error("No node"));
}
db = this.db;
return async.waterfall([
function(cb2) {
return db.query("SELECT \"user\", subscription FROM subscriptions WHERE node=$1 ORDER BY updated DESC", [node], cb2);
}, function(res, cb2) {
var row, subscribers;
subscribers = (function() {
var _i, _len, _ref, _results;
_ref = res.rows;
_results = [];
for (_i = 0, _len = _ref.length; _i < _len; _i++) {
row = _ref[_i];
_results.push({
user: row.user,
subscription: row.subscription
});
}
return _results;
})();
return cb2(null, subscribers);
}
], cb);
};
Transaction.prototype.getSubscriptions = function(actor, cb) {
return this.db.query("SELECT \"user\", node, subscription FROM subscriptions WHERE \"user\"=$1 OR listener=$1 ORDER BY updated DESC", [actor], function(err, res) {
return cb(err, res != null ? res.rows : void 0);
});
};
Transaction.prototype.getPending = function(node, cb) {
var db;
db = this.db;
return async.waterfall([
function(cb2) {
return db.query("SELECT \"user\" FROM subscriptions WHERE subscription = 'pending' AND node = $1 ORDER BY updated DESC", [node], cb2);
}, function(res, cb2) {
return cb2(null, res.rows.map(function(row) {
return row.user;
}));
}
], cb);
};
Transaction.prototype.getNodeListeners = function(node, cb) {
return this.db.query("SELECT DISTINCT listener\nFROM subscriptions\nWHERE node=$1\nAND subscription='subscribed'\nAND listener IS NOT NULL", [node], function(err, res) {
var _ref;
return cb(err, res != null ? (_ref = res.rows) != null ? _ref.map(function(row) {
return row.listener;
}) : void 0 : void 0);
});
};
Transaction.prototype.getNodeModeratorListeners = function(node, cb) {
return this.db.query("SELECT DISTINCT listener\nFROM subscriptions\nWHERE node=$1\nAND listener IS NOT NULL\nAND EXISTS (SELECT affiliation\n FROM affiliations\n WHERE node=$1\n AND (affiliation='owner' OR affiliation='moderator'))", [node], function(err, res) {
var _ref;
return cb(err, res != null ? (_ref = res.rows) != null ? _ref.map(function(row) {
return row.listener;
}) : void 0 : void 0);
});
};
Transaction.prototype.walkModeratorAuthorizationRequests = function(user, iter, cb) {
return this.db.query("SELECT \"user\", node\nFROM subscriptions\nWHERE subscription='pending'\nAND node IN (SELECT node\n FROM affiliations\n WHERE \"user\"=$1\n AND (affiliation='owner' OR affiliation='moderator'))", [user], function(err, res) {
var _ref;
if (res != null) {
if ((_ref = res.rows) != null) {
_ref.forEach(function(row) {
return iter(row);
});
}
}
return cb(err);
});
};
Transaction.prototype.getUserRemoteSubscriptions = function(user, cb) {
return this.db.query("SELECT node, listener, subscription FROM subscriptions WHERE \"user\"=$1 AND listener!=$1", [user], function(err, res) {
return cb(err, res != null ? res.rows : void 0);
});
};
Transaction.prototype.clearUserSubscriptions = function(user, cb) {
return this.db.query("DELETE FROM subscriptions WHERE \"user\"=$1", [user], function(err) {
return cb(err);
});
};
Transaction.prototype.getAffiliation = function(node, user, cb) {
if (!node) {
return cb(new Error("No node"));
}
if (!user) {
return cb(new Error("No user"));
}
return this.db.query("SELECT affiliation FROM affiliations WHERE node=$1 AND \"user\"=$2", [node, user], function(err, res) {
var _ref, _ref1;
return cb(err, (res != null ? (_ref = res.rows) != null ? (_ref1 = _ref[0]) != null ? _ref1.affiliation : void 0 : void 0 : void 0) || "none");
});
};
Transaction.prototype.getListenerAffiliations = function(node, listener, cb) {
if (!node) {
return cb(new Error("No node"));
}
if (!listener) {
return cb(new Error("No user"));
}
return this.db.query("SELECT DISTINCT affiliation FROM affiliations WHERE node=$1 AND \"user\" IN (SELECT \"user\" FROM subscriptions WHERE listener=$2)", [node, listener], function(err, res) {
var _ref;
return cb(err, res != null ? (_ref = res.rows) != null ? _ref.map(function(row) {
return row.affiliation || "none";
}) : void 0 : void 0);
});
};
Transaction.prototype.setAffiliation = function(node, user, affiliation, cb) {
var db;
if (!node) {
return cb(new Error("No node"));
}
if (!user) {
return cb(new Error("No user"));
}
db = this.db;
return async.waterfall([
this.validateNode(node), function(cb2) {
return db.query("SELECT affiliation FROM affiliations WHERE node=$1 AND \"user\"=$2", [node, user], cb2);
}, function(res, cb2) {
var isSet, toDelete;
isSet = res && res.rows && res.rows[0];
toDelete = !affiliation || affiliation === "none";
if (isSet && !toDelete) {
return db.query("UPDATE affiliations SET affiliation=$1 WHERE node=$2 AND \"user\"=$3", [affiliation, node, user], cb2);
} else if (!isSet && !toDelete) {
return db.query("INSERT INTO affiliations (node, \"user\", affiliation) VALUES ($1, $2, $3)", [node, user, affiliation], cb2);
} else if (isSet && toDelete) {
return db.query("DELETE FROM affiliations WHERE node=$1 AND \"user\"=$2", [node, user], cb2);
} else if (!isSet && toDelete) {
return cb2(null);
}
}
], function(err) {
return cb(err);
});
};
Transaction.prototype.getAffiliations = function(user, cb) {
var db;
if (!user) {
return cb(new Error("No user"));
}
db = this.db;
return async.waterfall([
function(cb2) {
return db.query("SELECT node, affiliation FROM affiliations WHERE \"user\"=$1 ORDER BY updated DESC", [user], cb2);
}, function(res, cb2) {
var affiliations, row;
affiliations = (function() {
var _i, _len, _ref, _results;
_ref = res.rows;
_results = [];
for (_i = 0, _len = _ref.length; _i < _len; _i++) {
row = _ref[_i];
_results.push({
node: row.node,
affiliation: row.affiliation
});
}
return _results;
})();
return cb2(null, affiliations);
}
], cb);
};
Transaction.prototype.getAffiliated = function(node, cb) {
var db;
db = this.db;
return async.waterfall([
function(cb2) {
return db.query("SELECT \"user\", affiliation FROM affiliations WHERE node=$1 ORDER BY updated DESC", [node], cb2);
}, function(res, cb2) {
var affiliations, row;
affiliations = (function() {
var _i, _len, _ref, _results;
_ref = res.rows;
_results = [];
for (_i = 0, _len = _ref.length; _i < _len; _i++) {
row = _ref[_i];
_results.push({
user: row.user,
affiliation: row.affiliation
});
}
return _results;
})();
return cb2(null, affiliations);
}
], cb);
};
Transaction.prototype.getOutcast = function(node, cb) {
var db;
db = this.db;
return async.waterfall([
function(cb2) {
return db.query("SELECT \"user\" FROM affiliations WHERE affiliation = 'outcast' AND node = $1 ORDER BY updated DESC", [node], cb2);
}, function(res, cb2) {
return cb2(null, res.rows.map(function(row) {
return row.user;
}));
}
], cb);
};
Transaction.prototype.getOwners = function(node, cb) {
var db;
db = this.db;
return async.waterfall([
function(cb2) {
return db.query("SELECT \"user\" FROM affiliations WHERE node=$1 AND affiliation='owner' ORDER BY updated DESC", [node], cb2);
}, function(res, cb2) {
return cb2(null, res.rows.map(function(row) {
return row.user;
}));
}
], cb);
};
Transaction.prototype.getOwnersByNodePrefix = function(nodePrefix, cb) {
var db;
db = this.db;
return async.waterfall([
function(cb2) {
return db.query("SELECT DISTINCT \"user\" FROM affiliations WHERE node LIKE ($1 || '%') AND affiliation='owner'", [nodePrefix], cb2);
}, function(res, cb2) {
return cb2(null, res.rows.map(function(row) {
return row.user;
}));
}
], cb);
};
Transaction.prototype.writeItem = function(node, id, el, cb) {
var db;
db = this.db;
return async.waterfall([
this.validateNode(node), function(cb2) {
return db.query("SELECT id FROM items WHERE node=$1 AND id=$2", [node, id], cb2);
}, function(res, cb2) {
var isSet, params, updated, updated_query, xml;
isSet = res && res.rows && res.rows[0];
xml = el.toString();
params = [node, id, xml];
updated = el.getChildText('updated') || el.getChildText('published');
if (updated) {
params.push(updated);
updated_query = "$4";
} else {
updated_query = "CURRENT_TIMESTAMP";
}
if (isSet) {
return db.query("UPDATE items SET xml=$3, updated=" + updated_query + " WHERE node=$1 AND id=$2", params, cb2);
} else if (!isSet) {
return db.query("INSERT INTO items (node, id, xml, updated) VALUES ($1, $2, $3, " + updated_query + ")", params, cb2);
}
}
], cb);
};
Transaction.prototype.getItemIds = function(node, cb) {
var db;
db = this.db;
return async.waterfall([
function(cb2) {
return db.query("SELECT id FROM items WHERE node=$1 ORDER BY updated DESC", [node], cb2);
}, function(res, cb2) {
var ids;
ids = res.rows.map(function(row) {
return row.id;
});
return cb2(null, ids);
}
], cb);
};
Transaction.prototype.getItem = function(node, id, cb) {
var db;
db = this.db;
return async.waterfall([
function(cb2) {
return db.query("SELECT xml FROM items WHERE node=$1 AND id=$2", [node, id], cb2);
}, function(res, cb2) {
var el, _ref, _ref1;
if (res != null ? (_ref = res.rows) != null ? (_ref1 = _ref[0]) != null ? _ref1.xml : void 0 : void 0 : void 0) {
el = parseEl(res.rows[0].xml);
if (el != null) {
return cb2(null, el);
} else {
return cb2(new errors.InternalServerError("Item XML parse error"));
}
} else {
return cb2(new errors.NotFound("No such item"));
}
}
], cb);
};
Transaction.prototype.getUpdatesByTime = function(subscriber, timeStart, timeEnd, itemCb, cb) {
var conditions, i, params, q;
conditions = ["node IN (SELECT node FROM subscriptions WHERE \"user\"=$1 AND subscription='subscribed')"];
params = [subscriber];
i = 1;
if (timeStart) {
conditions.push("updated >= $" + (++i) + "::timestamp");
params.push(timeStart);
}
if (timeEnd) {
conditions.push("updated <= $" + (++i) + "::timestamp");
params.push(timeEnd);
}
q = this.db.query("SELECT id, node, xml FROM items WHERE " + conditions.join(" AND ") + " ORDER BY updated ASC", params);
q.on("row", function(row) {
if (item) {
return itemCb({
node: row.node,
id: row.id,
item: row.xml
});
}
});
q.on("error", function(err_) {
var err;
return err = err_;
});
return q.on("end", function() {
return cb(err);
});
};
Transaction.prototype.getConfig = function(node, cb) {
var db;
db = this.db;
return async.waterfall([
this.validateNode(node), function(cb2) {
return db.query("SELECT \"key\", \"value\" FROM node_config WHERE node=$1", [node], cb2);
}, function(res, cb2) {
var config;
if (res.rows) {
config = {};
res.rows.forEach(function(row) {
return config[row.key] = row.value;
});
return cb2(null, config);
} else {
return cb2(new errors.NotFound("No such node"));
}
}
], cb);
};
Transaction.prototype.setConfig = function(node, config, cb) {
var db;
db = this.db;
logger.debug("setConfig " + node + ": " + require("util").inspect(config));
return async.waterfall([
this.validateNode(node), function(cb2) {
var key, value;
return async.parallel((function() {
var _results;
_results = [];
for (key in config) {
if (!__hasProp.call(config, key)) continue;
value = config[key];
_results.push((function(key, value) {
return function(cb3) {
if (value != null) {
return db.query("DELETE FROM node_config WHERE node=$1 AND key=$2", [node, key], function(err) {
if (err) {
return cb(err);
}
return db.query("INSERT INTO node_config (key, value, node, updated) VALUES ($1, $2, $3, CURRENT_TIMESTAMP)", [key, value, node], cb3);
});
} else {
return cb3(null);
}
};
})(key, value));
}
return _results;
})(), function(err) {
return cb2(err);
});
}
], cb);
};
Transaction.prototype.resetConfig = function(node, cb) {
return this.db.query("DELETE FROM node_config WHERE node=$1", [node], cb);
};
Transaction.prototype.resetItems = function(node, cb) {
return this.db.query("DELETE FROM items WHERE node=$1", [node], cb);
};
Transaction.prototype.resetSubscriptions = function(node, cb) {
var _this = this;
return this.db.query("SELECT \"user\", listener FROM subscriptions WHERE node=$1 AND listener IS NOT NULL", [node], function(err, res) {
var row, userListeners, _i, _len, _ref;
if (err) {
return cb(err);
}
userListeners = {};
_ref = res.rows;
for (_i = 0, _len = _ref.length; _i < _len; _i++) {
row = _ref[_i];
userListeners[row.user] = row.listener;
}
return _this.db.query("DELETE FROM subscriptions WHERE node=$1", [node], function(err) {
return cb(err, userListeners);
});
});
};
Transaction.prototype.resetAffiliations = function(node, cb) {
return this.db.query("DELETE FROM affiliations WHERE node=$1", [node], cb);
};
Transaction.prototype.walkListenerArchive = function(listener, start, end, max, iter, cb) {
var conds, db, i, limit, params, q;
db = this.db;
params = [listener];
conds = "";
i = params.length;
if (start) {
conds += "AND updated >= $" + (i += 1) + "::timestamp";
params.push(start);
}
if (end) {
conds += " AND updated <= $" + (i += 1) + "::timestamp";
params.push(end);
}
limit = max ? (params.push(max), "LIMIT $" + (i += 1)) : "";
q = function(fields, table, cb2, mapper) {
return db.query("SELECT " + fields + ", updated FROM " + table + " WHERE node in (SELECT node FROM subscriptions WHERE listener=$1) " + conds + " ORDER BY updated DESC " + limit, params, function(err, res) {
if (err) {
return cb2(err);
}
if (mapper) {
iter(res.rows.map(mapper));
} else {
iter(res.rows);
}
return cb2();
});
};
return async.parallel([
function(cb2) {
var _this = this;
return db.query("SELECT node, MAX(updated) AS updated FROM node_config WHERE node in (SELECT node FROM subscriptions WHERE listener=$1) " + conds + " GROUP BY node ORDER BY updated DESC " + limit, params, function(err, res) {
if (err) {
return cb2(err);
}
return async.forEach(res.rows, function(row, cb3) {
var node;
node = row.node;
return db.query("SELECT key, value FROM node_config WHERE node=$1", [node], function(err, res) {
var config, _i, _len, _ref;
if (err) {
return cb3(err);
}
config = {};
_ref = res.rows;
for (_i = 0, _len = _ref.length; _i < _len; _i++) {
row = _ref[_i];
config[row.key] = row.value;
}
iter([
{
type: 'config',
node: node,
config: config
}
]);
return cb3();
});
}, cb2);
});
}, function(cb2) {
return q("node, id, xml", "items", cb2, function(row) {
return {
type: 'items',
node: row.node,
items: [
{
id: row.id,
el: parseEl(row.xml)
}
]
};
});
}, function(cb2) {
return q("node, \"user\", subscription, 'subscription' as type", "subscriptions", cb2);
}, function(cb2) {
return q("node, \"user\", affiliation, 'affiliation' as type", "affiliations", cb2);
}
], function(err) {
logger.debug('walkListenerArchive done');
return cb(err);
});
};
Transaction.prototype.getTopFollowedNodes = function(count, timespan, nodePattern, cb) {
if (timespan == null) {
timespan = "7 days";
}
if (nodePattern == null) {
nodePattern = "/user/%@%/posts";
}
return this.db.query("SELECT node,\n COUNT(user) AS count\nFROM subscriptions\nWHERE node LIKE $1\n AND node IN (SELECT node FROM open_nodes)\n AND updated >= CURRENT_TIMESTAMP - $2 :: INTERVAL\nGROUP BY node\nORDER BY count DESC\nLIMIT $3", [nodePattern, timespan, count], function(err, res) {
return cb(err, res != null ? res.rows : void 0);
});
};
Transaction.prototype.getTopPublishedNodes = function(count, timespan, nodePattern, cb) {
if (timespan == null) {
timespan = "7 days";
}
if (nodePattern == null) {
nodePattern = "/user/%@%/posts";
}
return this.db.query("SELECT node,\n COUNT(xml) AS count\nFROM items\nWHERE node LIKE $1\n AND node IN (SELECT node FROM open_nodes)\n AND updated >= CURRENT_TIMESTAMP - $2 :: INTERVAL\nGROUP BY node\nORDER BY count DESC\nLIMIT $3", [nodePattern, timespan, count], function(err, res) {
return cb(err, res != null ? res.rows : void 0);
});
};
return Transaction;
})();
parseEl = function(xml) {
try {
return ltx.parse(xml);
} catch (e) {
logger.error("Parsing " + xml + ": " + e.stack);
return void 0;
}
};
}).call(this);
|