Skip to content

Commit

Permalink
fix bugs and simplify
Browse files Browse the repository at this point in the history
  • Loading branch information
sstrigler committed Jan 8, 2025
1 parent c5e658a commit 0c36b1f
Showing 1 changed file with 15 additions and 17 deletions.
32 changes: 15 additions & 17 deletions mod_pubsub_serverinfo/src/mod_pubsub_serverinfo.erl
Original file line number Diff line number Diff line change
Expand Up @@ -64,19 +64,17 @@ init([Host, _Opts]) ->
self() ! update_pubsub,
{ok, State}.

handle_cast({register_in, Host, LServer, RServer, Pid}, #state{monitors = Mons} = State) ->
handle_cast({Event, Host, LServer, RServer, Pid}, #state{monitors = Mons} = State) when Event == register_in; Event == register_out ->
Ref = monitor(process, Pid),
HasSupport = check_if_remote_has_support(Host, LServer, RServer, Mons),
NewMons = maps:put(Ref, {incoming, {LServer, RServer, HasSupport}}, Mons),
{noreply, State#state{monitors = NewMons}};
handle_cast({register_out, Host, LServer, RServer, Pid}, #state{monitors = Mons} = State) ->
Ref = monitor(process, Pid),
HasSupport = check_if_remote_has_support(Host, LServer, RServer, Mons),
NewMons = maps:put(Ref, {outgoing, {LServer, RServer, HasSupport}}, Mons),
NewMons = maps:put(Ref, {event_to_dir(Event), {LServer, RServer, HasSupport}}, Mons),
{noreply, State#state{monitors = NewMons}};
handle_cast(_, State) ->
{noreply, State}.

event_to_dir(register_in) -> incoming;
event_to_dir(register_out) -> outgoing.

handle_call(_Request, _From, State) ->
{noreply, State}.

Expand All @@ -88,15 +86,15 @@ handle_info({iq_reply, IQReply, {LServer, RServer}},
#disco_info{features = Features} ->
case lists:member(?NS_URN_SERVERINFO, Features) of
true ->
Mons2 = maps:fold(fun(Ref, {Dir, {LServer0, RServer0, _}}, NewMons)
when LServer == LServer0, RServer == RServer0 ->
maps:put(Ref, {Dir, {LServer, RServer, true}}, NewMons);
(Ref, Other, NewMons) ->
maps:put(Ref, Other, NewMons)
end,
#{},
Mons),
{noreply, State#state{monitors = Mons2}};
NewMons = maps:fold(fun(Ref, {Dir, {LServer0, RServer0, _}}, Acc)
when LServer == LServer0, RServer == RServer0 ->
maps:put(Ref, {Dir, {LServer, RServer, true}}, Acc);
(Ref, Other, Acc) ->
maps:put(Ref, Other, Acc)
end,
#{},
Mons),
{noreply, State#state{monitors = NewMons}};
_ ->
{noreply, State}
end;
Expand Down Expand Up @@ -165,7 +163,7 @@ check_if_remote_has_support(Host, LServer, RServer, Mons) ->
has_support(LServer, RServer, Mons) ->
maps:size(
maps:filter(
fun(_Ref, {_Dir, LServer0, RServer0, HasSupport})
fun(_Ref, {_Dir, {LServer0, RServer0, HasSupport}})
when LServer0 == LServer, RServer0 == RServer -> HasSupport;
(_Ref, _Other) -> false
end,
Expand Down

0 comments on commit 0c36b1f

Please sign in to comment.