1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2022 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Pleroma.Activity.Queries do
7 Contains queries for Activity.
10 import Ecto.Query, only: [from: 2, where: 3]
12 @type query :: Ecto.Queryable.t() | Activity.t()
14 alias Pleroma.Activity
17 @spec by_id(query(), String.t()) :: query()
18 def by_id(query \\ Activity, id) do
19 from(a in query, where: a.id == ^id)
22 @spec by_ap_id(query, String.t()) :: query
23 def by_ap_id(query \\ Activity, ap_id) do
26 where: fragment("(?)->>'id' = ?", activity.data, ^to_string(ap_id))
30 @spec by_actor(query, String.t()) :: query
31 def by_actor(query \\ Activity, actor) do
32 from(a in query, where: a.actor == ^actor)
35 @spec by_author(query, User.t()) :: query
36 def by_author(query \\ Activity, %User{ap_id: ap_id}) do
37 from(a in query, where: a.actor == ^ap_id)
40 def find_by_object_ap_id(activities, object_ap_id) do
43 &(object_ap_id in [is_map(&1.data["object"]) && &1.data["object"]["id"], &1.data["object"]])
47 @spec by_object_id(query, String.t() | [String.t()]) :: query
48 def by_object_id(query \\ Activity, object_id)
50 def by_object_id(query, object_ids) when is_list(object_ids) do
55 "associated_object_id((?)) = ANY(?)",
62 def by_object_id(query, object_id) when is_binary(object_id) do
63 from(activity in query,
66 "associated_object_id((?)) = ?",
73 @spec by_object_in_reply_to_id(query, String.t(), keyword()) :: query
74 def by_object_in_reply_to_id(query, in_reply_to_id, opts \\ []) do
76 if opts[:skip_preloading] do
77 Activity.with_joined_object(query)
79 Activity.with_preloaded_object(query)
84 [activity, object: o],
85 fragment("(?)->>'inReplyTo' = ?", o.data, ^to_string(in_reply_to_id))
89 @spec by_type(query, String.t()) :: query
90 def by_type(query \\ Activity, activity_type) do
93 where: fragment("(?)->>'type' = ?", activity.data, ^activity_type)
97 @spec exclude_type(query, String.t()) :: query
98 def exclude_type(query \\ Activity, activity_type) do
101 where: fragment("(?)->>'type' != ?", activity.data, ^activity_type)
105 def exclude_authors(query \\ Activity, actors) do
106 from(activity in query, where: activity.actor not in ^actors)