fix(auto_subscribe): make log if the topic is empty when auto subscribe
This commit is contained in:
parent
e4b5001a57
commit
c2b1571134
|
@ -20,6 +20,7 @@
|
||||||
|
|
||||||
-include_lib("emqx/include/emqx.hrl").
|
-include_lib("emqx/include/emqx.hrl").
|
||||||
-include_lib("emqx/include/emqx_mqtt.hrl").
|
-include_lib("emqx/include/emqx_mqtt.hrl").
|
||||||
|
-include_lib("emqx/include/logger.hrl").
|
||||||
|
|
||||||
%% emqx_gen_mod callbacks
|
%% emqx_gen_mod callbacks
|
||||||
-export([ load/1
|
-export([ load/1
|
||||||
|
@ -38,14 +39,29 @@ load(Topics) ->
|
||||||
emqx_hooks:add('client.connected', {?MODULE, on_client_connected, [Topics]}).
|
emqx_hooks:add('client.connected', {?MODULE, on_client_connected, [Topics]}).
|
||||||
|
|
||||||
on_client_connected(#{clientid := ClientId, username := Username}, _ConnInfo = #{proto_ver := ProtoVer}, Topics) ->
|
on_client_connected(#{clientid := ClientId, username := Username}, _ConnInfo = #{proto_ver := ProtoVer}, Topics) ->
|
||||||
Replace = fun(Topic) ->
|
|
||||||
rep(<<"%u">>, Username, rep(<<"%c">>, ClientId, Topic))
|
OptFun = case ProtoVer of
|
||||||
|
?MQTT_PROTO_V5 -> fun(X) -> X end;
|
||||||
|
_ -> fun(#{qos := Qos}) -> #{qos => Qos} end
|
||||||
end,
|
end,
|
||||||
TopicFilters = case ProtoVer of
|
|
||||||
?MQTT_PROTO_V5 -> [{Replace(Topic), SubOpts} || {Topic, SubOpts} <- Topics];
|
Fold = fun({Topic, SubOpts}, Acc) ->
|
||||||
_ -> [{Replace(Topic), #{qos => Qos}} || {Topic, #{qos := Qos}} <- Topics]
|
case rep(Topic, ClientId, Username) of
|
||||||
end,
|
{error, _} ->
|
||||||
self() ! {subscribe, TopicFilters}.
|
Acc;
|
||||||
|
<<>> ->
|
||||||
|
?LOG(warning, "Topic can't be empty when auto subscribe"),
|
||||||
|
Acc;
|
||||||
|
NewTopic ->
|
||||||
|
[{NewTopic, OptFun(SubOpts)} | Acc]
|
||||||
|
end
|
||||||
|
end,
|
||||||
|
|
||||||
|
case lists:foldl(Fold, [], Topics) of
|
||||||
|
[] -> ok;
|
||||||
|
TopicFilters ->
|
||||||
|
self() ! {subscribe, TopicFilters}
|
||||||
|
end.
|
||||||
|
|
||||||
unload(_) ->
|
unload(_) ->
|
||||||
emqx_hooks:del('client.connected', {?MODULE, on_client_connected}).
|
emqx_hooks:del('client.connected', {?MODULE, on_client_connected}).
|
||||||
|
@ -56,10 +72,25 @@ description() ->
|
||||||
%% Internal functions
|
%% Internal functions
|
||||||
%%--------------------------------------------------------------------
|
%%--------------------------------------------------------------------
|
||||||
|
|
||||||
rep(<<"%c">>, ClientId, Topic) ->
|
rep(Topic, ClientId, Username) ->
|
||||||
emqx_topic:feed_var(<<"%c">>, ClientId, Topic);
|
Words = emqx_topic:words(Topic),
|
||||||
rep(<<"%u">>, undefined, Topic) ->
|
rep(Words, ClientId, Username, []).
|
||||||
Topic;
|
|
||||||
rep(<<"%u">>, Username, Topic) ->
|
|
||||||
emqx_topic:feed_var(<<"%u">>, Username, Topic).
|
|
||||||
|
|
||||||
|
rep([<<"%c">> | T], ClientId, Username, Acc) ->
|
||||||
|
rep(T,
|
||||||
|
ClientId,
|
||||||
|
Username,
|
||||||
|
[ClientId | Acc]);
|
||||||
|
rep([<<"%u">> | _], _, undefined, _) ->
|
||||||
|
?LOG(error, "Username undefined when auto subscribe"),
|
||||||
|
{error, username_undefined};
|
||||||
|
rep([<<"%u">> | T], ClientId, Username, Acc) ->
|
||||||
|
rep(T,
|
||||||
|
ClientId,
|
||||||
|
Username,
|
||||||
|
[Username | Acc]);
|
||||||
|
rep([H | T], ClientId, UserName, Acc) ->
|
||||||
|
rep(T, ClientId, UserName, [H | Acc]);
|
||||||
|
|
||||||
|
rep([], _, _, Acc) ->
|
||||||
|
emqx_topic:join(lists:reverse(Acc)).
|
||||||
|
|
Loading…
Reference in New Issue