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
9 alias Pleroma.ActivityExpiration
10 alias Pleroma.Bookmark
11 alias Pleroma.Notification
14 alias Pleroma.ThreadMute
20 @type t :: %__MODULE__{}
21 @type actor :: String.t()
23 @primary_key {:id, Pleroma.FlakeId, autogenerate: true}
25 # https://github.com/tootsuite/mastodon/blob/master/app/models/notification.rb#L19
26 @mastodon_notification_types %{
27 "Create" => "mention",
29 "Announce" => "reblog",
33 @mastodon_to_ap_notification_types for {k, v} <- @mastodon_notification_types,
37 schema "activities" do
39 field(:local, :boolean, default: true)
40 field(:actor, :string)
41 field(:recipients, {:array, :string}, default: [])
42 field(:thread_muted?, :boolean, virtual: true)
43 # This is a fake relation, do not use outside of with_preloaded_bookmark/get_bookmark
44 has_one(:bookmark, Bookmark)
45 has_many(:notifications, Notification, on_delete: :delete_all)
47 # Attention: this is a fake relation, don't try to preload it blindly and expect it to work!
48 # The foreign key is embedded in a jsonb field.
50 # To use it, you probably want to do an inner join and a preload:
53 # |> join(:inner, [activity], o in Object,
54 # on: fragment("(?->>'id') = COALESCE((?)->'object'->> 'id', (?)->>'object')",
55 # o.data, activity.data, activity.data))
56 # |> preload([activity, object], [object: object])
59 # As a convenience, Activity.with_preloaded_object() sets up an inner join and preload for the
61 has_one(:object, Object, on_delete: :nothing, foreign_key: :id)
63 has_one(:expiration, ActivityExpiration, on_delete: :delete_all)
68 def with_joined_object(query) do
69 join(query, :inner, [activity], o in Object,
72 "(?->>'id') = COALESCE(?->'object'->>'id', ?->>'object')",
81 def with_preloaded_object(query) do
83 |> has_named_binding?(:object)
84 |> if(do: query, else: with_joined_object(query))
85 |> preload([activity, object: object], object: object)
88 def with_preloaded_bookmark(query, %User{} = user) do
90 left_join: b in Bookmark,
91 on: b.user_id == ^user.id and b.activity_id == a.id,
92 preload: [bookmark: b]
96 def with_preloaded_bookmark(query, _), do: query
98 def with_set_thread_muted_field(query, %User{} = user) do
100 left_join: tm in ThreadMute,
101 on: tm.user_id == ^user.id and tm.context == fragment("?->>'context'", a.data),
103 select: %Activity{a | thread_muted?: not is_nil(tm.id)}
107 def with_set_thread_muted_field(query, _), do: query
109 def get_by_ap_id(ap_id) do
112 activity in Activity,
113 where: fragment("(?)->>'id' = ?", activity.data, ^to_string(ap_id))
118 def get_bookmark(%Activity{} = activity, %User{} = user) do
119 if Ecto.assoc_loaded?(activity.bookmark) do
122 Bookmark.get(user.id, activity.id)
126 def get_bookmark(_, _), do: nil
128 def change(struct, params \\ %{}) do
130 |> cast(params, [:data, :recipients])
131 |> validate_required([:data])
132 |> unique_constraint(:ap_id, name: :activities_unique_apid_index)
135 def get_by_ap_id_with_object(ap_id) do
138 activity in Activity,
139 where: fragment("(?)->>'id' = ?", activity.data, ^to_string(ap_id)),
140 left_join: o in Object,
143 "(?->>'id') = COALESCE(?->'object'->>'id', ?->>'object')",
155 |> where([a], a.id == ^id)
156 |> restrict_deactivated_users()
160 def get_by_id_with_object(id) do
161 from(activity in Activity,
162 where: activity.id == ^id,
163 inner_join: o in Object,
166 "(?->>'id') = COALESCE(?->'object'->>'id', ?->>'object')",
176 def by_object_ap_id(ap_id) do
178 activity in Activity,
181 "coalesce((?)->'object'->>'id', (?)->>'object') = ?",
189 def create_by_object_ap_id(ap_ids) when is_list(ap_ids) do
191 activity in Activity,
194 "coalesce((?)->'object'->>'id', (?)->>'object') = ANY(?)",
199 where: fragment("(?)->>'type' = 'Create'", activity.data)
203 def create_by_object_ap_id(ap_id) when is_binary(ap_id) do
205 activity in Activity,
208 "coalesce((?)->'object'->>'id', (?)->>'object') = ?",
213 where: fragment("(?)->>'type' = 'Create'", activity.data)
217 def create_by_object_ap_id(_), do: nil
219 def get_all_create_by_object_ap_id(ap_id) do
220 Repo.all(create_by_object_ap_id(ap_id))
223 def get_create_by_object_ap_id(ap_id) when is_binary(ap_id) do
224 create_by_object_ap_id(ap_id)
225 |> restrict_deactivated_users()
229 def get_create_by_object_ap_id(_), do: nil
231 def create_by_object_ap_id_with_object(ap_ids) when is_list(ap_ids) do
233 activity in Activity,
236 "coalesce((?)->'object'->>'id', (?)->>'object') = ANY(?)",
241 where: fragment("(?)->>'type' = 'Create'", activity.data),
242 inner_join: o in Object,
245 "(?->>'id') = COALESCE(?->'object'->>'id', ?->>'object')",
254 def create_by_object_ap_id_with_object(ap_id) when is_binary(ap_id) do
256 activity in Activity,
259 "coalesce((?)->'object'->>'id', (?)->>'object') = ?",
264 where: fragment("(?)->>'type' = 'Create'", activity.data),
265 inner_join: o in Object,
268 "(?->>'id') = COALESCE(?->'object'->>'id', ?->>'object')",
277 def create_by_object_ap_id_with_object(_), do: nil
279 def get_create_by_object_ap_id_with_object(ap_id) when is_binary(ap_id) do
281 |> create_by_object_ap_id_with_object()
285 def get_create_by_object_ap_id_with_object(_), do: nil
287 defp get_in_reply_to_activity_from_object(%Object{data: %{"inReplyTo" => ap_id}}) do
288 get_create_by_object_ap_id_with_object(ap_id)
291 defp get_in_reply_to_activity_from_object(_), do: nil
293 def get_in_reply_to_activity(%Activity{} = activity) do
294 get_in_reply_to_activity_from_object(Object.normalize(activity))
297 def normalize(obj) when is_map(obj), do: get_by_ap_id_with_object(obj["id"])
298 def normalize(ap_id) when is_binary(ap_id), do: get_by_ap_id_with_object(ap_id)
299 def normalize(_), do: nil
301 def delete_by_ap_id(id) when is_binary(id) do
307 %{data: %{"type" => "Create", "object" => ap_id}} when is_binary(ap_id) -> ap_id == id
308 %{data: %{"type" => "Create", "object" => %{"id" => ap_id}}} -> ap_id == id
313 def delete_by_ap_id(_), do: nil
315 for {ap_type, type} <- @mastodon_notification_types do
316 def mastodon_notification_type(%Activity{data: %{"type" => unquote(ap_type)}}),
320 def mastodon_notification_type(%Activity{}), do: nil
322 def from_mastodon_notification_type(type) do
323 Map.get(@mastodon_to_ap_notification_types, type)
326 def all_by_actor_and_id(actor, status_ids \\ [])
327 def all_by_actor_and_id(_actor, []), do: []
329 def all_by_actor_and_id(actor, status_ids) do
331 |> where([s], s.id in ^status_ids)
332 |> where([s], s.actor == ^actor)
336 def follow_requests_for_actor(%Pleroma.User{ap_id: ap_id}) do
341 "? ->> 'type' = 'Follow'",
346 "? ->> 'state' = 'pending'",
351 "coalesce((?)->'object'->>'id', (?)->>'object') = ?",
359 @spec query_by_actor(actor()) :: Ecto.Query.t()
360 def query_by_actor(actor) do
361 from(a in Activity, where: a.actor == ^actor)
364 def restrict_deactivated_users(query) do
365 from(activity in query,
368 "? not in (SELECT ap_id FROM users WHERE info->'deactivated' @> 'true')",
374 defdelegate search(user, query, options \\ []), to: Pleroma.Activity.Search