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_joined_object(query) do
64 join(query, :inner, [activity], o in Object,
67 "(?->>'id') = COALESCE(?->'object'->>'id', ?->>'object')",
76 def with_preloaded_object(query) do
78 |> has_named_binding?(:object)
79 |> if(do: query, else: with_joined_object(query))
80 |> preload([activity, object: object], object: object)
83 def with_preloaded_bookmark(query, %User{} = user) do
85 left_join: b in Bookmark,
86 on: b.user_id == ^user.id and b.activity_id == a.id,
87 preload: [bookmark: b]
91 def with_preloaded_bookmark(query, _), do: query
93 def get_by_ap_id(ap_id) do
97 where: fragment("(?)->>'id' = ?", activity.data, ^to_string(ap_id))
102 def get_bookmark(%Activity{} = activity, %User{} = user) do
103 if Ecto.assoc_loaded?(activity.bookmark) do
106 Bookmark.get(user.id, activity.id)
110 def get_bookmark(_, _), do: nil
112 def change(struct, params \\ %{}) do
114 |> cast(params, [:data, :recipients])
115 |> validate_required([:data])
116 |> unique_constraint(:ap_id, name: :activities_unique_apid_index)
119 def get_by_ap_id_with_object(ap_id) do
122 activity in Activity,
123 where: fragment("(?)->>'id' = ?", activity.data, ^to_string(ap_id)),
124 left_join: o in Object,
127 "(?->>'id') = COALESCE(?->'object'->>'id', ?->>'object')",
139 |> where([a], a.id == ^id)
140 |> restrict_deactivated_users()
144 def get_by_id_with_object(id) do
145 from(activity in Activity,
146 where: activity.id == ^id,
147 inner_join: o in Object,
150 "(?->>'id') = COALESCE(?->'object'->>'id', ?->>'object')",
160 def by_object_ap_id(ap_id) do
162 activity in Activity,
165 "coalesce((?)->'object'->>'id', (?)->>'object') = ?",
173 def create_by_object_ap_id(ap_ids) when is_list(ap_ids) do
175 activity in Activity,
178 "coalesce((?)->'object'->>'id', (?)->>'object') = ANY(?)",
183 where: fragment("(?)->>'type' = 'Create'", activity.data)
187 def create_by_object_ap_id(ap_id) when is_binary(ap_id) do
189 activity in Activity,
192 "coalesce((?)->'object'->>'id', (?)->>'object') = ?",
197 where: fragment("(?)->>'type' = 'Create'", activity.data)
201 def create_by_object_ap_id(_), do: nil
203 def get_all_create_by_object_ap_id(ap_id) do
204 Repo.all(create_by_object_ap_id(ap_id))
207 def get_create_by_object_ap_id(ap_id) when is_binary(ap_id) do
208 create_by_object_ap_id(ap_id)
209 |> restrict_deactivated_users()
213 def get_create_by_object_ap_id(_), do: nil
215 def create_by_object_ap_id_with_object(ap_id) when is_binary(ap_id) do
217 activity in Activity,
220 "coalesce((?)->'object'->>'id', (?)->>'object') = ?",
225 where: fragment("(?)->>'type' = 'Create'", activity.data),
226 inner_join: o in Object,
229 "(?->>'id') = COALESCE(?->'object'->>'id', ?->>'object')",
238 def create_by_object_ap_id_with_object(_), do: nil
240 def get_create_by_object_ap_id_with_object(ap_id) when is_binary(ap_id) do
242 |> create_by_object_ap_id_with_object()
246 def get_create_by_object_ap_id_with_object(_), do: nil
248 defp get_in_reply_to_activity_from_object(%Object{data: %{"inReplyTo" => ap_id}}) do
249 get_create_by_object_ap_id_with_object(ap_id)
252 defp get_in_reply_to_activity_from_object(_), do: nil
254 def get_in_reply_to_activity(%Activity{data: %{"object" => object}}) do
255 get_in_reply_to_activity_from_object(Object.normalize(object))
258 def normalize(obj) when is_map(obj), do: get_by_ap_id_with_object(obj["id"])
259 def normalize(ap_id) when is_binary(ap_id), do: get_by_ap_id_with_object(ap_id)
260 def normalize(_), do: nil
262 def delete_by_ap_id(id) when is_binary(id) do
268 %{data: %{"type" => "Create", "object" => ap_id}} when is_binary(ap_id) -> ap_id == id
269 %{data: %{"type" => "Create", "object" => %{"id" => ap_id}}} -> ap_id == id
274 def delete_by_ap_id(_), do: nil
276 for {ap_type, type} <- @mastodon_notification_types do
277 def mastodon_notification_type(%Activity{data: %{"type" => unquote(ap_type)}}),
281 def mastodon_notification_type(%Activity{}), do: nil
283 def from_mastodon_notification_type(type) do
284 Map.get(@mastodon_to_ap_notification_types, type)
287 def all_by_actor_and_id(actor, status_ids \\ [])
288 def all_by_actor_and_id(_actor, []), do: []
290 def all_by_actor_and_id(actor, status_ids) do
292 |> where([s], s.id in ^status_ids)
293 |> where([s], s.actor == ^actor)
297 def follow_requests_for_actor(%Pleroma.User{ap_id: ap_id}) do
302 "? ->> 'type' = 'Follow'",
307 "? ->> 'state' = 'pending'",
312 "coalesce((?)->'object'->>'id', (?)->>'object') = ?",
320 @spec query_by_actor(actor()) :: Ecto.Query.t()
321 def query_by_actor(actor) do
322 from(a in Activity, where: a.actor == ^actor)
325 def restrict_deactivated_users(query) do
326 from(activity in query,
329 "? not in (SELECT ap_id FROM users WHERE info->'deactivated' @> 'true')",