1 defmodule Pleroma.Activity do
3 alias Pleroma.{Repo, Activity, Notification}
6 # https://github.com/tootsuite/mastodon/blob/master/app/models/notification.rb#L19
7 @mastodon_notification_types %{
10 "Announce" => "reblog",
14 schema "activities" do
16 field(:local, :boolean, default: true)
17 field(:actor, :string)
18 field(:recipients, {:array, :string})
19 has_many(:notifications, Notification, on_delete: :delete_all)
24 def get_by_ap_id(ap_id) do
28 where: fragment("(?)->>'id' = ?", activity.data, ^to_string(ap_id))
34 # Go through these and fix them everywhere.
35 # Wrong name, only returns create activities
36 def all_by_object_ap_id_q(ap_id) do
41 "coalesce((?)->'object'->>'id', (?)->>'object') = ?",
46 where: fragment("(?)->>'type' = 'Create'", activity.data)
50 # Wrong name, returns all.
51 def all_non_create_by_object_ap_id_q(ap_id) do
56 "coalesce((?)->'object'->>'id', (?)->>'object') = ?",
64 # Wrong name plz fix thx
65 def all_by_object_ap_id(ap_id) do
66 Repo.all(all_by_object_ap_id_q(ap_id))
69 def create_activity_by_object_id_query(ap_ids) do
74 "coalesce((?)->'object'->>'id', (?)->>'object') = ANY(?)",
79 where: fragment("(?)->>'type' = 'Create'", activity.data)
83 def get_create_activity_by_object_ap_id(ap_id) when is_binary(ap_id) do
84 create_activity_by_object_id_query([ap_id])
88 def get_create_activity_by_object_ap_id(_), do: nil
90 def normalize(obj) when is_map(obj), do: Activity.get_by_ap_id(obj["id"])
91 def normalize(ap_id) when is_binary(ap_id), do: Activity.get_by_ap_id(ap_id)
92 def normalize(_), do: nil
94 def get_in_reply_to_activity(%Activity{data: %{"object" => %{"inReplyTo" => ap_id}}}) do
95 get_create_activity_by_object_ap_id(ap_id)
98 def get_in_reply_to_activity(_), do: nil
100 for {ap_type, type} <- @mastodon_notification_types do
101 def mastodon_notification_type(%Activity{data: %{"type" => unquote(ap_type)}}),
105 def mastodon_notification_type(%Activity{}), do: nil