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
18 @type t :: %__MODULE__{}
19 @type actor :: String.t()
21 @primary_key {:id, Pleroma.FlakeId, autogenerate: true}
23 # https://github.com/tootsuite/mastodon/blob/master/app/models/notification.rb#L19
24 @mastodon_notification_types %{
25 "Create" => "mention",
27 "Announce" => "reblog",
31 @mastodon_to_ap_notification_types for {k, v} <- @mastodon_notification_types,
35 schema "activities" do
37 field(:local, :boolean, default: true)
38 field(:actor, :string)
39 field(:recipients, {:array, :string}, default: [])
40 # This is a fake relation, do not use outside of with_preloaded_bookmark/get_bookmark
41 has_one(:bookmark, Bookmark)
42 has_many(:notifications, Notification, on_delete: :delete_all)
44 # Attention: this is a fake relation, don't try to preload it blindly and expect it to work!
45 # The foreign key is embedded in a jsonb field.
47 # To use it, you probably want to do an inner join and a preload:
50 # |> join(:inner, [activity], o in Object,
51 # on: fragment("(?->>'id') = COALESCE((?)->'object'->> 'id', (?)->>'object')",
52 # o.data, activity.data, activity.data))
53 # |> preload([activity, object], [object: object])
56 # As a convenience, Activity.with_preloaded_object() sets up an inner join and preload for the
58 has_one(:object, Object, on_delete: :nothing, foreign_key: :id)
63 def with_preloaded_object(query) do
71 "(?->>'id') = COALESCE(?->'object'->>'id', ?->>'object')",
77 |> preload([activity, object], object: object)
80 def with_preloaded_bookmark(query, %User{} = user) do
82 left_join: b in Bookmark,
83 on: b.user_id == ^user.id and b.activity_id == a.id,
84 preload: [bookmark: b]
88 def with_preloaded_bookmark(query, _), do: query
90 def get_by_ap_id(ap_id) do
94 where: fragment("(?)->>'id' = ?", activity.data, ^to_string(ap_id))
99 def get_bookmark(%Activity{} = activity, %User{} = user) do
100 if Ecto.assoc_loaded?(activity.bookmark) do
103 Bookmark.get(user.id, activity.id)
107 def get_bookmark(_, _), do: nil
109 def change(struct, params \\ %{}) do
111 |> cast(params, [:data])
112 |> validate_required([:data])
113 |> unique_constraint(:ap_id, name: :activities_unique_apid_index)
116 def get_by_ap_id_with_object(ap_id) do
119 activity in Activity,
120 where: fragment("(?)->>'id' = ?", activity.data, ^to_string(ap_id)),
121 left_join: o in Object,
124 "(?->>'id') = COALESCE(?->'object'->>'id', ?->>'object')",
136 |> where([a], a.id == ^id)
137 |> restrict_deactivated_users()
141 def get_by_id_with_object(id) do
142 from(activity in Activity,
143 where: activity.id == ^id,
144 inner_join: o in Object,
147 "(?->>'id') = COALESCE(?->'object'->>'id', ?->>'object')",
157 def by_object_ap_id(ap_id) do
159 activity in Activity,
162 "coalesce((?)->'object'->>'id', (?)->>'object') = ?",
170 def create_by_object_ap_id(ap_ids) when is_list(ap_ids) do
172 activity in Activity,
175 "coalesce((?)->'object'->>'id', (?)->>'object') = ANY(?)",
180 where: fragment("(?)->>'type' = 'Create'", activity.data)
184 def create_by_object_ap_id(ap_id) when is_binary(ap_id) do
186 activity in Activity,
189 "coalesce((?)->'object'->>'id', (?)->>'object') = ?",
194 where: fragment("(?)->>'type' = 'Create'", activity.data)
198 def create_by_object_ap_id(_), do: nil
200 def get_all_create_by_object_ap_id(ap_id) do
201 Repo.all(create_by_object_ap_id(ap_id))
204 def get_create_by_object_ap_id(ap_id) when is_binary(ap_id) do
205 create_by_object_ap_id(ap_id)
206 |> restrict_deactivated_users()
210 def get_create_by_object_ap_id(_), do: nil
212 def create_by_object_ap_id_with_object(ap_id) when is_binary(ap_id) do
214 activity in Activity,
217 "coalesce((?)->'object'->>'id', (?)->>'object') = ?",
222 where: fragment("(?)->>'type' = 'Create'", activity.data),
223 inner_join: o in Object,
226 "(?->>'id') = COALESCE(?->'object'->>'id', ?->>'object')",
235 def create_by_object_ap_id_with_object(_), do: nil
237 def get_create_by_object_ap_id_with_object(ap_id) when is_binary(ap_id) do
239 |> create_by_object_ap_id_with_object()
243 def get_create_by_object_ap_id_with_object(_), do: nil
245 defp get_in_reply_to_activity_from_object(%Object{data: %{"inReplyTo" => ap_id}}) do
246 get_create_by_object_ap_id_with_object(ap_id)
249 defp get_in_reply_to_activity_from_object(_), do: nil
251 def get_in_reply_to_activity(%Activity{data: %{"object" => object}}) do
252 get_in_reply_to_activity_from_object(Object.normalize(object))
255 def normalize(obj) when is_map(obj), do: get_by_ap_id_with_object(obj["id"])
256 def normalize(ap_id) when is_binary(ap_id), do: get_by_ap_id_with_object(ap_id)
257 def normalize(_), do: nil
259 def delete_by_ap_id(id) when is_binary(id) do
265 %{data: %{"type" => "Create", "object" => ap_id}} when is_binary(ap_id) -> ap_id == id
266 %{data: %{"type" => "Create", "object" => %{"id" => ap_id}}} -> ap_id == id
271 def delete_by_ap_id(_), do: nil
273 for {ap_type, type} <- @mastodon_notification_types do
274 def mastodon_notification_type(%Activity{data: %{"type" => unquote(ap_type)}}),
278 def mastodon_notification_type(%Activity{}), do: nil
280 def from_mastodon_notification_type(type) do
281 Map.get(@mastodon_to_ap_notification_types, type)
284 def all_by_actor_and_id(actor, status_ids \\ [])
285 def all_by_actor_and_id(_actor, []), do: []
287 def all_by_actor_and_id(actor, status_ids) do
289 |> where([s], s.id in ^status_ids)
290 |> where([s], s.actor == ^actor)
294 def follow_requests_for_actor(%Pleroma.User{ap_id: ap_id}) do
299 "? ->> 'type' = 'Follow'",
304 "? ->> 'state' = 'pending'",
309 "coalesce((?)->'object'->>'id', (?)->>'object') = ?",
317 @spec query_by_actor(actor()) :: Ecto.Query.t()
318 def query_by_actor(actor) do
319 from(a in Activity, where: a.actor == ^actor)
322 def restrict_deactivated_users(query) do
323 from(activity in query,
326 "? not in (SELECT ap_id FROM users WHERE info->'deactivated' @> 'true')",