1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Pleroma.Activity do
10 alias Pleroma.Notification
17 @type t :: %__MODULE__{}
18 @type actor :: String.t()
20 @primary_key {:id, Pleroma.FlakeId, autogenerate: true}
22 # https://github.com/tootsuite/mastodon/blob/master/app/models/notification.rb#L19
23 @mastodon_notification_types %{
24 "Create" => "mention",
26 "Announce" => "reblog",
30 @mastodon_to_ap_notification_types for {k, v} <- @mastodon_notification_types,
34 schema "activities" do
36 field(:local, :boolean, default: true)
37 field(:actor, :string)
38 field(:recipients, {:array, :string}, default: [])
39 has_many(:notifications, Notification, on_delete: :delete_all)
40 has_many(:bookmarks, Bookmark, on_delete: :delete_all)
42 # Attention: this is a fake relation, don't try to preload it blindly and expect it to work!
43 # The foreign key is embedded in a jsonb field.
45 # To use it, you probably want to do an inner join and a preload:
48 # |> join(:inner, [activity], o in Object,
49 # on: fragment("(?->>'id') = COALESCE((?)->'object'->> 'id', (?)->>'object')",
50 # o.data, activity.data, activity.data))
51 # |> preload([activity, object], [object: object])
54 # As a convenience, Activity.with_preloaded_object() sets up an inner join and preload for the
56 has_one(:object, Object, on_delete: :nothing, foreign_key: :id)
61 def with_preloaded_object(query) do
69 "(?->>'id') = COALESCE(?->'object'->>'id', ?->>'object')",
75 |> preload([activity, object], object: object)
76 |> with_preloaded_bookmarks()
79 def with_preloaded_bookmarks(query) do
81 |> preload(:bookmarks)
84 def get_by_ap_id(ap_id) do
88 where: fragment("(?)->>'id' = ?", activity.data, ^to_string(ap_id))
93 def change(struct, params \\ %{}) do
95 |> cast(params, [:data])
96 |> validate_required([:data])
97 |> unique_constraint(:ap_id, name: :activities_unique_apid_index)
100 def get_by_ap_id_with_object(ap_id) do
103 activity in Activity,
104 where: fragment("(?)->>'id' = ?", activity.data, ^to_string(ap_id)),
105 left_join: o in Object,
108 "(?->>'id') = COALESCE(?->'object'->>'id', ?->>'object')",
115 |> with_preloaded_bookmarks()
120 Repo.get(Activity, id)
123 def get_by_id_with_object(id) do
124 from(activity in Activity,
125 where: activity.id == ^id,
126 inner_join: o in Object,
129 "(?->>'id') = COALESCE(?->'object'->>'id', ?->>'object')",
136 |> with_preloaded_bookmarks()
140 def by_object_ap_id(ap_id) do
142 activity in Activity,
145 "coalesce((?)->'object'->>'id', (?)->>'object') = ?",
153 def create_by_object_ap_id(ap_ids) when is_list(ap_ids) do
155 activity in Activity,
158 "coalesce((?)->'object'->>'id', (?)->>'object') = ANY(?)",
163 where: fragment("(?)->>'type' = 'Create'", activity.data)
167 def create_by_object_ap_id(ap_id) when is_binary(ap_id) do
169 activity in Activity,
172 "coalesce((?)->'object'->>'id', (?)->>'object') = ?",
177 where: fragment("(?)->>'type' = 'Create'", activity.data)
181 def create_by_object_ap_id(_), do: nil
183 def get_all_create_by_object_ap_id(ap_id) do
184 Repo.all(create_by_object_ap_id(ap_id))
187 def get_create_by_object_ap_id(ap_id) when is_binary(ap_id) do
188 create_by_object_ap_id(ap_id)
192 def get_create_by_object_ap_id(_), do: nil
194 def create_by_object_ap_id_with_object(ap_id) when is_binary(ap_id) do
196 activity in Activity,
199 "coalesce((?)->'object'->>'id', (?)->>'object') = ?",
204 where: fragment("(?)->>'type' = 'Create'", activity.data),
205 inner_join: o in Object,
208 "(?->>'id') = COALESCE(?->'object'->>'id', ?->>'object')",
215 |> with_preloaded_bookmarks()
218 def create_by_object_ap_id_with_object(_), do: nil
220 def get_create_by_object_ap_id_with_object(ap_id) when is_binary(ap_id) do
222 |> create_by_object_ap_id_with_object()
226 def get_create_by_object_ap_id_with_object(_), do: nil
228 defp get_in_reply_to_activity_from_object(%Object{data: %{"inReplyTo" => ap_id}}) do
229 get_create_by_object_ap_id_with_object(ap_id)
232 defp get_in_reply_to_activity_from_object(_), do: nil
234 def get_in_reply_to_activity(%Activity{data: %{"object" => object}}) do
235 get_in_reply_to_activity_from_object(Object.normalize(object))
238 def normalize(obj) when is_map(obj), do: get_by_ap_id_with_object(obj["id"])
239 def normalize(ap_id) when is_binary(ap_id), do: get_by_ap_id_with_object(ap_id)
240 def normalize(_), do: nil
242 def delete_by_ap_id(id) when is_binary(id) do
248 %{data: %{"type" => "Create", "object" => ap_id}} when is_binary(ap_id) -> ap_id == id
249 %{data: %{"type" => "Create", "object" => %{"id" => ap_id}}} -> ap_id == id
254 def delete_by_ap_id(_), do: nil
256 for {ap_type, type} <- @mastodon_notification_types do
257 def mastodon_notification_type(%Activity{data: %{"type" => unquote(ap_type)}}),
261 def mastodon_notification_type(%Activity{}), do: nil
263 def from_mastodon_notification_type(type) do
264 Map.get(@mastodon_to_ap_notification_types, type)
267 def all_by_actor_and_id(actor, status_ids \\ [])
268 def all_by_actor_and_id(_actor, []), do: []
270 def all_by_actor_and_id(actor, status_ids) do
272 |> where([s], s.id in ^status_ids)
273 |> where([s], s.actor == ^actor)
277 @spec query_by_actor(actor()) :: Ecto.Query.t()
278 def query_by_actor(actor) do
279 from(a in Activity, where: a.actor == ^actor)