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),
102 select: %Activity{a | thread_muted?: not is_nil(tm.id)}
106 def with_set_thread_muted_field(query, _), do: query
108 def get_by_ap_id(ap_id) do
111 activity in Activity,
112 where: fragment("(?)->>'id' = ?", activity.data, ^to_string(ap_id))
117 def get_bookmark(%Activity{} = activity, %User{} = user) do
118 if Ecto.assoc_loaded?(activity.bookmark) do
121 Bookmark.get(user.id, activity.id)
125 def get_bookmark(_, _), do: nil
127 def change(struct, params \\ %{}) do
129 |> cast(params, [:data, :recipients])
130 |> validate_required([:data])
131 |> unique_constraint(:ap_id, name: :activities_unique_apid_index)
134 def get_by_ap_id_with_object(ap_id) do
137 activity in Activity,
138 where: fragment("(?)->>'id' = ?", activity.data, ^to_string(ap_id)),
139 left_join: o in Object,
142 "(?->>'id') = COALESCE(?->'object'->>'id', ?->>'object')",
154 |> where([a], a.id == ^id)
155 |> restrict_deactivated_users()
159 def get_by_id_with_object(id) do
160 from(activity in Activity,
161 where: activity.id == ^id,
162 inner_join: o in Object,
165 "(?->>'id') = COALESCE(?->'object'->>'id', ?->>'object')",
175 def by_object_ap_id(ap_id) do
177 activity in Activity,
180 "coalesce((?)->'object'->>'id', (?)->>'object') = ?",
188 def create_by_object_ap_id(ap_ids) when is_list(ap_ids) do
190 activity in Activity,
193 "coalesce((?)->'object'->>'id', (?)->>'object') = ANY(?)",
198 where: fragment("(?)->>'type' = 'Create'", activity.data)
202 def create_by_object_ap_id(ap_id) when is_binary(ap_id) do
204 activity in Activity,
207 "coalesce((?)->'object'->>'id', (?)->>'object') = ?",
212 where: fragment("(?)->>'type' = 'Create'", activity.data)
216 def create_by_object_ap_id(_), do: nil
218 def get_all_create_by_object_ap_id(ap_id) do
219 Repo.all(create_by_object_ap_id(ap_id))
222 def get_create_by_object_ap_id(ap_id) when is_binary(ap_id) do
223 create_by_object_ap_id(ap_id)
224 |> restrict_deactivated_users()
228 def get_create_by_object_ap_id(_), do: nil
230 def create_by_object_ap_id_with_object(ap_id) when is_binary(ap_id) do
232 activity in Activity,
235 "coalesce((?)->'object'->>'id', (?)->>'object') = ?",
240 where: fragment("(?)->>'type' = 'Create'", activity.data),
241 inner_join: o in Object,
244 "(?->>'id') = COALESCE(?->'object'->>'id', ?->>'object')",
253 def create_by_object_ap_id_with_object(_), do: nil
255 def get_create_by_object_ap_id_with_object(ap_id) when is_binary(ap_id) do
257 |> create_by_object_ap_id_with_object()
261 def get_create_by_object_ap_id_with_object(_), do: nil
263 defp get_in_reply_to_activity_from_object(%Object{data: %{"inReplyTo" => ap_id}}) do
264 get_create_by_object_ap_id_with_object(ap_id)
267 defp get_in_reply_to_activity_from_object(_), do: nil
269 def get_in_reply_to_activity(%Activity{data: %{"object" => object}}) do
270 get_in_reply_to_activity_from_object(Object.normalize(object))
273 def normalize(obj) when is_map(obj), do: get_by_ap_id_with_object(obj["id"])
274 def normalize(ap_id) when is_binary(ap_id), do: get_by_ap_id_with_object(ap_id)
275 def normalize(_), do: nil
277 def delete_by_ap_id(id) when is_binary(id) do
283 %{data: %{"type" => "Create", "object" => ap_id}} when is_binary(ap_id) -> ap_id == id
284 %{data: %{"type" => "Create", "object" => %{"id" => ap_id}}} -> ap_id == id
289 def delete_by_ap_id(_), do: nil
291 for {ap_type, type} <- @mastodon_notification_types do
292 def mastodon_notification_type(%Activity{data: %{"type" => unquote(ap_type)}}),
296 def mastodon_notification_type(%Activity{}), do: nil
298 def from_mastodon_notification_type(type) do
299 Map.get(@mastodon_to_ap_notification_types, type)
302 def all_by_actor_and_id(actor, status_ids \\ [])
303 def all_by_actor_and_id(_actor, []), do: []
305 def all_by_actor_and_id(actor, status_ids) do
307 |> where([s], s.id in ^status_ids)
308 |> where([s], s.actor == ^actor)
312 def follow_requests_for_actor(%Pleroma.User{ap_id: ap_id}) do
317 "? ->> 'type' = 'Follow'",
322 "? ->> 'state' = 'pending'",
327 "coalesce((?)->'object'->>'id', (?)->>'object') = ?",
335 @spec query_by_actor(actor()) :: Ecto.Query.t()
336 def query_by_actor(actor) do
337 from(a in Activity, where: a.actor == ^actor)
340 def restrict_deactivated_users(query) do
341 from(activity in query,
344 "? not in (SELECT ap_id FROM users WHERE info->'deactivated' @> 'true')",
350 defdelegate search(user, query, options \\ []), to: Pleroma.Activity.Search