Terms&Conditions (#18) WIP:

* channel terms
This commit is contained in:
John Livingston
2024-06-25 11:28:46 +02:00
parent b110456029
commit a06ef00e2a
9 changed files with 101 additions and 42 deletions

View File

@ -7,30 +7,71 @@
-- Please see the Peertube livechat plugin copyright information.
-- https://livingston.frama.io/peertube-plugin-livechat/credits/
--
-- Exposed functions:
-- get_muc_terms
-- set_muc_terms
local jid_escape = require "util.jid".escape;
local jid_resource = require "util.jid".resource;
local st = require "util.stanza";
local id = require "util.id";
local service_nickname = module:get_option_string("muc_terms_service_nickname", "Service");
local global_terms = module:get_option_string("muc_terms", "");
local global_terms = module:get_option_string("muc_terms_global", "");
local function create_terms_message(room, type, terms)
local from = room.jid .. '/' .. jid_escape(service_nickname);
module:log("debug", "Creating %s terms message from %s (room %s)", type, from, room);
local msg = st.message({
type = "groupchat",
from = from,
id = id.medium()
}, terms)
:tag('x-livechat-terms', { type = type }):up(); -- adding a custom tag to specify that it is a "terms" message, so that frontend can display it with a special template.
return msg;
end
-- MUC Getter/Setter
function get_muc_terms(room)
return room._data.livechat_muc_terms or nil;
end
function set_muc_terms(room, terms)
terms = terms or nil;
if get_muc_terms(room) == terms then return false; end
room._data.livechat_muc_terms = terms;
if terms ~= nil then
-- we must send new terms to all occupants.
local msg = create_terms_message(room, "muc", terms);
module:log("debug", "Broadcasting terms message to room %s", room);
room:broadcast_message(msg);
end
return true;
end
-- send the terms when joining:
function send_terms(event)
local function send_terms(event)
local origin = event.origin;
local room = event.room;
local occupant = event.occupant;
if global_terms then
module:log("debug", "Sending global terms to %s", occupant.jid);
local msg = create_terms_message(room, "global", global_terms);
msg.attr.to = occupant.jid;
origin.send(msg);
end
local muc_terms = get_muc_terms(room);
if muc_terms then
local from = room.jid .. '/' .. jid_escape(service_nickname);
module:log("debug", "Sending global terms to %s from %s (room %s)", occupant.jid, from, room);
local message = st.message({
type = "groupchat",
to = occupant.jid,
from = from,
id = id.medium()
}, global_terms)
:tag('x-livechat-terms', { type = "global" }):up(); -- adding a custom tag to specify that it is a "terms" message, so that frontend can display it with a special template.
origin.send(message);
module:log("debug", "Sending muc terms to %s", occupant.jid);
local msg = create_terms_message(room, "muc", muc_terms);
msg.attr.to = occupant.jid;
origin.send(msg);
end
end
-- Note: we could do that on muc-occupant-joined or muc-occupant-session-new.
@ -39,7 +80,7 @@ end
module:hook("muc-occupant-session-new", send_terms);
-- reserve the service_nickname:
function enforce_nick_policy(event)
local function enforce_nick_policy(event)
local origin, stanza = event.origin, event.stanza;
local requested_nick = jid_resource(stanza.attr.to);
local room = event.room;