A free and open source instant messaging and VoIP chat app built for friends, groups, and communities.
refactor(gateway): flatten voice helpers, drop inline tests
Hampus307df1705ff3
diff --git a/fluxer_gateway/src/guild/guild_voice_lifecycle.erl b/fluxer_gateway/src/guild/guild_voice_lifecycle.erlindex 278675379..cbf4f8f63 100644--- a/fluxer_gateway/src/guild/guild_voice_lifecycle.erl+++ b/fluxer_gateway/src/guild/guild_voice_lifecycle.erl@@ -175,7 +175,7 @@ authoritative_voice_states(State) ->{error, _Reason} -> voice_state_utils:voice_states(State)end.--spec cast_disconnect_voice_user(integer(), guild_state()) -> ok.+-spec cast_disconnect_voice_user(integer() | undefined, guild_state()) -> ok.cast_disconnect_voice_user(UserId, State) when is_integer(UserId), UserId > 0 ->case voice_server_pid(State) of{ok, VoiceServerPid} ->@@ -248,52 +248,3 @@ read_from_pid(VoiceServerPid) ->catchexit:Reason -> {error, Reason}end.---ifdef(TEST).--include_lib("eunit/include/eunit.hrl").--cast_disconnect_voice_user_reaches_the_voice_server_test() ->- Self = self(),- VoicePid = spawn(fun() ->- receive- Message -> Self ! {voice_server_got, Message}- end- end),- ok = cast_disconnect_voice_user(7, #{id => 42, voice_server_pid => VoicePid}),- receive- {voice_server_got, {'$gen_cast', {disconnect_voice_user, Request}}} ->- ?assertEqual(#{user_id => 7, connection_id => null}, Request)- after 200 ->- exit(VoicePid, kill),- ?assert(false)- end.--cast_disconnect_voice_user_without_a_voice_server_is_a_noop_test() ->- ?assertEqual(ok, cast_disconnect_voice_user(7, #{id => 987654329})),- ?assertEqual(ok, cast_disconnect_voice_user(undefined, #{id => 987654329})).--authoritative_voice_states_falls_back_to_the_guild_copy_test() ->- Cached = #{<<"conn">> => #{<<"user_id">> => <<"7">>}},- State = #{id => 987654331, voice_states => Cached},- ?assertEqual(Cached, authoritative_voice_states(State)).--authoritative_voice_states_prefers_the_voice_server_test() ->- Live = #{<<"live">> => #{<<"user_id">> => <<"8">>}},- VoicePid = spawn(fun() -> voice_states_reply_loop(Live) end),- State = #{id => 987654333, voice_states => #{}, voice_server_pid => VoicePid},- try- ?assertEqual(Live, authoritative_voice_states(State))- after- exit(VoicePid, kill)- end.--voice_states_reply_loop(VoiceStates) ->- receive- {'$gen_call', From, {get_voice_states_map}} ->- gen_server:reply(From, VoiceStates),- voice_states_reply_loop(VoiceStates);- _ ->- voice_states_reply_loop(VoiceStates)- end.---endif.diff --git a/fluxer_gateway/src/guild/voice/dm_voice_state.erl b/fluxer_gateway/src/guild/voice/dm_voice_state.erlindex bf97c8ee8..f7b4ac991 100644--- a/fluxer_gateway/src/guild/voice/dm_voice_state.erl+++ b/fluxer_gateway/src/guild/voice/dm_voice_state.erl@@ -1,9 +1,6 @@%% SPDX-License-Identifier: AGPL-3.0-or-later-module(dm_voice_state).--ifdef(TEST).--include_lib("eunit/include/eunit.hrl").--endif.-typing([eqwalizer]).@@ -229,8 +226,10 @@ call_voice_states(ChannelId) ->-spec call_voice_states_from_pid(pid()) -> [voice_state()].call_voice_states_from_pid(CallPid) ->case gateway_rpc_call_lookup:safe_gen_server_call(CallPid, {get_state}, 5000) of- {ok, {ok, #{voice_states := VoiceStates}}} when is_list(VoiceStates) -> VoiceStates;- _ -> []+ {ok, {ok, #{voice_states := VoiceStates}}} when is_list(VoiceStates) ->+ eqwalizer:dynamic_cast(VoiceStates);+ _ ->+ []end.-spec voice_region_for_rpc(term()) -> binary() | null.@@ -317,71 +316,3 @@ maybe_put_field(VoiceState, Key, Value) -> VoiceState#{Key => Value}.-spec normalize_optional_binary(term()) -> binary() | undefined.normalize_optional_binary(Value) ->guild_voice_connection_normalize:normalize_optional_binary(Value).---ifdef(TEST).--session_voice_state(UserId, ChannelId, ConnId, SessionId) ->- #{- <<"user_id">> => integer_to_binary(UserId),- <<"channel_id">> => integer_to_binary(ChannelId),- <<"connection_id">> => ConnId,- <<"session_id">> => SessionId- }.--disconnect_voice_user_keeps_other_session_connections_test() ->- State = #{- id => <<"session-1">>,- user_id => 5,- channels => #{},- dm_voice_states => #{- <<"conn-own">> => session_voice_state(5, 100, <<"conn-own">>, <<"session-1">>),- <<"conn-other">> => session_voice_state(5, 100, <<"conn-other">>, <<"session-2">>),- <<"conn-peer">> => session_voice_state(6, 100, <<"conn-peer">>, <<"session-3">>)- }- },- {reply, #{success := true}, NewState} = disconnect_voice_user(5, State),- VoiceStates = maps:get(dm_voice_states, NewState),- ?assertNot(maps:is_key(<<"conn-own">>, VoiceStates)),- ?assert(maps:is_key(<<"conn-other">>, VoiceStates)),- ?assert(maps:is_key(<<"conn-peer">>, VoiceStates)).--disconnect_voice_user_without_session_id_test() ->- State = #{- user_id => 5,- channels => #{},- dm_voice_states => #{- <<"conn-a">> => session_voice_state(5, 100, <<"conn-a">>, <<"session-1">>),- <<"conn-b">> => session_voice_state(5, 100, <<"conn-b">>, <<"session-2">>)- }- },- {reply, #{success := true}, NewState} = disconnect_voice_user(5, State),- ?assertEqual(#{}, maps:get(dm_voice_states, NewState)).--disconnect_voice_user_keeps_unattributed_connections_test() ->- State = #{- id => <<"session-1">>,- user_id => 5,- channels => #{},- dm_voice_states => #{- <<"conn-legacy">> => #{- <<"user_id">> => <<"5">>,- <<"channel_id">> => <<"100">>,- <<"connection_id">> => <<"conn-legacy">>- },- <<"conn-own">> => session_voice_state(5, 100, <<"conn-own">>, <<"session-1">>)- }- },- {reply, #{success := true}, NewState} = disconnect_voice_user(5, State),- ?assertEqual(#{}, maps:get(dm_voice_states, NewState)).--call_region_from_dead_pid_returns_null_test() ->- DeadPid = spawn(fun() -> ok end),- Ref = monitor(process, DeadPid),- receive- {'DOWN', Ref, process, DeadPid, _Reason} -> ok- after 1000 ->- ?assert(false, call_pid_did_not_exit)- end,- ?assertEqual(null, call_region_from_pid(DeadPid, 250)).---endif.diff --git a/fluxer_gateway/src/guild/voice/guild_voice_permission_sync.erl b/fluxer_gateway/src/guild/voice/guild_voice_permission_sync.erlindex e5c472a20..e2521ade0 100644--- a/fluxer_gateway/src/guild/voice/guild_voice_permission_sync.erl+++ b/fluxer_gateway/src/guild/voice/guild_voice_permission_sync.erl@@ -1,14 +1,10 @@%% SPDX-License-Identifier: AGPL-3.0-or-later-module(guild_voice_permission_sync).--ifdef(TEST).--include_lib("eunit/include/eunit.hrl").--endif.-typing([eqwalizer]).-export([- sync_user_voice_permissions/2,sync_all_voice_permissions_for_channels/2,sync_roles_after_change/3,maybe_sync_permissions_on_member_update/2@@ -62,14 +58,18 @@ sync_all_voice_permissions_for_channels(ChannelIds, State) ->undefined ->ok;GuildId ->- lists:foreach(- fun(ChannelId) ->- do_sync_channel_permissions(GuildId, ChannelId, VoiceStates, State)- end,- ChannelIds- )+ sync_channels(GuildId, ChannelIds, VoiceStates, State)end.+-spec sync_channels(integer(), [channel_id()], map(), guild_state()) -> ok.+sync_channels(GuildId, ChannelIds, VoiceStates, State) ->+ lists:foreach(+ fun(ChannelId) ->+ do_sync_channel_permissions(GuildId, ChannelId, VoiceStates, State)+ end,+ ChannelIds+ ).+-spec do_sync_channel_permissions(integer(), channel_id(), map(), guild_state()) -> ok.do_sync_channel_permissions(GuildId, ChannelId, VoiceStates, State) ->maps:foreach(@@ -97,7 +97,7 @@ sync_channel_voice_state_permissions(GuildId, VoiceState, State) ->-spec maybe_sync_permissions_on_member_update(map(), guild_state()) -> ok.maybe_sync_permissions_on_member_update(MemberUpdate, State) ->- UserId = get_member_user_id(MemberUpdate),+ UserId = guild_members_common:member_user_id(MemberUpdate),case UserId ofundefined ->ok;@@ -142,7 +142,7 @@ sync_validated_voice_state(GuildId, UserId, ChId, ConnId, VoiceState, State) ->maybe_clear_self_stream(ChId, VoiceState, #{can_stream := false}, State) ->SelfStream = maps:get(<<"self_stream">>, VoiceState, false),SelfVideo = maps:get(<<"self_video">>, VoiceState, false),- case SelfStream =:= true orelse SelfVideo =:= true of+ case type_conv:is_true(SelfStream) orelse type_conv:is_true(SelfVideo) offalse ->ok;true ->@@ -295,271 +295,8 @@ sync_if_affected(GuildId, UserId, Affected, VoiceState, State) ->is_affected(_UserId, all) -> true;is_affected(UserId, Affected) -> sets:is_element(UserId, Affected).--spec get_member_user_id(map()) -> user_id() | undefined.-get_member_user_id(MemberUpdate) ->- User = map_utils:ensure_map(maps:get(<<"user">>, MemberUpdate, #{})),- snowflake_id:parse_maybe(- maps:get(<<"id">>, User, undefined)- ).--spec state_guild_id(guild_state()) -> integer() | undefined.state_guild_id(State) ->snowflake_id:parse_maybe(maps:get(id, State, undefined)).---ifdef(TEST).--build_sync_test_state(TestFun) ->- UserId = 10,- ChannelId = 500,- GuildId = 42,- RoleId = 999,- VoiceState = #{- <<"user_id">> => integer_to_binary(UserId),- <<"channel_id">> => integer_to_binary(ChannelId),- <<"connection_id">> => <<"test-conn">>,- <<"deaf">> => true- },- Permissions =- constants:view_channel_permission() bor- constants:connect_permission() bor- constants:speak_permission() bor- constants:stream_permission(),- #{- id => GuildId,- voice_states => #{<<"conn">> => VoiceState},- test_permission_sync_fun => TestFun,- data => build_sync_test_data(GuildId, RoleId, UserId, ChannelId, Permissions)- }.--build_sync_test_data(GuildId, RoleId, UserId, ChannelId, Permissions) ->- RoleIdBin = integer_to_binary(RoleId),- PermsBin = integer_to_binary(Permissions),- GuildIdBin = integer_to_binary(GuildId),- UserIdBin = integer_to_binary(UserId),- ChIdBin = integer_to_binary(ChannelId),- #{- <<"guild">> => #{<<"owner_id">> => <<"1">>},- <<"roles">> => [- #{<<"id">> => RoleIdBin, <<"permissions">> => PermsBin},- #{<<"id">> => GuildIdBin, <<"permissions">> => <<"0">>}- ],- <<"members">> => [- #{<<"user">> => #{<<"id">> => UserIdBin}, <<"roles">> => [RoleIdBin]}- ],- <<"channels">> => [- #{<<"id">> => ChIdBin, <<"permission_overwrites">> => []}- ]- }.--sync_user_voice_permissions_syncs_connected_user_test() ->- ok = drain_mailbox(),- TestFun = make_sync_test_fun(),- State = build_sync_test_state(TestFun),- ok = sync_user_voice_permissions(10, State),- receive- {synced, 42, 500, 10, <<"test-conn">>, Perms} ->- ?assertEqual(true, maps:get(can_speak, Perms)),- ?assertEqual(true, maps:get(can_stream, Perms)),- ?assertEqual(true, maps:get(deaf, Perms))- after 100 ->- ?assert(false)- end.--sync_roles_after_change_uses_role_index_test() ->- TestFun = make_sync_test_fun(),- GuildId = 42,- RoleId = 999,- OtherRoleId = 1000,- UserId = 10,- OtherUserId = 20,- ChannelId = 500,- Permissions =- constants:view_channel_permission() bor- constants:connect_permission() bor- constants:speak_permission(),- State = #{- id => GuildId,- voice_states => #{- <<"conn1">> => #{- <<"user_id">> => integer_to_binary(UserId),- <<"channel_id">> => integer_to_binary(ChannelId),- <<"connection_id">> => <<"conn1">>- },- <<"conn2">> => #{- <<"user_id">> => integer_to_binary(OtherUserId),- <<"channel_id">> => integer_to_binary(ChannelId),- <<"connection_id">> => <<"conn2">>- }- },- test_permission_sync_fun => TestFun,- data => role_sync_test_data(- GuildId, RoleId, OtherRoleId, UserId, OtherUserId, ChannelId, Permissions- )- },- ok = sync_roles_after_change([RoleId], State, State),- receive- {synced, GuildId, ChannelId, UserId, <<"conn1">>, _Perms} -> ok- after 200 ->- ?assert(false)- end,- receive- {synced, GuildId, ChannelId, OtherUserId, <<"conn2">>, _OtherPerms} ->- ?assert(false)- after 50 ->- ok- end.--sync_disconnects_when_connect_permission_lost_test() ->- ok = drain_mailbox(),- TestFun = make_sync_test_fun(),- UserId = 10,- ChannelId = 500,- GuildId = 42,- RoleId = 999,- ViewOnly = constants:view_channel_permission(),- State = build_sync_test_state_with_perms(- TestFun, GuildId, RoleId, UserId, ChannelId, ViewOnly- ),- ok = sync_user_voice_permissions(UserId, State),- receive- {synced, GuildId, ChannelId, UserId, <<"test-conn">>, Perms} ->- ?assertEqual(true, maps:get(disconnected, Perms, false))- after 200 ->- ?assert(false)- end.--sync_clears_self_stream_when_stream_permission_lost_test() ->- ok = drain_mailbox(),- TestFun = make_sync_test_fun(),- UserId = 10,- ChannelId = 500,- GuildId = 42,- RoleId = 999,- WithoutStream =- constants:view_channel_permission() bor- constants:connect_permission() bor- constants:speak_permission(),- State = build_streaming_sync_test_state(- TestFun, GuildId, RoleId, UserId, ChannelId, WithoutStream- ),- ok = sync_user_voice_permissions(UserId, State),- Broadcast = receive_voice_state_dispatch(),- ?assertEqual(false, maps:get(<<"self_stream">>, Broadcast)),- ?assertEqual(false, maps:get(<<"self_video">>, Broadcast)),- ?assertEqual(<<"test-conn">>, maps:get(<<"connection_id">>, Broadcast)).--sync_keeps_self_stream_when_stream_permission_held_test() ->- ok = drain_mailbox(),- TestFun = make_sync_test_fun(),- UserId = 10,- ChannelId = 500,- GuildId = 42,- RoleId = 999,- WithStream =- constants:view_channel_permission() bor- constants:connect_permission() bor- constants:speak_permission() bor- constants:stream_permission(),- State = build_streaming_sync_test_state(- TestFun, GuildId, RoleId, UserId, ChannelId, WithStream- ),- ok = sync_user_voice_permissions(UserId, State),- ?assertEqual(undefined, receive_optional_voice_state_dispatch()).--build_streaming_sync_test_state(TestFun, GuildId, RoleId, UserId, ChannelId, Permissions) ->- VoiceState = #{- <<"user_id">> => integer_to_binary(UserId),- <<"channel_id">> => integer_to_binary(ChannelId),- <<"connection_id">> => <<"test-conn">>,- <<"self_stream">> => true,- <<"self_video">> => true- },- #{- id => GuildId,- voice_states => #{<<"conn">> => VoiceState},- sessions => #{- <<"s1">> => #{- pid => self(),- user_id => UserId,- viewable_channels => #{ChannelId => true}- }- },- test_permission_sync_fun => TestFun,- data => build_sync_test_data(GuildId, RoleId, UserId, ChannelId, Permissions)- }.--receive_voice_state_dispatch() ->- case receive_optional_voice_state_dispatch() of- undefined -> error(voice_state_update_not_received);- Payload -> Payload- end.--receive_optional_voice_state_dispatch() ->- receive- {'$gen_cast', {dispatch, voice_state_update, {pre_encoded, Bin}}} -> json:decode(Bin);- {'$gen_cast', {dispatch, voice_state_update, Payload}} -> Payload- after 200 ->- undefined- end.--make_sync_test_fun() ->- Self = self(),- fun(GId, ChId, UId, ConnId, Perms) ->- Self ! {synced, GId, ChId, UId, ConnId, Perms}- end.--build_sync_test_state_with_perms(TestFun, GuildId, RoleId, UserId, ChannelId, Permissions) ->- VoiceState = #{- <<"user_id">> => integer_to_binary(UserId),- <<"channel_id">> => integer_to_binary(ChannelId),- <<"connection_id">> => <<"test-conn">>,- <<"deaf">> => false- },- #{- id => GuildId,- voice_states => #{<<"conn">> => VoiceState},- test_permission_sync_fun => TestFun,- data => build_sync_test_data(GuildId, RoleId, UserId, ChannelId, Permissions)- }.--role_sync_test_data(- GuildId, RoleId, OtherRoleId, UserId, OtherUserId, ChannelId, Permissions-) ->- guild_data_index:normalize_data(#{- <<"guild">> => #{<<"owner_id">> => <<"1">>},- <<"roles">> => [- #{- <<"id">> => integer_to_binary(RoleId),- <<"permissions">> => integer_to_binary(Permissions)- },- #{- <<"id">> => integer_to_binary(OtherRoleId),- <<"permissions">> => integer_to_binary(Permissions)- },- #{<<"id">> => integer_to_binary(GuildId), <<"permissions">> => <<"0">>}- ],- <<"members">> => [- #{- <<"user">> => #{<<"id">> => integer_to_binary(UserId)},- <<"roles">> => [integer_to_binary(RoleId)]- },- #{- <<"user">> => #{<<"id">> => integer_to_binary(OtherUserId)},- <<"roles">> => [integer_to_binary(OtherRoleId)]- }- ],- <<"channels">> => [- #{<<"id">> => integer_to_binary(ChannelId), <<"permission_overwrites">> => []}- ]- }).--drain_mailbox() ->- receive- _ -> drain_mailbox()- after 0 ->- ok- end.---endif.diff --git a/fluxer_gateway/src/guild/voice/guild_voice_state.erl b/fluxer_gateway/src/guild/voice/guild_voice_state.erlindex d9ba876f2..eee9e7133 100644--- a/fluxer_gateway/src/guild/voice/guild_voice_state.erl+++ b/fluxer_gateway/src/guild/voice/guild_voice_state.erl@@ -11,7 +11,6 @@-export([user_matches_voice_state/2]).-export([create_voice_state/2]).-export([extract_session_info_from_voice_state/2]).--export([has_voice_state_change/2]).-export_type([guild_state/0,diff --git a/fluxer_gateway/src/guild/voice/voice_disconnect_common.erl b/fluxer_gateway/src/guild/voice/voice_disconnect_common.erlindex 6892f8316..c8f27168e 100644--- a/fluxer_gateway/src/guild/voice/voice_disconnect_common.erl+++ b/fluxer_gateway/src/guild/voice/voice_disconnect_common.erl@@ -84,21 +84,34 @@ disconnect_user_if_in_channel(undefined ->{not_found, VoiceStates, Sessions};VoiceState ->- case connection_matches(VoiceState, ExpectedConnectionId) of- false ->- {not_found, VoiceStates, Sessions};- true ->- disconnect_matching_channel(- UserId,- ExpectedChannelId,- VoiceState,- VoiceStates,- Sessions,- CleanupFun- )- end+ disconnect_matching_connection(+ connection_matches(VoiceState, ExpectedConnectionId),+ UserId,+ ExpectedChannelId,+ VoiceState,+ VoiceStates,+ Sessions,+ CleanupFun+ )end.+-spec disconnect_matching_connection(+ boolean(), user_id(), integer(), map(), voice_states_map(), sessions_map(), cleanup_fun()+) ->+ {ok, voice_states_map(), sessions_map()}+ | {not_found, voice_states_map(), sessions_map()}+ | {channel_mismatch, voice_states_map(), sessions_map()}.+disconnect_matching_connection(+ false, _UserId, _ChannelId, _VoiceState, VoiceStates, Sessions, _+) ->+ {not_found, VoiceStates, Sessions};+disconnect_matching_connection(+ true, UserId, ExpectedChannelId, VoiceState, VoiceStates, Sessions, CleanupFun+) ->+ disconnect_matching_channel(+ UserId, ExpectedChannelId, VoiceState, VoiceStates, Sessions, CleanupFun+ ).+-spec connection_matches(map(), binary() | undefined) -> boolean().connection_matches(_VoiceState, undefined) ->true;diff --git a/fluxer_gateway/src/guild/voice/voice_p2p.erl b/fluxer_gateway/src/guild/voice/voice_p2p.erlindex 00bd53c88..ffd5daff9 100644--- a/fluxer_gateway/src/guild/voice/voice_p2p.erl+++ b/fluxer_gateway/src/guild/voice/voice_p2p.erl@@ -97,9 +97,9 @@ guild_join_decision(Agreed, ChannelId, State) ->-spec admit(join_decision(), token_grant() | term()) -> grant() | sfu | rejection().admit({p2p, _Role, _Count}, {p2p, _ConnectionId, _IceServers, _MaxParticipants} = Grant) ->- Grant;+ eqwalizer:dynamic_cast(Grant);admit({p2p, _Role, _Count}, {declined, ErrorAtom}) ->- {reject, ErrorAtom};+ {reject, eqwalizer:dynamic_cast(ErrorAtom)};admit({p2p, initiator, _Count}, _Token) ->sfu;admit({p2p, joiner, _Count}, _Token) ->@@ -121,14 +121,18 @@ invariant_violation(VoiceStates, MaxParticipants) ->mixed ->{error, voice_p2p_consent_required};p2p ->- case length(VoiceStates) > participant_cap(MaxParticipants) of- true -> {error, voice_channel_full};- false -> ok- end;+ p2p_capacity(VoiceStates, MaxParticipants);_ ->okend.+-spec p2p_capacity([voice_state()], term()) -> ok | {error, atom()}.+p2p_capacity(VoiceStates, MaxParticipants) ->+ case length(VoiceStates) > participant_cap(MaxParticipants) of+ true -> {error, voice_channel_full};+ false -> ok+ end.+-spec participant_cap(term()) -> pos_integer().participant_cap(MaxParticipants) whenis_integer(MaxParticipants), MaxParticipants >= 1, MaxParticipants =< ?MAX_PARTICIPANTSdiff --git a/fluxer_gateway/src/guild/voice/voice_utils.erl b/fluxer_gateway/src/guild/voice/voice_utils.erlindex 832efccb9..48d24a457 100644--- a/fluxer_gateway/src/guild/voice/voice_utils.erl+++ b/fluxer_gateway/src/guild/voice/voice_utils.erl@@ -40,12 +40,12 @@ apply_voice_permissions_to_flags(Flags, VoicePermissions) ->SelfStream =case maps:get(can_stream, VoicePermissions, true) offalse -> false;- _ -> maps:get(self_stream, Flags, false) =:= true+ _ -> type_conv:is_true(maps:get(self_stream, Flags, false))end,SelfVideo =case maps:get(can_video, VoicePermissions, true) offalse -> false;- _ -> maps:get(self_video, Flags, false) =:= true+ _ -> type_conv:is_true(maps:get(self_video, Flags, false))end,Flags#{suppress => Suppress,