refactor: check match topic before do subscriptions query
This commit is contained in:
parent
e9de7316b6
commit
3a09fdc495
|
@ -142,21 +142,9 @@ parameters() ->
|
|||
|
||||
subscriptions(get, #{query_string := QString}) ->
|
||||
Response =
|
||||
try
|
||||
begin
|
||||
case maps:get(<<"match_topic">>, QString, undefined) of
|
||||
undefined ->
|
||||
do_subscriptions_query(QString);
|
||||
MatchTopic ->
|
||||
case emqx_topic:parse(MatchTopic) of
|
||||
{#share{}, _} -> {error, invalid_match_topic};
|
||||
_ -> do_subscriptions_query(QString)
|
||||
end
|
||||
end
|
||||
end
|
||||
catch
|
||||
error:{invalid_topic_filter, _} ->
|
||||
{error, invalid_match_topic}
|
||||
case check_match_topic(QString) of
|
||||
ok -> do_subscriptions_query(QString);
|
||||
{error, _} = Err -> Err
|
||||
end,
|
||||
case Response of
|
||||
{error, invalid_match_topic} ->
|
||||
|
@ -170,6 +158,31 @@ subscriptions(get, #{query_string := QString}) ->
|
|||
{200, Result}
|
||||
end.
|
||||
|
||||
format(WhichNode, {{Topic, _Subscriber}, SubOpts}) ->
|
||||
maps:merge(
|
||||
#{
|
||||
topic => emqx_topic:maybe_format_share(Topic),
|
||||
clientid => maps:get(subid, SubOpts, null),
|
||||
node => WhichNode
|
||||
},
|
||||
maps:with([qos, nl, rap, rh], SubOpts)
|
||||
).
|
||||
|
||||
%%--------------------------------------------------------------------
|
||||
%% Internal functions
|
||||
%%--------------------------------------------------------------------
|
||||
|
||||
check_match_topic(#{<<"match_topic">> := MatchTopic}) ->
|
||||
try emqx_topic:parse(MatchTopic) of
|
||||
{#share{}, _} -> {error, invalid_match_topic};
|
||||
_ -> ok
|
||||
catch
|
||||
error:{invalid_topic_filter, _} ->
|
||||
{error, invalid_match_topic}
|
||||
end;
|
||||
check_match_topic(_) ->
|
||||
ok.
|
||||
|
||||
do_subscriptions_query(QString) ->
|
||||
Args = [?SUBOPTION, QString, ?SUBS_QSCHEMA, fun ?MODULE:qs2ms/2, fun ?MODULE:format/2],
|
||||
case maps:get(<<"node">>, QString, undefined) of
|
||||
|
@ -184,16 +197,6 @@ do_subscriptions_query(QString) ->
|
|||
end
|
||||
end.
|
||||
|
||||
format(WhichNode, {{Topic, _Subscriber}, SubOpts}) ->
|
||||
maps:merge(
|
||||
#{
|
||||
topic => emqx_topic:maybe_format_share(Topic),
|
||||
clientid => maps:get(subid, SubOpts, null),
|
||||
node => WhichNode
|
||||
},
|
||||
maps:with([qos, nl, rap, rh], SubOpts)
|
||||
).
|
||||
|
||||
%%--------------------------------------------------------------------
|
||||
%% QueryString to MatchSpec
|
||||
%%--------------------------------------------------------------------
|
||||
|
|
Loading…
Reference in New Issue