1
0
Fork 0
mirror of https://github.com/processone/ejabberd synced 2025-10-03 17:59:31 +02:00

Don't send empty direct Matrix messages (thanks to snoopcatt) (#4420)

This commit is contained in:
Alexey Shchepin 2025-08-15 04:50:33 +03:00
parent a02c75aa08
commit 8b61cf0742

View file

@ -265,105 +265,109 @@ route(#message{from = From, to = To, body = Body} = _Pkt) ->
Host = ejabberd_config:get_myname(), Host = ejabberd_config:get_myname(),
case user_id_from_jid(To, Host) of case user_id_from_jid(To, Host) of
{ok, ToMatrixID} -> {ok, ToMatrixID} ->
Key = {{From#jid.luser, From#jid.lserver}, ToMatrixID}, case xmpp:get_text(Body) of
Text = xmpp:get_text(Body), <<"">> ->
case mnesia:dirty_read(matrix_direct, Key) of ok;
[#matrix_direct{room_id = RoomID}] -> Text ->
?DEBUG("msg ~p~n", [{RoomID, From, ToMatrixID, Text}]), Key = {{From#jid.luser, From#jid.lserver}, ToMatrixID},
case get_existing_room_pid(Host, RoomID) of case mnesia:dirty_read(matrix_direct, Key) of
{ok, Pid} -> [#matrix_direct{room_id = RoomID}] ->
MatrixServer = mod_matrix_gw_opt:matrix_domain(Host), ?DEBUG("msg ~p~n", [{RoomID, From, ToMatrixID, Text}]),
FromMatrixID = case get_existing_room_pid(Host, RoomID) of
<<$@, (From#jid.luser)/binary, $:, MatrixServer/binary>>, {ok, Pid} ->
JSON = MatrixServer = mod_matrix_gw_opt:matrix_domain(Host),
#{<<"content">> => FromMatrixID =
#{<<"body">> => Text, <<$@, (From#jid.luser)/binary, $:, MatrixServer/binary>>,
<<"msgtype">> => <<"m.text">>}, JSON =
<<"sender">> => FromMatrixID, #{<<"content">> =>
<<"type">> => ?ROOM_MESSAGE}, #{<<"body">> => Text,
gen_statem:cast(Pid, {add_event, JSON}), <<"msgtype">> => <<"m.text">>},
ok; <<"sender">> => FromMatrixID,
{error, _} -> <<"type">> => ?ROOM_MESSAGE},
%%TODO gen_statem:cast(Pid, {add_event, JSON}),
ok ok;
end; {error, _} ->
_ -> %%TODO
RoomID = new_room_id(), ok
?DEBUG("new room id ~p~n", [RoomID]), end;
case get_room_pid(Host, RoomID) of _ ->
{ok, Pid} -> RoomID = new_room_id(),
MatrixServer = mod_matrix_gw_opt:matrix_domain(Host), ?DEBUG("new room id ~p~n", [RoomID]),
FromMatrixID = case get_room_pid(Host, RoomID) of
<<$@, (From#jid.luser)/binary, $:, MatrixServer/binary>>, {ok, Pid} ->
gen_statem:cast(Pid, {create, MatrixServer, RoomID, MatrixServer = mod_matrix_gw_opt:matrix_domain(Host),
FromMatrixID, ToMatrixID}), FromMatrixID =
JSONs = <<$@, (From#jid.luser)/binary, $:, MatrixServer/binary>>,
[#{<<"content">> => gen_statem:cast(Pid, {create, MatrixServer, RoomID,
#{<<"creator">> => FromMatrixID, FromMatrixID, ToMatrixID}),
<<"room_version">> => <<"9">>}, JSONs =
<<"sender">> => FromMatrixID, [#{<<"content">> =>
<<"state_key">> => <<"">>, #{<<"creator">> => FromMatrixID,
<<"type">> => ?ROOM_CREATE}, <<"room_version">> => <<"9">>},
#{<<"content">> => <<"sender">> => FromMatrixID,
#{<<"membership">> => <<"join">>}, <<"state_key">> => <<"">>,
<<"sender">> => FromMatrixID, <<"type">> => ?ROOM_CREATE},
<<"state_key">> => FromMatrixID, #{<<"content">> =>
<<"type">> => ?ROOM_MEMBER}, #{<<"membership">> => <<"join">>},
#{<<"content">> => <<"sender">> => FromMatrixID,
#{<<"ban">> => 50, <<"state_key">> => FromMatrixID,
<<"events">> => <<"type">> => ?ROOM_MEMBER},
#{<<"m.room.avatar">> => 50, #{<<"content">> =>
<<"m.room.canonical_alias">> => 50, #{<<"ban">> => 50,
<<"m.room.encryption">> => 100, <<"events">> =>
<<"m.room.history_visibility">> => 100, #{<<"m.room.avatar">> => 50,
<<"m.room.name">> => 50, <<"m.room.canonical_alias">> => 50,
<<"m.room.power_levels">> => 100, <<"m.room.encryption">> => 100,
<<"m.room.server_acl">> => 100, <<"m.room.history_visibility">> => 100,
<<"m.room.tombstone">> => 100}, <<"m.room.name">> => 50,
<<"events_default">> => 0, <<"m.room.power_levels">> => 100,
<<"historical">> => 100, <<"m.room.server_acl">> => 100,
<<"invite">> => 0, <<"m.room.tombstone">> => 100},
<<"kick">> => 50, <<"events_default">> => 0,
<<"redact">> => 50, <<"historical">> => 100,
<<"state_default">> => 50, <<"invite">> => 0,
<<"users">> => <<"kick">> => 50,
#{FromMatrixID => 100, <<"redact">> => 50,
ToMatrixID => 100}, <<"state_default">> => 50,
<<"users_default">> => 0}, <<"users">> =>
<<"sender">> => FromMatrixID, #{FromMatrixID => 100,
<<"state_key">> => <<"">>, ToMatrixID => 100},
<<"type">> => ?ROOM_POWER_LEVELS}, <<"users_default">> => 0},
#{<<"content">> => #{<<"join_rule">> => <<"invite">>}, <<"sender">> => FromMatrixID,
<<"sender">> => FromMatrixID, <<"state_key">> => <<"">>,
<<"state_key">> => <<"">>, <<"type">> => ?ROOM_POWER_LEVELS},
<<"type">> => ?ROOM_JOIN_RULES}, #{<<"content">> => #{<<"join_rule">> => <<"invite">>},
#{<<"content">> => #{<<"history_visibility">> => <<"shared">>}, <<"sender">> => FromMatrixID,
<<"sender">> => FromMatrixID, <<"state_key">> => <<"">>,
<<"state_key">> => <<"">>, <<"type">> => ?ROOM_JOIN_RULES},
<<"type">> => ?ROOM_HISTORY_VISIBILITY}, #{<<"content">> => #{<<"history_visibility">> => <<"shared">>},
#{<<"content">> => #{<<"guest_access">> => <<"can_join">>}, <<"sender">> => FromMatrixID,
<<"sender">> => FromMatrixID, <<"state_key">> => <<"">>,
<<"state_key">> => <<"">>, <<"type">> => ?ROOM_HISTORY_VISIBILITY},
<<"type">> => <<"m.room.guest_access">>}, #{<<"content">> => #{<<"guest_access">> => <<"can_join">>},
#{<<"content">> => <<"sender">> => FromMatrixID,
#{<<"is_direct">> => true, <<"state_key">> => <<"">>,
<<"membership">> => <<"invite">>}, <<"type">> => <<"m.room.guest_access">>},
<<"sender">> => FromMatrixID, #{<<"content">> =>
<<"state_key">> => ToMatrixID, #{<<"is_direct">> => true,
<<"type">> => ?ROOM_MEMBER}, <<"membership">> => <<"invite">>},
#{<<"content">> => <<"sender">> => FromMatrixID,
#{<<"body">> => Text, <<"state_key">> => ToMatrixID,
<<"msgtype">> => <<"m.text">>}, <<"type">> => ?ROOM_MEMBER},
<<"sender">> => FromMatrixID, #{<<"content">> =>
<<"type">> => ?ROOM_MESSAGE} #{<<"body">> => Text,
], <<"msgtype">> => <<"m.text">>},
lists:foreach(fun(JSON) -> <<"sender">> => FromMatrixID,
gen_statem:cast(Pid, {add_event, JSON}) <<"type">> => ?ROOM_MESSAGE}
end, JSONs), ],
ok; lists:foreach(fun(JSON) ->
{error, _} -> gen_statem:cast(Pid, {add_event, JSON})
%%TODO end, JSONs),
ok ok;
{error, _} ->
%%TODO
ok
end
end end
end; end;
error -> error ->