Pleroma.Activity.mastodon_notification_type/1

This commit is contained in:
href 2018-12-10 15:50:10 +01:00
parent 331396cbcd
commit ec0e613eca
No known key found for this signature in database
GPG Key ID: EE8296C1A152C325
3 changed files with 41 additions and 51 deletions

View File

@ -3,6 +3,14 @@ defmodule Pleroma.Activity do
alias Pleroma.{Repo, Activity, Notification} alias Pleroma.{Repo, Activity, Notification}
import Ecto.Query 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 schema "activities" do
field(:data, :map) field(:data, :map)
field(:local, :boolean, default: true) field(:local, :boolean, default: true)
@ -88,4 +96,11 @@ defmodule Pleroma.Activity do
end end
def get_in_reply_to_activity(_), do: nil 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 end

View File

@ -1055,52 +1055,37 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIController do
def render_notification(user, %{id: id, activity: activity, inserted_at: created_at} = _params) 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"]) 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 = response = %{
NaiveDateTime.to_iso8601(created_at) id: to_string(id),
|> String.replace(~r/(\.\d+)?$/, ".000Z", global: false) 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 mastodon_type do
"mention" ->
case activity.data["type"] do response
"Create" -> |> Map.merge(%{
%{
id: id,
type: "mention",
created_at: created_at,
account: AccountView.render("account.json", %{user: actor, for: user}),
status: StatusView.render("status.json", %{activity: activity, for: user}) status: StatusView.render("status.json", %{activity: activity, for: user})
} })
"Like" -> "favourite" ->
liked_activity = Activity.get_create_activity_by_object_ap_id(activity.data["object"]) response
|> Map.merge(%{
status: StatusView.render("status.json", %{activity: parent_activity, for: user})
})
%{ "reblog" ->
id: id, response
type: "favourite", |> Map.merge(%{
created_at: created_at, status: StatusView.render("status.json", %{activity: parent_activity, for: user})
account: AccountView.render("account.json", %{user: actor, for: user}), })
status: StatusView.render("status.json", %{activity: liked_activity, for: user})
}
"Announce" -> "follow" ->
announced_activity = Activity.get_create_activity_by_object_ap_id(activity.data["object"]) response
%{
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})
}
_ -> _ ->
nil nil

View File

@ -54,7 +54,7 @@ defmodule Pleroma.Web.Push do
when type in @types do when type in @types do
actor = User.get_cached_by_ap_id(notification.activity.data["actor"]) actor = User.get_cached_by_ap_id(notification.activity.data["actor"])
type = format_type(notification) type = Pleroma.Activity.mastodon_notification_type(notification.activity)
Subscription Subscription
|> where(user_id: ^user_id) |> where(user_id: ^user_id)
@ -114,16 +114,6 @@ defmodule Pleroma.Web.Push do
{:noreply, state} {:noreply, state}
end 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 defp format_title(%{activity: %{data: %{"type" => type}}}) do
case type do case type do
"Create" -> "New Mention" "Create" -> "New Mention"