toolbar and session
This commit is contained in:
@@ -247,6 +247,89 @@ local function get_active_participant_user_ids(room)
|
||||
return user_ids;
|
||||
end
|
||||
|
||||
local function rebuild_room_role_lists(room)
|
||||
if not room then
|
||||
return;
|
||||
end
|
||||
|
||||
ensure_room_data(room);
|
||||
|
||||
local moderators = {};
|
||||
local participants = {};
|
||||
|
||||
for _, occupant in room:each_occupant() do
|
||||
local occupant_jid = occupant and occupant.bare_jid;
|
||||
local user_id = occupant_jid and room._data.politictalk_jid_user_ids[occupant_jid] or nil;
|
||||
|
||||
if user_id and table_contains(room._data.politictalk_host_jids, occupant_jid) then
|
||||
moderators = add_unique(moderators, user_id);
|
||||
elseif user_id and table_contains(room._data.politictalk_participant_jids, occupant_jid) then
|
||||
participants = add_unique(participants, user_id);
|
||||
end
|
||||
end
|
||||
|
||||
room._data.moderators = moderators;
|
||||
room._data.participants = participants;
|
||||
module:fire_event("room-metadata-changed", { room = room; });
|
||||
end
|
||||
|
||||
local function remove_duplicate_user_occupants(room, current_occupant, user_id)
|
||||
if not room or not current_occupant or not current_occupant.bare_jid or not user_id then
|
||||
return 0;
|
||||
end
|
||||
|
||||
ensure_room_data(room);
|
||||
|
||||
local duplicate_nicks = {};
|
||||
|
||||
for _, occupant in room:each_occupant() do
|
||||
local occupant_jid = occupant and occupant.bare_jid;
|
||||
local occupant_user_id = occupant_jid and room._data.politictalk_jid_user_ids[occupant_jid] or nil;
|
||||
|
||||
if
|
||||
occupant
|
||||
and occupant.nick
|
||||
and occupant_jid
|
||||
and occupant_jid ~= current_occupant.bare_jid
|
||||
and occupant_user_id == user_id
|
||||
then
|
||||
table.insert(duplicate_nicks, occupant.nick);
|
||||
end
|
||||
end
|
||||
|
||||
local removed_count = 0;
|
||||
|
||||
for _, duplicate_nick in ipairs(duplicate_nicks) do
|
||||
local ok, err = room:set_role(true, duplicate_nick, nil, "Another PoliticTalk session was opened for this account");
|
||||
|
||||
if ok == false then
|
||||
module:log(
|
||||
"warn",
|
||||
"Failed to remove duplicate PoliticTalk occupant: room=%s user=%s nick=%s error=%s",
|
||||
tostring(room.jid),
|
||||
tostring(user_id),
|
||||
tostring(duplicate_nick),
|
||||
tostring(err)
|
||||
);
|
||||
else
|
||||
removed_count = removed_count + 1;
|
||||
end
|
||||
end
|
||||
|
||||
if removed_count > 0 then
|
||||
rebuild_room_role_lists(room);
|
||||
module:log(
|
||||
"info",
|
||||
"Removed duplicate PoliticTalk occupants: room=%s user=%s count=%s",
|
||||
tostring(room.jid),
|
||||
tostring(user_id),
|
||||
tostring(removed_count)
|
||||
);
|
||||
end
|
||||
|
||||
return removed_count;
|
||||
end
|
||||
|
||||
local function get_occupant_count(room)
|
||||
if not room then
|
||||
return 0;
|
||||
@@ -343,8 +426,8 @@ local function notify_room_occupancy(room, reason)
|
||||
|
||||
ensure_room_data(room);
|
||||
|
||||
local participant_count = table_count(room._data.politictalk_participant_jids);
|
||||
local participant_user_ids = get_active_participant_user_ids(room);
|
||||
local participant_count = table_count(participant_user_ids);
|
||||
local moderator_count = table_count(room._data.moderators);
|
||||
local occupant_count = get_occupant_count(room);
|
||||
local host_count = get_active_host_count(room);
|
||||
@@ -424,15 +507,17 @@ module:hook("muc-occupant-pre-join", function(event)
|
||||
if not is_moderator then
|
||||
ensure_room_data(room);
|
||||
local participant_limit = get_participant_limit(session);
|
||||
local active_participant_count = table_count(room._data.politictalk_participant_jids);
|
||||
local active_participant_user_ids = get_active_participant_user_ids(room);
|
||||
local active_participant_count = table_count(active_participant_user_ids);
|
||||
local active_room_occupancy_count = active_participant_count;
|
||||
if
|
||||
participant_limit
|
||||
and not table_contains(active_participant_user_ids, user_id)
|
||||
and active_room_occupancy_count >= participant_limit
|
||||
then
|
||||
module:log(
|
||||
"warn",
|
||||
"Blocking participant %s because PoliticTalk room is full: room=%s active=%s participantSeats=%s limit=%s",
|
||||
"Blocking participant %s because PoliticTalk room is full: room=%s active=%s uniqueParticipants=%s limit=%s",
|
||||
tostring(user_id),
|
||||
tostring(room and room.jid),
|
||||
tostring(active_room_occupancy_count),
|
||||
@@ -469,6 +554,7 @@ module:hook("muc-occupant-joined", function(event)
|
||||
mark_host(room, occupant, role.user_id);
|
||||
update_room_role_lists(room, role.user_id, true);
|
||||
room:set_affiliation(true, occupant.bare_jid, "owner");
|
||||
remove_duplicate_user_occupants(room, occupant, role.user_id);
|
||||
module:log(
|
||||
"info",
|
||||
"PoliticTalk host joined as moderator with AV moderation: %s room=%s",
|
||||
@@ -479,6 +565,7 @@ module:hook("muc-occupant-joined", function(event)
|
||||
mark_participant(room, occupant, role.user_id);
|
||||
update_room_role_lists(room, role.user_id, false);
|
||||
room:set_affiliation(true, occupant.bare_jid, "member");
|
||||
remove_duplicate_user_occupants(room, occupant, role.user_id);
|
||||
module:log("info", "PoliticTalk participant joined as member: %s room=%s", tostring(role.user_id), tostring(room.jid));
|
||||
end
|
||||
|
||||
@@ -494,8 +581,7 @@ module:hook("muc-occupant-left", function(event)
|
||||
|
||||
if room and user_id then
|
||||
ensure_room_data(room);
|
||||
room._data.moderators = remove_value(room._data.moderators, user_id);
|
||||
room._data.participants = remove_value(room._data.participants, user_id);
|
||||
rebuild_room_role_lists(room);
|
||||
end
|
||||
|
||||
if was_host and not has_active_host(room) then
|
||||
|
||||
Reference in New Issue
Block a user