1 defmodule Pleroma.Activity do
3 alias Pleroma.{Repo, Activity, Notification, Object}
4 import Ecto.{Query, Changeset}
8 field(:local, :boolean, default: true)
10 field(:recipients, {:array, :string})
11 has_many(:notifications, Notification, on_delete: :delete_all)
16 def get_by_ap_id(ap_id) do
20 where: fragment("(?)->>'id' = ?", activity.data, ^to_string(ap_id))
25 def change(struct, params \\ %{}) do
27 |> cast(params, [:data])
28 |> validate_required([:data])
29 |> unique_constraint(:ap_id, name: :activities_unique_apid_index)
33 # Go through these and fix them everywhere.
34 # Wrong name, only returns create activities
35 def all_by_object_ap_id_q(ap_id) do
40 "coalesce((?)->'object'->>'id', (?)->>'object') = ?",
45 where: fragment("(?)->>'type' = 'Create'", activity.data)
49 # Wrong name, returns all.
50 def all_non_create_by_object_ap_id_q(ap_id) do
55 "coalesce((?)->'object'->>'id', (?)->>'object') = ?",
63 # Wrong name plz fix thx
64 def all_by_object_ap_id(ap_id) do
65 Repo.all(all_by_object_ap_id_q(ap_id))
68 def create_activity_by_object_id_query(ap_ids) do
73 "coalesce((?)->'object'->>'id', (?)->>'object') = ANY(?)",
78 where: fragment("(?)->>'type' = 'Create'", activity.data)
82 def get_create_activity_by_object_ap_id(ap_id) when is_binary(ap_id) do
83 create_activity_by_object_id_query([ap_id])
87 def get_create_activity_by_object_ap_id(_), do: nil
89 def normalize(obj) when is_map(obj), do: normalize(obj["id"])
90 def normalize(ap_id) when is_binary(ap_id), do: get_by_ap_id(ap_id)
91 def normalize(_), do: nil
93 defp get_in_reply_to_activity_from_object(%Object{data: %{"inReplyTo" => ap_id}}) do
94 get_create_activity_by_object_ap_id(ap_id)
97 defp get_in_reply_to_activity_from_object(_), do: nil
99 def get_in_reply_to_activity(%Activity{data: %{"object" => object}}) do
100 get_in_reply_to_activity_from_object(Object.normalize(object))