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.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
29 where: fragment("(?)->>'actor' = ?", activity.data, ^actor)
33 @spec by_author(query, String.t()) :: query
34 def by_author(query \\ Activity, %User{ap_id: ap_id}) do
35 from(a in query, where: a.actor == ^ap_id)
38 @spec by_object_id(query, String.t() | [String.t()]) :: query
39 def by_object_id(query \\ Activity, object_id)
41 def by_object_id(query, object_ids) when is_list(object_ids) do
46 "coalesce((?)->'object'->>'id', (?)->>'object') = ANY(?)",
54 def by_object_id(query, object_id) when is_binary(object_id) do
55 from(activity in query,
58 "coalesce((?)->'object'->>'id', (?)->>'object') = ?",
66 @spec by_object_in_reply_to_id(query, String.t(), keyword()) :: query
67 def by_object_in_reply_to_id(query, in_reply_to_id, opts \\ []) do
69 if opts[:skip_preloading] do
70 Activity.with_joined_object(query)
72 Activity.with_preloaded_object(query)
77 [activity, object: o],
78 fragment("(?)->>'inReplyTo' = ?", o.data, ^to_string(in_reply_to_id))
82 @spec by_type(query, String.t()) :: query
83 def by_type(query \\ Activity, activity_type) do
86 where: fragment("(?)->>'type' = ?", activity.data, ^activity_type)
90 @spec exclude_type(query, String.t()) :: query
91 def exclude_type(query \\ Activity, activity_type) do
94 where: fragment("(?)->>'type' != ?", activity.data, ^activity_type)
98 def exclude_authors(query \\ Activity, actors) do
99 from(activity in query, where: activity.actor not in ^actors)