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.Activity.Queries
10 alias Pleroma.ActivityExpiration
11 alias Pleroma.Bookmark
12 alias Pleroma.Notification
15 alias Pleroma.ReportNote
16 alias Pleroma.ThreadMute
22 @type t :: %__MODULE__{}
23 @type actor :: String.t()
25 @primary_key {:id, FlakeId.Ecto.CompatType, autogenerate: true}
27 # https://github.com/tootsuite/mastodon/blob/master/app/models/notification.rb#L19
28 @mastodon_notification_types %{
29 "Create" => "mention",
31 "Announce" => "reblog",
35 @mastodon_to_ap_notification_types for {k, v} <- @mastodon_notification_types,
39 schema "activities" do
41 field(:local, :boolean, default: true)
42 field(:actor, :string)
43 field(:recipients, {:array, :string}, default: [])
44 field(:thread_muted?, :boolean, virtual: true)
46 # This is a fake relation,
47 # do not use outside of with_preloaded_user_actor/with_joined_user_actor
48 has_one(:user_actor, User, on_delete: :nothing, foreign_key: :id)
49 # This is a fake relation, do not use outside of with_preloaded_bookmark/get_bookmark
50 has_one(:bookmark, Bookmark)
51 # This is a fake relation, do not use outside of with_preloaded_report_notes
52 has_many(:report_notes, ReportNote)
53 has_many(:notifications, Notification, on_delete: :delete_all)
55 # Attention: this is a fake relation, don't try to preload it blindly and expect it to work!
56 # The foreign key is embedded in a jsonb field.
58 # To use it, you probably want to do an inner join and a preload:
61 # |> join(:inner, [activity], o in Object,
62 # on: fragment("(?->>'id') = COALESCE((?)->'object'->> 'id', (?)->>'object')",
63 # o.data, activity.data, activity.data))
64 # |> preload([activity, object], [object: object])
67 # As a convenience, Activity.with_preloaded_object() sets up an inner join and preload for the
69 has_one(:object, Object, on_delete: :nothing, foreign_key: :id)
71 has_one(:expiration, ActivityExpiration, on_delete: :delete_all)
76 def with_joined_object(query, join_type \\ :inner) do
77 join(query, join_type, [activity], o in Object,
80 "(?->>'id') = COALESCE(?->'object'->>'id', ?->>'object')",
89 def with_preloaded_object(query, join_type \\ :inner) do
91 |> has_named_binding?(:object)
92 |> if(do: query, else: with_joined_object(query, join_type))
93 |> preload([activity, object: object], object: object)
96 def with_joined_user_actor(query, join_type \\ :inner) do
97 join(query, join_type, [activity], u in User,
98 on: u.ap_id == activity.actor,
103 def with_preloaded_user_actor(query, join_type \\ :inner) do
105 |> with_joined_user_actor(join_type)
106 |> preload([activity, user_actor: user_actor], user_actor: user_actor)
109 def with_preloaded_bookmark(query, %User{} = user) do
111 left_join: b in Bookmark,
112 on: b.user_id == ^user.id and b.activity_id == a.id,
113 preload: [bookmark: b]
117 def with_preloaded_bookmark(query, _), do: query
119 def with_preloaded_report_notes(query) do
121 left_join: r in ReportNote,
122 on: a.id == r.activity_id,
123 preload: [report_notes: r]
127 def with_preloaded_report_notes(query, _), do: query
129 def with_set_thread_muted_field(query, %User{} = user) do
131 left_join: tm in ThreadMute,
132 on: tm.user_id == ^user.id and tm.context == fragment("?->>'context'", a.data),
134 select: %Activity{a | thread_muted?: not is_nil(tm.id)}
138 def with_set_thread_muted_field(query, _), do: query
140 def get_by_ap_id(ap_id) do
142 |> Queries.by_ap_id()
146 def get_bookmark(%Activity{} = activity, %User{} = user) do
147 if Ecto.assoc_loaded?(activity.bookmark) do
150 Bookmark.get(user.id, activity.id)
154 def get_bookmark(_, _), do: nil
156 def change(struct, params \\ %{}) do
158 |> cast(params, [:data, :recipients])
159 |> validate_required([:data])
160 |> unique_constraint(:ap_id, name: :activities_unique_apid_index)
163 def get_by_ap_id_with_object(ap_id) do
165 |> Queries.by_ap_id()
166 |> with_preloaded_object(:left)
170 @spec get_by_id(String.t()) :: Activity.t() | nil
172 case FlakeId.flake_id?(id) do
175 |> where([a], a.id == ^id)
176 |> restrict_deactivated_users()
184 def get_by_id_with_object(id) do
187 |> with_preloaded_object()
191 def all_by_ids_with_object(ids) do
193 |> where([a], a.id in ^ids)
194 |> with_preloaded_object()
199 Accepts `ap_id` or list of `ap_id`.
202 @spec create_by_object_ap_id(String.t() | [String.t()]) :: Ecto.Queryable.t()
203 def create_by_object_ap_id(ap_id) do
205 |> Queries.by_object_id()
206 |> Queries.by_type("Create")
209 def get_all_create_by_object_ap_id(ap_id) do
211 |> create_by_object_ap_id()
215 def get_create_by_object_ap_id(ap_id) when is_binary(ap_id) do
216 create_by_object_ap_id(ap_id)
217 |> restrict_deactivated_users()
221 def get_create_by_object_ap_id(_), do: nil
224 Accepts `ap_id` or list of `ap_id`.
227 @spec create_by_object_ap_id_with_object(String.t() | [String.t()]) :: Ecto.Queryable.t()
228 def create_by_object_ap_id_with_object(ap_id) do
230 |> create_by_object_ap_id()
231 |> with_preloaded_object()
234 def get_create_by_object_ap_id_with_object(ap_id) when is_binary(ap_id) do
236 |> create_by_object_ap_id_with_object()
240 def get_create_by_object_ap_id_with_object(_), do: nil
242 defp get_in_reply_to_activity_from_object(%Object{data: %{"inReplyTo" => ap_id}}) do
243 get_create_by_object_ap_id_with_object(ap_id)
246 defp get_in_reply_to_activity_from_object(_), do: nil
248 def get_in_reply_to_activity(%Activity{} = activity) do
249 get_in_reply_to_activity_from_object(Object.normalize(activity))
252 def normalize(obj) when is_map(obj), do: get_by_ap_id_with_object(obj["id"])
253 def normalize(ap_id) when is_binary(ap_id), do: get_by_ap_id_with_object(ap_id)
254 def normalize(_), do: nil
256 def delete_by_ap_id(id) when is_binary(id) do
258 |> Queries.by_object_id()
263 %{data: %{"type" => "Create", "object" => ap_id}} when is_binary(ap_id) -> ap_id == id
264 %{data: %{"type" => "Create", "object" => %{"id" => ap_id}}} -> ap_id == id
267 |> purge_web_resp_cache()
270 def delete_by_ap_id(_), do: nil
272 defp purge_web_resp_cache(%Activity{} = activity) do
273 %{path: path} = URI.parse(activity.data["id"])
274 Cachex.del(:web_resp_cache, path)
278 defp purge_web_resp_cache(nil), do: nil
280 for {ap_type, type} <- @mastodon_notification_types do
281 def mastodon_notification_type(%Activity{data: %{"type" => unquote(ap_type)}}),
285 def mastodon_notification_type(%Activity{}), do: nil
287 def from_mastodon_notification_type(type) do
288 Map.get(@mastodon_to_ap_notification_types, type)
291 def all_by_actor_and_id(actor, status_ids \\ [])
292 def all_by_actor_and_id(_actor, []), do: []
294 def all_by_actor_and_id(actor, status_ids) do
296 |> where([s], s.id in ^status_ids)
297 |> where([s], s.actor == ^actor)
301 def follow_requests_for_actor(%Pleroma.User{ap_id: ap_id}) do
303 |> Queries.by_object_id()
304 |> Queries.by_type("Follow")
305 |> where([a], fragment("? ->> 'state' = 'pending'", a.data))
308 def restrict_deactivated_users(query) do
310 from(u in User.Query.build(deactivated: true), select: u.ap_id)
313 from(activity in query,
314 where: activity.actor not in ^deactivated_users
318 defdelegate search(user, query, options \\ []), to: Pleroma.Activity.Search
320 def direct_conversation_id(activity, for_user) do
321 alias Pleroma.Conversation.Participation
323 with %{data: %{"context" => context}} when is_binary(context) <- activity,
324 %Pleroma.Conversation{} = conversation <- Pleroma.Conversation.get_for_ap_id(context),
325 %Participation{id: participation_id} <-
326 Participation.for_user_and_conversation(for_user, conversation) do