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
10 alias Pleroma.Notification
13 alias Pleroma.ThreadMute
19 @type t :: %__MODULE__{}
20 @type actor :: String.t()
22 @primary_key {:id, Pleroma.FlakeId, autogenerate: true}
24 # https://github.com/tootsuite/mastodon/blob/master/app/models/notification.rb#L19
25 @mastodon_notification_types %{
26 "Create" => "mention",
28 "Announce" => "reblog",
32 @mastodon_to_ap_notification_types for {k, v} <- @mastodon_notification_types,
36 schema "activities" do
38 field(:local, :boolean, default: true)
39 field(:actor, :string)
40 field(:recipients, {:array, :string}, default: [])
41 field(:thread_muted?, :boolean, virtual: true)
42 # This is a fake relation, do not use outside of with_preloaded_bookmark/get_bookmark
43 has_one(:bookmark, Bookmark)
44 has_many(:notifications, Notification, on_delete: :delete_all)
46 # Attention: this is a fake relation, don't try to preload it blindly and expect it to work!
47 # The foreign key is embedded in a jsonb field.
49 # To use it, you probably want to do an inner join and a preload:
52 # |> join(:inner, [activity], o in Object,
53 # on: fragment("(?->>'id') = COALESCE((?)->'object'->> 'id', (?)->>'object')",
54 # o.data, activity.data, activity.data))
55 # |> preload([activity, object], [object: object])
58 # As a convenience, Activity.with_preloaded_object() sets up an inner join and preload for the
60 has_one(:object, Object, on_delete: :nothing, foreign_key: :id)
65 def with_joined_object(query) do
66 join(query, :inner, [activity], o in Object,
69 "(?->>'id') = COALESCE(?->'object'->>'id', ?->>'object')",
78 def with_preloaded_object(query) do
80 |> has_named_binding?(:object)
81 |> if(do: query, else: with_joined_object(query))
82 |> preload([activity, object: object], object: object)
85 def with_preloaded_bookmark(query, %User{} = user) do
87 left_join: b in Bookmark,
88 on: b.user_id == ^user.id and b.activity_id == a.id,
89 preload: [bookmark: b]
93 def with_preloaded_bookmark(query, _), do: query
95 def with_set_thread_muted_field(query, %User{} = user) do
97 left_join: tm in ThreadMute,
98 on: tm.user_id == ^user.id and tm.context == fragment("?->>'context'", a.data),
99 select: %Activity{a | thread_muted?: not is_nil(tm.id)}
103 def with_set_thread_muted_field(query, _), do: query
105 def get_by_ap_id(ap_id) do
108 activity in Activity,
109 where: fragment("(?)->>'id' = ?", activity.data, ^to_string(ap_id))
114 def get_bookmark(%Activity{} = activity, %User{} = user) do
115 if Ecto.assoc_loaded?(activity.bookmark) do
118 Bookmark.get(user.id, activity.id)
122 def get_bookmark(_, _), do: nil
124 def change(struct, params \\ %{}) do
126 |> cast(params, [:data, :recipients])
127 |> validate_required([:data])
128 |> unique_constraint(:ap_id, name: :activities_unique_apid_index)
131 def get_by_ap_id_with_object(ap_id) do
134 activity in Activity,
135 where: fragment("(?)->>'id' = ?", activity.data, ^to_string(ap_id)),
136 left_join: o in Object,
139 "(?->>'id') = COALESCE(?->'object'->>'id', ?->>'object')",
151 |> where([a], a.id == ^id)
152 |> restrict_deactivated_users()
156 def get_by_id_with_object(id) do
157 from(activity in Activity,
158 where: activity.id == ^id,
159 inner_join: o in Object,
162 "(?->>'id') = COALESCE(?->'object'->>'id', ?->>'object')",
172 def by_object_ap_id(ap_id) do
174 activity in Activity,
177 "coalesce((?)->'object'->>'id', (?)->>'object') = ?",
185 def create_by_object_ap_id(ap_ids) when is_list(ap_ids) do
187 activity in Activity,
190 "coalesce((?)->'object'->>'id', (?)->>'object') = ANY(?)",
195 where: fragment("(?)->>'type' = 'Create'", activity.data)
199 def create_by_object_ap_id(ap_id) when is_binary(ap_id) do
201 activity in Activity,
204 "coalesce((?)->'object'->>'id', (?)->>'object') = ?",
209 where: fragment("(?)->>'type' = 'Create'", activity.data)
213 def create_by_object_ap_id(_), do: nil
215 def get_all_create_by_object_ap_id(ap_id) do
216 Repo.all(create_by_object_ap_id(ap_id))
219 def get_create_by_object_ap_id(ap_id) when is_binary(ap_id) do
220 create_by_object_ap_id(ap_id)
221 |> restrict_deactivated_users()
225 def get_create_by_object_ap_id(_), do: nil
227 def create_by_object_ap_id_with_object(ap_id) when is_binary(ap_id) do
229 activity in Activity,
232 "coalesce((?)->'object'->>'id', (?)->>'object') = ?",
237 where: fragment("(?)->>'type' = 'Create'", activity.data),
238 inner_join: o in Object,
241 "(?->>'id') = COALESCE(?->'object'->>'id', ?->>'object')",
250 def create_by_object_ap_id_with_object(_), do: nil
252 def get_create_by_object_ap_id_with_object(ap_id) when is_binary(ap_id) do
254 |> create_by_object_ap_id_with_object()
258 def get_create_by_object_ap_id_with_object(_), do: nil
260 defp get_in_reply_to_activity_from_object(%Object{data: %{"inReplyTo" => ap_id}}) do
261 get_create_by_object_ap_id_with_object(ap_id)
264 defp get_in_reply_to_activity_from_object(_), do: nil
266 def get_in_reply_to_activity(%Activity{data: %{"object" => object}}) do
267 get_in_reply_to_activity_from_object(Object.normalize(object))
270 def normalize(obj) when is_map(obj), do: get_by_ap_id_with_object(obj["id"])
271 def normalize(ap_id) when is_binary(ap_id), do: get_by_ap_id_with_object(ap_id)
272 def normalize(_), do: nil
274 def delete_by_ap_id(id) when is_binary(id) do
280 %{data: %{"type" => "Create", "object" => ap_id}} when is_binary(ap_id) -> ap_id == id
281 %{data: %{"type" => "Create", "object" => %{"id" => ap_id}}} -> ap_id == id
286 def delete_by_ap_id(_), do: nil
288 for {ap_type, type} <- @mastodon_notification_types do
289 def mastodon_notification_type(%Activity{data: %{"type" => unquote(ap_type)}}),
293 def mastodon_notification_type(%Activity{}), do: nil
295 def from_mastodon_notification_type(type) do
296 Map.get(@mastodon_to_ap_notification_types, type)
299 def all_by_actor_and_id(actor, status_ids \\ [])
300 def all_by_actor_and_id(_actor, []), do: []
302 def all_by_actor_and_id(actor, status_ids) do
304 |> where([s], s.id in ^status_ids)
305 |> where([s], s.actor == ^actor)
309 def follow_requests_for_actor(%Pleroma.User{ap_id: ap_id}) do
314 "? ->> 'type' = 'Follow'",
319 "? ->> 'state' = 'pending'",
324 "coalesce((?)->'object'->>'id', (?)->>'object') = ?",
332 @spec query_by_actor(actor()) :: Ecto.Query.t()
333 def query_by_actor(actor) do
334 from(a in Activity, where: a.actor == ^actor)
337 def restrict_deactivated_users(query) do
338 from(activity in query,
341 "? not in (SELECT ap_id FROM users WHERE info->'deactivated' @> 'true')",
347 defdelegate search(user, query), to: Pleroma.Activity.Search