1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2020 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_ap_id(query, String.t()) :: query
18 def by_ap_id(query \\ Activity, ap_id) do
21 where: fragment("(?)->>'id' = ?", activity.data, ^to_string(ap_id))
25 @spec by_actor(query, String.t()) :: query
26 def by_actor(query \\ Activity, actor) do
27 from(a in query, where: a.actor == ^actor)
30 @spec by_author(query, User.t()) :: query
31 def by_author(query \\ Activity, %User{ap_id: ap_id}) do
32 from(a in query, where: a.actor == ^ap_id)
35 def find_by_object_ap_id(activities, object_ap_id) do
38 &(object_ap_id in [is_map(&1.data["object"]) && &1.data["object"]["id"], &1.data["object"]])
42 @spec by_object_id(query, String.t() | [String.t()]) :: query
43 def by_object_id(query \\ Activity, object_id)
45 def by_object_id(query, object_ids) when is_list(object_ids) do
50 "coalesce((?)->'object'->>'id', (?)->>'object') = ANY(?)",
58 def by_object_id(query, object_id) when is_binary(object_id) do
59 from(activity in query,
62 "coalesce((?)->'object'->>'id', (?)->>'object') = ?",
70 @spec by_object_in_reply_to_id(query, String.t(), keyword()) :: query
71 def by_object_in_reply_to_id(query, in_reply_to_id, opts \\ []) do
73 if opts[:skip_preloading] do
74 Activity.with_joined_object(query)
76 Activity.with_preloaded_object(query)
81 [activity, object: o],
82 fragment("(?)->>'inReplyTo' = ?", o.data, ^to_string(in_reply_to_id))
86 @spec by_type(query, String.t()) :: query
87 def by_type(query \\ Activity, activity_type) do
90 where: fragment("(?)->>'type' = ?", activity.data, ^activity_type)
94 @spec exclude_type(query, String.t()) :: query
95 def exclude_type(query \\ Activity, activity_type) do
98 where: fragment("(?)->>'type' != ?", activity.data, ^activity_type)
102 def exclude_authors(query \\ Activity, actors) do
103 from(activity in query, where: activity.actor not in ^actors)