Comparison

util/pubsub.lua @ 11200:bf8f2da84007

Merge 0.11->trunk
author Kim Alvefur <zash@zash.se>
date Thu, 05 Nov 2020 22:31:25 +0100
parent 10537:c5558138ce33
child 11567:c471e19a238e
comparison
equal deleted inserted replaced
11199:6c7c50a4de32 11200:bf8f2da84007
1 local events = require "util.events"; 1 local events = require "util.events";
2 local cache = require "util.cache"; 2 local cache = require "util.cache";
3 local errors = require "util.error";
3 4
4 local service_mt = {}; 5 local service_mt = {};
5 6
6 local default_config = { 7 local default_config = {
7 itemstore = function (config, _) return cache.new(config["max_items"]) end; 8 itemstore = function (config, _) return cache.new(config["max_items"]) end;
278 jid = self.config.normalize_jid(jid); 279 jid = self.config.normalize_jid(jid);
279 local old_affiliation = node_obj.affiliations[jid]; 280 local old_affiliation = node_obj.affiliations[jid];
280 node_obj.affiliations[jid] = affiliation; 281 node_obj.affiliations[jid] = affiliation;
281 282
282 if self.config.nodestore then 283 if self.config.nodestore then
283 local ok, err = save_node_to_store(self, node_obj); 284 -- TODO pass the error from storage to caller eg wrapped in an util.error
285 local ok, err = save_node_to_store(self, node_obj); -- luacheck: ignore 211/err
284 if not ok then 286 if not ok then
285 node_obj.affiliations[jid] = old_affiliation; 287 node_obj.affiliations[jid] = old_affiliation;
286 return ok, "internal-server-error"; 288 return ok, "internal-server-error";
287 end 289 end
288 end 290 end
342 else 344 else
343 self.subscriptions[normal_jid] = { [jid] = { [node] = true } }; 345 self.subscriptions[normal_jid] = { [jid] = { [node] = true } };
344 end 346 end
345 347
346 if self.config.nodestore then 348 if self.config.nodestore then
347 local ok, err = save_node_to_store(self, node_obj); 349 -- TODO pass the error from storage to caller eg wrapped in an util.error
350 local ok, err = save_node_to_store(self, node_obj); -- luacheck: ignore 211/err
348 if not ok then 351 if not ok then
349 node_obj.subscribers[jid] = old_subscription; 352 node_obj.subscribers[jid] = old_subscription;
350 self.subscriptions[normal_jid][jid][node] = old_subscription and true or nil; 353 self.subscriptions[normal_jid][jid][node] = old_subscription and true or nil;
351 return ok, "internal-server-error"; 354 return ok, "internal-server-error";
352 end 355 end
394 self.subscriptions[normal_jid] = nil; 397 self.subscriptions[normal_jid] = nil;
395 end 398 end
396 end 399 end
397 400
398 if self.config.nodestore then 401 if self.config.nodestore then
399 local ok, err = save_node_to_store(self, node_obj); 402 -- TODO pass the error from storage to caller eg wrapped in an util.error
403 local ok, err = save_node_to_store(self, node_obj); -- luacheck: ignore 211/err
400 if not ok then 404 if not ok then
401 node_obj.subscribers[jid] = old_subscription; 405 node_obj.subscribers[jid] = old_subscription;
402 self.subscriptions[normal_jid][jid][node] = old_subscription and true or nil; 406 self.subscriptions[normal_jid][jid][node] = old_subscription and true or nil;
403 return ok, "internal-server-error"; 407 return ok, "internal-server-error";
404 end 408 end
452 config = config; 456 config = config;
453 affiliations = {}; 457 affiliations = {};
454 }; 458 };
455 459
456 if self.config.nodestore then 460 if self.config.nodestore then
457 local ok, err = save_node_to_store(self, self.nodes[node]); 461 -- TODO pass the error from storage to caller eg wrapped in an util.error
462 local ok, err = save_node_to_store(self, self.nodes[node]); -- luacheck: ignore 211/err
458 if not ok then 463 if not ok then
459 self.nodes[node] = nil; 464 self.nodes[node] = nil;
460 return ok, "internal-server-error"; 465 return ok, "internal-server-error";
461 end 466 end
462 end 467 end
509 if not (node_config and required_config) then 514 if not (node_config and required_config) then
510 return false; 515 return false;
511 end 516 end
512 for config_field, value in pairs(required_config) do 517 for config_field, value in pairs(required_config) do
513 if node_config[config_field] ~= value then 518 if node_config[config_field] ~= value then
514 return false; 519 return false, config_field;
515 end 520 end
516 end 521 end
517 return true; 522 return true;
518 end 523 end
519 524
545 return ok, err; 550 return ok, err;
546 end 551 end
547 node_obj = self.nodes[node]; 552 node_obj = self.nodes[node];
548 elseif requested_config and not requested_config._defaults_only then 553 elseif requested_config and not requested_config._defaults_only then
549 -- Check that node has the requested config before we publish 554 -- Check that node has the requested config before we publish
550 if not check_preconditions(node_obj.config, requested_config) then 555 local ok, field = check_preconditions(node_obj.config, requested_config);
551 return false, "precondition-not-met"; 556 if not ok then
557 local err = errors.new({
558 type = "cancel", condition = "conflict", text = "Field does not match: "..field;
559 });
560 err.pubsub_condition = "precondition-not-met";
561 return false, err;
552 end 562 end
553 end 563 end
554 if not self.config.itemcheck(item) then 564 if not self.config.itemcheck(item) then
555 return nil, "invalid-item"; 565 return nil, "invalid-item";
556 end 566 end
766 776
767 local old_config = node_obj.config; 777 local old_config = node_obj.config;
768 node_obj.config = new_config; 778 node_obj.config = new_config;
769 779
770 if self.config.nodestore then 780 if self.config.nodestore then
771 local ok, err = save_node_to_store(self, node_obj); 781 -- TODO pass the error from storage to caller eg wrapped in an util.error
782 local ok, err = save_node_to_store(self, node_obj); -- luacheck: ignore 211/err
772 if not ok then 783 if not ok then
773 node_obj.config = old_config; 784 node_obj.config = old_config;
774 return ok, "internal-server-error"; 785 return ok, "internal-server-error";
775 end 786 end
776 end 787 end