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
13 alias Pleroma.ThreadMute
19 @type t :: %__MODULE__{}
20 @type actor :: String.t()
22 @primary_key {:id, Pleroma.FlakeId, autogenerate: true}
24 # https://github.com/tootsuite/mastodon/blob/master/app/models/notification.rb#L19
25 @mastodon_notification_types %{
26 "Create" => "mention",
28 "Announce" => "reblog",
32 @mastodon_to_ap_notification_types for {k, v} <- @mastodon_notification_types,
36 schema "activities" do
38 field(:local, :boolean, default: true)
39 field(:actor, :string)
40 field(:recipients, {:array, :string}, default: [])
41 field(:thread_muted?, :boolean, virtual: true)
42 # This is a fake relation, do not use outside of with_preloaded_bookmark/get_bookmark
43 has_one(:bookmark, Bookmark)
44 has_many(:notifications, Notification, on_delete: :delete_all)
46 # Attention: this is a fake relation, don't try to preload it blindly and expect it to work!
47 # The foreign key is embedded in a jsonb field.
49 # To use it, you probably want to do an inner join and a preload:
52 # |> join(:inner, [activity], o in Object,
53 # on: fragment("(?->>'id') = COALESCE((?)->'object'->> 'id', (?)->>'object')",
54 # o.data, activity.data, activity.data))
55 # |> preload([activity, object], [object: object])
58 # As a convenience, Activity.with_preloaded_object() sets up an inner join and preload for the
60 has_one(:object, Object, on_delete: :nothing, foreign_key: :id)
65 def with_joined_object(query) do
66 join(query, :inner, [activity], o in Object,
69 "(?->>'id') = COALESCE(?->'object'->>'id', ?->>'object')",
78 def with_preloaded_object(query) do
80 |> has_named_binding?(:object)
81 |> if(do: query, else: with_joined_object(query))
82 |> preload([activity, object: object], object: object)
85 def with_preloaded_bookmark(query, %User{} = user) do
87 left_join: b in Bookmark,
88 on: b.user_id == ^user.id and b.activity_id == a.id,
89 preload: [bookmark: b]
93 def with_preloaded_bookmark(query, _), do: query
95 def with_set_thread_muted_field(query, %User{} = user) do
97 left_join: tm in ThreadMute,
98 on: tm.user_id == ^user.id and tm.context == fragment("?->>'context'", a.data),
100 select: %Activity{a | thread_muted?: not is_nil(tm.id)}
104 def with_set_thread_muted_field(query, _), do: query
106 def get_by_ap_id(ap_id) do
109 activity in Activity,
110 where: fragment("(?)->>'id' = ?", activity.data, ^to_string(ap_id))
115 def get_bookmark(%Activity{} = activity, %User{} = user) do
116 if Ecto.assoc_loaded?(activity.bookmark) do
119 Bookmark.get(user.id, activity.id)
123 def get_bookmark(_, _), do: nil
125 def change(struct, params \\ %{}) do
127 |> cast(params, [:data, :recipients])
128 |> validate_required([:data])
129 |> unique_constraint(:ap_id, name: :activities_unique_apid_index)
132 def get_by_ap_id_with_object(ap_id) do
135 activity in Activity,
136 where: fragment("(?)->>'id' = ?", activity.data, ^to_string(ap_id)),
137 left_join: o in Object,
140 "(?->>'id') = COALESCE(?->'object'->>'id', ?->>'object')",
152 |> where([a], a.id == ^id)
153 |> restrict_deactivated_users()
157 def get_by_id_with_object(id) do
158 from(activity in Activity,
159 where: activity.id == ^id,
160 inner_join: o in Object,
163 "(?->>'id') = COALESCE(?->'object'->>'id', ?->>'object')",
173 def by_object_ap_id(ap_id) do
175 activity in Activity,
178 "coalesce((?)->'object'->>'id', (?)->>'object') = ?",
186 def create_by_object_ap_id(ap_ids) when is_list(ap_ids) do
188 activity in Activity,
191 "coalesce((?)->'object'->>'id', (?)->>'object') = ANY(?)",
196 where: fragment("(?)->>'type' = 'Create'", activity.data)
200 def create_by_object_ap_id(ap_id) when is_binary(ap_id) do
202 activity in Activity,
205 "coalesce((?)->'object'->>'id', (?)->>'object') = ?",
210 where: fragment("(?)->>'type' = 'Create'", activity.data)
214 def create_by_object_ap_id(_), do: nil
216 def get_all_create_by_object_ap_id(ap_id) do
217 Repo.all(create_by_object_ap_id(ap_id))
220 def get_create_by_object_ap_id(ap_id) when is_binary(ap_id) do
221 create_by_object_ap_id(ap_id)
222 |> restrict_deactivated_users()
226 def get_create_by_object_ap_id(_), do: nil
228 def create_by_object_ap_id_with_object(ap_ids) when is_list(ap_ids) do
230 activity in Activity,
233 "coalesce((?)->'object'->>'id', (?)->>'object') = ANY(?)",
238 where: fragment("(?)->>'type' = 'Create'", activity.data),
239 inner_join: o in Object,
242 "(?->>'id') = COALESCE(?->'object'->>'id', ?->>'object')",
251 def create_by_object_ap_id_with_object(ap_id) when is_binary(ap_id) do
253 activity in Activity,
256 "coalesce((?)->'object'->>'id', (?)->>'object') = ?",
261 where: fragment("(?)->>'type' = 'Create'", activity.data),
262 inner_join: o in Object,
265 "(?->>'id') = COALESCE(?->'object'->>'id', ?->>'object')",
274 def create_by_object_ap_id_with_object(_), do: nil
276 def get_create_by_object_ap_id_with_object(ap_id) when is_binary(ap_id) do
278 |> create_by_object_ap_id_with_object()
282 def get_create_by_object_ap_id_with_object(_), do: nil
284 defp get_in_reply_to_activity_from_object(%Object{data: %{"inReplyTo" => ap_id}}) do
285 get_create_by_object_ap_id_with_object(ap_id)
288 defp get_in_reply_to_activity_from_object(_), do: nil
290 def get_in_reply_to_activity(%Activity{} = activity) do
291 get_in_reply_to_activity_from_object(Object.normalize(activity))
294 def normalize(obj) when is_map(obj), do: get_by_ap_id_with_object(obj["id"])
295 def normalize(ap_id) when is_binary(ap_id), do: get_by_ap_id_with_object(ap_id)
296 def normalize(_), do: nil
298 def delete_by_ap_id(id) when is_binary(id) do
304 %{data: %{"type" => "Create", "object" => ap_id}} when is_binary(ap_id) -> ap_id == id
305 %{data: %{"type" => "Create", "object" => %{"id" => ap_id}}} -> ap_id == id
310 def delete_by_ap_id(_), do: nil
312 for {ap_type, type} <- @mastodon_notification_types do
313 def mastodon_notification_type(%Activity{data: %{"type" => unquote(ap_type)}}),
317 def mastodon_notification_type(%Activity{}), do: nil
319 def from_mastodon_notification_type(type) do
320 Map.get(@mastodon_to_ap_notification_types, type)
323 def all_by_actor_and_id(actor, status_ids \\ [])
324 def all_by_actor_and_id(_actor, []), do: []
326 def all_by_actor_and_id(actor, status_ids) do
328 |> where([s], s.id in ^status_ids)
329 |> where([s], s.actor == ^actor)
333 def follow_requests_for_actor(%Pleroma.User{ap_id: ap_id}) do
338 "? ->> 'type' = 'Follow'",
343 "? ->> 'state' = 'pending'",
348 "coalesce((?)->'object'->>'id', (?)->>'object') = ?",
356 @spec query_by_actor(actor()) :: Ecto.Query.t()
357 def query_by_actor(actor) do
358 from(a in Activity, where: a.actor == ^actor)
361 def restrict_deactivated_users(query) do
362 from(activity in query,
365 "? not in (SELECT ap_id FROM users WHERE info->'deactivated' @> 'true')",
371 defdelegate search(user, query, options \\ []), to: Pleroma.Activity.Search