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 all_by_ids_with_object(ids) do
178 |> where([a], a.id in ^ids)
179 |> with_preloaded_object()
183 def by_object_ap_id(ap_id) do
185 activity in Activity,
188 "coalesce((?)->'object'->>'id', (?)->>'object') = ?",
196 def create_by_object_ap_id(ap_ids) when is_list(ap_ids) do
198 activity in Activity,
201 "coalesce((?)->'object'->>'id', (?)->>'object') = ANY(?)",
206 where: fragment("(?)->>'type' = 'Create'", activity.data)
210 def create_by_object_ap_id(ap_id) when is_binary(ap_id) do
212 activity in Activity,
215 "coalesce((?)->'object'->>'id', (?)->>'object') = ?",
220 where: fragment("(?)->>'type' = 'Create'", activity.data)
224 def create_by_object_ap_id(_), do: nil
226 def get_all_create_by_object_ap_id(ap_id) do
227 Repo.all(create_by_object_ap_id(ap_id))
230 def get_create_by_object_ap_id(ap_id) when is_binary(ap_id) do
231 create_by_object_ap_id(ap_id)
232 |> restrict_deactivated_users()
236 def get_create_by_object_ap_id(_), do: nil
238 def create_by_object_ap_id_with_object(ap_ids) when is_list(ap_ids) do
240 activity in Activity,
243 "coalesce((?)->'object'->>'id', (?)->>'object') = ANY(?)",
248 where: fragment("(?)->>'type' = 'Create'", activity.data),
249 inner_join: o in Object,
252 "(?->>'id') = COALESCE(?->'object'->>'id', ?->>'object')",
261 def create_by_object_ap_id_with_object(ap_id) when is_binary(ap_id) do
263 activity in Activity,
266 "coalesce((?)->'object'->>'id', (?)->>'object') = ?",
271 where: fragment("(?)->>'type' = 'Create'", activity.data),
272 inner_join: o in Object,
275 "(?->>'id') = COALESCE(?->'object'->>'id', ?->>'object')",
284 def create_by_object_ap_id_with_object(_), do: nil
286 def get_create_by_object_ap_id_with_object(ap_id) when is_binary(ap_id) do
288 |> create_by_object_ap_id_with_object()
292 def get_create_by_object_ap_id_with_object(_), do: nil
294 defp get_in_reply_to_activity_from_object(%Object{data: %{"inReplyTo" => ap_id}}) do
295 get_create_by_object_ap_id_with_object(ap_id)
298 defp get_in_reply_to_activity_from_object(_), do: nil
300 def get_in_reply_to_activity(%Activity{} = activity) do
301 get_in_reply_to_activity_from_object(Object.normalize(activity))
304 def normalize(obj) when is_map(obj), do: get_by_ap_id_with_object(obj["id"])
305 def normalize(ap_id) when is_binary(ap_id), do: get_by_ap_id_with_object(ap_id)
306 def normalize(_), do: nil
308 def delete_by_ap_id(id) when is_binary(id) do
314 %{data: %{"type" => "Create", "object" => ap_id}} when is_binary(ap_id) -> ap_id == id
315 %{data: %{"type" => "Create", "object" => %{"id" => ap_id}}} -> ap_id == id
318 |> purge_web_resp_cache()
321 def delete_by_ap_id(_), do: nil
323 defp purge_web_resp_cache(%Activity{} = activity) do
324 %{path: path} = URI.parse(activity.data["id"])
325 Cachex.del(:web_resp_cache, path)
329 defp purge_web_resp_cache(nil), do: nil
331 for {ap_type, type} <- @mastodon_notification_types do
332 def mastodon_notification_type(%Activity{data: %{"type" => unquote(ap_type)}}),
336 def mastodon_notification_type(%Activity{}), do: nil
338 def from_mastodon_notification_type(type) do
339 Map.get(@mastodon_to_ap_notification_types, type)
342 def all_by_actor_and_id(actor, status_ids \\ [])
343 def all_by_actor_and_id(_actor, []), do: []
345 def all_by_actor_and_id(actor, status_ids) do
347 |> where([s], s.id in ^status_ids)
348 |> where([s], s.actor == ^actor)
352 def follow_requests_for_actor(%Pleroma.User{ap_id: ap_id}) do
357 "? ->> 'type' = 'Follow'",
362 "? ->> 'state' = 'pending'",
367 "coalesce((?)->'object'->>'id', (?)->>'object') = ?",
375 @spec query_by_actor(actor()) :: Ecto.Query.t()
376 def query_by_actor(actor) do
377 from(a in Activity, where: a.actor == ^actor)
380 def restrict_deactivated_users(query) do
382 from(u in User.Query.build(deactivated: true), select: u.ap_id)
385 from(activity in query,
386 where: activity.actor not in ^deactivated_users
390 defdelegate search(user, query, options \\ []), to: Pleroma.Activity.Search