File

mod_muc_restrict_avatars/mod_muc_restrict_avatars.lua @ 5931:d194d1012fd3

Updating dox for mod_rest. Ideas expressed / clarified: 1) Making clear that mod_rest isn't to be installed under VirtualHosts AND as a component. 2) Understanding some of the implications of this choice: A) Changes to user authentication B) How it affects subdomains 3) More consistent use of domain names for clarity. 4) Using different heading sizes to show scope of section. Essentially, I added all the tidbits I had to clarify in getting this to work in my own example.
author Ben Smith <bens@effortlessis.com>
date Mon, 13 May 2024 13:25:13 -0700
parent 5920:5b95e06d75d5
line wrap: on
line source

local bare_jid = require"util.jid".bare;
local mod_muc = module:depends("muc");

local function filter_avatar_advertisement(tag)
	if tag.attr.xmlns == "vcard-temp:x:update" then
		return nil;
	end

	return tag;
end

-- Function to determine if avatar restriction is enabled
local function is_avatar_restriction_enabled(room)
	return room._data.restrict_avatars;
end

-- Add MUC configuration form option for avatar restriction
module:hook("muc-config-form", function(event)
	local room, form = event.room, event.form;
	table.insert(form, {
		name = "restrict_avatars",
		type = "boolean",
		label = "Restrict avatars to members only",
		value = is_avatar_restriction_enabled(room)
	});
end);

-- Handle MUC configuration form submission
module:hook("muc-config-submitted", function(event)
	local room, fields, changed = event.room, event.fields, event.changed;
	local restrict_avatars = fields["restrict_avatars"];

	if room and restrict_avatars ~= is_avatar_restriction_enabled(room) then
		-- Update room settings based on the submitted value
		room._data.restrict_avatars = restrict_avatars;
		-- Mark the configuration as changed
		if type(changed) == "table" then
			changed["restrict_avatars"] = true;
		else
			event.changed = true;
		end
	end
end);

-- Handle presence/full events to filter avatar advertisements
module:hook("presence/full", function(event)
	local stanza = event.stanza;
	local room = mod_muc.get_room_from_jid(bare_jid(stanza.attr.to));
	if room and not room:get_affiliation(stanza.attr.from) then
		if is_avatar_restriction_enabled(room) then
			stanza:maptags(filter_avatar_advertisement);
		end
	end
end, 1);