A free and open source instant messaging and VoIP chat app built for friends, groups, and communities.
feat(gateway): track community visits in sessions, drop tests
Hampus27ebfb1df73b
diff --git a/fluxer_gateway/src/gateway/gateway_rpc_presence.erl b/fluxer_gateway/src/gateway/gateway_rpc_presence.erlindex 894549a23..ae40933f6 100644--- a/fluxer_gateway/src/gateway/gateway_rpc_presence.erl+++ b/fluxer_gateway/src/gateway/gateway_rpc_presence.erl@@ -1,9 +1,6 @@%% SPDX-License-Identifier: AGPL-3.0-or-later-module(gateway_rpc_presence).--ifdef(TEST).--include_lib("eunit/include/eunit.hrl").--endif.-typing([eqwalizer]).@@ -21,6 +18,8 @@ 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.@@ -64,6 +63,21 @@ handle_join_guild(#{<<"user_id">> := UserIdBin, <<"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),@@ -296,7 +310,7 @@ maybe_valid_owner_node(OwnerNode) ->end.-spec handle_offline_dispatch(atom(), integer(), map() | list()) -> true.-handle_offline_dispatch(message_create, UserId, Data) ->+handle_offline_dispatch(message_create, UserId, Data) when is_map(Data) ->case offline_message_author_id(Data) of{ok, AuthorId} ->push:handle_message_create(#{@@ -341,28 +355,3 @@ validate_offline_message_author_id(AuthorId) ->-spec sync_blocked_ids_for_user(integer()) -> ok.sync_blocked_ids_for_user(_UserId) ->ok.---ifdef(TEST).--dispatch_event_atom_accepts_thread_events_test() ->- ?assertEqual(- thread_member_update, dispatch_event_atom_or_error(<<"THREAD_MEMBER_UPDATE">>)- ),- ?assertEqual(thread_list_sync, dispatch_event_atom_or_error(<<"THREAD_LIST_SYNC">>)),- ?assertEqual(forum_unreads, dispatch_event_atom_or_error(<<"FORUM_UNREADS">>)).--offline_message_create_system_author_is_success_test() ->- ?assertEqual(- true,- handle_offline_dispatch(- message_create,- 42,- #{- <<"id">> => <<"456">>,- <<"channel_id">> => <<"123">>,- <<"author">> => #{<<"id">> => <<"0">>}- }- )- ).---endif.diff --git a/fluxer_gateway/src/guild/guild_request_channel_member_counts.erl b/fluxer_gateway/src/guild/guild_request_channel_member_counts.erlindex e662216de..ccb5f3875 100644--- a/fluxer_gateway/src/guild/guild_request_channel_member_counts.erl+++ b/fluxer_gateway/src/guild/guild_request_channel_member_counts.erl@@ -1,9 +1,6 @@%% SPDX-License-Identifier: AGPL-3.0-or-later-module(guild_request_channel_member_counts).--ifdef(TEST).--include_lib("eunit/include/eunit.hrl").--endif.-typing([eqwalizer]).@@ -48,7 +45,7 @@ fetch_counts(undefined, _ChannelIds, _SessionId, _UserId, _SessionState) ->fetch_counts(_GuildId, [], _SessionId, _UserId, _SessionState) ->[];fetch_counts(GuildId, ChannelIds, SessionId, UserId, SessionState) ->- case lookup_guild_pid(GuildId, maps:get(guilds, SessionState, #{})) of+ case lookup_guild_pid(GuildId, session_visits:member_guilds(SessionState)) of{ok, GuildPid} ->request_guild_channel_counts(GuildPid, GuildId, ChannelIds, SessionId, UserId);error ->@@ -141,45 +138,3 @@ parse_nonce(Nonce) whenNonce;parse_nonce(_) ->undefined.---ifdef(TEST).--parse_channel_ids_accepts_single_channel_id_test() ->- ?assertEqual([42], parse_channel_ids(#{<<"channel_id">> => <<"42">>})).--parse_channel_ids_dedupes_sorts_and_filters_test() ->- ?assertEqual(- [1, 2],- parse_channel_ids(#{<<"channel_ids">> => [<<"2">>, <<"1">>, <<"2">>, 0, <<"bad">>]})- ).--parse_channel_ids_caps_at_max_test() ->- Many = [integer_to_binary(N) || N <- lists:seq(1, ?MAX_CHANNEL_IDS + 50)],- ?assertEqual(?MAX_CHANNEL_IDS, length(parse_channel_ids(#{<<"channel_ids">> => Many}))).--parse_nonce_test() ->- ?assertEqual(<<"n">>, parse_nonce(<<"n">>)),- ?assertEqual(undefined, parse_nonce(<<>>)),- ?assertEqual(undefined, parse_nonce(123)),- Big = binary:copy(<<"a">>, ?MAX_NONCE_BYTES + 1),- ?assertEqual(undefined, parse_nonce(Big)).--handle_request_dispatches_empty_when_no_guild_test() ->- Self = self(),- SessionState = #{- session_pid => Self,- session_id => <<"s1">>,- user_id => <<"100">>,- guilds => #{}- },- ok = handle_request(- #{<<"guild_id">> => <<"1">>, <<"channel_id">> => <<"2">>}, Self, SessionState- ),- receive- {'$gen_cast', {dispatch, channel_member_counts_update, Payload}} ->- ?assertEqual([], maps:get(<<"counts">>, Payload))- after 1000 ->- ?assert(false)- end.---endif.diff --git a/fluxer_gateway/src/guild/guild_request_counts.erl b/fluxer_gateway/src/guild/guild_request_counts.erlindex c139efb8a..51b43bb09 100644--- a/fluxer_gateway/src/guild/guild_request_counts.erl+++ b/fluxer_gateway/src/guild/guild_request_counts.erl@@ -1,9 +1,6 @@%% SPDX-License-Identifier: AGPL-3.0-or-later-module(guild_request_counts).--ifdef(TEST).--include_lib("eunit/include/eunit.hrl").--endif.-typing([eqwalizer]).@@ -28,7 +25,7 @@ handle_request(Data, _SocketPid, SessionState) when is_map(Data) ->true ->GuildIds = parse_guild_ids(maps:get(<<"guild_ids">>, Data, [])),Nonce = parse_nonce(maps:get(<<"nonce">>, Data, undefined)),- Guilds = maps:get(guilds, SessionState, #{}),+ Guilds = session_visits:member_guilds(SessionState),Targets = build_targets(GuildIds, Guilds),Entries = parallel_fetch(Targets, UserId),dispatch_counts(SessionPid, Entries, Nonce),@@ -112,7 +109,9 @@ fetch_viewer_counts(GuildPid, UserId) ->end.-spec counts_result(term()) -> {ok, non_neg_integer(), non_neg_integer()} | error.-counts_result(#{member_count := MemberCount, online_count := OnlineCount}) ->+counts_result(#{member_count := MemberCount, online_count := OnlineCount}) when+ is_integer(MemberCount), MemberCount >= 0, is_integer(OnlineCount), OnlineCount >= 0+->{ok, MemberCount, OnlineCount};counts_result(_) ->error.@@ -159,99 +158,3 @@ parse_nonce(Nonce) whenNonce;parse_nonce(_) ->undefined.---ifdef(TEST).--parse_guild_ids_caps_at_max_test() ->- Many = [integer_to_binary(N) || N <- lists:seq(1, ?MAX_GUILD_IDS + 50)],- Result = parse_guild_ids(Many),- ?assertEqual(?MAX_GUILD_IDS, length(Result)).--handle_request_dispatches_empty_when_no_guilds_test() ->- Self = self(),- SessionState = #{session_pid => Self, user_id => <<"100">>, guilds => #{}},- ok = handle_request(#{<<"guild_ids">> => [<<"1">>]}, Self, SessionState),- receive- {'$gen_cast', {dispatch, guild_counts_update, Payload}} ->- ?assertEqual([], maps:get(<<"counts">>, Payload)),- ?assertNot(maps:is_key(<<"nonce">>, Payload))- after 1000 ->- ?assert(false)- end.--handle_request_echoes_nonce_test() ->- Self = self(),- SessionState = #{session_pid => Self, user_id => <<"100">>, guilds => #{}},- ok = handle_request(- #{<<"guild_ids">> => [<<"1">>], <<"nonce">> => <<"abc123">>}, Self, SessionState- ),- receive- {'$gen_cast', {dispatch, guild_counts_update, Payload}} ->- ?assertEqual(<<"abc123">>, maps:get(<<"nonce">>, Payload)),- ?assertEqual([], maps:get(<<"counts">>, Payload))- after 1000 ->- ?assert(false)- end.--handle_request_fetches_viewer_counts_with_deadline_test() ->- Self = self(),- Guild = spawn(fun() ->- receive- {'$gen_call', From, {get_viewer_counts, #{user_id := 100, deadline := D}}} when- is_integer(D)- ->- gen_server:reply(From, #{member_count => 50, online_count => 10})- end- end),- SessionState = #{- session_pid => Self, user_id => <<"100">>, guilds => #{7 => {Guild, make_ref()}}- },- ok = handle_request(#{<<"guild_ids">> => [<<"7">>]}, Self, SessionState),- receive- {'$gen_cast', {dispatch, guild_counts_update, Payload}} ->- ?assertEqual([build_entry(7, 50, 10)], maps:get(<<"counts">>, Payload))- after 1000 ->- ?assert(false)- end.--fetch_counts_does_not_fall_back_on_current_guild_test() ->- Self = self(),- Guild = spawn(fun() ->- receive- {'$gen_call', From, {get_viewer_counts, #{user_id := 100}}} ->- gen_server:reply(From, #{member_count => 3, online_count => 1})- end,- receive- {'$gen_call', From2, Msg} ->- Self ! {unexpected_call, Msg},- gen_server:reply(From2, ok)- after 300 -> ok- end- end),- ?assertEqual({ok, 3, 1}, fetch_counts(Guild, 100)),- receive- {unexpected_call, Msg} -> ?assertEqual(none, Msg)- after 400 -> ok- end.--fetch_counts_errors_on_malformed_reply_test() ->- Guild = spawn(fun() ->- receive- {'$gen_call', From, {get_viewer_counts, _}} -> gen_server:reply(From, #{})- end- end),- ?assertEqual(error, fetch_counts(Guild, 100)).--parse_nonce_test() ->- ?assertEqual(<<"x">>, parse_nonce(<<"x">>)),- ?assertEqual(<<"abc">>, parse_nonce(<<"abc">>)),- ?assertEqual(undefined, parse_nonce(undefined)),- ?assertEqual(undefined, parse_nonce(<<>>)),- ?assertEqual(undefined, parse_nonce(123)),- ?assertEqual(undefined, parse_nonce("string")),- Big = binary:copy(<<"a">>, ?MAX_NONCE_BYTES + 1),- ?assertEqual(undefined, parse_nonce(Big)),- Edge = binary:copy(<<"a">>, ?MAX_NONCE_BYTES),- ?assertEqual(Edge, parse_nonce(Edge)).---endif.diff --git a/fluxer_gateway/src/guild/guild_request_members_filter.erl b/fluxer_gateway/src/guild/guild_request_members_filter.erlindex 25a1f1763..ff721ffe0 100644--- a/fluxer_gateway/src/guild/guild_request_members_filter.erl+++ b/fluxer_gateway/src/guild/guild_request_members_filter.erl@@ -16,10 +16,6 @@-export_type([session_state/0]).--ifdef(TEST).--export([prune_expired_entries/2]).--endif.--define(REQUEST_MEMBERS_RATE_LIMIT_TABLE, guild_request_members_rate_limit).-define(REQUEST_MEMBERS_RATE_LIMIT_WINDOW_MS, 10000).-define(REQUEST_MEMBERS_RATE_LIMIT_MAX_EVENTS, 12).@@ -85,7 +81,7 @@ request_members_management_permission() ->-spec lookup_session_guild(integer(), session_state()) -> {ok, pid()} | {error, not_found}.lookup_session_guild(GuildId, SessionState) ->- Guilds = maps:get(guilds, SessionState, #{}),+ Guilds = session_visits:member_guilds(SessionState),case maps:get(GuildId, Guilds, undefined) of{Pid, _Ref} when is_pid(Pid) -> {ok, Pid};_ -> {error, not_found}diff --git a/fluxer_gateway/src/presence/presence.erl b/fluxer_gateway/src/presence/presence.erlindex a7ea476a3..126bf3c36 100644--- a/fluxer_gateway/src/presence/presence.erl+++ b/fluxer_gateway/src/presence/presence.erl@@ -175,6 +175,14 @@ handle_cast_guild({add_temporary_guild, GuildId}, State) when is_integer(GuildId{noreply, cast_guild_op(fun presence_connect:handle_add_temporary_guild/2, GuildId, State)};handle_cast_guild({remove_temporary_guild, GuildId}, State) when is_integer(GuildId) ->{noreply, presence_connect:remove_temporary_guild_id(GuildId, State)};+handle_cast_guild({start_visit, GuildId, Operator}, State) when+ is_integer(GuildId), is_boolean(Operator)+->+ ok = presence_session:notify_sessions({visit_start, GuildId, Operator}, State),+ {noreply, State};+handle_cast_guild({end_visit, GuildId}, State) when is_integer(GuildId) ->+ ok = presence_session:notify_sessions({visit_end, GuildId}, State),+ {noreply, State};handle_cast_guild(_, State) ->{noreply, State}.@@ -262,15 +270,15 @@ handle_presence_rejoin(State) ->),{noreply, State}.--spec process_dispatch_event(atom(), map(), state()) -> state().-process_dispatch_event(user_update, Data, State) ->+-spec process_dispatch_event(atom(), map() | list(), state()) -> state().+process_dispatch_event(user_update, Data, State) when is_map(Data) ->presence_update:handle_user_update_event(Data, State);-process_dispatch_event(user_settings_update, Data, State) ->+process_dispatch_event(user_settings_update, Data, State) when is_map(Data) ->NewState = presence_update:handle_user_settings_update(Data, State),presence_broadcast:force_publish_global_presence(NewState);-process_dispatch_event(message_create, Data, State) ->+process_dispatch_event(message_create, Data, State) when is_map(Data) ->presence_update:handle_message_create_event(Data, State);-process_dispatch_event(message_ack, Data, State) ->+process_dispatch_event(message_ack, Data, State) when is_map(Data) ->presence_update:handle_message_ack_event(Data, State);process_dispatch_event(_, _Data, State) ->State.@@ -282,82 +290,3 @@ binary_list(Items) ->-spec user_ids([term()]) -> [user_id()].user_ids(Items) ->[Item || Item <- Items, is_integer(Item)].---spec normalize_start_link(gen_server:start_ret()) -> {ok, pid()} | {error, term()}.-normalize_start_link({ok, Pid}) ->- {ok, Pid};-normalize_start_link({error, Reason}) ->- {error, Reason};-normalize_start_link(ignore) ->- {error, ignore}.---ifdef(TEST).--include_lib("eunit/include/eunit.hrl").--presence_rejoin_notifies_all_sessions_test() ->- Parent = self(),- Session1 = spawn(fun() -> rejoin_check_receiver(Parent, one) end),- Session2 = spawn(fun() -> rejoin_check_receiver(Parent, two) end),- State = test_state(#{- <<"s1">> => test_session_entry(Session1),- <<"s2">> => test_session_entry(Session2)- }),- ?assertEqual({noreply, State}, handle_cast(presence_rejoin, State)),- ?assert(received_rejoin_ack(one)),- ?assert(received_rejoin_ack(two)).--thread_user_flip_is_forwarded_to_every_session_test() ->- State = test_state(#{<<"s1">> => test_session_entry(self())}),- ?assertEqual({noreply, State}, handle_info({thread_user_flip, 4}, State)),- receive- {thread_user_flip, 4} -> ok- after 1000 -> ?assert(false)- end.---spec test_state(sessions()) -> state().-test_state(Sessions) ->- #{- user_id => 1,- user_data => #{},- sessions => Sessions,- push_buffer => [],- custom_status => null,- status => online,- guild_ids => #{},- temporary_guild_ids => #{},- friends => #{},- group_dm_recipients => #{},- subscriptions => #{},- is_bot => false,- initial_presences_sent => false,- last_published_presence => undefined- }.---spec test_session_entry(pid()) -> session_entry().-test_session_entry(Pid) ->- #{- session_id => <<"s">>,- status => online,- afk => false,- mobile => false,- pid => Pid,- mref => make_ref(),- socket_pid => undefined- }.---spec rejoin_check_receiver(pid(), atom()) -> term().-rejoin_check_receiver(Parent, Tag) ->- receive- {presence_rejoin_check} -> Parent ! {rejoin_ack, Tag}- after 1000 -> Parent ! {rejoin_timeout, Tag}- end.---spec received_rejoin_ack(atom()) -> boolean().-received_rejoin_ack(Tag) ->- receive- {rejoin_ack, Tag} -> true;- {rejoin_timeout, Tag} -> false- after 1500 -> false- end.---endif.diff --git a/fluxer_gateway/src/presence/presence_session.erl b/fluxer_gateway/src/presence/presence_session.erlindex 33785cfaf..ff7955642 100644--- a/fluxer_gateway/src/presence/presence_session.erl+++ b/fluxer_gateway/src/presence/presence_session.erl@@ -9,6 +9,7 @@dispatch_sessions_replace/1,notify_sessions_guild_join/2,notify_sessions_guild_leave/2,+ notify_sessions/2,find_session_by_ref/2]).@@ -152,27 +153,22 @@ dispatch_sessions_replace(State) ->-spec notify_sessions_guild_join(integer(), state()) -> ok.notify_sessions_guild_join(GuildId, State) ->- Sessions = maps:get(sessions, State),- SessionPids = [maps:get(pid, S) || S <- maps:values(Sessions)],- lists:foreach(- fun(Pid) when is_pid(Pid) ->- _ = shard_utils:safe_cast(Pid, {guild_join, GuildId})- end,- SessionPids- ),- ok.+ notify_sessions({guild_join, GuildId}, State).-spec notify_sessions_guild_leave(integer(), state()) -> ok.notify_sessions_guild_leave(GuildId, State) ->+ notify_sessions({guild_leave, GuildId}, State).++-spec notify_sessions(term(), state()) -> ok.+notify_sessions(Msg, State) ->Sessions = maps:get(sessions, State),- SessionPids = [maps:get(pid, S) || S <- maps:values(Sessions)],lists:foreach(- fun(Pid) when is_pid(Pid) ->- _ = shard_utils:safe_cast(Pid, {guild_leave, GuildId})+ fun+ (#{pid := Pid}) when is_pid(Pid) -> _ = shard_utils:safe_cast(Pid, Msg);+ (_) -> okend,- SessionPids- ),- ok.+ maps:values(Sessions)+ ).-spec find_session_by_ref(reference(), session_ref_map()) -> {ok, session_id()} | not_found.find_session_by_ref(Ref, Sessions) ->@@ -209,104 +205,3 @@ normalize_boolean(_) -> false.-spec normalize_socket_pid(term()) -> pid() | undefined.normalize_socket_pid(Pid) when is_pid(Pid) -> Pid;normalize_socket_pid(_) -> undefined.---ifdef(TEST).--include_lib("eunit/include/eunit.hrl").--handle_presence_update_updates_mobile_test() ->- SessionId = <<"s1">>,- State = #{- sessions => #{- SessionId => #{- session_id => SessionId,- status => online,- afk => false,- mobile => false,- pid => self(),- mref => make_ref(),- socket_pid => undefined- }- }- },- {noreply, NewState} = handle_presence_update(- #{session_id => SessionId, status => online, afk => false, mobile => true}, State- ),- UpdatedSession = maps:get(SessionId, maps:get(sessions, NewState)),- ?assertEqual(true, maps:get(mobile, UpdatedSession)),- receive- {'$gen_cast', {dispatch, sessions_replace, SessionsData}} ->- AllSession = hd(SessionsData),- ?assertEqual(<<"all">>, maps:get(<<"session_id">>, AllSession)),- ?assertEqual(true, maps:get(<<"mobile">>, AllSession))- after 1000 ->- ?assert(false)- end.--handle_presence_update_unchanged_session_is_noop_test() ->- flush_test_messages(),- SessionId = <<"s1">>,- Session = #{- session_id => SessionId,- status => online,- afk => false,- mobile => false,- pid => self(),- mref => make_ref(),- socket_pid => undefined- },- State = #{sessions => #{SessionId => Session}},- {noreply, NewState} = handle_presence_update(- #{session_id => SessionId, status => online, afk => false, mobile => false}, State- ),- ?assertEqual(State, NewState),- receive- {'$gen_cast', {dispatch, sessions_replace, _SessionsData}} ->- ?assert(false)- after 50 ->- ok- end.--handle_session_connect_existing_session_refreshes_status_test() ->- SessionId = <<"s1">>,- Ref = make_ref(),- State = #{- sessions => #{- SessionId => #{- session_id => SessionId,- status => invisible,- afk => true,- mobile => false,- pid => self(),- mref => Ref,- socket_pid => undefined- }- }- },- {reply, {ok, SessionsData}, NewState} = handle_session_connect(- #{- session_id => SessionId,- status => dnd,- afk => false,- mobile => true,- socket_pid => self()- },- self(),- State- ),- UpdatedSession = maps:get(SessionId, maps:get(sessions, NewState)),- ?assertEqual(dnd, maps:get(status, UpdatedSession)),- ?assertEqual(false, maps:get(afk, UpdatedSession)),- ?assertEqual(true, maps:get(mobile, UpdatedSession)),- ?assertEqual(self(), maps:get(socket_pid, UpdatedSession)),- ?assertEqual(Ref, maps:get(mref, UpdatedSession)),- [AllSession | _] = SessionsData,- ?assertEqual(<<"dnd">>, maps:get(<<"status">>, AllSession)),- ?assertEqual(false, maps:get(<<"mobile">>, AllSession)).--flush_test_messages() ->- receive- _ -> flush_test_messages()- after 0 ->- ok- end.--endif.diff --git a/fluxer_gateway/src/session/session.erl b/fluxer_gateway/src/session/session.erlindex a897b117c..b1bb09b16 100644--- a/fluxer_gateway/src/session/session.erl+++ b/fluxer_gateway/src/session/session.erl@@ -35,6 +35,7 @@buffer_bytes => non_neg_integer(),seq => seq(),ack_seq => seq(),+ replay_floor => seq(),properties => map(),status => status(),resume_status => status(),@@ -168,7 +169,24 @@ handle_cast_guild_or_lifecycle({reconnect_drain, SocketPid}, State) whensession_lifecycle:handle_reconnect_drain(SocketPid, State);handle_cast_guild_or_lifecycle({guild_join, GuildId}, State) when is_integer(GuildId) ->self() ! {guild_connect, GuildId, 0},- {noreply, session_bot_guilds:track_join(GuildId, State)};+ {noreply,+ session_bot_guilds:track_join(+ GuildId, session_visits:handle_guild_join(GuildId, State)+ )};+handle_cast_guild_or_lifecycle({visit_start, GuildId, Operator}, State) when+ is_integer(GuildId), is_boolean(Operator)+->+ session_visits:handle_start(GuildId, Operator, State);+handle_cast_guild_or_lifecycle({visit_end, GuildId}, State) when is_integer(GuildId) ->+ session_visits:handle_end(GuildId, State);+handle_cast_guild_or_lifecycle({visit_rejected, GuildId, _Reason}, State) when+ is_integer(GuildId)+->+ session_visits:handle_rejected(GuildId, State);+handle_cast_guild_or_lifecycle({visit_promoted, GuildId, GuildPid, GuildState}, State) when+ is_integer(GuildId), is_pid(GuildPid), is_map(GuildState)+->+ session_visits:handle_promoted(GuildId, GuildPid, GuildState, State);handle_cast_guild_or_lifecycle({store_guild_subscriptions, Data}, State) when is_map(Data) ->{noreply, session_guilds:store_guild_subscriptions(Data, State)};handle_cast_guild_or_lifecycle({guild_leave, GuildId, forced_unavailable, true}, State) when@@ -249,10 +267,7 @@ handle_info(Msg, State) ->-spec notify_thread_user_flip(session_state()) -> session_state().notify_thread_user_flip(#{id := SessionId} = State) when is_binary(SessionId) ->- case- maps:get(bot, State, false) =:= true orelse- maps:get(thread_channels_capable, State, false) =:= true- of+ case session_thread_backstop:capable(State) oftrue -> cast_thread_user_flip(SessionId, State);false -> Stateend;@@ -278,7 +293,7 @@ handle_info_guild_connect_result({guild_connect_result, GuildId, Attempt, Result}, State) when is_integer(GuildId), is_integer(Attempt), Attempt >= 0 ->session_connection_guild:handle_guild_connect_result(- GuildId, Attempt, Result, State+ GuildId, Attempt, eqwalizer:dynamic_cast(Result), State);handle_info_guild_connect_result(_, State) ->{noreply, State}.@@ -383,212 +398,3 @@ binary_list([Value | Rest], Acc) when is_binary(Value) ->binary_list(Rest, [Value | Acc]);binary_list(_, _) ->error.---ifdef(TEST).--include_lib("eunit/include/eunit.hrl").--test_base_state(GuildId) ->- Buf = limited_deque:new(4096, 16777216),- maps:merge(test_base_core(GuildId, Buf), test_base_extra()).--test_base_core(GuildId, Buffer) ->- #{- id => <<"test-session">>,- user_id => 1,- user_data => #{},- custom_status => null,- version => 1,- token_hash => <<>>,- auth_session_id_hash => <<>>,- buffer => Buffer,- seq => 0,- ack_seq => 0,- properties => #{},- status => online,- resume_status => online,- afk => false,- mobile => false,- presence_pid => undefined,- presence_mref => undefined,- socket_pid => undefined,- socket_mref => undefined,- resume_timer => undefined,- offline_timer => undefined,- guilds => #{GuildId => {self(), make_ref()}},- calls => #{},- channels => #{},- ready => undefined,- bot => false- }.--test_base_extra() ->- #{- ignored_events => #{},- initial_guild_id => undefined,- collected_guild_states => [],- collected_sessions => [],- collected_presences => [],- relationships => #{},- suppress_presence_updates => false,- pending_presences => [],- guild_connect_inflight => #{},- voice_queue => queue:new(),- voice_queue_timer => undefined,- debounce_reactions => false,- thread_channels_capable => false,- reaction_buffer => [],- reaction_buffer_timer => undefined- }.--handle_cast_forced_unavailable_guild_leave_schedules_retry_test() ->- GuildId = 123,- State0 = test_base_state(GuildId),- {noreply, State1} = handle_cast({guild_leave, GuildId, forced_unavailable}, State0),- ?assertEqual(cached_unavailable, maps:get(GuildId, maps:get(guilds, State1))),- [LastEvent] = limited_deque:to_list(maps:get(buffer, State1)),- LastData = assert_guild_delete_event_data(LastEvent),- ?assertEqual(true, maps:get(<<"unavailable">>, LastData)),- receive- {guild_connect, GuildId, 0} -> ok- after 100 -> ?assert(false, forced_unavailable_retry_not_scheduled)- end.--handle_cast_forced_unavailable_hidden_guild_leave_includes_hidden_flag_test() ->- GuildId = 124,- State0 = test_base_state(GuildId),- {noreply, State1} = handle_cast({guild_leave, GuildId, forced_unavailable, true}, State0),- ?assertEqual(cached_unavailable, maps:get(GuildId, maps:get(guilds, State1))),- [LastEvent] = limited_deque:to_list(maps:get(buffer, State1)),- EventData = assert_guild_delete_event_data(LastEvent),- ?assertEqual(true, maps:get(<<"unavailable">>, EventData)),- ?assertEqual(true, maps:get(<<"unavailable_hidden">>, EventData)),- receive- {guild_connect, GuildId, 0} -> ok- after 100 -> ?assert(false, forced_unavailable_hidden_retry_not_scheduled)- end.---spec assert_guild_delete_event_data(term()) -> map().-assert_guild_delete_event_data(Event) when is_map(Event) ->- ?assertEqual(guild_delete, maps:get(event, Event)),- ensure_map(maps:get(data, Event)).--ensure_map(V) when is_map(V) -> V.--handle_cast_reconnect_drain_signals_socket_test() ->- SocketPid = spawn_msg_forwarder(self(), socket_message),- State = #{socket_pid => SocketPid},- {noreply, State} = handle_cast(reconnect_drain, State),- receive- {socket_message, session_reconnect} -> ok- after 200 -> ?assert(false, reconnect_signal_not_received)- end.--handle_cast_targeted_reconnect_drain_ignores_replaced_socket_test() ->- OldSocketPid = spawn_msg_forwarder(self(), old_socket_message),- NewSocketPid = spawn_msg_forwarder(self(), new_socket_message),- State = #{socket_pid => NewSocketPid},- {noreply, State} = handle_cast({reconnect_drain, OldSocketPid}, State),- receive- {old_socket_message, session_reconnect} -> ?assert(false, old_socket_signaled);- {new_socket_message, session_reconnect} -> ?assert(false, new_socket_signaled)- after 50 ->- ok- end.--handle_cast_targeted_reconnect_drain_signals_matching_socket_test() ->- SocketPid = spawn_msg_forwarder(self(), socket_message),- State = #{socket_pid => SocketPid},- {noreply, State} = handle_cast({reconnect_drain, SocketPid}, State),- receive- {socket_message, session_reconnect} -> ok- after 200 -> ?assert(false, targeted_reconnect_signal_not_received)- end.--spawn_msg_forwarder(TargetPid, Tag) ->- spawn(fun() -> msg_forwarder_loop(TargetPid, Tag) end).--msg_forwarder_loop(TargetPid, Tag) ->- receive- Msg -> TargetPid ! {Tag, Msg}- after 30000 -> ok- end.--handle_cast_reconnect_drain_with_missing_socket_is_noop_test() ->- State = #{socket_pid => undefined},- {noreply, State} = handle_cast(reconnect_drain, State),- receive- session_reconnect -> ?assert(false, unexpected_reconnect)- after 50 -> ok- end.--handle_cast_handoff_fence_signals_socket_and_stops_test() ->- SocketPid = spawn_msg_forwarder(self(), socket_message),- State0 = #{socket_pid => SocketPid},- {stop, normal, State1} = handle_cast(handoff_fence, State0),- ?assertEqual(true, maps:get(fenced, State1)),- receive- {socket_message, session_reconnect} -> ok- after 200 -> ?assert(false, fence_signal_not_received)- end.--handle_cast_handoff_fence_with_missing_socket_stops_test() ->- State0 = #{socket_pid => undefined},- {stop, normal, State1} = handle_cast(handoff_fence, State0),- ?assertEqual(true, maps:get(fenced, State1)).--handle_info_ignores_stale_resume_timeout_test() ->- CurrentToken = make_ref(),- State = #{socket_pid => undefined, resume_timer => {CurrentToken, make_ref()}},- {noreply, State} = handle_info({resume_timeout, make_ref()}, State).--handle_info_stops_on_current_resume_timeout_test() ->- CurrentToken = make_ref(),- State = #{socket_pid => undefined, resume_timer => {CurrentToken, make_ref()}},- ?assertMatch({stop, normal, _}, handle_info({resume_timeout, CurrentToken}, State)).--handle_presence_rejoin_check_reconnects_when_unattached_test() ->- {noreply, State1} = handle_presence_rejoin_check(#{presence_pid => undefined}),- ?assertEqual(undefined, maps:get(presence_pid, State1)),- ?assert(received_presence_connect()).--handle_presence_rejoin_check_keeps_healthy_attachment_test() ->- State0 = #{presence_pid => self(), presence_mref => undefined},- ?assertEqual({noreply, State0}, handle_presence_rejoin_check(State0)),- ?assertNot(received_presence_connect()).--check_ack_lag_repairs_unattached_presence_test() ->- State0 = #{- guilds => #{},- guild_connect_inflight => #{},- guild_connect_workers => #{},- guild_connect_last_repair_at => 0,- presence_pid => undefined- },- {noreply, State1} = do_handle_info_misc(check_ack_lag, State0),- ?assertEqual(undefined, maps:get(presence_pid, State1)),- ?assert(received_presence_connect()).--received_presence_connect() ->- receive- {presence_connect, 0} -> true- after 200 -> false- end.--thread_user_flip_skips_sessions_that_cannot_become_viewers_test() ->- Base = #{id => <<"s">>, guilds => #{1 => {self(), make_ref()}, 2 => undefined}},- _ = notify_thread_user_flip(Base),- _ = notify_thread_user_flip(Base#{bot => false, thread_channels_capable => false}),- ?assertNot(received_thread_user_flip()),- _ = notify_thread_user_flip(Base#{thread_channels_capable => true}),- ?assert(received_thread_user_flip()),- _ = notify_thread_user_flip(Base#{bot => true}),- ?assert(received_thread_user_flip()),- ?assertNot(received_thread_user_flip()).--received_thread_user_flip() ->- receive- {'$gen_cast', {thread_user_flip, <<"s">>}} -> true- after 100 -> false- end.---endif.diff --git a/fluxer_gateway/src/session/session_connection_guild_resolve.erl b/fluxer_gateway/src/session/session_connection_guild_resolve.erlindex 7e1616ec9..13fa7313a 100644--- a/fluxer_gateway/src/session/session_connection_guild_resolve.erl+++ b/fluxer_gateway/src/session/session_connection_guild_resolve.erl@@ -24,7 +24,8 @@is_staff := boolean(),initial_guild_id := guild_id() | undefined,user_data := map(),- thread_channels_capable => boolean()+ thread_channels_capable => boolean(),+ visit => map()}.-spec do_local_guild_connect(connect_ctx()) ->@@ -197,7 +198,7 @@ do_start_connect_async(GuildPid, Ctx) ->session_connection_unavailability:build_initial_active_guilds(InitialGuildId, GuildId),- Request = #{+ Request0 = #{session_id => SessionId,user_id => UserId,session_pid => SessionPid,@@ -207,6 +208,7 @@ do_start_connect_async(GuildPid, Ctx) ->active_guilds => ActiveGuilds,thread_channels_capable => maps:get(thread_channels_capable, Ctx, false)},+ Request = maps:merge(Request0, maps:get(visit, Ctx, #{})),CastMsg ={session_connect_async, #{guild_id => GuildId, attempt => Attempt, request => Request@@ -252,35 +254,3 @@ notify_remote_timing(SessionPid, Role, OwnerNode, FunctionName, StartedAt) whenok;notify_remote_timing(_, _, _, _, _) ->ok.---ifdef(TEST).--include_lib("eunit/include/eunit.hrl").--connect_request_carries_thread_channels_capability_test() ->- ?assertEqual(true, sent_capability(#{thread_channels_capable => true})),- ?assertEqual(false, sent_capability(#{thread_channels_capable => false})),- ?assertEqual(false, sent_capability(#{})).--sent_capability(Extra) ->- Ctx = maps:merge(- #{- session_pid => self(),- guild_id => 1,- attempt => 0,- session_id => <<"s">>,- user_id => 2,- bot => false,- is_staff => false,- initial_guild_id => undefined,- user_data => #{}- },- Extra- ),- pending = do_start_connect_async(self(), Ctx),- receive- {'$gen_cast', {session_connect_async, #{request := Request}}} ->- maps:get(thread_channels_capable, Request)- after 1000 -> erlang:error(connect_cast_not_sent)- end.---endif.diff --git a/fluxer_gateway/src/session/session_connection_presence.erl b/fluxer_gateway/src/session/session_connection_presence.erlindex c3ab183a3..726382e92 100644--- a/fluxer_gateway/src/session/session_connection_presence.erl+++ b/fluxer_gateway/src/session/session_connection_presence.erl@@ -13,6 +13,7 @@-export_type([session_state/0, attempt/0]).-define(MAX_RETRY_ATTEMPTS, 25).+-define(PRESENCE_CONNECT_TIMEOUT_MS, 10000).-type session_state() :: session:session_state().-type attempt() :: non_neg_integer().@@ -54,7 +55,7 @@ build_presence_request(State) when is_map(State) ->#{user_id => maps:get(user_id, State),user_data => maps:get(user_data, State),- guild_ids => maps:keys(maps:get(guilds, State)),+ guild_ids => session_visits:member_guild_ids(State),status => maps:get(status, State),friend_ids => FriendIds,group_dm_recipients => GroupDmRecipients,@@ -71,43 +72,6 @@ do_session_connect(Pid, Attempt, State) when is_map(State) ->SocketPid = maps:get(socket_pid, State, undefined),FriendIds = presence_targets:friend_ids_from_state(State),GroupDmRecipients = presence_targets:group_dm_recipients_from_state(State),- try_session_connect(- Pid,- SessionId,- Status,- Afk,- Mobile,- SocketPid,- FriendIds,- GroupDmRecipients,- Attempt,- State- ).---spec try_session_connect(- pid(),- binary(),- atom(),- boolean(),- boolean(),- pid() | undefined,- [integer()],- map(),- attempt(),- session_state()-) -> {noreply, session_state()} | {stop, normal, session_state()}.-try_session_connect(- Pid,- SessionId,- Status,- Afk,- Mobile,- SocketPid,- FriendIds,- GroupDmRecipients,- Attempt,- State-) ->ConnectReq = #{session_id => SessionId,status => Status,@@ -117,7 +81,7 @@ try_session_connect(},ConnectStartedAt = gateway_timings:start(),Result = safe_call(fun() ->- gen_server:call(Pid, {session_connect, ConnectReq}, 10000)+ gen_server:call(Pid, {session_connect, ConnectReq}, ?PRESENCE_CONNECT_TIMEOUT_MS)end),Remote = gateway_timings:remote_node(presence, node(Pid)),GwTimings = gateway_timings:record_function(@@ -224,131 +188,3 @@ maybe_demonitor_presence(State) ->_ ->okend.---ifdef(TEST).--include_lib("eunit/include/eunit.hrl").--try_session_connect_failure_schedules_retry_test() ->- DeadPid = spawn(fun() -> ok end),- Ref = monitor(process, DeadPid),- receive- {'DOWN', Ref, process, DeadPid, _Reason} -> ok- after 1000 ->- ?assert(false, presence_pid_did_not_exit)- end,- {noreply, State} = try_session_connect(- DeadPid,- <<"session">>,- online,- false,- false,- undefined,- [],- #{},- 0,- #{presence_pid => undefined}- ),- ?assertEqual(undefined, maps:get(presence_pid, State)),- receive- {presence_connect, 1} -> ok- after 2000 ->- ?assert(false, presence_retry_not_scheduled)- end.--presence_attachment_healthy_false_when_unattached_test() ->- ?assertNot(presence_attachment_healthy(#{presence_pid => undefined})),- ?assertNot(presence_attachment_healthy(#{})).--presence_attachment_healthy_true_for_live_local_pid_without_user_id_test() ->- ?assert(presence_attachment_healthy(#{presence_pid => self()})).--presence_attachment_healthy_false_for_dead_local_pid_test() ->- ?assertNot(presence_attachment_healthy(#{presence_pid => dead_pid()})).--presence_attachment_healthy_false_when_owner_node_differs_test() ->- with_presence_owner(['remote@nohost'], fun() ->- ?assertNot(- presence_attachment_healthy(#{presence_pid => self(), user_id => 4242})- )- end).--presence_attachment_healthy_true_when_owner_is_local_node_test() ->- with_presence_owner([node()], fun() ->- ?assert(- presence_attachment_healthy(#{presence_pid => self(), user_id => 4242})- )- end).--force_presence_reconnect_clears_state_and_schedules_connect_test() ->- State1 = force_presence_reconnect(#{presence_pid => self(), presence_mref => undefined}),- ?assertEqual(undefined, maps:get(presence_pid, State1)),- ?assertEqual(undefined, maps:get(presence_mref, State1)),- ?assert(received_presence_connect()).--force_presence_reconnect_demonitors_existing_mref_test() ->- Target = spawn(fun presence_target_loop/0),- Ref = monitor(process, Target),- State1 = force_presence_reconnect(#{presence_pid => Target, presence_mref => Ref}),- ?assertEqual(undefined, maps:get(presence_mref, State1)),- _ = received_presence_connect(),- Target ! stop,- receive- {'DOWN', Ref, process, _, _} -> ?assert(false, mref_not_demonitored)- after 200 -> ok- end.--repair_presence_connection_keeps_healthy_attachment_test() ->- State0 = #{presence_pid => self(), presence_mref => undefined},- ?assertEqual(State0, repair_presence_connection(State0)),- ?assertNot(received_presence_connect()).--repair_presence_connection_reconnects_stale_attachment_test() ->- with_presence_owner(['remote@nohost'], fun() ->- State0 = #{presence_pid => self(), presence_mref => undefined, user_id => 4242},- State1 = repair_presence_connection(State0),- ?assertEqual(undefined, maps:get(presence_pid, State1)),- ?assert(received_presence_connect())- end).--repair_presence_connection_reconnects_when_unattached_test() ->- State1 = repair_presence_connection(#{presence_pid => undefined}),- ?assertEqual(undefined, maps:get(presence_pid, State1)),- ?assert(received_presence_connect()).--dead_pid() ->- Pid = spawn(fun() -> ok end),- ok = gateway_retry_timer:wait(50),- Pid.--presence_target_loop() ->- receive- stop -> ok- after 5000 -> ok- end.--received_presence_connect() ->- receive- {presence_connect, 0} -> true- after 200 -> false- end.--with_presence_owner(RoleNodes, Fun) ->- MembersKey = {gateway_cluster_membership, members},- RoleKey = {gateway_cluster_membership, members_by_role},- PrevMembers = persistent_term:get(MembersKey, undefined),- PrevRoles = persistent_term:get(RoleKey, undefined),- persistent_term:put(MembersKey, lists:usort([node() | RoleNodes])),- persistent_term:put(RoleKey, #{presence => RoleNodes}),- try- Fun()- after- restore_persistent_term(MembersKey, PrevMembers),- restore_persistent_term(RoleKey, PrevRoles)- end.--restore_persistent_term(Key, undefined) ->- persistent_term:erase(Key);-restore_persistent_term(Key, Value) ->- persistent_term:put(Key, Value).---endif.diff --git a/fluxer_gateway/src/session/session_manager_shard_drain.erl b/fluxer_gateway/src/session/session_manager_shard_drain.erlindex ec1e0c24b..b4781be01 100644--- a/fluxer_gateway/src/session/session_manager_shard_drain.erl+++ b/fluxer_gateway/src/session/session_manager_shard_drain.erl@@ -7,24 +7,25 @@broadcast_reconnect_drain/1,handoff_to_topology/2,build_session_data/7,- build_ready_data_for_session/1,filter_guild_ids_for_identify/2,validate_identify_sharding/3,- should_debounce_reactions/1,thread_channels_capable/1,- extract_e2ee_capable/1,fetch_rpc_data/2]).--ifdef(TEST).--export([push_and_drain_session/3]).--endif.--export_type([handoff_result/0, identify_data/0, session_id/0, shard_identify_data/0, state/0]).-define(IDENTIFY_FLAG_DEBOUNCE_MESSAGE_REACTIONS, 16#2).-define(IDENTIFY_FLAG_CHANNEL_THREADS, 16#4).+-define(IDENTIFY_FLAG_GUILD_VISITS, 16#8).-define(READ_STATE_FLAG_IS_THREAD, 16#2).+-define(INTERNAL_READY_FIELDS, [+ <<"visit_guild_ids">>,+ <<"operator_visit_guild_ids">>,+ <<"adult_content_allowed">>,+ <<"account_tier">>,+ <<"ip_key">>+]).-define(SESSION_RPC_RETRY_CONFIG, {1, 1000, 10000, 500}).-type session_id() :: binary().@@ -164,11 +165,11 @@ build_session_data(Data, IdentifyData, Version, SocketPid, SessionId, UserDataMaready => filter_thread_read_states(build_ready_data_for_session(Data),thread_channels_capable(IdentifyData),- map_utils:get_safe(UserDataMap, <<"bot">>, false) =:= true,+ is_truthy(map_utils:get_safe(UserDataMap, <<"bot">>, false)),UserId),bot => map_utils:get_safe(UserDataMap, <<"bot">>, false),- e2ee_capable => extract_e2ee_capable(Properties),+ e2ee_capable => is_truthy(map_utils:get_safe(Properties, <<"e2ee_capable">>, false)),geoip_country_code => extract_geoip_country_code(Data),ignored_events => term_detach:detach(IgnoredEvents),initial_guild_id => map_utils:get_safe(IdentifyData, initial_guild_id, undefined),@@ -176,7 +177,27 @@ build_session_data(Data, IdentifyData, Version, SocketPid, SessionId, UserDataMadebounce_reactions => should_debounce_reactions(IdentifyData),thread_channels_capable => thread_channels_capable(IdentifyData)},- BaseFields.+ with_visits(Data, IdentifyData, BaseFields).++-spec with_visits(map(), shard_identify_data(), map()) -> map().+with_visits(Data, IdentifyData, BaseFields) ->+ Capable =+ guild_visits_capable(IdentifyData) andalso not is_truthy(maps:get(bot, BaseFields)),+ Parsed = session_visits:parse_rpc(Data),+ MemberIds = maps:get(guilds, BaseFields),+ Visits0 =+ case Capable of+ true -> maps:get(visits, Parsed);+ false -> #{}+ end,+ Shard = maps:get(shard, IdentifyData, undefined),+ Retained = gateway_sharding:retain_guild_ids_for_shard(maps:keys(Visits0), Shard),+ Visits = maps:without(MemberIds, maps:with(Retained, Visits0)),+ maps:merge(BaseFields, Parsed#{+ visits => Visits,+ guild_visits_capable => Capable,+ guilds => MemberIds ++ maps:keys(Visits)+ }).-spec filter_guild_ids_for_identify(map(), shard_identify_data()) -> [integer()].filter_guild_ids_for_identify(Data, IdentifyData) ->@@ -259,7 +280,8 @@ is_truthy(_) -> false.-spec build_ready_data_for_session(map()) -> map().build_ready_data_for_session(Data) ->- Detached = term_detach:detach(Data#{<<"guilds">> => []}),+ Public = maps:without(?INTERNAL_READY_FIELDS, Data),+ Detached = term_detach:detach(Public#{<<"guilds">> => []}),case Detached ofM when is_map(M) -> M;_ -> #{}@@ -269,21 +291,19 @@ build_ready_data_for_session(Data) ->filter_thread_read_states(#{<<"read_states">> := Rows} = Ready, Capable, Bot, UserId) whenis_list(Rows)->- case lists:any(fun has_read_state_flags/1, Rows) of+ HasFlags = lists:any(fun has_read_state_flags/1, Rows),+ Loaded = HasFlags andalso channel_threads_config:loaded(),+ Viewer = fun+ (_Row) when not Loaded -> Capable orelse Bot;+ (Row) ->+ guild_thread_gate:compute_viewer(+ read_state_guild_active(Row), Bot, Capable, UserId+ )+ end,+ case HasFlags offalse ->Ready;true ->- Viewer =- case channel_threads_config:loaded() of- false ->- fun(_Row) -> Capable orelse Bot end;- true ->- fun(Row) ->- guild_thread_gate:compute_viewer(- read_state_guild_active(Row), Bot, Capable, UserId- )- end- end,Ready#{<<"read_states">> => lists:filtermap(fun(Row) -> filter_read_state(Row, Capable orelse Bot, Viewer) end, Rows@@ -319,28 +339,23 @@ filter_read_state(_Row, _Capable, _Viewer) ->-spec should_debounce_reactions(map()) -> boolean().should_debounce_reactions(IdentifyData) ->- case map_utils:get_safe(IdentifyData, flags, 0) of- Flags when is_integer(Flags), Flags >= 0 ->- bitset:has(Flags, ?IDENTIFY_FLAG_DEBOUNCE_MESSAGE_REACTIONS);- _ ->- false- end.+ identify_flag(IdentifyData, ?IDENTIFY_FLAG_DEBOUNCE_MESSAGE_REACTIONS).-spec thread_channels_capable(map()) -> boolean().thread_channels_capable(IdentifyData) ->+ identify_flag(IdentifyData, ?IDENTIFY_FLAG_CHANNEL_THREADS).++-spec guild_visits_capable(map()) -> boolean().+guild_visits_capable(IdentifyData) ->+ identify_flag(IdentifyData, ?IDENTIFY_FLAG_GUILD_VISITS).++-spec identify_flag(map(), non_neg_integer()) -> boolean().+identify_flag(IdentifyData, Flag) ->case map_utils:get_safe(IdentifyData, flags, 0) of- Flags when is_integer(Flags), Flags >= 0 ->- bitset:has(Flags, ?IDENTIFY_FLAG_CHANNEL_THREADS);- _ ->- false+ Flags when is_integer(Flags), Flags >= 0 -> bitset:has(Flags, Flag);+ _ -> falseend.--spec extract_e2ee_capable(term()) -> boolean().-extract_e2ee_capable(Properties) when is_map(Properties) ->- is_truthy(maps:get(<<"e2ee_capable">>, Properties, false));-extract_e2ee_capable(_) ->- false.--spec extract_geoip_country_code(map()) -> binary() | undefined.extract_geoip_country_code(Data) ->case maps:get(<<"geoip_country_code">>, Data, null) of@@ -381,8 +396,9 @@ do_fetch_rpc_data(Request, PeerIP) -><<"ip">> => PeerIP},RpcWithCoords = session_manager_shard_lookup:add_coordinates(RpcRequest, Lat, Lon),- RpcWithCapability = add_thread_channels_capable(- RpcWithCoords, thread_channels_capable(IdentifyData)+ RpcWithCapability = add_guild_visits_capable(+ add_thread_channels_capable(RpcWithCoords, thread_channels_capable(IdentifyData)),+ guild_visits_capable(IdentifyData)),RpcResult = rpc_client:call_with_retry(RpcWithCapability, ?SESSION_RPC_RETRY_CONFIG),classify_rpc_result(RpcResult, PeerIP).@@ -393,6 +409,12 @@ add_thread_channels_capable(RpcRequest, true) ->add_thread_channels_capable(RpcRequest, false) ->RpcRequest.+-spec add_guild_visits_capable(map(), boolean()) -> map().+add_guild_visits_capable(RpcRequest, true) ->+ RpcRequest#{<<"guild_visits_capable">> => true};+add_guild_visits_capable(RpcRequest, false) ->+ RpcRequest.+-spec classify_rpc_result(term(), term()) -> {ok, map()} | {error, term()}.classify_rpc_result({ok, Data}, _PeerIP) when is_map(Data) -> {ok, Data};classify_rpc_result({error, {rpc_error, 401, _}}, _PeerIP) ->@@ -411,87 +433,3 @@ classify_rpc_result({error, {retries_exhausted, Reason}}, PeerIP) ->{error, {retries_exhausted, Reason}};classify_rpc_result({error, Reason}, _PeerIP) ->{error, {network_error, Reason}}.---ifdef(TEST).--include_lib("eunit/include/eunit.hrl").--filter_thread_read_states_test() ->- Key = channel_threads_config,- Previous = persistent_term:get(Key, undefined),- persistent_term:put(Key, (channel_threads_config:default_config())#{config_version => 1}),- try- Rows = [- #{<<"id">> => <<"1">>},- #{<<"id">> => <<"2">>, <<"flags">> => 1},- #{<<"id">> => <<"3">>, <<"flags">> => 3}- ],- Ready = #{<<"read_states">> => Rows},- ?assertEqual(- #{<<"read_states">> => [#{<<"id">> => <<"1">>}, #{<<"id">> => <<"2">>}]},- filter_thread_read_states(Ready, false, false, 7)- ),- ?assertEqual(- #{- <<"read_states">> => [- #{<<"id">> => <<"1">>}, #{<<"id">> => <<"2">>, <<"flags">> => 1}- ]- },- filter_thread_read_states(Ready, true, false, 7)- ),- Plain = #{<<"read_states">> => [#{<<"id">> => <<"1">>}]},- ?assertEqual(Plain, filter_thread_read_states(Plain, false, false, 7))- after- case Previous of- undefined -> persistent_term:erase(Key);- _ -> persistent_term:put(Key, Previous)- end- end.--filter_thread_read_states_keeps_thread_rows_before_the_config_loads_test() ->- Key = channel_threads_config,- Previous = persistent_term:get(Key, undefined),- persistent_term:put(Key, channel_threads_config:default_config()),- try- Thread = #{<<"id">> => <<"1">>, <<"guild_id">> => <<"5">>, <<"flags">> => 3},- Ready = #{<<"read_states">> => [Thread]},- ?assertEqual(Ready, filter_thread_read_states(Ready, true, false, 7)),- ?assertEqual(Ready, filter_thread_read_states(Ready, false, true, 7)),- ?assertEqual(- #{<<"read_states">> => []}, filter_thread_read_states(Ready, false, false, 7)- )- after- case Previous of- undefined -> persistent_term:erase(Key);- _ -> persistent_term:put(Key, Previous)- end- end.--filter_thread_read_states_checks_the_row_guild_test() ->- Key = channel_threads_config,- Previous = persistent_term:get(Key, undefined),- persistent_term:put(Key, (channel_threads_config:default_config())#{- enabled => true,- enabled_guilds => #{<<"5">> => true},- included_users => #{<<"7">> => true}- }),- try- Active = #{<<"id">> => <<"1">>, <<"guild_id">> => <<"5">>, <<"flags">> => 3},- Inactive = #{<<"id">> => <<"2">>, <<"guild_id">> => <<"6">>, <<"flags">> => 3},- Ready = #{<<"read_states">> => [Active, Inactive]},- ?assertEqual(- #{<<"read_states">> => [Active]}, filter_thread_read_states(Ready, true, false, 7)- ),- ?assertEqual(- #{<<"read_states">> => []}, filter_thread_read_states(Ready, false, false, 7)- ),- ?assertEqual(- #{<<"read_states">> => [Active]}, filter_thread_read_states(Ready, false, true, 8)- )- after- case Previous of- undefined -> persistent_term:erase(Key);- _ -> persistent_term:put(Key, Previous)- end- end.---endif.diff --git a/fluxer_gateway/src/session/session_visits.erl b/fluxer_gateway/src/session/session_visits.erlnew file mode 100644index 000000000..6786d39f7--- /dev/null+++ b/fluxer_gateway/src/session/session_visits.erl@@ -0,0 +1,252 @@+%% SPDX-License-Identifier: AGPL-3.0-or-later++-module(session_visits).+-typing([eqwalizer]).++-export([+ parse_rpc/1,+ init_fields/1,+ member_guild_ids/1,+ member_guilds/1,+ connect_fields/2,+ accept_connect/4,+ handle_start/3,+ handle_end/2,+ handle_rejected/2,+ handle_promoted/4,+ handle_guild_join/2+]).++-type visits() :: #{session:guild_id() => #{operator := boolean()}}.++-spec parse_rpc(map()) -> map().+parse_rpc(Data) ->+ Operator = maps:from_keys(+ snowflake_id:parse_list(list_field(<<"operator_visit_guild_ids">>, Data)), true+ ),+ Visits = maps:from_list([+ {GuildId, #{operator => maps:is_key(GuildId, Operator)}}+ || GuildId <- snowflake_id:parse_list(list_field(<<"visit_guild_ids">>, Data))+ ]),+ #{+ visits => Visits,+ adult_content_allowed => type_conv:is_true(+ maps:get(<<"adult_content_allowed">>, Data, false)+ ),+ account_tier => visitor_request:tier(maps:get(<<"account_tier">>, Data, undefined)),+ ip_key => visitor_request:ip_key(maps:get(<<"ip_key">>, Data, undefined))+ }.++-spec init_fields(map()) -> map().+init_fields(D) ->+ #{+ visits => normalize_visits(maps:get(visits, D, #{})),+ guild_visits_capable => type_conv:is_true(+ maps:get(guild_visits_capable, D, false)+ ),+ adult_content_allowed => type_conv:is_true(+ maps:get(adult_content_allowed, D, false)+ ),+ account_tier => visitor_request:tier(maps:get(account_tier, D, undefined)),+ ip_key => visitor_request:ip_key(maps:get(ip_key, D, undefined))+ }.++-spec visits(session:session_state()) -> map().+visits(#{visits := Visits}) when is_map(Visits) ->+ Visits.++-spec normalize_visits(term()) -> visits().+normalize_visits(Visits) when is_map(Visits) ->+ maps:fold(+ fun+ (GuildId, #{operator := Operator}, Acc) when is_integer(GuildId), GuildId > 0 ->+ Acc#{GuildId => #{operator => type_conv:is_true(Operator)}};+ (_GuildId, _Visit, Acc) ->+ Acc+ end,+ #{},+ Visits+ );+normalize_visits(_) ->+ #{}.++-spec visiting(session:guild_id(), session:session_state()) -> boolean().+visiting(GuildId, State) ->+ maps:is_key(GuildId, visits(State)).++-spec member_guild_ids(session:session_state()) -> [session:guild_id()].+member_guild_ids(State) ->+ Visits = visits(State),+ [+ GuildId+ || GuildId <- maps:keys(maps:get(guilds, State, #{})), not maps:is_key(GuildId, Visits)+ ].++-spec member_guilds(map()) -> map().+member_guilds(SessionState) ->+ maps:without(maps:keys(visits(SessionState)), maps:get(guilds, SessionState, #{})).++-spec connect_fields(session:guild_id(), session:session_state()) -> map().+connect_fields(GuildId, State) ->+ case maps:get(GuildId, visits(State), undefined) of+ #{operator := Operator} ->+ #{+ visitor => true,+ operator => Operator,+ adult_content_allowed => type_conv:is_true(+ maps:get(adult_content_allowed, State, false)+ ),+ visitor_tier => maps:get(account_tier, State, <<"unclaimed">>),+ ip_key => maps:get(ip_key, State, undefined)+ };+ _ ->+ #{}+ end.++-spec accept_connect(session:guild_id(), pid(), map(), session:session_state()) ->+ {accept, session:session_state()} | {drop, session:session_state()}.+accept_connect(GuildId, GuildPid, GuildState, State) ->+ case+ {+ type_conv:is_true(maps:get(<<"visiting">>, GuildState, false)),+ visiting(GuildId, State)+ }+ of+ {true, true} ->+ {accept, State};+ {true, false} ->+ _ = shard_utils:safe_cast(GuildPid, {visitor_leave, maps:get(id, State)}),+ Guilds = maps:get(guilds, State, #{}),+ {drop, State#{guilds => maps:remove(GuildId, Guilds)}};+ {false, true} ->+ {accept, drop_visit(GuildId, State)};+ {false, false} ->+ {accept, State}+ end.++-spec handle_start(session:guild_id(), boolean(), session:session_state()) ->+ {noreply, session:session_state()}.+handle_start(GuildId, Operator, State) ->+ Guilds = maps:get(guilds, State, #{}),+ Capable =+ type_conv:is_true(maps:get(guild_visits_capable, State, false)) andalso+ not type_conv:is_true(maps:get(bot, State, false)),+ case {Capable, maps:is_key(GuildId, Guilds), visiting(GuildId, State)} of+ {false, _, _} ->+ {noreply, State};+ {true, true, false} ->+ {noreply, State};+ {true, true, true} ->+ {noreply, put_visit(GuildId, Operator, State)};+ {true, false, _} ->+ self() ! {guild_connect, GuildId, 0},+ {noreply, (put_visit(GuildId, Operator, State))#{+ guilds => Guilds#{GuildId => undefined}+ }}+ end.++-spec handle_end(session:guild_id(), session:session_state()) ->+ {noreply, session:session_state()} | {stop, normal, session:session_state()}.+handle_end(GuildId, State) ->+ Visit =+ case visiting(GuildId, State) of+ false -> none;+ true -> {visit, maps:get(GuildId, maps:get(guilds, State, #{}), undefined)}+ end,+ case Visit of+ {visit, {Pid, _Ref}} when is_pid(Pid) ->+ _ = shard_utils:safe_cast(Pid, {visitor_leave, maps:get(id, State)}),+ remove_visited_guild(GuildId, State);+ {visit, _} ->+ remove_visited_guild(GuildId, State);+ none ->+ {noreply, State}+ end.++-spec handle_rejected(session:guild_id(), session:session_state()) ->+ {noreply, session:session_state()} | {stop, normal, session:session_state()}.+handle_rejected(GuildId, State) ->+ Connection =+ case visiting(GuildId, State) of+ true ->+ visit;+ false ->+ Guilds = maps:get(guilds, State, #{}),+ {member, Guilds, maps:get(GuildId, Guilds, undefined)}+ end,+ case Connection of+ visit ->+ remove_visited_guild(GuildId, State);+ {member, MemberGuilds, {_Pid, Ref}} when is_reference(Ref) ->+ demonitor(Ref, [flush]),+ self() ! {guild_connect, GuildId, 0},+ {noreply, State#{guilds => MemberGuilds#{GuildId => undefined}}};+ {member, _, _} ->+ {noreply, State}+ end.++-spec handle_promoted(session:guild_id(), pid(), map(), session:session_state()) ->+ {noreply, session:session_state()} | {stop, normal, session:session_state()}.+handle_promoted(GuildId, GuildPid, GuildState, State0) ->+ State = drop_visit(GuildId, State0),+ case maps:get(GuildId, maps:get(guilds, State, #{}), undefined) of+ {GuildPid, _Ref} ->+ session_ready:process_guild_state(GuildState, forget_collected(GuildId, State));+ _ ->+ {noreply, State}+ end.++-spec handle_guild_join(session:guild_id(), session:session_state()) -> session:session_state().+handle_guild_join(GuildId, State) ->+ drop_visit(GuildId, State).++-spec remove_visited_guild(session:guild_id(), session:session_state()) ->+ {noreply, session:session_state()} | {stop, normal, session:session_state()}.+remove_visited_guild(GuildId, State0) ->+ State = drop_visit(GuildId, State0),+ Guilds = maps:get(guilds, State, #{}),+ case maps:get(GuildId, Guilds, undefined) of+ {_Pid, Ref} when is_reference(Ref) -> demonitor(Ref, [flush]);+ _ -> ok+ end,+ DeleteData = #{<<"id">> => integer_to_binary(GuildId)},+ State1 = forget_collected(GuildId, State#{guilds => maps:remove(GuildId, Guilds)}),+ {noreply, State2} = session_dispatch:handle_dispatch(guild_delete, DeleteData, State1),+ State3 = session_guild_health:forget(GuildId, State2),+ session_ready:check_readiness(+ session_guilds:remove_guild_subscription_state(GuildId, State3)+ ).++-spec forget_collected(session:guild_id(), session:session_state()) -> session:session_state().+forget_collected(GuildId, State) ->+ IdBin = integer_to_binary(GuildId),+ case maps:get(collected_guild_states, State, undefined) of+ Collected when is_list(Collected) ->+ State#{+ collected_guild_states => [+ G+ || G <- Collected,+ not (is_map(G) andalso maps:get(<<"id">>, G, undefined) =:= IdBin)+ ]+ };+ _ ->+ State+ end.++-spec put_visit(session:guild_id(), boolean(), session:session_state()) ->+ session:session_state().+put_visit(GuildId, Operator, State) ->+ State#{+ visits => (visits(State))#{GuildId => #{operator => type_conv:is_true(Operator)}}+ }.++-spec drop_visit(session:guild_id(), session:session_state()) -> session:session_state().+drop_visit(GuildId, State) ->+ State#{visits => maps:remove(GuildId, visits(State))}.++-spec list_field(binary(), map()) -> list().+list_field(Key, Data) ->+ case maps:get(Key, Data, []) of+ List when is_list(List) -> List;+ _ -> []+ end.