function on_message()

in resources/prosody-plugins/mod_av_moderation_component.lua [116:256]


function on_message(event)
    local session = event.origin;

    
    if event.stanza.attr.type == 'error' then
        return; 
    end

    if not session or not session.jitsi_web_query_room then
        return false;
    end

    local moderation_command = event.stanza:get_child('av_moderation');

    if moderation_command then
        
        local room = get_room_by_name_and_subdomain(session.jitsi_web_query_room, session.jitsi_web_query_prefix);

        if not room then
            module:log('warn', 'No room found found for %s/%s',
                    session.jitsi_web_query_prefix, session.jitsi_web_query_room);
            return false;
        end

        
        local from = event.stanza.attr.from;
        local occupant = room:get_occupant_by_real_jid(from);
        if not occupant then
            log('warn', 'No occupant %s found for %s', from, room.jid);
            return false;
        end
        if occupant.role ~= 'moderator' then
            log('warn', 'Occupant %s is not moderator and not allowed this operation for %s', from, room.jid);
            return false;
        end

        local mediaType = moderation_command.attr.mediaType;
        if mediaType then
            if mediaType ~= 'audio' and mediaType ~= 'video' then
                module:log('warn', 'Wrong mediaType %s for %s', mediaType, room.jid);
                return false;
            end
        else
            module:log('warn', 'Missing mediaType for %s', room.jid);
            return false;
        end

        if moderation_command.attr.enable ~= nil then
            local enabled;
            if moderation_command.attr.enable == 'true' then
                enabled = true;
                if room.av_moderation and room.av_moderation[mediaType] then
                    module:log('warn', 'Concurrent moderator enable/disable request or something is out of sync');
                    return true;
                else
                    if not room.av_moderation then
                        room.av_moderation = {};
                        room.av_moderation_actors = {};
                    end
                    room.av_moderation[mediaType] = array{};
                    room.av_moderation_actors[mediaType] = occupant.nick;
                end
            else
                enabled = false;
                if not room.av_moderation then
                    module:log('warn', 'Concurrent moderator enable/disable request or something is out of sync');
                    return true;
                else
                    room.av_moderation[mediaType] = nil;
                    room.av_moderation_actors[mediaType] = nil;

                    
                    local is_empty = true;
                    for key,_ in pairs(room.av_moderation) do
                        if room.av_moderation[key] then
                            is_empty = false;
                        end
                    end
                    if is_empty then
                        room.av_moderation = nil;
                    end
                end
            end

            
            notify_occupants_enable(nil, enabled, room, occupant.nick, mediaType);
            return true;
        elseif moderation_command.attr.jidToWhitelist then
            local occupant_jid = moderation_command.attr.jidToWhitelist;
            
            
            local occupant_to_add = room:get_occupant_by_nick(room_jid_match_rewrite(occupant_jid));
            if not occupant_to_add then
                module:log('warn', 'No occupant %s found for %s', occupant_jid, room.jid);
                return false;
            end

            if room.av_moderation then
                local whitelist = room.av_moderation[mediaType];
                if not whitelist then
                    whitelist = array{};
                    room.av_moderation[mediaType] = whitelist;
                end
                whitelist:push(occupant_jid);

                notify_whitelist_change(occupant_to_add.jid, true, room, mediaType, false);

                return true;
            else
                
                
                notify_jid_approved(occupant_to_add.jid, occupant.nick, room, mediaType);
            end
        elseif moderation_command.attr.jidToBlacklist then
            local occupant_jid = moderation_command.attr.jidToBlacklist;
            
            
            local occupant_to_remove = room:get_occupant_by_nick(room_jid_match_rewrite(occupant_jid));
            if not occupant_to_remove then
                module:log('warn', 'No occupant %s found for %s', occupant_jid, room.jid);
                return false;
            end

            if room.av_moderation then
                local whitelist = room.av_moderation[mediaType];
                if whitelist then
                    local index = get_index_in_table(whitelist, occupant_jid)
                    if(index) then
                        whitelist:pop(index);
                        notify_whitelist_change(occupant_to_remove.jid, true, room, mediaType, true);
                    end
                end

                return true;
            end
        end
    end

    
    return false
end