mod_pubsub_serverinfo/mod_pubsub_serverinfo.lua
changeset 5821 7905766d01f6
parent 5820 45d0802d0787
child 5847 79ae71f52c81
equal deleted inserted replaced
5820:45d0802d0787 5821:7905766d01f6
       
     1 local http = require "net.http";
       
     2 local json = require "util.json";
     1 local st = require "util.stanza";
     3 local st = require "util.stanza";
     2 local new_id = require"util.id".medium;
     4 local new_id = require"util.id".medium;
     3 local dataform = require "util.dataforms".new;
     5 local dataform = require "util.dataforms".new;
     4 
     6 
     5 local local_domain = module:get_host();
     7 local local_domain = module:get_host();
     6 local service = module:get_option(module.name .. "_service") or "pubsub." .. local_domain;
     8 local service = module:get_option(module.name .. "_service") or "pubsub." .. local_domain;
     7 local node = module:get_option(module.name .. "_node") or "serverinfo";
     9 local node = module:get_option(module.name .. "_node") or "serverinfo";
     8 local actor = module.host .. "/modules/" .. module.name;
    10 local actor = module.host .. "/modules/" .. module.name;
     9 local publication_interval = module:get_option(module.name .. "_publication_interval") or 300;
    11 local publication_interval = module:get_option(module.name .. "_publication_interval") or 300;
    10 local cache_ttl = module:get_option(module.name .. "_cache_ttl") or 3600;
    12 local cache_ttl = module:get_option(module.name .. "_cache_ttl") or 3600;
       
    13 local public_providers_url = module:get_option_string(module.name.."_public_providers_url", "https://data.xmpp.net/providers/v2/providers-Ds.json");
    11 local delete_node_on_unload = module:get_option_boolean(module.name.."_delete_node_on_unload", false);
    14 local delete_node_on_unload = module:get_option_boolean(module.name.."_delete_node_on_unload", false);
    12 local persist_items = module:get_option_boolean(module.name.."_persist_items", true);
    15 local persist_items = module:get_option_boolean(module.name.."_persist_items", true);
    13 
    16 
    14 local xmlns_pubsub = "http://jabber.org/protocol/pubsub";
    17 local xmlns_pubsub = "http://jabber.org/protocol/pubsub";
    15 
    18 
   219 	return publication_interval;
   222 	return publication_interval;
   220 end
   223 end
   221 
   224 
   222 local opt_in_cache = {}
   225 local opt_in_cache = {}
   223 
   226 
       
   227 -- Public providers are already public, so we fetch the list of providers
       
   228 -- registered on providers.xmpp.net so we don't have to disco them individually
       
   229 local function update_public_providers()
       
   230 	return http.request(public_providers_url)
       
   231 		:next(function (response)
       
   232 			assert(
       
   233 				response.headers["content-type"] == "application/json",
       
   234 				"invalid mimetype: "..tostring(response.headers["content-type"])
       
   235 			);
       
   236 			return json.decode(response.body);
       
   237 		end)
       
   238 		:next(function (public_server_domains)
       
   239 			module:log("debug", "Retrieved list of %d public providers", #public_server_domains);
       
   240 			for _, domain in ipairs(public_server_domains) do
       
   241 				opt_in_cache[domain] = {
       
   242 					opt_in = true;
       
   243 					expires = os.time() + (86400 * 1.5);
       
   244 				};
       
   245 			end
       
   246 		end, function (err)
       
   247 			module:log("warn", "Failed to fetch/decode provider list: %s", err);
       
   248 		end);
       
   249 end
       
   250 
       
   251 module:daily("update public provider list", update_public_providers);
       
   252 
   224 function cache_warm_up()
   253 function cache_warm_up()
   225 	module:log("debug", "Warming up opt-in cache")
   254 	module:log("debug", "Warming up opt-in cache")
   226 	local domains_by_host = get_remote_domain_names()
   255 
   227 	local remotes = domains_by_host[local_domain]
   256 	update_public_providers():finally(function ()
   228 	if remotes ~= nil then
   257 		module:log("debug", "Querying known domains for opt-in cache...");
   229 		for remote, _ in pairs(remotes) do
   258 		local domains_by_host = get_remote_domain_names()
   230 			does_opt_in(remote)
   259 		local remotes = domains_by_host[local_domain]
   231 		end
   260 		if remotes ~= nil then
   232 	end
   261 			for remote in pairs(remotes) do
       
   262 				does_opt_in(remote)
       
   263 			end
       
   264 		end
       
   265 	end);
   233 end
   266 end
   234 
   267 
   235 function does_opt_in(remoteDomain)
   268 function does_opt_in(remoteDomain)
   236 
   269 
   237 	-- try to read answer from cache.
   270 	-- try to read answer from cache.