A free and open source instant messaging and VoIP chat app built for friends, groups, and communities.
feat(gateway): mature push rules and structured push fields (#3840)
Hampusba168ba7a71b
diff --git a/fluxer_gateway/src/gateway/gateway_rpc_presence.erl b/fluxer_gateway/src/gateway/gateway_rpc_presence.erlindex ae40933f6..01881bb45 100644--- a/fluxer_gateway/src/gateway/gateway_rpc_presence.erl+++ b/fluxer_gateway/src/gateway/gateway_rpc_presence.erl@@ -26,6 +26,7 @@ execute_method(Method, _P) -> gateway_rpc_error:raise(<<"Unknown method: ", Methhandle_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) ofok -> true;{error, not_found} -> handle_offline_dispatch(EventAtom, UserId, Data);@@ -33,6 +34,14 @@ handle_dispatch(#{<<"user_id">> := UserIdBin, <<"event">> := Event, <<"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),@@ -327,12 +336,6 @@ handle_offline_dispatch(message_create, UserId, Data) when is_map(Data) ->})end,true;-handle_offline_dispatch(relationship_add, UserId, _Data) ->- sync_blocked_ids_for_user(UserId),- true;-handle_offline_dispatch(relationship_remove, UserId, _Data) ->- sync_blocked_ids_for_user(UserId),- true;handle_offline_dispatch(_Event, _UserId, _Data) ->true.@@ -351,7 +354,3 @@ validate_offline_message_author_id(AuthorId) ->{ok, ParsedAuthorId} -> {ok, ParsedAuthorId};_ -> invalidend.---spec sync_blocked_ids_for_user(integer()) -> ok.-sync_blocked_ids_for_user(_UserId) ->- ok.diff --git a/fluxer_gateway/src/guild/guild_push_message.erl b/fluxer_gateway/src/guild/guild_push_message.erlindex 58120e299..ab2b85363 100644--- a/fluxer_gateway/src/guild/guild_push_message.erl+++ b/fluxer_gateway/src/guild/guild_push_message.erl@@ -31,9 +31,10 @@ send(ok;AuthorId ->ChannelIdBin = maps:get(<<"channel_id">>, MessageData),- {PushMessageData, ChannelName} = push_message_data(+ {PushMessageData0, ChannelName} = push_message_data(MessageData, ChannelIdBin, EligibleUserIds ++ element(1, Held), Data),+ PushMessageData = mark_mature(PushMessageData0, ChannelIdBin, Data),RoleNames = build_role_names_map(Data),Params = push_params(Request#{message_data := PushMessageData,@@ -45,6 +46,25 @@ send(guild_push_engagement:hold(Held, Params)end.+-spec mark_mature(event_data(), term(), map()) -> event_data().+mark_mature(PushMessageData, ChannelIdBin, Data) ->+ case guild_dispatch_decorate:parse_snowflake(<<"channel_id">>, ChannelIdBin) of+ undefined ->+ PushMessageData;+ ChannelId ->+ ThreadParentId = maps:get(+ <<"parent_id">>, maps:get(<<"__thread_push">>, PushMessageData, #{}), undefined+ ),+ case+ guild_visitor_permissions:channel_effective_nsfw(+ Data, ChannelId, ThreadParentId+ )+ of+ true -> PushMessageData#{<<"__push_mature">> => true};+ false -> PushMessageData+ end+ end.+-spec push_message_data(event_data(), term(), [user_id()], map()) -> {event_data(), binary()}.push_message_data(MessageData, ChannelIdBin, UserIds, Data) ->ChannelId = guild_dispatch_decorate:parse_snowflake(<<"channel_id">>, ChannelIdBin),diff --git a/fluxer_gateway/src/guild/guild_visitor_permissions.erl b/fluxer_gateway/src/guild/guild_visitor_permissions.erlindex be4cb1ac2..d5f9e6150 100644--- a/fluxer_gateway/src/guild/guild_visitor_permissions.erl+++ b/fluxer_gateway/src/guild/guild_visitor_permissions.erl@@ -6,7 +6,8 @@-export([viewable_channel_map/2,strip_overwrites/2,- guild_nsfw/1+ guild_nsfw/1,+ channel_effective_nsfw/3]).-define(AGE_RESTRICTED_LEVEL, 3).@@ -71,6 +72,25 @@ guild_nsfw(Data) ->_ -> maps:get(<<"nsfw_level">>, Guild, 0) =:= ?AGE_RESTRICTED_LEVELend.+-spec channel_effective_nsfw(map(), integer(), integer() | undefined) -> boolean().+channel_effective_nsfw(Data, ChannelId, ThreadParentId) ->+ Index = guild_data_index_channels:channel_index(Data),+ GuildNsfw = guild_nsfw(Data),+ case maps:get(resolved_channel_id(ChannelId, ThreadParentId, Index), Index, undefined) of+ Channel when is_map(Channel) ->+ effective_nsfw(Channel, parent_channel(Channel, Index), GuildNsfw);+ _ ->+ GuildNsfw+ end.++-spec resolved_channel_id(integer(), integer() | undefined, map()) -> integer().+resolved_channel_id(ChannelId, _ThreadParentId, Index) when is_map_key(ChannelId, Index) ->+ ChannelId;+resolved_channel_id(_ChannelId, ThreadParentId, _Index) when is_integer(ThreadParentId) ->+ ThreadParentId;+resolved_channel_id(ChannelId, _ThreadParentId, _Index) ->+ ChannelId.+-spec everyone_permissions(map(), integer()) -> non_neg_integer().everyone_permissions(Data, GuildId) ->lists:foldl(diff --git a/fluxer_gateway/src/push/push.erl b/fluxer_gateway/src/push/push.erlindex b7c79d0c3..d49b23c37 100644--- a/fluxer_gateway/src/push/push.erl+++ b/fluxer_gateway/src/push/push.erl@@ -11,7 +11,8 @@handle_buffered_message_creates/1,sync_user_guild_settings_local/3,clear_channel_notifications/3,- clear_notifications_enabled/0+ clear_notifications_enabled/0,+ invalidate_blocked_ids/1]).-export([get_cache_stats/0]).@@ -137,6 +138,10 @@ clear_notifications_enabled() ->_ -> env_boolean(push_enrolled_clear_notifications_enabled, true)end.+-spec invalidate_blocked_ids(integer()) -> ok.+invalidate_blocked_ids(UserId) ->+ erpc:multicast([node() | nodes()], push_ets_cache, delete_blocked_ids, [UserId]).+-spec get_cache_stats() -> {ok, map()}.get_cache_stats() ->gen_server:call(?MODULE, get_cache_stats, 5000).diff --git a/fluxer_gateway/src/push/push_ets_cache.erl b/fluxer_gateway/src/push/push_ets_cache.erlindex d777311ec..00cb03468 100644--- a/fluxer_gateway/src/push/push_ets_cache.erl+++ b/fluxer_gateway/src/push/push_ets_cache.erl@@ -11,6 +11,7 @@get_blocked_ids/1,put_blocked_ids_fetched/3,reserve_blocked_ids/1,+ delete_blocked_ids/1,release/1,rebalance/0,cache_stats/0,@@ -94,6 +95,14 @@ put_blocked_ids_fetched(UserId, BlockedIds, {?BLOCKED_IDS, _, _} = Fill) ->reserve_blocked_ids(UserIds) ->reserve(?BLOCKED_IDS, UserIds).+-spec delete_blocked_ids(integer()) -> ok.+delete_blocked_ids(UserId) ->+ try ets:delete(?BLOCKED_IDS, UserId) of+ true -> ok+ catch+ error:badarg -> ok+ end.+-spec blocked_ids_ttl_seconds() -> pos_integer().blocked_ids_ttl_seconds() ->Value = push_utils:app_pos_integer(diff --git a/fluxer_gateway/src/push/push_job_fields.erl b/fluxer_gateway/src/push/push_job_fields.erlindex f30126167..188e48b01 100644--- a/fluxer_gateway/src/push/push_job_fields.erl+++ b/fluxer_gateway/src/push/push_job_fields.erl@@ -3,9 +3,12 @@-module(push_job_fields).-typing([eqwalizer]).--export([notification/7, thread/2, caller/1]).+-export([notification/7, thread/2, caller/1, is_mature/1]).-define(MAX_CALLER_NAME_BYTES, 128).+-define(MATURE_TITLE, <<"Fluxer">>).+-define(MATURE_CHANNEL_BODY, <<"Message in an 18+ channel">>).+-define(MATURE_GROUP_DM_BODY, <<"Message in an 18+ group DM">>).-spec caller(map()) -> map().caller(#{caller_id := CallerId, caller_name := Name} = Caller) when@@ -34,20 +37,60 @@ caller_avatar_url(CallerIdBin, _Hash) -><<"id">> => CallerIdBin, <<"avatar">> => null}).+-spec is_mature(map()) -> boolean().+is_mature(#{<<"__push_mature">> := true}) -> true;+is_mature(#{<<"channel_nsfw">> := true}) -> true;+is_mature(_MessageData) -> false.+-spec notification(map(), map(), integer(), integer(), integer(), binary() | undefined, binary() | undefined) -> map().notification(MessageData, MarkdownContext, GuildId, ChannelId, MessageId, GuildName, ChannelName) ->+ ChannelIdBin = integer_to_binary(ChannelId),+ MessageIdBin = integer_to_binary(MessageId),+ Common = #{+ <<"tag">> => <<"channel:", ChannelIdBin/binary, ":", MessageIdBin/binary>>,+ <<"notification_tag">> => <<"channel:", ChannelIdBin/binary>>,+ <<"url">> => push_notification_format:build_url(GuildId, ChannelId, MessageId)+ },+ Fields =+ case is_mature(MessageData) of+ true ->+ mature_fields(GuildId);+ false ->+ message_fields(+ MessageData, MarkdownContext, GuildId, GuildName, ChannelName+ )+ end,+ with_badge(maps:merge(Common, Fields)).++-spec mature_fields(integer()) -> map().+mature_fields(GuildId) ->+ {Kind, Body} =+ case GuildId of+ 0 -> {<<"group_dm">>, ?MATURE_GROUP_DM_BODY};+ _ -> {<<"guild">>, ?MATURE_CHANNEL_BODY}+ end,+ #{+ <<"title">> => ?MATURE_TITLE,+ <<"body">> => Body,+ <<"icon">> => push_notification_format:resolve_author_avatar_url(#{}),+ <<"image_url">> => null,+ <<"conversation_kind">> => Kind,+ <<"preview_kind">> => <<"mature">>+ }.++-spec message_fields(map(), map(), integer(), binary() | undefined, binary() | undefined) ->+ map().+message_fields(MessageData, MarkdownContext, GuildId, GuildName, ChannelName) ->AuthorData = maps:get(<<"author">>, MessageData, #{}),AuthorUsername = maps:get(<<"username">>, AuthorData, <<"Unknown">>),AuthorName = push_notification_format:resolve_author_name(MessageData, MarkdownContext, AuthorUsername),- ChannelIdBin = integer_to_binary(ChannelId),- MessageIdBin = integer_to_binary(MessageId),- with_badge(#{+ Fields = #{<<"title">> => push_notification:build_notification_title(AuthorName, MessageData, GuildId, GuildName, ChannelName),@@ -55,12 +98,44 @@ notification(MessageData, MarkdownContext),<<"icon">> => push_notification_format:resolve_author_avatar_url(AuthorData),- <<"tag">> => <<"channel:", ChannelIdBin/binary, ":", MessageIdBin/binary>>,- <<"notification_tag">> => <<"channel:", ChannelIdBin/binary>>,- <<"url">> => push_notification_format:build_url(GuildId, ChannelId, MessageId),<<"image_url">> => nullable(push_image_url:extract(MessageData))+ },+ maps:merge(+ Fields,+ maps:merge(+ identity(AuthorName, MessageData, GuildId, GuildName, ChannelName),+ push_notification_format:preview_fields(MessageData, MarkdownContext)+ )+ ).++-spec identity(binary(), map(), integer(), binary() | undefined, binary() | undefined) -> map().+identity(AuthorName, MessageData, 0, _GuildName, ChannelName) ->+ Kind =+ case maps:get(<<"channel_type">>, MessageData, 1) of+ 3 -> <<"group_dm">>;+ _ -> <<"dm">>+ end,+ binaries(#{+ <<"author_name">> => AuthorName,+ <<"conversation_kind">> => Kind,+ <<"conversation_name">> => group_name(Kind, ChannelName)+ });+identity(AuthorName, MessageData, _GuildId, GuildName, ChannelName) ->+ binaries(#{+ <<"author_name">> => AuthorName,+ <<"conversation_kind">> => <<"guild">>,+ <<"guild_name">> => GuildName,+ <<"conversation_name">> => push_notification:channel_label(MessageData, ChannelName)}).+-spec group_name(binary(), term()) -> binary() | undefined.+group_name(<<"group_dm">>, Name) when is_binary(Name), byte_size(Name) > 0 -> Name;+group_name(_Kind, _Name) -> undefined.++-spec binaries(map()) -> map().+binaries(Map) ->+ maps:filter(fun(_Key, Value) -> is_binary(Value) andalso byte_size(Value) > 0 end, Map).+-spec with_badge(map()) -> map().with_badge(Fields) ->case fluxer_gateway_env:get(self_hosted) of@@ -78,7 +153,17 @@ with_badge(Fields) ->thread(MessageData, ChannelName) ->case push_notification:thread_fields(MessageData, ChannelName) ofFields when map_size(Fields) =:= 0 -> Fields;- Fields -> Fields#{<<"kind">> => thread_job_kind(MessageData)}+ Fields ->+ without_mature_names(MessageData, Fields#{+ <<"kind">> => thread_job_kind(MessageData)+ })+ end.++-spec without_mature_names(map(), map()) -> map().+without_mature_names(MessageData, Fields) ->+ case is_mature(MessageData) of+ true -> maps:without([<<"channel_name">>, <<"parent_name">>], Fields);+ false -> Fieldsend.-spec thread_job_kind(map()) -> binary().diff --git a/fluxer_gateway/src/push/push_notification.erl b/fluxer_gateway/src/push/push_notification.erlindex 7ae153190..cf82a9ed0 100644--- a/fluxer_gateway/src/push/push_notification.erl+++ b/fluxer_gateway/src/push/push_notification.erl@@ -3,7 +3,7 @@-module(push_notification).-typing([eqwalizer]).--export([build_notification_title/5, thread_fields/2]).+-export([build_notification_title/5, channel_label/2, thread_fields/2]).-define(THREAD_PUSH_KEY, <<"__thread_push">>).diff --git a/fluxer_gateway/src/push/push_notification_format.erl b/fluxer_gateway/src/push/push_notification_format.erlindex 4e8cbfadf..574cc58d6 100644--- a/fluxer_gateway/src/push/push_notification_format.erl+++ b/fluxer_gateway/src/push/push_notification_format.erl@@ -5,6 +5,7 @@-export([build_content_preview/2,+ preview_fields/2,build_markdown_context/4,resolve_author_name/3,resolve_author_avatar_url/1,@@ -31,6 +32,47 @@ build_content_preview(MessageData, MarkdownContext) ->truncate_preview(Preview)end.+-spec preview_fields(map(), map()) -> map().+preview_fields(MessageData, MarkdownContext) ->+ Content = push_utils:normalize_binary(maps:get(<<"content">>, MessageData, <<>>), <<>>),+ case push_markdown_plaintext:render_push_preview(Content, MarkdownContext) of+ <<>> -> fallback_preview_fields(MessageData);+ _ -> #{}+ end.++-spec fallback_preview_fields(map()) -> map().+fallback_preview_fields(MessageData) ->+ case+ {+ maps:get(<<"stickers">>, MessageData, []),+ maps:get(<<"attachments">>, MessageData, [])+ }+ of+ {[Sticker], _} when is_map(Sticker) ->+ with_arg(+ <<"sticker">>, push_utils:normalize_binary(maps:get(<<"name">>, Sticker, <<>>))+ );+ {[_, _ | _] = Stickers, _} ->+ with_arg(<<"stickers">>, name_lines(sticker_names(Stickers)));+ {_, [Attachment | _]} when is_map(Attachment) ->+ with_arg(+ <<"attachment">>,+ push_utils:normalize_binary(maps:get(<<"filename">>, Attachment, undefined))+ );+ _ ->+ #{}+ end.++-spec with_arg(binary(), term()) -> map().+with_arg(Kind, Arg) when is_binary(Arg), byte_size(Arg) > 0 ->+ #{<<"preview_kind">> => Kind, <<"preview_arg">> => truncate_preview(Arg)};+with_arg(Kind, _Arg) ->+ #{<<"preview_kind">> => Kind}.++-spec name_lines([binary()]) -> binary().+name_lines([]) -> <<>>;+name_lines([_ | _] = Names) -> join_binary(Names, <<"\n">>).+-spec build_markdown_context(map(), non_neg_integer(), map(), map()) -> map().build_markdown_context(MessageData, GuildId, RoleNames, GuildData) ->UserNicknames = user_nicknames(MessageData, GuildId, GuildData),diff --git a/fluxer_gateway/src/push/push_recipients.erl b/fluxer_gateway/src/push/push_recipients.erlindex 9711aafe5..e3e20374a 100644--- a/fluxer_gateway/src/push/push_recipients.erl+++ b/fluxer_gateway/src/push/push_recipients.erl@@ -57,7 +57,34 @@ filter_eligible(end,Candidates),- drop_blocked_recipients(EligibleUsers, AuthorId, fetch_missing_blocked_ids(EligibleUsers)).+ Unblocked = drop_blocked_recipients(+ EligibleUsers, AuthorId, fetch_missing_blocked_ids(EligibleUsers)+ ),+ case push_job_fields:is_mature(MessageData) of+ true -> adult_recipients(Unblocked);+ false -> Unblocked+ end.++-spec adult_recipients([integer()]) -> [integer()].+adult_recipients([]) ->+ [];+adult_recipients(UserIds) ->+ Chunks = chunk_user_ids(UserIds, blocked_ids_fetch_chunk(), []),+ lists:append([adult_recipients_chunk(Chunk) || Chunk <- Chunks]).++-spec adult_recipients_chunk([integer()]) -> [integer()].+adult_recipients_chunk(UserIds) ->+ Request = #{+ <<"type">> => <<"get_users_adult_content_allowed">>,+ <<"user_ids">> => [integer_to_binary(UserId) || UserId <- UserIds]+ },+ case rpc_client:call(Request) of+ {ok, #{<<"user_ids">> := Allowed}} when is_list(Allowed) ->+ AllowedIds = lists:filtermap(fun snowflake_id:filter/1, Allowed),+ [UserId || UserId <- UserIds, lists:member(UserId, AllowedIds)];+ _ ->+ []+ end.-spec resolve_large_guild_metadata(integer(), map() | undefined) -> map() | undefined.resolve_large_guild_metadata(0, _SuppliedMetadata) ->