aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--core/rostermanager.lua38
-rw-r--r--core/storagemanager.lua17
-rw-r--r--plugins/mod_storage_sql.lua39
3 files changed, 69 insertions, 25 deletions
diff --git a/core/rostermanager.lua b/core/rostermanager.lua
index 0b72d0a6..176eba5b 100644
--- a/core/rostermanager.lua
+++ b/core/rostermanager.lua
@@ -18,7 +18,6 @@ local type = type;
local hosts = hosts;
local bare_sessions = prosody.bare_sessions;
-local datamanager = require "util.datamanager"
local um_user_exists = require "core.usermanager".user_exists;
local st = require "util.stanza";
@@ -30,7 +29,7 @@ local function add_to_roster(session, jid, item)
if session.roster then
local old_item = session.roster[jid];
session.roster[jid] = item;
- if save_roster(session.username, session.host) then
+ if save_roster(session.username, session.host, nil, jid) then
return true;
else
session.roster[jid] = old_item;
@@ -45,7 +44,7 @@ local function remove_from_roster(session, jid)
if session.roster then
local old_item = session.roster[jid];
session.roster[jid] = nil;
- if save_roster(session.username, session.host) then
+ if save_roster(session.username, session.host, nil, jid) then
return true;
else
session.roster[jid] = old_item;
@@ -109,7 +108,8 @@ local function load_roster(username, host)
else -- Attempt to load roster for non-loaded user
log("debug", "load_roster: loading for offline user: %s@%s", username, host);
end
- local data, err = datamanager.load(username, host, "roster");
+ local roster_store = require "core.storagemanager".open(host, "roster", "keyval");
+ local data, err = roster_store:get(username);
roster = data or {};
if user then user.roster = roster; end
roster_metadata(roster, err);
@@ -123,13 +123,13 @@ local function load_roster(username, host)
return roster, err;
end
-function save_roster(username, host, roster)
+function save_roster(username, host, roster, jid)
if not um_user_exists(username, host) then
log("debug", "not saving roster for %s@%s: the user doesn't exist", username, host);
return nil;
end
- log("debug", "save_roster: saving roster for %s@%s", username, host);
+ log("debug", "save_roster: saving roster for %s@%s, (%s)", username, host, jid or "all contacts");
if not roster then
roster = hosts[host] and hosts[host].sessions[username] and hosts[host].sessions[username].roster;
--if not roster then
@@ -143,7 +143,13 @@ function save_roster(username, host, roster)
metadata.version = (metadata.version or 0) + 1;
end
if metadata.broken then return nil, "Not saving broken roster" end
- return datamanager.store(username, host, "roster", roster);
+ if jid == nil then
+ local roster_store = require "core.storagemanager".open(host, "roster", "keyval");
+ return roster_store:set(username, roster);
+ else
+ local roster_store = require "core.storagemanager".open(host, "roster", "map");
+ return roster_store:set_keys(username, { [false] = metadata, [jid] = roster[jid] or roster_store.remove });
+ end
end
log("warn", "save_roster: user had no roster to save");
return nil;
@@ -159,7 +165,7 @@ local function process_inbound_subscription_approval(username, host, jid)
item.subscription = "both";
end
item.ask = nil;
- return save_roster(username, host, roster);
+ return save_roster(username, host, roster, jid);
end
end
@@ -183,7 +189,7 @@ local function process_inbound_subscription_cancellation(username, host, jid)
end
end
if changed then
- return save_roster(username, host, roster);
+ return save_roster(username, host, roster, jid);
end
end
@@ -207,7 +213,7 @@ local function process_inbound_unsubscribe(username, host, jid)
end
end
if changed then
- return save_roster(username, host, roster);
+ return save_roster(username, host, roster, jid);
end
end
@@ -240,7 +246,7 @@ local function set_contact_pending_in(username, host, jid)
return; -- false
end
roster[false].pending[jid] = true;
- return save_roster(username, host, roster);
+ return save_roster(username, host, roster, jid);
end
function is_contact_pending_out(username, host, jid)
local roster = load_roster(username, host);
@@ -259,7 +265,7 @@ local function set_contact_pending_out(username, host, jid) -- subscribe
end
item.ask = "subscribe";
log("debug", "set_contact_pending_out: saving roster; set %s@%s.roster[%q].ask=subscribe", username, host, jid);
- return save_roster(username, host, roster);
+ return save_roster(username, host, roster, jid);
end
local function unsubscribe(username, host, jid)
local roster = load_roster(username, host);
@@ -274,7 +280,7 @@ local function unsubscribe(username, host, jid)
elseif item.subscription == "to" then
item.subscription = "none";
end
- return save_roster(username, host, roster);
+ return save_roster(username, host, roster, jid);
end
local function subscribed(username, host, jid)
if is_contact_pending_in(username, host, jid) then
@@ -290,7 +296,7 @@ local function subscribed(username, host, jid)
item.subscription = "both";
end
roster[false].pending[jid] = nil;
- return save_roster(username, host, roster);
+ return save_roster(username, host, roster, jid);
end -- TODO else implement optional feature pre-approval (ask = subscribed)
end
local function unsubscribed(username, host, jid)
@@ -310,7 +316,7 @@ local function unsubscribed(username, host, jid)
is_subscribed = true;
end
end
- local success = (pending or is_subscribed) and save_roster(username, host, roster);
+ local success = (pending or is_subscribed) and save_roster(username, host, roster, jid);
return success, pending, subscribed;
end
@@ -319,7 +325,7 @@ local function process_outbound_subscription_request(username, host, jid)
local item = roster[jid];
if item and (item.subscription == "none" or item.subscription == "from") then
item.ask = "subscribe";
- return save_roster(username, host, roster);
+ return save_roster(username, host, roster, jid);
end
end
diff --git a/core/storagemanager.lua b/core/storagemanager.lua
index 33fd5d49..de68c2ac 100644
--- a/core/storagemanager.lua
+++ b/core/storagemanager.lua
@@ -118,6 +118,23 @@ local map_shim_mt = {
current[key] = data;
return self.keyval_store:set(username, current);
end;
+ set_keys = function (self, username, keydatas)
+ local current, err = self.keyval_store:get(username);
+ if current == nil then
+ if err then
+ return nil, err;
+ else
+ current = keydatas;
+ end
+ else
+ for k,v in pairs(keydatas) do
+ if v == self.remove then v = nil; end
+ current[k] = v;
+ end
+ end
+ return self.keyval_store:set(username, current);
+ end;
+ remove = {};
};
}
diff --git a/plugins/mod_storage_sql.lua b/plugins/mod_storage_sql.lua
index 57d964e2..34fcacee 100644
--- a/plugins/mod_storage_sql.lua
+++ b/plugins/mod_storage_sql.lua
@@ -1,4 +1,6 @@
+-- luacheck: ignore 212/self
+
local json = require "util.json";
local sql = require "util.sql";
local xml_parse = require "util.xml".parse;
@@ -125,8 +127,10 @@ end
--- Archive store API
+-- luacheck: ignore 512 431/user 431/store
local map_store = {};
map_store.__index = map_store;
+map_store.remove = {};
function map_store:get(username, key)
local ok, result = engine:transaction(function()
if type(key) == "string" and key ~= "" then
@@ -134,23 +138,40 @@ function map_store:get(username, key)
return deserialize(row[1], row[2]);
end
else
- error("TODO: non-string keys");
+ for row in engine:select("SELECT `type`, `value` FROM `prosody` WHERE `host`=? AND `user`=? AND `store`=? AND `key`=?", host, username or "", self.store, "") do
+ local data = deserialize(row[1], row[2]);
+ return data and data[key] or nil;
+ end
end
end);
if not ok then return nil, result; end
return result;
end
function map_store:set(username, key, data)
+ return self:set_keys(username, { [key] = data or self.remove });
+end
+function map_store:set_keys(username, keydatas)
local ok, result = engine:transaction(function()
- if type(key) == "string" and key ~= "" then
- engine:delete("DELETE FROM `prosody` WHERE `host`=? AND `user`=? AND `store`=? AND `key`=?",
- host, username or "", self.store, key);
- if data ~= nil then
- local t, value = assert(serialize(data));
- engine:insert("INSERT INTO `prosody` (`host`,`user`,`store`,`key`,`type`,`value`) VALUES (?,?,?,?,?,?)", host, username or "", self.store, key, t, value);
+ for key, data in pairs(keydatas) do
+ if type(key) == "string" and key ~= "" then
+ engine:delete("DELETE FROM `prosody` WHERE `host`=? AND `user`=? AND `store`=? AND `key`=?",
+ host, username or "", self.store, key);
+ if data ~= self.remove then
+ local t, value = assert(serialize(data));
+ engine:insert("INSERT INTO `prosody` (`host`,`user`,`store`,`key`,`type`,`value`) VALUES (?,?,?,?,?,?)", host, username or "", self.store, key, t, value);
+ end
+ else
+ local extradata = {};
+ for row in engine:select("SELECT `type`, `value` FROM `prosody` WHERE `host`=? AND `user`=? AND `store`=? AND `key`=?", host, username or "", self.store, "") do
+ extradata = deserialize(row[1], row[2]);
+ break;
+ end
+ engine:delete("DELETE FROM `prosody` WHERE `host`=? AND `user`=? AND `store`=? AND `key`=?",
+ host, username or "", self.store, "");
+ extradata[key] = data;
+ local t, value = assert(serialize(extradata));
+ engine:insert("INSERT INTO `prosody` (`host`,`user`,`store`,`key`,`type`,`value`) VALUES (?,?,?,?,?,?)", host, username or "", self.store, "", t, value);
end
- else
- error("TODO: non-string keys");
end
return true;
end);