From ec0e613ecaa8dc411e0c821c4c9b2ca893b45f67 Mon Sep 17 00:00:00 2001 From: href Date: Mon, 10 Dec 2018 15:50:10 +0100 Subject: [PATCH] Pleroma.Activity.mastodon_notification_type/1 --- lib/pleroma/activity.ex | 15 +++++ .../mastodon_api/mastodon_api_controller.ex | 65 +++++++------------ lib/pleroma/web/push/push.ex | 12 +--- 3 files changed, 41 insertions(+), 51 deletions(-) diff --git a/lib/pleroma/activity.ex b/lib/pleroma/activity.ex index c065f3b6c..200addd6e 100644 --- a/lib/pleroma/activity.ex +++ b/lib/pleroma/activity.ex @@ -3,6 +3,14 @@ defmodule Pleroma.Activity do alias Pleroma.{Repo, Activity, Notification} import Ecto.Query + # https://github.com/tootsuite/mastodon/blob/master/app/models/notification.rb#L19 + @mastodon_notification_types %{ + "Create" => "mention", + "Follow" => "follow", + "Announce" => "reblog", + "Like" => "favourite" + } + schema "activities" do field(:data, :map) field(:local, :boolean, default: true) @@ -88,4 +96,11 @@ def get_in_reply_to_activity(%Activity{data: %{"object" => %{"inReplyTo" => ap_i end def get_in_reply_to_activity(_), do: nil + + for {ap_type, type} <- @mastodon_notification_types do + def mastodon_notification_type(%Activity{data: %{"type" => unquote(ap_type)}}), + do: unquote(type) + end + + def mastodon_notification_type(%Activity{}), do: nil end diff --git a/lib/pleroma/web/mastodon_api/mastodon_api_controller.ex b/lib/pleroma/web/mastodon_api/mastodon_api_controller.ex index e105a1e15..4db7cd60f 100644 --- a/lib/pleroma/web/mastodon_api/mastodon_api_controller.ex +++ b/lib/pleroma/web/mastodon_api/mastodon_api_controller.ex @@ -1055,52 +1055,37 @@ def empty_object(conn, _) do def render_notification(user, %{id: id, activity: activity, inserted_at: created_at} = _params) do actor = User.get_cached_by_ap_id(activity.data["actor"]) + parent_activity = Activity.get_create_activity_by_object_ap_id(activity.data["object"]) + mastodon_type = Activity.mastodon_notification_type(activity) - created_at = - NaiveDateTime.to_iso8601(created_at) - |> String.replace(~r/(\.\d+)?$/, ".000Z", global: false) + response = %{ + id: to_string(id), + type: mastodon_type, + created_at: CommonAPI.Utils.to_masto_date(activity.inserted_at), + account: AccountView.render("account.json", %{user: actor, for: user}) + } - id = id |> to_string - - case activity.data["type"] do - "Create" -> - %{ - id: id, - type: "mention", - created_at: created_at, - account: AccountView.render("account.json", %{user: actor, for: user}), + case mastodon_type do + "mention" -> + response + |> Map.merge(%{ status: StatusView.render("status.json", %{activity: activity, for: user}) - } + }) - "Like" -> - liked_activity = Activity.get_create_activity_by_object_ap_id(activity.data["object"]) + "favourite" -> + response + |> Map.merge(%{ + status: StatusView.render("status.json", %{activity: parent_activity, for: user}) + }) - %{ - id: id, - type: "favourite", - created_at: created_at, - account: AccountView.render("account.json", %{user: actor, for: user}), - status: StatusView.render("status.json", %{activity: liked_activity, for: user}) - } + "reblog" -> + response + |> Map.merge(%{ + status: StatusView.render("status.json", %{activity: parent_activity, for: user}) + }) - "Announce" -> - announced_activity = Activity.get_create_activity_by_object_ap_id(activity.data["object"]) - - %{ - id: id, - type: "reblog", - created_at: created_at, - account: AccountView.render("account.json", %{user: actor, for: user}), - status: StatusView.render("status.json", %{activity: announced_activity, for: user}) - } - - "Follow" -> - %{ - id: id, - type: "follow", - created_at: created_at, - account: AccountView.render("account.json", %{user: actor, for: user}) - } + "follow" -> + response _ -> nil diff --git a/lib/pleroma/web/push/push.ex b/lib/pleroma/web/push/push.ex index 35e14c243..8fa28ee8e 100644 --- a/lib/pleroma/web/push/push.ex +++ b/lib/pleroma/web/push/push.ex @@ -54,7 +54,7 @@ def handle_cast( when type in @types do actor = User.get_cached_by_ap_id(notification.activity.data["actor"]) - type = format_type(notification) + type = Pleroma.Activity.mastodon_notification_type(notification.activity) Subscription |> where(user_id: ^user_id) @@ -114,16 +114,6 @@ def handle_cast({:send, _}, state) do {:noreply, state} end - # https://github.com/tootsuite/mastodon/blob/master/app/models/notification.rb#L19 - defp format_type(%{activity: %{data: %{"type" => type}}}) do - case type do - "Create" -> "mention" - "Follow" -> "follow" - "Announce" -> "reblog" - "Like" -> "favourite" - end - end - defp format_title(%{activity: %{data: %{"type" => type}}}) do case type do "Create" -> "New Mention"