A free and open source instant messaging and VoIP chat app built for friends, groups, and communities.
%% SPDX-License-Identifier: AGPL-3.0-or-later
-module(gateway_rpc_presence).
-typing([eqwalizer]).
-export([execute_method/2]).
-define(PRESENCE_LOOKUP_TIMEOUT, 2000).
-spec execute_method(binary(), map()) -> term().
execute_method(<<"presence.dispatch">>, P) -> handle_dispatch(P);
execute_method(<<"presence.join_guild">>, P) -> handle_join_guild(P);
execute_method(<<"presence.leave_guild">>, P) -> handle_leave_guild(P);
execute_method(<<"presence.terminate_sessions">>, P) -> handle_terminate_sessions(P);
execute_method(<<"presence.terminate_all_sessions">>, P) -> handle_terminate_all(P);
execute_method(<<"presence.has_active">>, P) -> handle_has_active(P);
execute_method(<<"presence.add_temporary_guild">>, P) -> handle_add_temp_guild(P);
execute_method(<<"presence.remove_temporary_guild">>, P) -> handle_remove_temp_guild(P);
execute_method(<<"presence.sync_group_dm_recipients">>, P) -> handle_sync_dm_recipients(P);
execute_method(<<"presence.start_visit">>, P) -> handle_start_visit(P);
execute_method(<<"presence.end_visit">>, P) -> handle_end_visit(P);
execute_method(Method, _P) -> gateway_rpc_error:raise(<<"Unknown method: ", Method/binary>>).
-spec handle_dispatch(map()) -> true.
handle_dispatch(#{<<"user_id">> := UserIdBin, <<"event">> := Event, <<"data">> := Data}) ->
UserId = validation:snowflake_or_throw(<<"user_id">>, UserIdBin),
EventAtom = dispatch_event_atom_or_error(Event),
ok = maybe_invalidate_push_blocked_ids(EventAtom, UserId),
case dispatch_to_owner(UserId, EventAtom, Data) of
ok -> true;
{error, not_found} -> handle_offline_dispatch(EventAtom, UserId, Data);
{error, unavailable} -> handle_unreachable_dispatch(EventAtom, UserId, Data);
_ -> gateway_rpc_error:raise(<<"presence_dispatch_error">>)
end.
-spec maybe_invalidate_push_blocked_ids(atom(), integer()) -> ok.
maybe_invalidate_push_blocked_ids(relationship_add, UserId) ->
push:invalidate_blocked_ids(UserId);
maybe_invalidate_push_blocked_ids(relationship_remove, UserId) ->
push:invalidate_blocked_ids(UserId);
maybe_invalidate_push_blocked_ids(_EventAtom, _UserId) ->
ok.
-spec handle_unreachable_dispatch(atom(), integer(), map()) -> no_return().
handle_unreachable_dispatch(message_create, UserId, Data) ->
_ = handle_offline_dispatch(message_create, UserId, Data),
gateway_rpc_error:raise(<<"presence_dispatch_error">>);
handle_unreachable_dispatch(_EventAtom, _UserId, _Data) ->
gateway_rpc_error:raise(<<"presence_dispatch_error">>).
-spec dispatch_event_atom_or_error(term()) -> atom().
dispatch_event_atom_or_error(Event) when is_binary(Event) ->
normalize_dispatch_event_atom(Event);
dispatch_event_atom_or_error(Event) ->
case is_atom(Event) of
true -> normalize_dispatch_event_atom(Event);
false -> gateway_rpc_error:raise(<<"presence_dispatch_error">>)
end.
-spec normalize_dispatch_event_atom(atom() | binary()) -> atom().
normalize_dispatch_event_atom(Event) ->
case constants:dispatch_event_atom(Event) of
EventAtom when is_atom(EventAtom) -> EventAtom;
_ -> gateway_rpc_error:raise(<<"presence_dispatch_error">>)
end.
-spec handle_join_guild(map()) -> true.
handle_join_guild(#{<<"user_id">> := UserIdBin, <<"guild_id">> := GuildIdBin}) ->
UserId = validation:snowflake_or_throw(<<"user_id">>, UserIdBin),
GuildId = validation:snowflake_or_throw(<<"guild_id">>, GuildIdBin),
forward_presence_async(UserId, {join_guild, GuildId}),
true.
-spec handle_start_visit(map()) -> true.
handle_start_visit(#{<<"user_id">> := UserIdBin, <<"guild_id">> := GuildIdBin} = P) ->
UserId = validation:snowflake_or_throw(<<"user_id">>, UserIdBin),
GuildId = validation:snowflake_or_throw(<<"guild_id">>, GuildIdBin),
Operator = type_conv:is_true(maps:get(<<"operator">>, P, false)),
forward_presence_async(UserId, {start_visit, GuildId, Operator}),
true.
-spec handle_end_visit(map()) -> true.
handle_end_visit(#{<<"user_id">> := UserIdBin, <<"guild_id">> := GuildIdBin}) ->
UserId = validation:snowflake_or_throw(<<"user_id">>, UserIdBin),
GuildId = validation:snowflake_or_throw(<<"guild_id">>, GuildIdBin),
forward_presence_async(UserId, {end_visit, GuildId}),
true.
-spec handle_leave_guild(map()) -> true.
handle_leave_guild(#{<<"user_id">> := UserIdBin, <<"guild_id">> := GuildIdBin}) ->
UserId = validation:snowflake_or_throw(<<"user_id">>, UserIdBin),
GuildId = validation:snowflake_or_throw(<<"guild_id">>, GuildIdBin),
forward_presence_async(UserId, {leave_guild, GuildId}),
true.
-spec handle_terminate_sessions(map()) -> true.
handle_terminate_sessions(#{
<<"user_id">> := UserIdBin,
<<"session_id_hashes">> := SessionIdHashes
}) ->
UserId = validation:snowflake_or_throw(<<"user_id">>, UserIdBin),
case forward_presence_sync(UserId, {terminate_session, SessionIdHashes}) of
ok -> true;
{error, not_found} -> true;
_ -> gateway_rpc_error:raise(<<"presence_terminate_sessions_error">>)
end.
-spec handle_terminate_all(map()) -> true.
handle_terminate_all(#{<<"user_id">> := UserIdBin}) ->
UserId = validation:snowflake_or_throw(<<"user_id">>, UserIdBin),
forward_presence_async(UserId, {terminate_all_sessions}),
true.
-spec handle_has_active(map()) -> map().
handle_has_active(#{<<"user_id">> := UserIdBin}) ->
UserId = validation:snowflake_or_throw(<<"user_id">>, UserIdBin),
case lookup_owner_presence(UserId) of
{ok, _Pid} -> #{<<"has_active">> => true};
{error, not_found} -> #{<<"has_active">> => false};
_ -> gateway_rpc_error:raise(<<"presence_lookup_error">>)
end.
-spec handle_add_temp_guild(map()) -> true.
handle_add_temp_guild(#{<<"user_id">> := UserIdBin, <<"guild_id">> := GuildIdBin}) ->
UserId = validation:snowflake_or_throw(<<"user_id">>, UserIdBin),
GuildId = validation:snowflake_or_throw(<<"guild_id">>, GuildIdBin),
forward_presence_async(UserId, {add_temporary_guild, GuildId}),
true.
-spec handle_remove_temp_guild(map()) -> true.
handle_remove_temp_guild(#{<<"user_id">> := UserIdBin, <<"guild_id">> := GuildIdBin}) ->
UserId = validation:snowflake_or_throw(<<"user_id">>, UserIdBin),
GuildId = validation:snowflake_or_throw(<<"guild_id">>, GuildIdBin),
forward_presence_async(UserId, {remove_temporary_guild, GuildId}),
true.
-spec handle_sync_dm_recipients(map()) -> true.
handle_sync_dm_recipients(#{
<<"user_id">> := UserIdBin,
<<"recipients_by_channel">> := RecipientsByChannel
}) ->
UserId = validation:snowflake_or_throw(<<"user_id">>, UserIdBin),
_ = normalize_recipients(RecipientsByChannel),
case lookup_owner_presence(UserId) of
{ok, Pid} ->
gen_server:cast(Pid, presence_rejoin),
true;
{error, not_found} ->
true;
_ ->
gateway_rpc_error:raise(<<"presence_lookup_error">>)
end.
-spec normalize_recipients(map()) -> map().
normalize_recipients(RecipientsByChannel) ->
maps:fold(
fun(ChannelIdBin, Recipients, Acc) ->
Acc#{
validation:snowflake_or_throw(<<"channel_id">>, ChannelIdBin) =>
[
validation:snowflake_or_throw(<<"recipient_id">>, RBin)
|| RBin <- Recipients
]
}
end,
#{},
RecipientsByChannel
).
-spec dispatch_to_owner(integer(), atom(), map()) -> ok | {error, not_found} | {error, term()}.
dispatch_to_owner(UserId, EventAtom, Data) ->
case resolve_owner_node(UserId) of
{ok, OwnerNode} ->
Request = {dispatch, UserId, EventAtom, Data},
normalize_dispatch_result(
call_presence_manager_node(
OwnerNode,
Request,
?PRESENCE_LOOKUP_TIMEOUT
)
);
unavailable ->
{error, unavailable}
end.
-spec normalize_dispatch_result(term()) -> ok | {error, not_found} | {error, term()}.
normalize_dispatch_result(ok) ->
ok;
normalize_dispatch_result({error, not_found}) ->
{error, not_found};
normalize_dispatch_result({error, _Reason} = Error) ->
Error;
normalize_dispatch_result(_Result) ->
{error, invalid_reply}.
-spec forward_presence_async(integer(), term()) -> ok.
forward_presence_async(UserId, Message) ->
case lookup_owner_presence(UserId) of
{ok, Pid} ->
gen_server:cast(Pid, Message),
ok;
{error, not_found} ->
ok;
_ ->
gateway_rpc_error:raise(<<"presence_lookup_error">>)
end.
-spec forward_presence_sync(integer(), term()) ->
ok | {error, not_found | timeout | unavailable | term()}.
forward_presence_sync(UserId, Message) ->
case lookup_owner_presence(UserId) of
{ok, Pid} ->
call_presence(Pid, Message);
{error, not_found} ->
{error, not_found};
{error, _Reason} = Error ->
Error
end.
-spec call_presence(pid(), term()) -> ok | {error, timeout | unavailable | term()}.
call_presence(Pid, Message) ->
try gen_server:call(Pid, Message, ?PRESENCE_LOOKUP_TIMEOUT) of
ok ->
ok;
{error, _Reason} = Error ->
Error;
Other ->
{error, {invalid_reply, Other}}
catch
throw:ok -> ok;
throw:{error, _Reason} = Error -> Error;
throw:Other -> {error, {invalid_reply, Other}};
exit:{timeout, _} -> {error, timeout};
exit:{noproc, _} -> {error, unavailable};
exit:Reason -> {error, Reason};
error:Reason:Stack -> {error, {Reason, Stack}}
end.
-spec lookup_owner_presence(integer()) ->
{ok, pid()} | {error, not_found | timeout | unavailable | term()}.
lookup_owner_presence(UserId) ->
case resolve_owner_node(UserId) of
{ok, OwnerNode} -> lookup_presence_on_node(OwnerNode, UserId);
unavailable -> {error, unavailable}
end.
-spec call_presence_manager_node(node(), term(), timeout()) -> term().
call_presence_manager_node(TargetNode, Request, Timeout) ->
try gen_server:call(presence_manager_server_ref(TargetNode), Request, Timeout) of
Reply -> Reply
catch
throw:{'EXIT', {timeout, _}} -> {error, timeout};
throw:{'EXIT', {nodedown, _}} -> {error, unavailable};
throw:{'EXIT', {noproc, _}} -> {error, unavailable};
throw:{'EXIT', _} -> {error, unavailable};
throw:Reply -> Reply;
exit:{timeout, _} -> {error, timeout};
exit:{nodedown, _} -> {error, unavailable};
exit:{noproc, _} -> {error, unavailable};
exit:_ -> {error, unavailable};
error:_ -> {error, unavailable}
end.
-spec lookup_presence_on_node(node(), integer()) ->
{ok, pid()} | {error, not_found | timeout | unavailable | term()}.
lookup_presence_on_node(TargetNode, UserId) ->
case call_presence_manager_node(TargetNode, {lookup, UserId}, ?PRESENCE_LOOKUP_TIMEOUT) of
{ok, Pid} when is_pid(Pid) ->
{ok, Pid};
{error, not_found} ->
{error, not_found};
{error, _Reason} = Error ->
Error;
_ ->
{error, invalid_reply}
end.
-spec presence_manager_server_ref(node()) -> atom() | {atom(), node()}.
presence_manager_server_ref(TargetNode) when TargetNode =:= node() ->
presence_manager;
presence_manager_server_ref(TargetNode) ->
{presence_manager, TargetNode}.
-spec resolve_owner_node(integer()) -> {ok, node()} | unavailable.
resolve_owner_node(UserId) ->
resolve_owner_node(UserId, fun owner_node_result_for_presence/1).
-spec owner_node_result_for_presence(integer()) -> {ok, node()} | unavailable.
owner_node_result_for_presence(UserId) ->
case gateway_node_router:owner_node_result(UserId, presence) of
{ok, Node} when is_atom(Node) -> {ok, Node};
_ -> unavailable
end.
-spec resolve_owner_node(integer(), fun((integer()) -> term())) -> {ok, node()} | unavailable.
resolve_owner_node(UserId, OwnerResolver) ->
try OwnerResolver(UserId) of
{ok, OwnerNode} when is_atom(OwnerNode) ->
maybe_valid_owner_node(OwnerNode);
_ ->
unavailable
catch
throw:{ok, OwnerNode} when is_atom(OwnerNode) ->
maybe_valid_owner_node(OwnerNode);
throw:_ ->
unavailable;
error:_ ->
unavailable;
exit:_ ->
unavailable
end.
-spec maybe_valid_owner_node(node()) -> {ok, node()} | unavailable.
maybe_valid_owner_node(OwnerNode) ->
case lists:member($@, atom_to_list(OwnerNode)) of
true -> {ok, OwnerNode};
false -> unavailable
end.
-spec handle_offline_dispatch(atom(), integer(), map() | list()) -> true.
handle_offline_dispatch(message_create, UserId, Data) when is_map(Data) ->
case offline_message_author_id(Data) of
{ok, AuthorId} ->
push:handle_message_create(#{
message_data => Data,
user_ids => [UserId],
guild_id => 0,
author_id => AuthorId
});
system ->
ok;
invalid ->
logger:debug("Presence offline message dispatch skipped push for invalid author", #{
user_id => UserId
})
end,
true;
handle_offline_dispatch(_Event, _UserId, _Data) ->
true.
-spec offline_message_author_id(map()) -> {ok, pos_integer()} | system | invalid.
offline_message_author_id(Data) ->
AuthorId = maps:get(<<"id">>, maps:get(<<"author">>, Data, #{}), undefined),
case AuthorId of
0 -> system;
<<"0">> -> system;
_ -> validate_offline_message_author_id(AuthorId)
end.
-spec validate_offline_message_author_id(term()) -> {ok, pos_integer()} | invalid.
validate_offline_message_author_id(AuthorId) ->
case validation:validate_snowflake(<<"author_id">>, AuthorId) of
{ok, ParsedAuthorId} -> {ok, ParsedAuthorId};
_ -> invalid
end.