feat: Move to use cjson everywhere.

We were using prosody,util.json and cjson at the same time, but the latter is more performant.
Adds some error handling which were missing with the prosody util json one.
This commit is contained in:
damencho
2024-04-05 07:58:29 -05:00
committed by Дамян Минков
parent 9fc89ba04e
commit a76f9d548b
15 changed files with 128 additions and 44 deletions

View File

@@ -5,7 +5,7 @@ local internal_room_jid_match_rewrite = util.internal_room_jid_match_rewrite;
local room_jid_match_rewrite = util.room_jid_match_rewrite;
local process_host_module = util.process_host_module;
local array = require "util.array";
local json = require 'util.json';
local json = require 'cjson.safe';
local st = require 'util.stanza';
local muc_component_host = module:get_option_string('muc_component');
@@ -49,7 +49,12 @@ function notify_occupants_enable(jid, enable, room, actorJid, mediaType)
body_json.room = internal_room_jid_match_rewrite(room.jid);
body_json.actor = actorJid;
body_json.mediaType = mediaType;
local body_json_str = json.encode(body_json);
local body_json_str, error = json.encode(body_json);
if not body_json_str then
module:log('error', 'error encoding json room:%s error:%s', room.jid, error);
return;
end
if jid then
send_json_message(jid, body_json_str)
@@ -75,12 +80,23 @@ function notify_whitelist_change(jid, moderators, room, mediaType, removed)
body_json.removed = true;
end
body_json.mediaType = mediaType;
local moderators_body_json_str = json.encode(body_json);
local moderators_body_json_str, error = json.encode(body_json);
if not moderators_body_json_str then
module:log('error', 'error encoding moderator json room:%s error:%s', room.jid, error);
return;
end
body_json.whitelists = nil;
if not removed then
body_json.approved = true; -- we want to send to participants only that they were approved to unmute
end
local participant_body_json_str = json.encode(body_json);
local participant_body_json_str, error = json.encode(body_json);
if not participant_body_json_str then
module:log('error', 'error encoding participant json room:%s error:%s', room.jid, error);
return;
end
for _, occupant in room:each_occupant() do
if moderators and occupant.role == 'moderator' then
@@ -110,7 +126,13 @@ function notify_jid_approved(jid, from, room, mediaType)
body_json.mediaType = mediaType;
body_json.from = from;
send_json_message(jid, json.encode(body_json));
local json_message, error = json.encode(body_json);
if not json_message then
module:log('error', 'skip sending json message to:%s error:%s', jid, error);
return;
end
send_json_message(jid, json_message);
end
-- receives messages from clients to the component sending A/V moderation enable/disable commands or adding

View File

@@ -1,7 +1,7 @@
-- DEPRECATED and will be removed, giving time for mobile clients to update
local st = require "util.stanza";
local socket = require "socket";
local json = require "util.json";
local json = require 'cjson.safe';
local it = require "util.iterators";
local process_host_module = module:require "util".process_host_module;

View File

@@ -29,7 +29,7 @@ end
local jid_node = require 'util.jid'.node;
local jid_host = require 'util.jid'.host;
local jid_split = require 'util.jid'.split;
local json = require 'util.json';
local json = require 'cjson.safe';
local st = require 'util.stanza';
local uuid_gen = require 'util.uuid'.generate;
@@ -166,13 +166,18 @@ function broadcast_breakout_rooms(room_jid)
end
end
local json_msg = json.encode({
local json_msg, error = json.encode({
type = BREAKOUT_ROOMS_IDENTITY_TYPE,
event = JSON_TYPE_UPDATE_BREAKOUT_ROOMS,
roomCounter = main_room._data.breakout_rooms_counter,
rooms = rooms
});
if not json_msg then
module:log('error', 'not broadcasting breakout room information room:%s error:%s', main_room_jid, error);
return;
end
for _, occupant in main_room:each_occupant() do
if jid_node(occupant.jid) ~= 'focus' then
send_json_msg(occupant.jid, json_msg)
@@ -329,12 +334,16 @@ function on_message(event)
local participant_jid = message.attr.participantJid;
local target_room_jid = message.attr.roomJid;
local json_msg = json.encode({
local json_msg, error = json.encode({
type = BREAKOUT_ROOMS_IDENTITY_TYPE,
event = JSON_TYPE_MOVE_TO_ROOM_REQUEST,
roomJid = target_room_jid
});
if not json_msg then
module:log('error', 'skip sending request room:%s error:%s', room.jid, error);
end
send_json_msg(participant_jid, json_msg)
return true;
end

View File

@@ -19,7 +19,7 @@
-- network filters
local jid = require "util.jid";
local json = require "util.json";
local json = require 'cjson.safe';
local iterators = require "util.iterators";
local util = module:require "util";
local is_healthcheck_room = util.is_healthcheck_room;

View File

@@ -6,7 +6,7 @@ local jid_split = require "util.jid".split;
local hashes = require "util.hashes";
local random = require "util.random";
local st = require("util.stanza");
local json = require "util.json";
local json = require 'cjson.safe';
local util = module:require "util";
local async_handler_wrapper = util.async_handler_wrapper;
local process_host_module = util.process_host_module;
@@ -133,7 +133,12 @@ local function handle_jigasi_invite(event)
module:log("warn", "Wrong content type: %s or missing payload", request.headers.content_type);
return { status_code = 400; }
end
local payload = json.decode(request.body);
local payload, error = json.decode(request.body);
if not payload then
module:log('error', 'Cannot decode json error:%s', error);
return { status_code = 400; }
end
local conference = payload["conference"];
local phone_no = payload["phoneNo"];

View File

@@ -8,7 +8,7 @@ local starts_with = util.starts_with;
local formdecode = require "util.http".formdecode;
local urlencode = require "util.http".urlencode;
local jid = require "util.jid";
local json = require "util.json";
local json = require 'cjson.safe';
local muc_domain_prefix = module:get_option_string("muc_mapper_domain_prefix", "conference");
@@ -107,9 +107,9 @@ function handle_kick_participant (event)
return { status_code = 400; }
end
local params = json.decode(request.body);
local params, error = json.decode(request.body);
if not params then
module:log("warn", "Missing params");
module:log("warn", "Missing params error:%s", error);
return { status_code = 400; }
end

View File

@@ -30,7 +30,7 @@ local jid_bare = require 'util.jid'.bare;
local jid_prep = require "util.jid".prep;
local jid_resource = require "util.jid".resource;
local resourceprep = require "util.encodings".stringprep.resourceprep;
local json = require 'util.json';
local json = require 'cjson.safe';
local filters = require 'util.filters';
local st = require 'util.stanza';
local muc_util = module:require "muc/util";
@@ -80,10 +80,17 @@ function broadcast_json_msg(room, from, json_msg)
local occupant = room:get_occupant_by_real_jid(from);
if occupant then
local json_msg_str, error = json.encode(json_msg);
if not json_msg_str then
module:log('error', 'Error broadcasting message room:%s', room.jid, error);
return;
end
room:broadcast_message(
st.message({ type = 'groupchat', from = occupant.nick })
:tag('json-message', {xmlns='http://jitsi.org/jitmeet'})
:text(json.encode(json_msg)):up());
:text(json_msg_str):up());
end
end

View File

@@ -2,7 +2,7 @@ local inspect = require "inspect";
local formdecode = require "util.http".formdecode;
local urlencode = require "util.http".urlencode;
local jid = require "util.jid";
local json = require "util.json";
local json = require 'cjson.safe';
local util = module:require "util";
local async_handler_wrapper = util.async_handler_wrapper;
local starts_with = util.starts_with;
@@ -106,9 +106,9 @@ function handle_validate_room_password (event)
return { status_code = 400; }
end
local params = json.decode(request.body);
local params, error = json.decode(request.body);
if not params then
module:log("warn", "Missing params");
module:log("warn", "Missing params error:%s", error);
return { status_code = 400; }
end
@@ -125,9 +125,15 @@ function handle_validate_room_password (event)
return { status_code = error_code; }
end
local json_msg_str, error_encode = json.encode({ valid = (room:get_password() == passcode) });
if not json_msg_str then
module:log('error', 'Cannot encode json room:%s error:%s', room.jid, error_encode);
return { status_code = 400; };
end
local PUT_response = {
headers = { content_type = "application/json"; };
body = json.encode({ valid = (room:get_password() == passcode) })
body = json_msg_str;
};
-- module:log("debug","Sending response for room password validate: %s", inspect(PUT_response));
@@ -150,11 +156,17 @@ function handle_get_room_password (event)
room_details["passcodeProtected"] = room:get_password() ~= nil;
room_details["lobbyEnabled"] = room._data ~= nil and room._data.lobbyroom ~= nil;
local json_msg_str, error = json.encode(room_details);
if not json_msg_str then
module:log('error', 'Cannot encode json room:%s error:%s', room.jid, error);
return { status_code = 400; };
end
local GET_response = {
headers = {
content_type = "application/json";
};
body = json.encode(room_details);
body = json_msg_str;
};
-- module:log("debug","Sending response for room password: %s", inspect(GET_response));

View File

@@ -4,7 +4,7 @@
local jid = require "util.jid";
local it = require "util.iterators";
local json = require "util.json";
local json = require 'cjson.safe';
local iterators = require "util.iterators";
local array = require"util.array";

View File

@@ -2,7 +2,7 @@
-- by keeping track of the state of polls in each room, and sending
-- that state to new participants when they join.
local json = require("util.json");
local json = require 'cjson.safe';
local st = require("util.stanza");
local jid = require "util.jid";
local util = module:require("util");
@@ -22,8 +22,11 @@ local function get_poll_message(stanza)
if json_data == nil then
return nil;
end
local data = json.decode(json_data);
local data, error = json.decode(json_data);
if not data or (data.type ~= "new-poll" and data.type ~= "answer-poll") then
if error then
module:log('error', 'Error decoding data error:%s', error);
end
return nil;
end
return data;
@@ -192,12 +195,17 @@ module:hook("muc-occupant-joined", function(event)
};
end
local json_msg_str, error = json.encode(data);
if not json_msg_str then
module:log('error', 'Error encoding data room:%s error:%s', room.jid, error);
end
local stanza = st.message({
from = room.jid,
to = event.occupant.jid
})
:tag("json-message", { xmlns = "http://jitsi.org/jitmeet" })
:text(json.encode(data))
:text(json_msg_str)
:up();
room:route_stanza(stanza);
end);

View File

@@ -72,7 +72,7 @@
local jid = require 'util.jid';
local http = require "net.http";
local json = require "util.json";
local json = require 'cjson.safe';
local st = require "util.stanza";
local timer = require 'util.timer';
local datetime = require 'util.datetime';
@@ -396,10 +396,10 @@ end
-- Ref: https://github.com/jitsi/jicofo/blob/master/doc/reservation.md
-- @return nil if invalid, or table with payload parsed from JSON response
function RoomReservation:parse_conference_response(response_body)
local data = json.decode(response_body);
local data, error = json.decode(response_body);
if data == nil then -- invalid JSON payload
module:log("error", "Invalid JSON response from API - %s", response_body);
module:log("error", "Invalid JSON response from API - %s error:%s", response_body, error);
return;
end

View File

@@ -12,7 +12,7 @@
-- breakout_rooms_component = "breakout.jitmeet.example.com"
local jid_node = require 'util.jid'.node;
local json = require 'util.json';
local json = require 'cjson.safe';
local st = require 'util.stanza';
local util = module:require 'util';
@@ -39,10 +39,16 @@ module:log("info", "Starting room metadata for %s", muc_component_host);
-- Utility functions
function getMetadataJSON(room)
return json.encode({
local res, error = json.encode({
type = COMPONENT_IDENTITY_TYPE,
metadata = room.jitsiMetadata or {}
});
if not res then
module:log('error', 'Error encoding data room:%s', room.jid, error);
end
return res;
end
-- Putting the information on the config form / disco-info allows us to save
@@ -124,9 +130,9 @@ function on_message(event)
return false;
end
local jsonData = json.decode(messageText);
local jsonData, error = json.decode(messageText);
if jsonData == nil then -- invalid JSON
module:log("error", "Invalid JSON message: %s", messageText);
module:log("error", "Invalid JSON message: %s error:%s", messageText, error);
return false;
end

View File

@@ -6,7 +6,7 @@ local process_host_module = util.process_host_module;
local jid_resource = require "util.jid".resource;
local st = require "util.stanza";
local socket = require "socket";
local json = require "util.json";
local json = require 'cjson.safe';
local um_is_admin = require "core.usermanager".is_admin;
local jid_split = require 'util.jid'.split;
@@ -264,13 +264,19 @@ function occupant_joined(event)
body_json.type = 'speakerstats';
body_json.users = users_json;
local stanza = st.message({
from = module.host;
to = occupant.jid; })
:tag("json-message", {xmlns='http://jitsi.org/jitmeet'})
:text(json.encode(body_json)):up();
local json_msg_str, error = json.encode(body_json);
room:route_stanza(stanza);
if json_msg_str then
local stanza = st.message({
from = module.host;
to = occupant.jid; })
:tag("json-message", {xmlns='http://jitsi.org/jitmeet'})
:text(json_msg_str):up();
room:route_stanza(stanza);
else
module:log('error', 'Error encoding room:%s error:%s', room.jid, error);
end
end
end

View File

@@ -5,7 +5,7 @@ local ext_services = module:depends("external_services");
local get_services = ext_services.get_services;
local async_handler_wrapper = module:require "util".async_handler_wrapper;
local json = require "util.json";
local json = require 'cjson.safe';
--- Handles request for retrieving turn credentials
-- @param event the http event, holds the request query

View File

@@ -14,7 +14,7 @@ local is_sip_jibri_join = util.is_sip_jibri_join;
local process_host_module = util.process_host_module;
local new_id = require 'util.id'.medium;
local um_is_admin = require 'core.usermanager'.is_admin;
local json = require 'util.json';
local json = require 'cjson.safe';
local inspect = require 'inspect';
local MUC_NS = 'http://jabber.org/protocol/muc';
@@ -127,7 +127,13 @@ local function request_promotion_received(room, from_jid, from_vnode, nick, time
body_json.on = false;
end
local msg_to_send = json.encode(body_json);
local msg_to_send, error = json.encode(body_json);
if not msg_to_send then
module:log('error', 'Error encoding msg room:%s error:%s', room.jid, error)
return true;
end
if visitors_promotion_requests[room.jid] then
visitors_promotion_requests[room.jid][from_jid] = {
msg = msg_to_send;
@@ -386,9 +392,12 @@ process_host_module(muc_domain_prefix..'.'..muc_domain_base, function(host_modul
if json_data == nil then
return;
end
local data = json.decode(json_data);
local data, error = json.decode(json_data);
if not data or data.type ~= 'visitors'
or (data.action ~= "promotion-response" and data.action ~= "demote-request") then
if error then
module:log('error', 'Error decoding error:%s', error);
end
return;
end