X-Git-Url: https://git.squeep.com/?a=blobdiff_plain;f=lib%2Fpleroma%2Fweb%2Fpush%2Fpush.ex;h=4af3e159afabbff05e90c1267e195caf551862fe;hb=324933a0ac8db810669375315d43fc9250a93a7b;hp=d27750ab67b18dc5542da322550e5107dbe48b02;hpb=04a48286e69704bf83429b85dbcdb70863bdcff1;p=akkoma diff --git a/lib/pleroma/web/push/push.ex b/lib/pleroma/web/push/push.ex index d27750ab6..4af3e159a 100644 --- a/lib/pleroma/web/push/push.ex +++ b/lib/pleroma/web/push/push.ex @@ -18,11 +18,11 @@ defmodule Pleroma.Web.Push do def init(:ok) do case Application.get_env(:web_push_encryption, :vapid_details) do nil -> - Logger.error( + Logger.warn( "VAPID key pair is not found. Please, add VAPID configuration to config. Run `mix web_push.gen.keypair` mix task to create a key pair" ) - {:error, %{}} + :ignore _ -> {:ok, %{}} @@ -30,7 +30,9 @@ defmodule Pleroma.Web.Push do end def send(notification) do - GenServer.cast(Pleroma.Web.Push, {:send, notification}) + if Application.get_env(:web_push_encryption, :vapid_details) do + GenServer.cast(Pleroma.Web.Push, {:send, notification}) + end end def handle_cast( @@ -39,10 +41,10 @@ defmodule Pleroma.Web.Push do ) when type in @types do actor = User.get_cached_by_ap_id(notification.activity.data["actor"]) - body = notification |> format(actor) |> Jason.encode!() Subscription |> where(user_id: ^user_id) + |> preload(:token) |> Repo.all() |> Enum.each(fn record -> subscription = %{ @@ -53,6 +55,16 @@ defmodule Pleroma.Web.Push do endpoint: record.endpoint } + body = + Jason.encode!(%{ + title: format_title(notification), + body: format_body(notification, actor), + notification_id: notification.id, + icon: User.avatar_url(actor), + preferred_locale: "en", + access_token: record.token.token + }) + case WebPushEncryption.send_web_push(body, subscription, @gcm_api_key) do {:ok, %{status_code: code}} when 400 <= code and code < 500 -> Logger.debug("Removing subscription record") @@ -66,9 +78,8 @@ defmodule Pleroma.Web.Push do Logger.error("Web Push Nonification failed with code: #{code}") :error - data -> + _ -> Logger.error("Web Push Nonification failed with unknown error") - IO.inspect(data) :error end end) @@ -81,46 +92,21 @@ defmodule Pleroma.Web.Push do {:noreply, state} end - def format(%{activity: %{data: %{"type" => "Create"}}}, actor) do - %{ - title: "New Mention", - body: "@#{actor.nickname} has mentiond you", - icon: get_avatar_url(actor) - } - end - - def format(%{activity: %{data: %{"type" => "Follow"}}}, actor) do - %{ - title: "New Follower", - body: "@#{actor.nickname} has followed you", - icon: get_avatar_url(actor) - } - end - - def format(%{activity: %{data: %{"type" => "Announce"}}}, actor) do - %{ - title: "New Announce", - body: "@#{actor.nickname} has announced your post", - icon: get_avatar_url(actor) - } - end - - def format(%{activity: %{data: %{"type" => "Like"}}}, actor) do - %{ - title: "New Like", - body: "@#{actor.nickname} has liked your post", - icon: get_avatar_url(actor) - } - end - - def get_avatar_url(%{avatar: %{"type" => "Image", "url" => urls}}) do - case List.first(urls) do - %{"href" => url} -> url - _ -> get_avatar_url(nil) + defp format_title(%{activity: %{data: %{"type" => type}}}) do + case type do + "Create" -> "New Mention" + "Follow" -> "New Follower" + "Announce" -> "New Repeat" + "Like" -> "New Favorite" end end - def get_avatar_url(_) do - Pleroma.Web.Endpoint.static_url() <> "/images/avi.png" + defp format_body(%{activity: %{data: %{"type" => type}}}, actor) do + case type do + "Create" -> "@#{actor.nickname} has mentiond you" + "Follow" -> "@#{actor.nickname} has followed you" + "Announce" -> "@#{actor.nickname} has repeated your post" + "Like" -> "@#{actor.nickname} has favorited your post" + end end end