Merge 0.10->trunk
[prosody.git] / util / pubsub.lua
index 1ecbdfa81df130b0177ee0fb9b273f82b3884bb8..e0d428c00ccafeb9eb526d79a4e40e12ce609ac6 100644 (file)
@@ -29,13 +29,13 @@ end
 
 function service:may(node, actor, action)
        if actor == true then return true; end
-       
+
        local node_obj = self.nodes[node];
        local node_aff = node_obj and node_obj.affiliations[actor];
        local service_aff = self.affiliations[actor]
                         or self.config.get_affiliation(actor, node, action)
                         or "none";
-       
+
        -- Check if node allows/forbids it
        local node_capabilities = node_obj and node_obj.capabilities;
        if node_capabilities then
@@ -47,7 +47,7 @@ function service:may(node, actor, action)
                        end
                end
        end
-       
+
        -- Check service-wide capabilities instead
        local service_capabilities = self.config.capabilities;
        local caps = service_capabilities[node_aff or service_aff];
@@ -57,7 +57,7 @@ function service:may(node, actor, action)
                        return can;
                end
        end
-       
+
        return false;
 end
 
@@ -211,7 +211,7 @@ function service:create(node, actor)
        if self.nodes[node] then
                return false, "conflict";
        end
-       
+
        self.nodes[node] = {
                name = node;
                subscribers = {};
@@ -233,6 +233,9 @@ function service:delete(node, actor)
        end
        --
        local node_obj = self.nodes[node];
+       if not node_obj then
+               return false, "item-not-found";
+       end
        self.nodes[node] = nil;
        self.config.broadcaster("delete", node, node_obj.subscribers);
        return true;
@@ -255,6 +258,7 @@ function service:publish(node, actor, id, item)
                end
                node_obj = self.nodes[node];
        end
+       node_obj.data[#node_obj.data + 1] = id;
        node_obj.data[id] = item;
        self.events.fire_event("item-published", { node = node, actor = actor, id = id, item = item });
        self.config.broadcaster("items", node, node_obj.subscribers, item);
@@ -272,6 +276,12 @@ function service:retract(node, actor, id, retract)
                return false, "item-not-found";
        end
        node_obj.data[id] = nil;
+       for i, _id in ipairs(node_obj.data) do
+               if id == _id then
+                       table.remove(node_obj, i);
+                       break;
+               end
+       end
        if retract then
                self.config.broadcaster("items", node, node_obj.subscribers, retract);
        end
@@ -306,7 +316,7 @@ function service:get_items(node, actor, id)
                return false, "item-not-found";
        end
        if id then -- Restrict results to a single specific item
-               return true, { [id] = node_obj.data[id] };
+               return true, { id, [id] = node_obj.data[id] };
        else
                return true, node_obj.data;
        end