From 80ededc04f4d4ba8318290976025b645a0ba3f95 Mon Sep 17 00:00:00 2001 From: Egor Kislitsyn Date: Tue, 26 Nov 2019 19:53:43 +0700 Subject: [PATCH] Add `direct_conversation_id` to web push payload --- lib/pleroma/activity.ex | 13 +++++++++++++ lib/pleroma/web/mastodon_api/views/status_view.ex | 10 ++-------- lib/pleroma/web/push/impl.ex | 6 +++++- 3 files changed, 20 insertions(+), 9 deletions(-) diff --git a/lib/pleroma/activity.ex b/lib/pleroma/activity.ex index 7e283df32..cd7a5aae9 100644 --- a/lib/pleroma/activity.ex +++ b/lib/pleroma/activity.ex @@ -303,4 +303,17 @@ defmodule Pleroma.Activity do end defdelegate search(user, query, options \\ []), to: Pleroma.Activity.Search + + def direct_conversation_id(activity, for_user) do + alias Pleroma.Conversation.Participation + + with %{data: %{"context" => context}} when is_binary(context) <- activity, + %Pleroma.Conversation{} = conversation <- Pleroma.Conversation.get_for_ap_id(context), + %Participation{id: participation_id} <- + Participation.for_user_and_conversation(for_user, conversation) do + participation_id + else + _ -> nil + end + end end diff --git a/lib/pleroma/web/mastodon_api/views/status_view.ex b/lib/pleroma/web/mastodon_api/views/status_view.ex index baff54151..a0257dfa6 100644 --- a/lib/pleroma/web/mastodon_api/views/status_view.ex +++ b/lib/pleroma/web/mastodon_api/views/status_view.ex @@ -9,8 +9,6 @@ defmodule Pleroma.Web.MastodonAPI.StatusView do alias Pleroma.Activity alias Pleroma.ActivityExpiration - alias Pleroma.Conversation - alias Pleroma.Conversation.Participation alias Pleroma.HTML alias Pleroma.Object alias Pleroma.Repo @@ -245,12 +243,8 @@ defmodule Pleroma.Web.MastodonAPI.StatusView do direct_conversation_id = with {_, nil} <- {:direct_conversation_id, opts[:direct_conversation_id]}, {_, true} <- {:include_id, opts[:with_direct_conversation_id]}, - {_, %User{} = for_user} <- {:for_user, opts[:for]}, - %{data: %{"context" => context}} when is_binary(context) <- activity, - %Conversation{} = conversation <- Conversation.get_for_ap_id(context), - %Participation{id: participation_id} <- - Participation.for_user_and_conversation(for_user, conversation) do - participation_id + {_, %User{} = for_user} <- {:for_user, opts[:for]} do + Activity.direct_conversation_id(activity, for_user) else {:direct_conversation_id, participation_id} when is_integer(participation_id) -> participation_id diff --git a/lib/pleroma/web/push/impl.ex b/lib/pleroma/web/push/impl.ex index dd445e8bf..b01ae0f80 100644 --- a/lib/pleroma/web/push/impl.ex +++ b/lib/pleroma/web/push/impl.ex @@ -33,6 +33,8 @@ defmodule Pleroma.Web.Push.Impl do gcm_api_key = Application.get_env(:web_push_encryption, :gcm_api_key) avatar_url = User.avatar_url(actor) object = Object.normalize(activity) + user = User.get_cached_by_id(user_id) + direct_conversation_id = Activity.direct_conversation_id(activity, user) for subscription <- fetch_subsriptions(user_id), get_in(subscription.data, ["alerts", type]) do @@ -45,7 +47,9 @@ defmodule Pleroma.Web.Push.Impl do icon: avatar_url, preferred_locale: "en", pleroma: %{ - activity_id: activity_id + activity_id: activity_id, + direct_conversation_id: direct_conversation_id, + account: user.ap_id } } |> Jason.encode!()