Diff

mod_nodeinfo2/mod_nodeinfo2.lua @ 3789:e3b673df3906

mod_nodeinfo2: Only expose message stats if nodeinfo2_expose_posts isn’t set to false
author Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
date Mon, 30 Dec 2019 22:20:11 +0100
parent 3786:58e484426d13
child 3790:352f3efe1b67
line wrap: on
line diff
--- a/mod_nodeinfo2/mod_nodeinfo2.lua	Mon Dec 30 12:04:35 2019 +0000
+++ b/mod_nodeinfo2/mod_nodeinfo2.lua	Mon Dec 30 22:20:11 2019 +0100
@@ -5,7 +5,11 @@
 
 module:depends("http");
 module:depends("lastlog");
-module:depends("measure_message_e2ee");
+
+local expose_posts = module:get_option_boolean("nodeinfo2_expose_posts", true);
+if expose_posts then
+	module:depends("measure_message_e2ee");
+end
 
 local main_store = module:open_store();
 local lastlog_store = module:open_store("lastlog");
@@ -38,21 +42,38 @@
 	week_users = nil;
 end
 
-local data = main_store:get("nodeinfo2") or { message_count = 0 };
+local data;
+if expose_posts then
+	data = main_store:get("nodeinfo2") or { message_count = 0 };
+end
 
 module:provides("http", {
 	default_path = "/.well-known/x-nodeinfo2";
 	route = {
 		GET = function (event)
-			local stats, changed_only, extras = get_stats();
-			for stat, _ in pairs(stats) do
-				if stat == "/"..module.host.."/mod_measure_message_e2ee/message:rate" then
-					local new_message_count = extras[stat].total;
-					if new_message_count ~= data.message_count then
-						data = { message_count = new_message_count };
-						main_store:set("nodeinfo2", data);
+			local usage = {
+				users = {
+					total = total_users;
+					activeHalfyear = half_year_users;
+					activeMonth = month_users;
+					activeWeek = week_users;
+				};
+			};
+
+			if expose_posts then
+				local stats, changed_only, extras = get_stats();
+				for stat, _ in pairs(stats) do
+					if stat == "/"..module.host.."/mod_measure_message_e2ee/message:rate" then
+						local new_message_count = extras[stat].total;
+						if new_message_count ~= data.message_count then
+							data = { message_count = new_message_count };
+							main_store:set("nodeinfo2", data);
+						end
 					end
 				end
+				usage.localPosts = data.message_count;
+				-- TODO: also count PubSub replies here.
+				usage.localComments = 0;
 			end
 
 			event.response.headers.content_type = "application/json";
@@ -84,17 +105,7 @@
 				};
 				--]]
 				openRegistrations = module:get_option_boolean("allow_registration", false);
-				usage = {
-					users = {
-						total = total_users;
-						activeHalfyear = half_year_users;
-						activeMonth = month_users;
-						activeWeek = week_users;
-					};
-					localPosts = data.message_count;
-					-- TODO: also count PubSub replies here.
-					localComments = 0;
-				};
+				usage = usage;
 			});
 		end;
 	}