Skip to content

Commit

Permalink
Refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
chrzaszcz committed May 26, 2023
1 parent 986baf4 commit c9ab439
Show file tree
Hide file tree
Showing 20 changed files with 219 additions and 258 deletions.
6 changes: 2 additions & 4 deletions include/mod_vcard.hrl
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,7 @@
-export_type([vcard_search/0]).

-define(TLFIELD(Type, Label, Var),
mongoose_data_forms:form_field(#{var => Var, type => Type,
label => translate:translate(Lang, Label)})).
#{var => Var, type => Type, label => translate:translate(Lang, Label)}).

-define(FIELD(Var, Val),
mongoose_data_forms:form_field(#{var => Var, values => [Val]})).

#{var => Var, values => [Val]}).
11 changes: 3 additions & 8 deletions src/event_pusher/mod_event_pusher_push_plugin_defaults.erl
Original file line number Diff line number Diff line change
Expand Up @@ -195,14 +195,9 @@ push_notification_iq(Node, Form, PushPayload) ->
]}.

-spec make_form(binary(), mod_event_pusher_push:form()) -> exml:element().
make_form(FormType, Fields) ->
Children = [mongoose_data_forms:form_type_field(FormType) |
[make_form_field(Field) || Field <- Fields]],
mongoose_data_forms:form(Children, <<"submit">>).

-spec make_form_field(mod_event_pusher_push:form_field()) -> exml:element().
make_form_field({Name, Value}) ->
mongoose_data_forms:form_field(#{var => Name, values => [Value]}).
make_form(FormType, FieldKVs) ->
Fields = [#{var => Name, values => [Value]} || {Name, Value} <- FieldKVs],
mongoose_data_forms:form(#{ns => FormType, type => <<"submit">>, fields => Fields}).

-spec maybe_publish_options(mod_event_pusher_push:form()) -> [exml:element()].
maybe_publish_options([]) ->
Expand Down
6 changes: 2 additions & 4 deletions src/http_upload/mod_http_upload.erl
Original file line number Diff line number Diff line change
Expand Up @@ -299,10 +299,8 @@ parse_request(Request) ->

-spec get_disco_info_form(MaxFileSizeBin :: binary()) -> exml:element().
get_disco_info_form(MaxFileSizeBin) ->
Fields = [mongoose_data_forms:form_type_field(?NS_HTTP_UPLOAD_030),
mongoose_data_forms:form_field(#{var => <<"max-file-size">>,
values => [MaxFileSizeBin]})],
mongoose_data_forms:form(Fields, <<"result">>).
Fields = [#{var => <<"max-file-size">>, values => [MaxFileSizeBin]}],
mongoose_data_forms:form(#{type => <<"result">>, ns => ?NS_HTTP_UPLOAD_030, fields => Fields}).


-spec header_to_xmlel({Key :: binary(), Value :: binary()}) -> exml:element().
Expand Down
19 changes: 9 additions & 10 deletions src/inbox/mod_inbox.erl
Original file line number Diff line number Diff line change
Expand Up @@ -433,16 +433,15 @@ build_inbox_form(HostType) ->
AllBoxes = mod_inbox_utils:all_valid_boxes_for_query(HostType),
OrderOptions = [{<<"Ascending by timestamp">>, <<"asc">>},
{<<"Descending by timestamp">>, <<"desc">>}],
FieldSpecs = [#{var => <<"start">>, type => <<"text-single">>},
#{var => <<"end">>, type => <<"text-single">>},
#{var => <<"hidden_read">>, type => <<"text-single">>, values => [<<"false">>]},
#{var => <<"order">>, type => <<"list-single">>, values => [<<"desc">>],
options => OrderOptions},
#{var => <<"box">>, type => <<"list-single">>, values => [<<"all">>],
options => AllBoxes},
#{var => <<"archive">>, type => <<"boolean">>, values => [<<"false">>]}],
Fields = [mongoose_data_forms:form_field(Spec) || Spec <- FieldSpecs],
mongoose_data_forms:form([mongoose_data_forms:form_type_field(?NS_ESL_INBOX) | Fields]).
Fields = [#{var => <<"start">>, type => <<"text-single">>},
#{var => <<"end">>, type => <<"text-single">>},
#{var => <<"hidden_read">>, type => <<"text-single">>, values => [<<"false">>]},
#{var => <<"order">>, type => <<"list-single">>, values => [<<"desc">>],
options => OrderOptions},
#{var => <<"box">>, type => <<"list-single">>, values => [<<"all">>],
options => AllBoxes},
#{var => <<"archive">>, type => <<"boolean">>, values => [<<"false">>]}],
mongoose_data_forms:form(#{ns => ?NS_ESL_INBOX, fields => Fields}).

%%%%%%%%%%%%%%%%%%%
%% iq-set
Expand Down
6 changes: 2 additions & 4 deletions src/inbox/mod_inbox_entries.erl
Original file line number Diff line number Diff line change
Expand Up @@ -54,14 +54,12 @@ maybe_get_full_entry(SubEl) ->
-spec build_inbox_entry_form(mongooseim:host_type()) -> exml:element().
build_inbox_entry_form(HostType) ->
AllBoxes = mod_inbox_utils:all_valid_boxes_for_query(HostType),
FieldSpecs =
Fields =
[#{var => <<"box">>, type => <<"list-single">>, values => [<<"all">>], options => AllBoxes},
#{var => <<"archive">>, type => <<"boolean">>, values => [<<"false">>]},
#{var => <<"read">>, type => <<"boolean">>, values => [<<"false">>]},
#{var => <<"mute">>, type => <<"text-single">>, values => [<<"0">>]}],
Fields = lists:map(fun mongoose_data_forms:form_field/1, FieldSpecs),
FormType = mongoose_data_forms:form_type_field(?NS_ESL_INBOX_CONVERSATION),
mongoose_data_forms:form([FormType | Fields]).
mongoose_data_forms:form(#{ns => ?NS_ESL_INBOX_CONVERSATION, fields => Fields}).

fetch_right_query(HostType, InboxEntryKey, only_properties) ->
mod_inbox_backend:get_entry_properties(HostType, InboxEntryKey);
Expand Down
18 changes: 8 additions & 10 deletions src/mam/mod_mam_utils.erl
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,6 @@
get_one_of_path/3, is_arcid_elem_for/3, maybe_encode_compact_uuid/2,
maybe_last/1, result_query/2, send_message/4, wrap_message/7, wrapper_id/0]).

-import(mongoose_data_forms, [form/1, form_type_field/1, form_field/1]).

%-define(MAM_INLINE_UTILS, true).

-ifdef(MAM_INLINE_UTILS).
Expand Down Expand Up @@ -681,19 +679,19 @@ retraction_features(Module, HostType) ->
HostType :: mongooseim:host_type(), binary()) ->
exml:element().
message_form(Module, HostType, MamNs) ->
Children = message_form_fields(Module, HostType, MamNs),
result_query(form(Children), MamNs).
Fields = message_form_fields(Module, HostType),
Form = mongoose_data_forms:form(#{ns => MamNs, fields => Fields}),
result_query(Form, MamNs).

message_form_fields(Mod, HostType, MamNs) ->
message_form_fields(Mod, HostType) ->
TextSearch =
case has_full_text_search(Mod, HostType) of
true -> [form_field(#{type => <<"text-single">>, var => <<"full-text-search">>})];
true -> [#{type => <<"text-single">>, var => <<"full-text-search">>}];
false -> []
end,
[form_type_field(MamNs),
form_field(#{type => <<"jid-single">>, var => <<"with">>}),
form_field(#{type => <<"text-single">>, var => <<"start">>}),
form_field(#{type => <<"text-single">>, var => <<"end">>}) | TextSearch].
[#{type => <<"jid-single">>, var => <<"with">>},
#{type => <<"text-single">>, var => <<"start">>},
#{type => <<"text-single">>, var => <<"end">>} | TextSearch].

-spec form_to_text(_) -> 'undefined' | binary().
form_to_text(#{<<"full-text-search">> := [Text]}) ->
Expand Down
14 changes: 7 additions & 7 deletions src/mod_muc.erl
Original file line number Diff line number Diff line change
Expand Up @@ -1088,15 +1088,15 @@ iq_get_register_info(HostType, MucHost, From, Lang) ->
ClientReqEl = #xmlel{name = <<"instructions">>,
children = [#xmlcdata{content = ClientReqText}]},
EnterNicknameText = translate:translate(Lang, <<"Enter nickname you want to register">>),
EnterNicknameEl = mongoose_data_forms:form_instructions(EnterNicknameText),
TitleText = <<(translate:translate(Lang, <<"Nickname Registration at ">>))/binary,
MucHost/binary>>,
TitleEl = mongoose_data_forms:form_title(TitleText),
NickField = mongoose_data_forms:form_field(#{type => <<"text-single">>,
label => translate:translate(Lang, <<"Nickname">>),
var => <<"nick">>,
values => [Nick]}),
Registered ++ [ClientReqEl, mongoose_data_forms:form([TitleEl, EnterNicknameEl, NickField])].
NickField = #{type => <<"text-single">>,
label => translate:translate(Lang, <<"Nickname">>),
var => <<"nick">>,
values => [Nick]},
Registered ++ [ClientReqEl, mongoose_data_forms:form(#{title => TitleText,
instructions => EnterNicknameText,
fields => [NickField]})].

-spec iq_set_register_info(host_type(), jid:server(),
jid:simple_jid() | jid:jid(), nick(), ejabberd:lang())
Expand Down
2 changes: 1 addition & 1 deletion src/mod_muc_api.erl
Original file line number Diff line number Diff line change
Expand Up @@ -535,7 +535,7 @@ declination(Sender, Recipient) ->
iq(<<"set">>, Sender, Recipient, [data_submission()]).

data_submission() ->
query(?NS_MUC_OWNER, [mongoose_data_forms:form([], <<"submit">>)]).
query(?NS_MUC_OWNER, [mongoose_data_forms:form(#{type => <<"submit">>})]).

address_attributes(Sender, Recipient) ->
[{<<"from">>, jid:to_binary(Sender)},
Expand Down
77 changes: 29 additions & 48 deletions src/mod_muc_room.erl
Original file line number Diff line number Diff line change
Expand Up @@ -3334,13 +3334,10 @@ get_default_room_maxusers(RoomState) ->
get_config(Lang, StateData, From) ->
AccessPersistent = access_persistent(StateData),
Config = StateData#state.config,

TitleTxt = translate:translate(Lang, <<"Configuration of room ">>),
Res =
[mongoose_data_forms:form_title(<<TitleTxt/binary,
(jid:to_binary(StateData#state.jid))/binary>>),
mongoose_data_forms:form_type_field(?NS_MUC_CONFIG),
stringxfield(<<"Room title">>,
Title = <<TitleTxt/binary, (jid:to_binary(StateData#state.jid))/binary>>,
Fields =
[stringxfield(<<"Room title">>,
<<"muc#roomconfig_roomname">>,
Config#config.title, Lang),
stringxfield(<<"Room description">>,
Expand Down Expand Up @@ -3416,20 +3413,17 @@ get_config(Lang, StateData, From) ->
InstructionsTxt = translate:translate(
Lang, <<"You need an x:data capable client to configure room">>),
{result, [#xmlel{name = <<"instructions">>, children = [#xmlcdata{content = InstructionsTxt}]},
mongoose_data_forms:form(Res)],
mongoose_data_forms:form(#{title => Title, ns => ?NS_MUC_CONFIG, fields => Fields})],
StateData}.

-spec getmemberlist_field(Lang :: ejabberd:lang()) -> exml:element().
-spec getmemberlist_field(Lang :: ejabberd:lang()) -> mongoose_data_forms:field().
getmemberlist_field(Lang) ->
LabelTxt = translate:translate(
Lang, <<"Roles and affiliations that may retrieve member list">>),
Values = [<<"moderator">>, <<"participant">>, <<"visitor">>],
Options = [{translate:translate(Lang, Opt), Opt} || Opt <- Values],
mongoose_data_forms:form_field(#{type => <<"list-multi">>,
label => LabelTxt,
var => <<"muc#roomconfig_getmemberlist">>,
values => Values,
options => Options}).
#{type => <<"list-multi">>, label => LabelTxt,
var => <<"muc#roomconfig_getmemberlist">>, values => Values, options => Options}.

maxusers_field(Lang, StateData) ->
ServiceMaxUsers = get_service_max_users(StateData),
Expand All @@ -3448,24 +3442,18 @@ maxusers_field(Lang, StateData) ->
[integer_to_binary(N) ||
N <- lists:usort([ServiceMaxUsers, DefaultRoomMaxUsers, MaxUsersRoomInteger |
?MAX_USERS_DEFAULT_LIST]), N =< ServiceMaxUsers],
mongoose_data_forms:form_field(#{type => <<"list-single">>,
label => LabelTxt,
var => <<"muc#roomconfig_maxusers">>,
values => [MaxUsersRoomString],
options => Options}).
#{type => <<"list-single">>, label => LabelTxt,
var => <<"muc#roomconfig_maxusers">>, values => [MaxUsersRoomString], options => Options}.

-spec whois_field(Lang :: ejabberd:lang(), Config :: config()) -> exml:element().
-spec whois_field(Lang :: ejabberd:lang(), Config :: config()) -> mongoose_data_forms:field().
whois_field(Lang, Config) ->
Value = if Config#config.anonymous -> <<"moderators">>;
true -> <<"anyone">>
end,
Options = [{translate:translate(Lang, <<"moderators only">>), <<"moderators">>},
{translate:translate(Lang, <<"anyone">>), <<"anyone">>}],
mongoose_data_forms:form_field(#{type => <<"list-single">>,
label => translate:translate(Lang, <<"moderators only">>),
var => <<"muc#roomconfig_whois">>,
values => [Value],
options => Options}).
#{type => <<"list-single">>, label => translate:translate(Lang, <<"moderators only">>),
var => <<"muc#roomconfig_whois">>, values => [Value], options => Options}.

-spec set_config([{binary(), [binary()]}], state()) -> any().
set_config(XData, StateData) ->
Expand Down Expand Up @@ -4564,34 +4552,27 @@ decode_reason(Elem) ->
-spec make_voice_approval_form(From :: jid:simple_jid() | jid:jid(),
Nick :: binary(), Role :: binary()) -> exml:element().
make_voice_approval_form(From, Nick, Role) ->
FieldSpecs =
[#{var => <<"muc#role">>, type => <<"text-single">>, value => Role,
label => <<"Request role">>},
#{var => <<"muc#jid">>, type => <<"jid-single">>, value => jid:to_binary(From),
label => <<"User ID">>},
#{var => <<"muc#roomnick">>, type => <<"text-single">>, value => Nick,
label => <<"Room Nickname">>},
#{var => <<"muc#request_allow">>, type => <<"boolean">>, value => <<"false">>,
label => <<"Grant voice to this person?">>}],
FormChildren =
[mongoose_data_forms:form_title(<<"Voice request">>),
mongoose_data_forms:form_instructions(
<<"To approve this request",
" for voice, select the &quot;Grant voice to this person?&quot; checkbox",
" and click OK. To skip this request, click the cancel button.">>),
mongoose_data_forms:form_type_field(?NS_MUC_REQUEST) |
lists:map(fun mongoose_data_forms:form_field/1, FieldSpecs)],
Form = mongoose_data_forms:form(FormChildren),
Title = <<"Voice request">>,
Instructions = <<"To approve this request"
" for voice, select the &quot;Grant voice to this person?&quot; checkbox"
" and click OK. To skip this request, click the cancel button.">>,
Fields = [#{var => <<"muc#role">>, type => <<"text-single">>,
label => <<"Request role">>, values => [Role]},
#{var => <<"muc#jid">>, type => <<"jid-single">>,
label => <<"User ID">>, values => [jid:to_binary(From)]},
#{var => <<"muc#roomnick">>, type => <<"text-single">>,
label => <<"Room Nickname">>, values => [Nick]},
#{var => <<"muc#request_allow">>, type => <<"boolean">>,
label => <<"Grant voice to this person?">>, values => [<<"false">>]}],
Form = mongoose_data_forms:form(#{title => Title, instructions => Instructions,
ns => ?NS_MUC_REQUEST, fields => Fields}),
#xmlel{name = <<"message">>, children = [Form]}.

-spec xfield(binary(), any(), binary(), binary(), ejabberd:lang()) -> exml:element().
-spec xfield(binary(), any(), binary(), binary(), ejabberd:lang()) -> mongoose_data_forms:field().
xfield(Type, Label, Var, Val, Lang) ->
mongoose_data_forms:form_field(#{type => Type,
label => translate:translate(Lang, Label),
var => Var,
values => [Val]}).
#{type => Type, label => translate:translate(Lang, Label), var => Var, values => [Val]}.

-spec boolxfield(any(), binary(), any(), ejabberd:lang()) -> exml:element().
-spec boolxfield(any(), binary(), any(), ejabberd:lang()) -> mongoose_data_forms:field().
boolxfield(Label, Var, Val, Lang) ->
xfield(<<"boolean">>, Label, Var,
case Val of
Expand Down
47 changes: 30 additions & 17 deletions src/mongoose_data_forms.erl
Original file line number Diff line number Diff line change
Expand Up @@ -6,27 +6,23 @@
form_type/1,
find_form/1, find_form/2,
form_to_kvs/1, form_to_kvs/2,
form_to_map/1, form_to_map/2,
form_fields_to_kvs/1]).
form_to_map/1, form_to_map/2]).

%% Form construction
-export([form/1, form/2,
form_type_field/1,
form_field/1,
form_title/1,
form_instructions/1,
reported_element/1]).
-export([form/1]).

-include_lib("exml/include/exml.hrl").
-include("mongoose_ns.hrl").

-type form() :: #{type => binary(), title => binary(), instructions => binary(), ns => binary(),
fields => [field()], reported => [field()], items => [[field()]]}.
-type field() :: #{var => binary(), type => binary(), label => binary(),
values => [binary()], options => [option()]}.
-type option() :: binary() | {binary(), binary()}.
-type kv_list() :: [{binary(), [binary()]}].
-type kv_map() :: #{binary() => [binary()]}.

-export_type([field/0, option/0, kv_list/0, kv_map/0]).
-export_type([form/0, field/0, option/0, kv_list/0, kv_map/0]).

-ignore_xref([form_to_map/2]). % exported for consistency, might be used later

Expand Down Expand Up @@ -88,15 +84,28 @@ form_field_to_kv(_) ->

%% Form construction

-spec form([exml:element()]) -> exml:element().
form(Children) ->
form(Children, <<"form">>).

-spec form([exml:element()], binary()) -> exml:element().
form(Children, Type) ->
-spec form(form()) -> exml:element().
form(Spec) ->
#xmlel{name = <<"x">>,
attrs = [{<<"xmlns">>, ?NS_XDATA}, {<<"type">>, Type}],
children = Children}.
attrs = [{<<"xmlns">>, ?NS_XDATA}, {<<"type">>, maps:get(type, Spec, <<"form">>)}],
children = lists:flatmap(fun(Item) -> form_children(Item, Spec) end,
[title, instructions, ns, fields, reported])
}.

form_children(title, #{title := Title}) ->
[form_title(Title)];
form_children(instructions, #{instructions := Instructions}) ->
[form_instructions(Instructions)];
form_children(ns, #{ns := NS}) ->
[form_type_field(NS)];
form_children(fields, #{fields := Fields}) ->
[form_field(Field) || Field <- Fields];
form_children(reported, #{reported := ReportedFields}) ->
[reported_element([form_field(Field) || Field <- ReportedFields])];
form_children(items, #{items := Items}) ->
[item_element([form_field(Field) || Field <- ItemFields]) || ItemFields <- Items];
form_children(_, #{}) ->
[].

-spec form_type_field(binary()) -> exml:element().
form_type_field(NS) when is_binary(NS) ->
Expand All @@ -121,6 +130,10 @@ form_instructions(Instructions) ->
reported_element(Fields) ->
#xmlel{name = <<"reported">>, attrs = [], children = Fields}.

-spec item_element([exml:element()]) -> exml:element().
item_element(Fields) ->
#xmlel{name = <<"item">>, attrs = [], children = Fields}.

-spec form_field_option(option()) -> exml:element().
form_field_option({Label, Value}) ->
#xmlel{name = <<"option">>,
Expand Down
4 changes: 1 addition & 3 deletions src/mongoose_disco.erl
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,4 @@ identity_to_xml(Identity) ->

-spec info_to_xml(info()) -> exml:element().
info_to_xml(#{xmlns := NS, fields := Fields}) ->
Children = [mongoose_data_forms:form_type_field(NS) |
[mongoose_data_forms:form_field(Field) || Field <- Fields]],
mongoose_data_forms:form(Children, <<"result">>).
mongoose_data_forms:form(#{type => <<"result">>, ns => NS, fields => Fields}).
Loading

0 comments on commit c9ab439

Please sign in to comment.