Comparison

util/pubsub.lua @ 11719:3986b5a0c3fc

util.pubsub: Handle absence of node data interface This is in preparation for fixing the behavior of 'persist_items', which was misunderstood at some point. In mod_pep it toggles between persistent storage and in-memory storage, while the correct behavior would be to toggle whether published items are stored at all or forgotten after being broadcast.
author Kim Alvefur <zash@zash.se>
date Wed, 21 Jul 2021 21:22:17 +0200
parent 11567:c471e19a238e
child 11720:72512c0858b3
comparison
equal deleted inserted replaced
11718:d79f5431f31b 11719:3986b5a0c3fc
563 end 563 end
564 if not self.config.itemcheck(item) then 564 if not self.config.itemcheck(item) then
565 return nil, "invalid-item"; 565 return nil, "invalid-item";
566 end 566 end
567 local node_data = self.data[node]; 567 local node_data = self.data[node];
568 if not node_data then 568 if node_data then
569 -- FIXME how is this possible? #1657 569 local ok = node_data:set(id, item);
570 return nil, "internal-server-error"; 570 if not ok then
571 end 571 return nil, "internal-server-error";
572 local ok = node_data:set(id, item); 572 end
573 if not ok then 573 if type(ok) == "string" then id = ok; end
574 return nil, "internal-server-error"; 574 end
575 end
576 if type(ok) == "string" then id = ok; end
577 local event_data = { service = self, node = node, actor = actor, id = id, item = item }; 575 local event_data = { service = self, node = node, actor = actor, id = id, item = item };
578 self.events.fire_event("item-published/"..node, event_data); 576 self.events.fire_event("item-published/"..node, event_data);
579 self.events.fire_event("item-published", event_data); 577 self.events.fire_event("item-published", event_data);
580 self:broadcast("items", node, node_obj.subscribers, item, actor, node_obj); 578 self:broadcast("items", node, node_obj.subscribers, item, actor, node_obj);
581 return true; 579 return true;
591 if not self:may(node, actor, "retract") then 589 if not self:may(node, actor, "retract") then
592 return false, "forbidden"; 590 return false, "forbidden";
593 end 591 end
594 -- 592 --
595 local node_obj = self.nodes[node]; 593 local node_obj = self.nodes[node];
596 if (not node_obj) or (not self.data[node]:get(id)) then 594 if not node_obj then
597 return false, "item-not-found"; 595 return false, "item-not-found";
598 end 596 end
599 local ok = self.data[node]:set(id, nil); 597 if self.data[node] then
600 if not ok then 598 if not self.data[node]:get(id) then
601 return nil, "internal-server-error"; 599 return false, "item-not-found";
600 end
601 local ok = self.data[node]:set(id, nil);
602 if not ok then
603 return nil, "internal-server-error";
604 end
602 end 605 end
603 self.events.fire_event("item-retracted", { service = self, node = node, actor = actor, id = id }); 606 self.events.fire_event("item-retracted", { service = self, node = node, actor = actor, id = id });
604 if retract then 607 if retract then
605 self:broadcast("retract", node, node_obj.subscribers, retract, actor, node_obj); 608 self:broadcast("retract", node, node_obj.subscribers, retract, actor, node_obj);
606 end 609 end
615 -- 618 --
616 local node_obj = self.nodes[node]; 619 local node_obj = self.nodes[node];
617 if not node_obj then 620 if not node_obj then
618 return false, "item-not-found"; 621 return false, "item-not-found";
619 end 622 end
620 if self.data[node] and self.data[node].clear then 623 if self.data[node] then
621 self.data[node]:clear() 624 if self.data[node].clear then
622 else 625 self.data[node]:clear()
623 self.data[node] = self.config.itemstore(self.nodes[node].config, node); 626 else
627 self.data[node] = self.config.itemstore(self.nodes[node].config, node);
628 end
624 end 629 end
625 self.events.fire_event("node-purged", { service = self, node = node, actor = actor }); 630 self.events.fire_event("node-purged", { service = self, node = node, actor = actor });
626 if notify then 631 if notify then
627 self:broadcast("purge", node, node_obj.subscribers, nil, actor, node_obj); 632 self:broadcast("purge", node, node_obj.subscribers, nil, actor, node_obj);
628 end 633 end
641 end 646 end
642 if type(ids) == "string" then -- COMPAT see #1305 647 if type(ids) == "string" then -- COMPAT see #1305
643 ids = { ids }; 648 ids = { ids };
644 end 649 end
645 local data = {}; 650 local data = {};
651 if not self.data[node] then
652 return true, data;
653 end
646 if ids then 654 if ids then
647 for _, key in ipairs(ids) do 655 for _, key in ipairs(ids) do
648 local value = self.data[node]:get(key); 656 local value = self.data[node]:get(key);
649 if value then 657 if value then
650 data[#data+1] = key; 658 data[#data+1] = key;
668 -- 676 --
669 677
670 -- Check node exists 678 -- Check node exists
671 if not self.nodes[node] then 679 if not self.nodes[node] then
672 return false, "item-not-found"; 680 return false, "item-not-found";
681 end
682
683 if not self.data[node] then
684 -- FIXME Should this be a success or failure?
685 return true, nil;
673 end 686 end
674 687
675 -- Returns success, id, item 688 -- Returns success, id, item
676 return true, self.data[node]:head(); 689 return true, self.data[node]:head();
677 end 690 end
803 end 816 end
804 817
805 if old_config["persist_items"] ~= node_obj.config["persist_items"] then 818 if old_config["persist_items"] ~= node_obj.config["persist_items"] then
806 self.data[node] = self.config.itemstore(self.nodes[node].config, node); 819 self.data[node] = self.config.itemstore(self.nodes[node].config, node);
807 elseif old_config["max_items"] ~= node_obj.config["max_items"] then 820 elseif old_config["max_items"] ~= node_obj.config["max_items"] then
808 self.data[node]:resize(self.nodes[node].config["max_items"]); 821 if self.data[node] then
822 self.data[node]:resize(self.nodes[node].config["max_items"]);
823 end
809 end 824 end
810 825
811 return true; 826 return true;
812 end 827 end
813 828