Cleanup to make the code easier to follow

This commit is contained in:
Mark Felder 2024-06-08 19:48:41 -04:00
parent 86fa0889bc
commit b1ef6e5e9a

View File

@ -27,10 +27,10 @@ defmodule Pleroma.Web.Push.Impl do
} = notification } = notification
) )
when activity_type in @types do when activity_type in @types do
actor = User.get_cached_by_ap_id(notification.activity.data["actor"]) user = User.get_cached_by_ap_id(notification.activity.data["actor"])
gcm_api_key = Application.get_env(:web_push_encryption, :gcm_api_key) gcm_api_key = Application.get_env(:web_push_encryption, :gcm_api_key)
avatar_url = User.avatar_url(actor) avatar_url = User.avatar_url(user)
object = Object.normalize(activity, fetch: false) object = Object.normalize(activity, fetch: false)
user = User.get_cached_by_id(user_id) user = User.get_cached_by_id(user_id)
direct_conversation_id = Activity.direct_conversation_id(activity, user) direct_conversation_id = Activity.direct_conversation_id(activity, user)
@ -48,7 +48,7 @@ defmodule Pleroma.Web.Push.Impl do
direct_conversation_id: direct_conversation_id direct_conversation_id: direct_conversation_id
} }
} }
|> Map.merge(build_content(notification, actor, object)) |> Map.merge(build_content(notification, user, object))
|> Jason.encode!() |> Jason.encode!()
|> push_message(build_sub(subscription), gcm_api_key, subscription) |> push_message(build_sub(subscription), gcm_api_key, subscription)
end end
@ -109,71 +109,73 @@ defmodule Pleroma.Web.Push.Impl do
%{ %{
user: %{notification_settings: %{hide_notification_contents: true}} user: %{notification_settings: %{hide_notification_contents: true}}
} = notification, } = notification,
_actor, _user,
_object _object
) do ) do
%{body: format_title(notification)} %{body: format_title(notification)}
end end
def build_content(notification, actor, object) do def build_content(notification, user, object) do
%{ %{
title: format_title(notification), title: format_title(notification),
body: format_body(notification, actor, object) body: format_body(notification, user, object)
} }
end end
def format_body(_activity, actor, %{data: %{"type" => "ChatMessage"} = data}) do @spec format_body(Notification.t(), User.t(), Object.t()) :: String.t()
case data["content"] do def format_body(_notification, user, %{data: %{"type" => "ChatMessage"} = object}) do
nil -> "@#{actor.nickname}: (Attachment)" case object["content"] do
content -> "@#{actor.nickname}: #{Utils.scrub_html_and_truncate(content, 80)}" nil -> "@#{user.nickname}: (Attachment)"
content -> "@#{user.nickname}: #{Utils.scrub_html_and_truncate(content, 80)}"
end end
end end
def format_body( def format_body(
%{activity: %{data: %{"type" => "Create"}}}, %{activity: %{data: %{"type" => "Create"}}},
actor, user,
%{data: %{"content" => content}} %{data: %{"content" => content}}
) do ) do
"@#{actor.nickname}: #{Utils.scrub_html_and_truncate(content, 80)}" "@#{user.nickname}: #{Utils.scrub_html_and_truncate(content, 80)}"
end end
def format_body( def format_body(
%{activity: %{data: %{"type" => "Announce"}}}, %{activity: %{data: %{"type" => "Announce"}}},
actor, user,
%{data: %{"content" => content}} %{data: %{"content" => content}}
) do ) do
"@#{actor.nickname} repeated: #{Utils.scrub_html_and_truncate(content, 80)}" "@#{user.nickname} repeated: #{Utils.scrub_html_and_truncate(content, 80)}"
end end
def format_body( def format_body(
%{activity: %{data: %{"type" => "EmojiReact", "content" => content}}}, %{activity: %{data: %{"type" => "EmojiReact", "content" => content}}},
actor, user,
_object _object
) do ) do
"@#{actor.nickname} reacted with #{content}" "@#{user.nickname} reacted with #{content}"
end end
def format_body( def format_body(
%{activity: %{data: %{"type" => type}}} = notification, %{activity: %{data: %{"type" => type}}} = notification,
actor, user,
_object _object
) )
when type in ["Follow", "Like"] do when type in ["Follow", "Like"] do
case notification.type do case notification.type do
"follow" -> "@#{actor.nickname} has followed you" "follow" -> "@#{user.nickname} has followed you"
"follow_request" -> "@#{actor.nickname} has requested to follow you" "follow_request" -> "@#{user.nickname} has requested to follow you"
"favourite" -> "@#{actor.nickname} has favorited your post" "favourite" -> "@#{user.nickname} has favorited your post"
end end
end end
def format_body( def format_body(
%{activity: %{data: %{"type" => "Update"}}}, %{activity: %{data: %{"type" => "Update"}}},
actor, user,
_object _object
) do ) do
"@#{actor.nickname} edited a status" "@#{user.nickname} edited a status"
end end
@spec format_title(Notification.t()) :: String.t()
def format_title(%{activity: %{data: %{"directMessage" => true}}}) do def format_title(%{activity: %{data: %{"directMessage" => true}}}) do
"New Direct Message" "New Direct Message"
end end