Comparison

util/pubsub.lua @ 9232:65c83bfcf2ee

util.pubsub: Comments describing the return values of methods
author Kim Alvefur <zash@zash.se>
date Wed, 22 Aug 2018 13:27:36 +0200
parent 9230:711fea229e48
child 9233:885dd6845e62
comparison
equal deleted inserted replaced
9231:3d12b4f41b23 9232:65c83bfcf2ee
170 --- Service methods 170 --- Service methods
171 171
172 local service = {}; 172 local service = {};
173 service_mt.__index = service; 173 service_mt.__index = service;
174 174
175 function service:jids_equal(jid1, jid2) 175 function service:jids_equal(jid1, jid2) --> boolean
176 local normalize = self.config.normalize_jid; 176 local normalize = self.config.normalize_jid;
177 return normalize(jid1) == normalize(jid2); 177 return normalize(jid1) == normalize(jid2);
178 end 178 end
179 179
180 function service:may(node, actor, action) 180 function service:may(node, actor, action) --> boolean
181 if actor == true then return true; end 181 if actor == true then return true; end
182 182
183 local node_obj = self.nodes[node]; 183 local node_obj = self.nodes[node];
184 local node_aff = node_obj and (node_obj.affiliations[actor] 184 local node_aff = node_obj and (node_obj.affiliations[actor]
185 or node_obj.affiliations[self.config.normalize_jid(actor)]); 185 or node_obj.affiliations[self.config.normalize_jid(actor)]);
210 end 210 end
211 211
212 return false; 212 return false;
213 end 213 end
214 214
215 function service:get_default_affiliation(node, actor) 215 function service:get_default_affiliation(node, actor) --> affiliation
216 local node_obj = self.nodes[node]; 216 local node_obj = self.nodes[node];
217 local access_model = node_obj and node_obj.config.access_model 217 local access_model = node_obj and node_obj.config.access_model
218 or self.node_defaults.access_model; 218 or self.node_defaults.access_model;
219 219
220 if access_model == "open" then 220 if access_model == "open" then
232 end 232 end
233 end 233 end
234 end 234 end
235 end 235 end
236 236
237 function service:set_affiliation(node, actor, jid, affiliation) 237 function service:set_affiliation(node, actor, jid, affiliation) --> ok, err
238 -- Access checking 238 -- Access checking
239 if not self:may(node, actor, "set_affiliation") then 239 if not self:may(node, actor, "set_affiliation") then
240 return false, "forbidden"; 240 return false, "forbidden";
241 end 241 end
242 -- 242 --
269 end 269 end
270 end 270 end
271 return true; 271 return true;
272 end 272 end
273 273
274 function service:add_subscription(node, actor, jid, options) 274 function service:add_subscription(node, actor, jid, options) --> ok, err
275 -- Access checking 275 -- Access checking
276 local cap; 276 local cap;
277 if actor == true or jid == actor or self:jids_equal(actor, jid) then 277 if actor == true or jid == actor or self:jids_equal(actor, jid) then
278 cap = "subscribe"; 278 cap = "subscribe";
279 else 279 else
323 323
324 self.events.fire_event("subscription-added", { service = self, node = node, jid = jid, normalized_jid = normal_jid, options = options }); 324 self.events.fire_event("subscription-added", { service = self, node = node, jid = jid, normalized_jid = normal_jid, options = options });
325 return true; 325 return true;
326 end 326 end
327 327
328 function service:remove_subscription(node, actor, jid) 328 function service:remove_subscription(node, actor, jid) --> ok, err
329 -- Access checking 329 -- Access checking
330 local cap; 330 local cap;
331 if actor == true or jid == actor or self:jids_equal(actor, jid) then 331 if actor == true or jid == actor or self:jids_equal(actor, jid) then
332 cap = "unsubscribe"; 332 cap = "unsubscribe";
333 else 333 else
375 375
376 self.events.fire_event("subscription-removed", { service = self, node = node, jid = jid, normalized_jid = normal_jid }); 376 self.events.fire_event("subscription-removed", { service = self, node = node, jid = jid, normalized_jid = normal_jid });
377 return true; 377 return true;
378 end 378 end
379 379
380 function service:get_subscription(node, actor, jid) 380 function service:get_subscription(node, actor, jid) --> (true, subscription) or (false, err)
381 -- Access checking 381 -- Access checking
382 local cap; 382 local cap;
383 if actor == true or jid == actor or self:jids_equal(actor, jid) then 383 if actor == true or jid == actor or self:jids_equal(actor, jid) then
384 cap = "get_subscription"; 384 cap = "get_subscription";
385 else 385 else
394 return false, "item-not-found"; 394 return false, "item-not-found";
395 end 395 end
396 return true, node_obj.subscribers[jid]; 396 return true, node_obj.subscribers[jid];
397 end 397 end
398 398
399 function service:create(node, actor, options) 399 function service:create(node, actor, options) --> ok, err
400 -- Access checking 400 -- Access checking
401 if not self:may(node, actor, "create") then 401 if not self:may(node, actor, "create") then
402 return false, "forbidden"; 402 return false, "forbidden";
403 end 403 end
404 -- 404 --
433 end 433 end
434 434
435 return true; 435 return true;
436 end 436 end
437 437
438 function service:delete(node, actor) 438 function service:delete(node, actor) --> ok, err
439 -- Access checking 439 -- Access checking
440 if not self:may(node, actor, "delete") then 440 if not self:may(node, actor, "delete") then
441 return false, "forbidden"; 441 return false, "forbidden";
442 end 442 end
443 -- 443 --
475 end 475 end
476 end 476 end
477 return true; 477 return true;
478 end 478 end
479 479
480 function service:publish(node, actor, id, item, required_config) 480 function service:publish(node, actor, id, item, required_config) --> ok, err
481 -- Access checking 481 -- Access checking
482 local may_publish = false; 482 local may_publish = false;
483 483
484 if self:may(node, actor, "publish") then 484 if self:may(node, actor, "publish") then
485 may_publish = true; 485 may_publish = true;
522 self.events.fire_event("item-published", event_data); 522 self.events.fire_event("item-published", event_data);
523 self.config.broadcaster("items", node, node_obj.subscribers, item, actor, node_obj, self); 523 self.config.broadcaster("items", node, node_obj.subscribers, item, actor, node_obj, self);
524 return true; 524 return true;
525 end 525 end
526 526
527 function service:retract(node, actor, id, retract) 527 function service:retract(node, actor, id, retract) --> ok, err
528 -- Access checking 528 -- Access checking
529 if not self:may(node, actor, "retract") then 529 if not self:may(node, actor, "retract") then
530 return false, "forbidden"; 530 return false, "forbidden";
531 end 531 end
532 -- 532 --
543 self.config.broadcaster("retract", node, node_obj.subscribers, retract, actor, node_obj, self); 543 self.config.broadcaster("retract", node, node_obj.subscribers, retract, actor, node_obj, self);
544 end 544 end
545 return true 545 return true
546 end 546 end
547 547
548 function service:purge(node, actor, notify) 548 function service:purge(node, actor, notify) --> ok, err
549 -- Access checking 549 -- Access checking
550 if not self:may(node, actor, "retract") then 550 if not self:may(node, actor, "retract") then
551 return false, "forbidden"; 551 return false, "forbidden";
552 end 552 end
553 -- 553 --
565 self.config.broadcaster("purge", node, node_obj.subscribers, nil, actor, node_obj, self); 565 self.config.broadcaster("purge", node, node_obj.subscribers, nil, actor, node_obj, self);
566 end 566 end
567 return true 567 return true
568 end 568 end
569 569
570 function service:get_items(node, actor, id) 570 function service:get_items(node, actor, id) --> ok, err
571 -- Access checking 571 -- Access checking
572 if not self:may(node, actor, "get_items") then 572 if not self:may(node, actor, "get_items") then
573 return false, "forbidden"; 573 return false, "forbidden";
574 end 574 end
575 -- 575 --
591 end 591 end
592 return true, data; 592 return true, data;
593 end 593 end
594 end 594 end
595 595
596 function service:get_last_item(node, actor) 596 function service:get_last_item(node, actor) --> (true, id, node) or (false, err)
597 -- Access checking 597 -- Access checking
598 if not self:may(node, actor, "get_items") then 598 if not self:may(node, actor, "get_items") then
599 return false, "forbidden"; 599 return false, "forbidden";
600 end 600 end
601 -- 601 --
607 607
608 -- Returns success, id, item 608 -- Returns success, id, item
609 return true, self.data[node]:head(); 609 return true, self.data[node]:head();
610 end 610 end
611 611
612 function service:get_nodes(actor) 612 function service:get_nodes(actor) --> (true, map) or (false, err)
613 -- Access checking 613 -- Access checking
614 if not self:may(nil, actor, "get_nodes") then 614 if not self:may(nil, actor, "get_nodes") then
615 return false, "forbidden"; 615 return false, "forbidden";
616 end 616 end
617 -- 617 --
639 end 639 end
640 end 640 end
641 end 641 end
642 end 642 end
643 643
644 function service:get_subscriptions(node, actor, jid) 644 function service:get_subscriptions(node, actor, jid) --> (true, array) or (false, err)
645 -- Access checking 645 -- Access checking
646 local cap; 646 local cap;
647 if actor == true or jid == actor or self:jids_equal(actor, jid) then 647 if actor == true or jid == actor or self:jids_equal(actor, jid) then
648 cap = "get_subscriptions"; 648 cap = "get_subscriptions";
649 else 649 else
676 end 676 end
677 return true, ret; 677 return true, ret;
678 end 678 end
679 679
680 -- Access models only affect 'none' affiliation caps, service/default access level... 680 -- Access models only affect 'none' affiliation caps, service/default access level...
681 function service:set_node_capabilities(node, actor, capabilities) 681 function service:set_node_capabilities(node, actor, capabilities) --> ok, err
682 -- Access checking 682 -- Access checking
683 if not self:may(node, actor, "configure") then 683 if not self:may(node, actor, "configure") then
684 return false, "forbidden"; 684 return false, "forbidden";
685 end 685 end
686 -- 686 --
690 end 690 end
691 node_obj.capabilities = capabilities; 691 node_obj.capabilities = capabilities;
692 return true; 692 return true;
693 end 693 end
694 694
695 function service:set_node_config(node, actor, new_config) 695 function service:set_node_config(node, actor, new_config) --> ok, err
696 if not self:may(node, actor, "configure") then 696 if not self:may(node, actor, "configure") then
697 return false, "forbidden"; 697 return false, "forbidden";
698 end 698 end
699 699
700 local node_obj = self.nodes[node]; 700 local node_obj = self.nodes[node];
741 end 741 end
742 742
743 return true; 743 return true;
744 end 744 end
745 745
746 function service:get_node_config(node, actor) 746 function service:get_node_config(node, actor) --> ok, err
747 if not self:may(node, actor, "get_configuration") then 747 if not self:may(node, actor, "get_configuration") then
748 return false, "forbidden"; 748 return false, "forbidden";
749 end 749 end
750 750
751 local node_obj = self.nodes[node]; 751 local node_obj = self.nodes[node];