1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2021 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Pleroma.Activity do
9 alias Pleroma.Activity.Queries
10 alias Pleroma.Bookmark
11 alias Pleroma.Notification
14 alias Pleroma.ReportNote
15 alias Pleroma.ThreadMute
17 alias Pleroma.Web.ActivityPub.ActivityPub
22 @type t :: %__MODULE__{}
23 @type actor :: String.t()
25 @primary_key {:id, FlakeId.Ecto.CompatType, autogenerate: true}
27 @cachex Pleroma.Config.get([:cachex, :provider], Cachex)
29 schema "activities" do
31 field(:local, :boolean, default: true)
32 field(:actor, :string)
33 field(:recipients, {:array, :string}, default: [])
34 field(:thread_muted?, :boolean, virtual: true)
36 # A field that can be used if you need to join some kind of other
37 # id to order / paginate this field by
38 field(:pagination_id, :string, virtual: true)
40 # This is a fake relation,
41 # do not use outside of with_preloaded_user_actor/with_joined_user_actor
42 has_one(:user_actor, User, on_delete: :nothing, foreign_key: :id)
43 # This is a fake relation, do not use outside of with_preloaded_bookmark/get_bookmark
44 has_one(:bookmark, Bookmark)
45 # This is a fake relation, do not use outside of with_preloaded_report_notes
46 has_many(:report_notes, ReportNote)
47 has_many(:notifications, Notification, on_delete: :delete_all)
49 # Attention: this is a fake relation, don't try to preload it blindly and expect it to work!
50 # The foreign key is embedded in a jsonb field.
52 # To use it, you probably want to do an inner join and a preload:
55 # |> join(:inner, [activity], o in Object,
56 # on: fragment("(?->>'id') = COALESCE((?)->'object'->> 'id', (?)->>'object')",
57 # o.data, activity.data, activity.data))
58 # |> preload([activity, object], [object: object])
61 # As a convenience, Activity.with_preloaded_object() sets up an inner join and preload for the
63 has_one(:object, Object, on_delete: :nothing, foreign_key: :id)
68 def with_joined_object(query, join_type \\ :inner) do
69 join(query, join_type, [activity], o in Object,
72 "(?->>'id') = COALESCE(?->'object'->>'id', ?->>'object')",
81 def with_preloaded_object(query, join_type \\ :inner) do
83 |> has_named_binding?(:object)
84 |> if(do: query, else: with_joined_object(query, join_type))
85 |> preload([activity, object: object], object: object)
88 # Note: applies to fake activities (ActivityPub.Utils.get_notified_from_object/1 etc.)
89 def user_actor(%Activity{actor: nil}), do: nil
91 def user_actor(%Activity{} = activity) do
92 with %User{} <- activity.user_actor do
95 _ -> User.get_cached_by_ap_id(activity.actor)
99 def with_joined_user_actor(query, join_type \\ :inner) do
100 join(query, join_type, [activity], u in User,
101 on: u.ap_id == activity.actor,
106 def with_preloaded_user_actor(query, join_type \\ :inner) do
108 |> with_joined_user_actor(join_type)
109 |> preload([activity, user_actor: user_actor], user_actor: user_actor)
112 def with_preloaded_bookmark(query, %User{} = user) do
114 left_join: b in Bookmark,
115 on: b.user_id == ^user.id and b.activity_id == a.id,
117 preload: [bookmark: b]
121 def with_preloaded_bookmark(query, _), do: query
123 def with_preloaded_report_notes(query) do
125 left_join: r in ReportNote,
126 on: a.id == r.activity_id,
128 preload: [report_notes: r]
132 def with_preloaded_report_notes(query, _), do: query
134 def with_set_thread_muted_field(query, %User{} = user) do
136 left_join: tm in ThreadMute,
137 on: tm.user_id == ^user.id and tm.context == fragment("?->>'context'", a.data),
139 select: %Activity{a | thread_muted?: not is_nil(tm.id)}
143 def with_set_thread_muted_field(query, _), do: query
145 def get_by_ap_id(ap_id) do
147 |> Queries.by_ap_id()
151 def get_bookmark(%Activity{} = activity, %User{} = user) do
152 if Ecto.assoc_loaded?(activity.bookmark) do
155 Bookmark.get(user.id, activity.id)
159 def get_bookmark(_, _), do: nil
161 def get_report(activity_id) do
165 preload_report_notes: true
168 ActivityPub.fetch_activities_query([], opts)
169 |> where(id: ^activity_id)
173 def change(struct, params \\ %{}) do
175 |> cast(params, [:data, :recipients])
176 |> validate_required([:data])
177 |> unique_constraint(:ap_id, name: :activities_unique_apid_index)
180 def get_by_ap_id_with_object(ap_id) do
182 |> Queries.by_ap_id()
183 |> with_preloaded_object(:left)
187 @spec get_by_id(String.t()) :: Activity.t() | nil
189 case FlakeId.flake_id?(id) do
192 |> where([a], a.id == ^id)
193 |> restrict_deactivated_users()
201 def get_by_id_with_user_actor(id) do
202 case FlakeId.flake_id?(id) do
205 |> where([a], a.id == ^id)
206 |> with_preloaded_user_actor()
214 def get_by_id_with_object(id) do
217 |> with_preloaded_object()
221 def all_by_ids_with_object(ids) do
223 |> where([a], a.id in ^ids)
224 |> with_preloaded_object()
229 Accepts `ap_id` or list of `ap_id`.
232 @spec create_by_object_ap_id(String.t() | [String.t()]) :: Ecto.Queryable.t()
233 def create_by_object_ap_id(ap_id) do
235 |> Queries.by_object_id()
236 |> Queries.by_type("Create")
239 def get_all_create_by_object_ap_id(ap_id) do
241 |> create_by_object_ap_id()
245 def get_create_by_object_ap_id(ap_id) when is_binary(ap_id) do
246 create_by_object_ap_id(ap_id)
247 |> restrict_deactivated_users()
251 def get_create_by_object_ap_id(_), do: nil
254 Accepts `ap_id` or list of `ap_id`.
257 @spec create_by_object_ap_id_with_object(String.t() | [String.t()]) :: Ecto.Queryable.t()
258 def create_by_object_ap_id_with_object(ap_id) do
260 |> create_by_object_ap_id()
261 |> with_preloaded_object()
264 def get_create_by_object_ap_id_with_object(ap_id) when is_binary(ap_id) do
266 |> create_by_object_ap_id_with_object()
270 def get_create_by_object_ap_id_with_object(_), do: nil
272 defp get_in_reply_to_activity_from_object(%Object{data: %{"inReplyTo" => ap_id}}) do
273 get_create_by_object_ap_id_with_object(ap_id)
276 defp get_in_reply_to_activity_from_object(_), do: nil
278 def get_in_reply_to_activity(%Activity{} = activity) do
279 get_in_reply_to_activity_from_object(Object.normalize(activity, fetch: false))
282 def normalize(obj) when is_map(obj), do: get_by_ap_id_with_object(obj["id"])
283 def normalize(ap_id) when is_binary(ap_id), do: get_by_ap_id_with_object(ap_id)
284 def normalize(_), do: nil
286 def delete_all_by_object_ap_id(id) when is_binary(id) do
288 |> Queries.by_object_id()
289 |> Queries.exclude_type("Delete")
294 %{data: %{"type" => "Create", "object" => ap_id}} when is_binary(ap_id) -> ap_id == id
295 %{data: %{"type" => "Create", "object" => %{"id" => ap_id}}} -> ap_id == id
298 |> purge_web_resp_cache()
301 def delete_all_by_object_ap_id(_), do: nil
303 defp purge_web_resp_cache(%Activity{} = activity) do
304 %{path: path} = URI.parse(activity.data["id"])
305 @cachex.del(:web_resp_cache, path)
309 defp purge_web_resp_cache(nil), do: nil
311 def follow_accepted?(
312 %Activity{data: %{"type" => "Follow", "object" => followed_ap_id}} = activity
314 with %User{} = follower <- Activity.user_actor(activity),
315 %User{} = followed <- User.get_cached_by_ap_id(followed_ap_id) do
316 Pleroma.FollowingRelationship.following?(follower, followed)
322 def follow_accepted?(_), do: false
324 def all_by_actor_and_id(actor, status_ids \\ [])
325 def all_by_actor_and_id(_actor, []), do: []
327 def all_by_actor_and_id(actor, status_ids) do
329 |> where([s], s.id in ^status_ids)
330 |> where([s], s.actor == ^actor)
334 def follow_requests_for_actor(%User{ap_id: ap_id}) do
336 |> Queries.by_object_id()
337 |> Queries.by_type("Follow")
338 |> where([a], fragment("? ->> 'state' = 'pending'", a.data))
341 def following_requests_for_actor(%User{ap_id: ap_id}) do
342 Queries.by_type("Follow")
343 |> where([a], fragment("?->>'state' = 'pending'", a.data))
344 |> where([a], a.actor == ^ap_id)
348 def restrict_deactivated_users(query) do
350 from(u in User.Query.build(%{deactivated: true}), select: u.ap_id)
353 Activity.Queries.exclude_authors(query, deactivated_users)
356 defdelegate search(user, query, options \\ []), to: Pleroma.Activity.Search
358 def direct_conversation_id(activity, for_user) do
359 alias Pleroma.Conversation.Participation
361 with %{data: %{"context" => context}} when is_binary(context) <- activity,
362 %Pleroma.Conversation{} = conversation <- Pleroma.Conversation.get_for_ap_id(context),
363 %Participation{id: participation_id} <-
364 Participation.for_user_and_conversation(for_user, conversation) do
371 @spec pinned_by_actor?(Activity.t()) :: boolean()
372 def pinned_by_actor?(%Activity{} = activity) do
373 actor = user_actor(activity)
374 activity.id in actor.pinned_activities
377 @spec get_by_object_ap_id_with_object(String.t()) :: t() | nil
378 def get_by_object_ap_id_with_object(ap_id) when is_binary(ap_id) do
380 |> Queries.by_object_id()
381 |> with_preloaded_object()
386 def get_by_object_ap_id_with_object(_), do: nil