Skip to content

Commit

Permalink
Changing jiffy:encode to [jiffy:encode] everywhere in the code to che…
Browse files Browse the repository at this point in the history
…ck where returning io_list except of binary causes problems
  • Loading branch information
JanuszJakubiec committed Dec 6, 2024
1 parent ae7aad7 commit d0a1572
Show file tree
Hide file tree
Showing 24 changed files with 74 additions and 39 deletions.
3 changes: 2 additions & 1 deletion src/ejabberd_ctl.erl
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,8 @@ handle_graphql_result({ok, Result}) ->
end;
handle_graphql_result({error, Reason}) ->
{_Code, Error} = mongoose_graphql_errors:format_error(Reason),
JSONResult = jiffy:encode(#{errors => [Error]}, [pretty]),
%CHANGED
JSONResult = [jiffy:encode(#{errors => [Error]}, [pretty])],
?PRINT("~s\n", [JSONResult]),
?STATUS_ERROR.

Expand Down
3 changes: 2 additions & 1 deletion src/event_pusher/mod_event_pusher_push_rdbms.erl
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,8 @@ decode_row({NodeID, PubSubBin, FormJSON}) ->
decode_form(FormJSON)}.

encode_form(Form) ->
jiffy:encode(Form).
%CHANGED
[jiffy:encode(Form)].

Check warning on line 86 in src/event_pusher/mod_event_pusher_push_rdbms.erl

View check run for this annotation

Codecov / codecov/patch

src/event_pusher/mod_event_pusher_push_rdbms.erl#L86

Added line #L86 was not covered by tests

decode_form(FormJSON) ->
jiffy:decode(FormJSON, [return_maps]).
Expand Down
4 changes: 2 additions & 2 deletions src/event_pusher/mod_event_pusher_rabbit.erl
Original file line number Diff line number Diff line change
Expand Up @@ -178,15 +178,15 @@ user_topic_routing_key(JID, Topic) ->
-spec presence_msg(JID :: jid:jid(), Status :: atom()) -> binary().
presence_msg(JID, Status) ->
Msg = #{user_id => jid:to_binary(jid:to_lower(JID)), present => is_user_online(Status)},
jiffy:encode(Msg).
iolist_to_binary(jiffy:encode(Msg)).

-spec chat_msg(From :: jid:jid(), To :: jid:jid(), UserMsg :: binary()) ->
binary().
chat_msg(From, To, UserMsg) ->
Msg = #{to_user_id => jid:to_binary(jid:to_lower(To)),
message => UserMsg,
from_user_id => jid:to_binary(jid:to_lower(From))},
jiffy:encode(Msg).
iolist_to_binary(jiffy:encode(Msg)).

-spec is_user_online(online | offline) -> boolean().
is_user_online(online) -> true;
Expand Down
3 changes: 2 additions & 1 deletion src/graphql/mongoose_graphql_handler.erl
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,8 @@ make_error(Phase, Term) ->

reply_error(Msg, Req, State) ->
{Code, Error} = mongoose_graphql_errors:format_error(Msg),
Body = jiffy:encode(#{errors => [Error]}),
%CHANGED
Body = [jiffy:encode(#{errors => [Error]})],
Req2 = cowboy_req:set_resp_body(Body, Req),
Reply = cowboy_req:reply(Code, Req2),
{stop, Reply, State}.
Expand Down
3 changes: 2 additions & 1 deletion src/graphql/mongoose_graphql_sse_handler.erl
Original file line number Diff line number Diff line change
Expand Up @@ -107,5 +107,6 @@ make_error(Phase, Term) ->

reply_error(Reason, Req, State) ->
{Code, Error} = mongoose_graphql_errors:format_error(Reason),
Body = jiffy:encode(#{errors => [Error]}),
%CHANGED
Body = [jiffy:encode(#{errors => [Error]})],
{shutdown, Code, #{}, Body, Req, State}.
3 changes: 2 additions & 1 deletion src/mod_caps_rdbms.erl
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ read(HostType, {Node, SubNode}) ->
-spec write(mongooseim:host_type(), mod_caps:node_pair(),
mod_caps:maybe_pending_features()) -> ok.
write(HostType, {Node, SubNode}, Features) ->
Encoded = jiffy:encode(Features),
%CHANGED
Encoded = [jiffy:encode(Features)],

Check warning on line 35 in src/mod_caps_rdbms.erl

View check run for this annotation

Codecov / codecov/patch

src/mod_caps_rdbms.erl#L35

Added line #L35 was not covered by tests
InsertParams = [Node, SubNode, Encoded],
UpdateParams = [Encoded],
UniqueKeyValues = [Node, SubNode],
Expand Down
3 changes: 2 additions & 1 deletion src/mod_push_service_mongoosepush.erl
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,8 @@ push_notifications(AccIn,
ReqHeaders = [{<<"content-type">>, <<"application/json">>}],
{ok, JSON} =
make_notification(Notification, Options),
Payload = jiffy:encode(JSON),
%CHANGED
Payload = [jiffy:encode(JSON)],
call(Host, ?MODULE, http_notification, [Host, post, Path, ReqHeaders, Payload])
end,
{ok, send_push_notifications(Notifications, Fun, ok)}.
Expand Down
6 changes: 4 additions & 2 deletions src/mongoose_admin_api/mongoose_admin_api.erl
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,8 @@ resource_created(Req, State, Path, Body) ->
%% @doc Send response when it can't be returned in a tuple from the handler (e.g. for DELETE)
-spec respond(req(), state(), jiffy:json_value()) -> {stop, req(), state()}.
respond(Req, State, Response) ->
Req2 = cowboy_req:set_resp_body(jiffy:encode(Response), Req),
%CHANGED
Req2 = cowboy_req:set_resp_body([jiffy:encode(Response)], Req),
Req3 = cowboy_req:reply(200, Req2),
{stop, Req3, State}.

Expand All @@ -150,7 +151,8 @@ error_response(ErrorType, Message, Req, State) ->
error_type => ErrorType,
message => BinMessage,
req => Req}),
Req1 = cowboy_req:reply(error_code(ErrorType), #{}, jiffy:encode(BinMessage), Req),
%CHANGED
Req1 = cowboy_req:reply(error_code(ErrorType), #{}, [jiffy:encode(BinMessage)], Req),
{stop, Req1, State}.

-spec error_code(error_type()) -> non_neg_integer().
Expand Down
3 changes: 2 additions & 1 deletion src/mongoose_admin_api/mongoose_admin_api_contacts.erl
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,8 @@ handle_get(Req, State) ->
UserJid = get_user_jid(Bindings),
case mod_roster_api:list_contacts(UserJid) of
{ok, Rosters} ->
{jiffy:encode(lists:map(fun roster_info/1, Rosters)), Req, State};
%CHANGED
{[jiffy:encode(lists:map(fun roster_info/1, Rosters))], Req, State};
{unknown_domain, Reason} ->
throw_error(not_found, Reason)
end.
Expand Down
3 changes: 2 additions & 1 deletion src/mongoose_admin_api/mongoose_admin_api_domain.erl
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,8 @@ handle_get(Req, State) ->
Domain = get_domain(Bindings),
case mongoose_domain_api:get_domain_details(Domain) of
{ok, Props} ->
{jiffy:encode(maps:with([host_type, status], Props)), Req, State};
%CHANGED
{[jiffy:encode(maps:with([host_type, status], Props))], Req, State};
{not_found, Msg} ->
throw_error(not_found, Msg);
{_, Msg} ->
Expand Down
3 changes: 2 additions & 1 deletion src/mongoose_admin_api/mongoose_admin_api_messages.erl
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,8 @@ handle_get(Req, State) ->
case mongoose_stanza_api:lookup_recent_messages(OwnerJid, WithJid, Before, Limit, true) of
{ok, {Rows, _Limit}} ->
Messages = lists:map(fun row_to_map/1, Rows),
{jiffy:encode(Messages), Req, State};
%CHANGED
{[jiffy:encode(Messages)], Req, State};
{unknown_user, Msg} ->
throw_error(bad_request, Msg)
end.
Expand Down
12 changes: 8 additions & 4 deletions src/mongoose_admin_api/mongoose_admin_api_metrics.erl
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,8 @@ handle_get(Req, State = #{suffix := all}) ->
end;
all_metrics ->
Values = get_sum_metrics(),
{jiffy:encode(#{metrics => Values}), Req, State}
%CHANGED
{[jiffy:encode(#{metrics => Values})], Req, State}
end;
handle_get(Req, State = #{suffix := global}) ->
Bindings = cowboy_req:bindings(Req),
Expand All @@ -80,15 +81,17 @@ handle_get(Req, State) ->
Reply = #{host_types => HostTypes,
metrics => lists:map(fun prepare_name/1, Metrics),
global => lists:map(fun prepare_name/1, Global)},
{jiffy:encode(Reply), Req, State}
%CHANGED
{[jiffy:encode(Reply)], Req, State}
end.

handle_get_values(Req, State, Bindings, HostType) ->
case get_metric_name(Bindings) of
{metric, Metric} ->
case mongoose_instrument_exometer:get_metric_value(HostType, Metric) of
{ok, Value} ->
{jiffy:encode(#{metric => prepare_value(Value)}), Req, State};
%CHANGED
{[jiffy:encode(#{metric => prepare_value(Value)})], Req, State};
_Other ->
throw_error(not_found, <<"Metric not found">>)
end;
Expand All @@ -98,7 +101,8 @@ handle_get_values(Req, State, Bindings, HostType) ->
throw_error(not_found, <<"No metrics found">>);
Metrics ->
Values = prepare_metrics(Metrics),
{jiffy:encode(#{metrics => Values}), Req, State}
%CHANGED
{[jiffy:encode(#{metrics => Values})], Req, State}
end
end.

Expand Down
3 changes: 2 additions & 1 deletion src/mongoose_admin_api/mongoose_admin_api_sessions.erl
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,8 @@ delete_resource(Req, State) ->
handle_get(Req, State) ->
#{domain := Domain} = cowboy_req:bindings(Req),
{ok, Sessions} = mongoose_session_api:list_resources(Domain),
{jiffy:encode(Sessions), Req, State}.
%CHANGED
{[jiffy:encode(Sessions)], Req, State}.

handle_delete(Req, State) ->
#{domain := Domain} = Bindings = cowboy_req:bindings(Req),
Expand Down
3 changes: 2 additions & 1 deletion src/mongoose_admin_api/mongoose_admin_api_users.erl
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,8 @@ handle_get(Req, State) ->
#{domain := Domain} = cowboy_req:bindings(Req),
Users = mongoose_account_api:list_users(Domain),
{_, UsersList} = Users,
{jiffy:encode(UsersList), Req, State}.
%CHANGED
{[jiffy:encode(UsersList)], Req, State}.

handle_post(Req, State) ->
#{domain := Domain} = cowboy_req:bindings(Req),
Expand Down
3 changes: 2 additions & 1 deletion src/mongoose_client_api/mongoose_client_api.erl
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,8 @@ error_response(ErrorType, Message, Req, State) ->
error_type => ErrorType,
message => BinMessage,
req => Req}),
Req1 = cowboy_req:reply(error_code(ErrorType), #{}, jiffy:encode(BinMessage), Req),
%CHANGED
Req1 = cowboy_req:reply(error_code(ErrorType), #{}, [jiffy:encode(BinMessage)], Req),
{stop, Req1, State}.

-spec error_code(error_type()) -> non_neg_integer().
Expand Down
6 changes: 4 additions & 2 deletions src/mongoose_client_api/mongoose_client_api_contacts.erl
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,8 @@ handle_get(Req, State = #{jid := UserJid}) ->
Bindings = cowboy_req:bindings(Req),
assert_no_jid(Bindings),
{ok, Contacts} = mod_roster_api:list_contacts(UserJid),
{jiffy:encode(lists:map(fun roster_info/1, Contacts)), Req, State}.
%CHANGED
{[jiffy:encode(lists:map(fun roster_info/1, Contacts))], Req, State}.

handle_post(Req, State = #{jid := UserJid}) ->
Args = parse_body(Req),
Expand Down Expand Up @@ -109,7 +110,8 @@ handle_delete(Req, State = #{jid := UserJid}) ->
ContactJids = get_jids_to_delete(Args),
NotDeleted = delete_contacts(UserJid, ContactJids),
RespBody = #{not_deleted => lists:map(fun jid:to_binary/1, NotDeleted)},
Req2 = cowboy_req:set_resp_body(jiffy:encode(RespBody), Req),
%CHANGED
Req2 = cowboy_req:set_resp_body([jiffy:encode(RespBody)], Req),
Req3 = cowboy_req:set_resp_header(<<"content-type">>, <<"application/json">>, Req2),
{true, Req3, State};
ContactJid ->
Expand Down
6 changes: 4 additions & 2 deletions src/mongoose_client_api/mongoose_client_api_messages.erl
Original file line number Diff line number Diff line change
Expand Up @@ -77,14 +77,16 @@ handle_get(Req, State = #{jid := OwnerJid}) ->
{ok, {Rows, _Limit}} =
mongoose_stanza_api:lookup_recent_messages(OwnerJid, WithJid, Before, Limit, false),
Resp = [make_json_msg(Msg, MAMId) || #{id := MAMId, packet := Msg} <- Rows],
{jiffy:encode(Resp), Req, State}.
%CHANGED
{[jiffy:encode(Resp)], Req, State}.

handle_post(Req, State = #{jid := UserJid}) ->
Args = parse_body(Req),
To = get_to(Args),
Body = get_body(Args),
{ok, Resp} = mongoose_stanza_api:send_chat_message(UserJid, undefined, To, Body),
Req2 = cowboy_req:set_resp_body(jiffy:encode(Resp), Req),
%CHANGED
Req2 = cowboy_req:set_resp_body([jiffy:encode(Resp)], Req),
{true, Req2, State}.

get_limit(#{limit := LimitBin}) ->
Expand Down
9 changes: 6 additions & 3 deletions src/mongoose_client_api/mongoose_client_api_rooms.erl
Original file line number Diff line number Diff line change
Expand Up @@ -81,11 +81,13 @@ handle_get(Req, State = #{jid := UserJid}) ->
case get_room_jid(Bindings, State, optional) of
undefined ->
{ok, Rooms} = mod_muc_light_api:get_user_rooms(UserJid),
{jiffy:encode(lists:flatmap(fun room_us_to_json/1, Rooms)), Req, State};
%CHANGED
{[jiffy:encode(lists:flatmap(fun room_us_to_json/1, Rooms))], Req, State};
RoomJid ->
case mod_muc_light_api:get_room_info(RoomJid, UserJid) of
{ok, Info} ->
{jiffy:encode(room_info_to_json(Info)), Req, State};
%CHANGED
{[jiffy:encode(room_info_to_json(Info))], Req, State};
{room_not_found, Msg} ->
throw_error(not_found, Msg);
{not_room_member, Msg} ->
Expand Down Expand Up @@ -118,7 +120,8 @@ handle_put(Req, State = #{jid := UserJid}) ->

room_created(Req, State, RoomJid) ->
RespBody = #{<<"id">> => RoomJid#jid.luser},
Req2 = cowboy_req:set_resp_body(jiffy:encode(RespBody), Req),
%CHANGED
Req2 = cowboy_req:set_resp_body([jiffy:encode(RespBody)], Req),
Req3 = cowboy_req:reply(201, Req2),
{stop, Req3, State}.

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,8 @@ handle_get(Req, State = #{jid := UserJid}) ->
case mod_muc_light_api:get_room_messages(RoomJid, UserJid, Limit, Before) of
{ok, Msgs} ->
JSONData = [make_json_item(Msg) || Msg <- Msgs],
{jiffy:encode(JSONData), Req, State};
%CHANGED
{[jiffy:encode(JSONData)], Req, State};
{room_not_found, Msg} ->
throw_error(not_found, Msg);
{not_room_member, Msg} ->
Expand All @@ -92,7 +93,8 @@ handle_post(Req, State = #{jid := UserJid}) ->
case mod_muc_light_api:send_message(RoomJid, UserJid, Children, Attrs) of
{ok, _} ->
Resp = #{id => UUID},
Req3 = cowboy_req:set_resp_body(jiffy:encode(Resp), Req),
%CHANGED
Req3 = cowboy_req:set_resp_body([jiffy:encode(Resp)], Req),
{true, Req3, State};
{room_not_found, Msg} ->
throw_error(not_found, Msg);
Expand Down
4 changes: 2 additions & 2 deletions src/mongoose_client_api/mongoose_client_api_sse.erl
Original file line number Diff line number Diff line change
Expand Up @@ -64,11 +64,11 @@ terminate(_Reason, _Req, _State) ->
ok.

maybe_send_message_event(<<"chat">>, Packet, Timestamp, #{id := ID} = State) ->
Data = jiffy:encode(mongoose_client_api_messages:encode(Packet, Timestamp)),
Data = iolist_to_binary(jiffy:encode(mongoose_client_api_messages:encode(Packet, Timestamp))),
Event = #{id => integer_to_binary(ID), event => <<"message">>, data => Data},
{send, Event, State#{id := ID + 1}};
maybe_send_message_event(<<"groupchat">>, Packet, Timestamp, #{id := ID} = State) ->
Data = jiffy:encode(mongoose_client_api_rooms_messages:encode(Packet, Timestamp)),
Data = iolist_to_binary(jiffy:encode(mongoose_client_api_rooms_messages:encode(Packet, Timestamp))),
Event = #{id => integer_to_binary(ID), event => <<"room.message">>, data => Data},
{send, Event, State#{id := ID + 1}};
maybe_send_message_event(_, _, _, State) ->
Expand Down
3 changes: 2 additions & 1 deletion src/muc/mod_muc_rdbms.erl
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,8 @@ remove_domain(HostType, MucHost, Domain) ->
store_room(HostType, MucHost, RoomName, Opts) ->
Affs = proplists:get_value(affiliations, Opts),
NewOpts = proplists:delete(affiliations, Opts),
ExtOpts = jiffy:encode({NewOpts}),
%CHANGED
ExtOpts = [jiffy:encode({NewOpts})],
F = fun() ->
forget_room_transaction(HostType, MucHost, RoomName),
store_room_transaction(HostType, MucHost, RoomName, ExtOpts, Affs)
Expand Down
15 changes: 10 additions & 5 deletions src/pubsub/mod_pubsub_db_rdbms.erl
Original file line number Diff line number Diff line change
Expand Up @@ -711,8 +711,10 @@ del_node(Nidx) ->
set_node(#pubsub_node{nodeid = {Key, Name}, id = undefined, type = Type,
owners = Owners, options = Opts, parents = Parents}) ->
ExtKey = encode_key(Key),
ExtOwners = jiffy:encode([jid:to_binary(Owner) || Owner <- Owners]),
ExtOpts = jiffy:encode({Opts}),
%CHANGED
ExtOwners = [jiffy:encode([jid:to_binary(Owner) || Owner <- Owners])],

Check warning on line 715 in src/pubsub/mod_pubsub_db_rdbms.erl

View check run for this annotation

Codecov / codecov/patch

src/pubsub/mod_pubsub_db_rdbms.erl#L715

Added line #L715 was not covered by tests
%CHANGED
ExtOpts = [jiffy:encode({Opts})],

Check warning on line 717 in src/pubsub/mod_pubsub_db_rdbms.erl

View check run for this annotation

Codecov / codecov/patch

src/pubsub/mod_pubsub_db_rdbms.erl#L717

Added line #L717 was not covered by tests
{updated, 1} = execute_insert_pubsub_node(ExtKey, Name, Type, ExtOwners, ExtOpts),
{selected, [Row]} = execute_select_node_by_key_and_name(ExtKey, Name),
#pubsub_node{id = Nidx} = decode_pubsub_node_row(Row),
Expand All @@ -722,7 +724,8 @@ set_node(#pubsub_node{nodeid = {Key, Name}, id = undefined, type = Type,
set_node(#pubsub_node{nodeid = {_, Name}, id = Nidx, type = Type,
owners = Owners, options = Opts, parents = Parents}) ->
OwnersJid = [jid:to_binary(Owner) || Owner <- Owners],
execute_update_pubsub_node(Type, jiffy:encode(OwnersJid), jiffy:encode({Opts}), Nidx),
%CHANGED
execute_update_pubsub_node(Type, [jiffy:encode(OwnersJid)], [jiffy:encode({Opts})], Nidx),

Check warning on line 728 in src/pubsub/mod_pubsub_db_rdbms.erl

View check run for this annotation

Codecov / codecov/patch

src/pubsub/mod_pubsub_db_rdbms.erl#L728

Added line #L728 was not covered by tests
execute_del_parents(Name),
set_parents(Name, Parents),
{ok, Nidx}.
Expand Down Expand Up @@ -915,7 +918,8 @@ get_affiliation(Nidx, { LU, LS, _ }) ->
SubId :: mod_pubsub:subId(),
SubOpts :: mod_pubsub:subOptions()) -> ok.
add_subscription(Nidx, { LU, LS, LR }, Sub, SubId, SubOpts) ->
EncodedOpts = jiffy:encode({SubOpts}),
%CHANGED
EncodedOpts = [jiffy:encode({SubOpts})],

Check warning on line 922 in src/pubsub/mod_pubsub_db_rdbms.erl

View check run for this annotation

Codecov / codecov/patch

src/pubsub/mod_pubsub_db_rdbms.erl#L922

Added line #L922 was not covered by tests
{updated, _} = execute_insert_subscription(Nidx, LU, LS, LR, sub2int(Sub), SubId, EncodedOpts),
ok.

Expand All @@ -924,7 +928,8 @@ add_subscription(Nidx, { LU, LS, LR }, Sub, SubId, SubOpts) ->
SubId :: mod_pubsub:subId(),
Opts :: mod_pubsub:subOptions()) -> ok.
set_subscription_opts(Nidx, { LU, LS, LR }, SubId, Opts) ->
EncodedOpts = jiffy:encode({Opts}),
%CHANGED
EncodedOpts = [jiffy:encode({Opts})],

Check warning on line 932 in src/pubsub/mod_pubsub_db_rdbms.erl

View check run for this annotation

Codecov / codecov/patch

src/pubsub/mod_pubsub_db_rdbms.erl#L932

Added line #L932 was not covered by tests
{updated, _} = execute_update_subscription_opts(EncodedOpts, Nidx, LU, LS, LR, SubId),
ok.

Expand Down
3 changes: 2 additions & 1 deletion src/system_metrics/mongoose_system_metrics_file.erl
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ location() ->

-spec save([mongoose_system_metrics_collector:report_struct()]) -> ok.
save(Reports) ->
JSON = jiffy:encode(Reports, [pretty]),
%CHANGED
JSON = [jiffy:encode(Reports, [pretty])],
file:write_file(location(), JSON),
ok.
3 changes: 2 additions & 1 deletion src/system_metrics/mongoose_system_metrics_sender.erl
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ flush_reports(ReportUrl, Reports, ClientId,
#{id := TrackingId, secret := TrackingSecret}) when length(Reports) =< 25 ->
Headers = [],
ContentType = "application/json",
Body = jiffy:encode(#{client_id => list_to_binary(ClientId), events => Reports}),
%CHANGED
Body = [jiffy:encode(#{client_id => list_to_binary(ClientId), events => Reports})],
ReportUrl2 = uri_string:normalize(
ReportUrl ++ "?api_secret=" ++ TrackingSecret ++ "&measurement_id=" ++ TrackingId),
Request = {ReportUrl2, Headers, ContentType, Body},
Expand Down

0 comments on commit d0a1572

Please sign in to comment.