2019-08-27 13:21:03 +00:00
|
|
|
# Pleroma: A lightweight social networking server
|
2021-01-13 06:49:20 +00:00
|
|
|
# Copyright © 2017-2021 Pleroma Authors <https://pleroma.social/>
|
2019-08-27 13:21:03 +00:00
|
|
|
# SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
|
|
|
defmodule Pleroma.Activity.Queries do
|
|
|
|
@moduledoc """
|
|
|
|
Contains queries for Activity.
|
|
|
|
"""
|
|
|
|
|
2020-01-22 18:10:17 +00:00
|
|
|
import Ecto.Query, only: [from: 2, where: 3]
|
2019-08-27 13:21:03 +00:00
|
|
|
|
|
|
|
@type query :: Ecto.Queryable.t() | Activity.t()
|
|
|
|
|
|
|
|
alias Pleroma.Activity
|
2020-01-20 18:47:44 +00:00
|
|
|
alias Pleroma.User
|
2019-08-27 13:21:03 +00:00
|
|
|
|
2021-02-03 13:09:28 +00:00
|
|
|
@spec by_id(query(), String.t()) :: query()
|
|
|
|
def by_id(query \\ Activity, id) do
|
|
|
|
from(a in query, where: a.id == ^id)
|
|
|
|
end
|
|
|
|
|
2019-09-03 14:58:30 +00:00
|
|
|
@spec by_ap_id(query, String.t()) :: query
|
|
|
|
def by_ap_id(query \\ Activity, ap_id) do
|
|
|
|
from(
|
|
|
|
activity in query,
|
|
|
|
where: fragment("(?)->>'id' = ?", activity.data, ^to_string(ap_id))
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2019-08-27 13:21:03 +00:00
|
|
|
@spec by_actor(query, String.t()) :: query
|
|
|
|
def by_actor(query \\ Activity, actor) do
|
2020-05-26 15:46:16 +00:00
|
|
|
from(a in query, where: a.actor == ^actor)
|
2019-08-27 13:21:03 +00:00
|
|
|
end
|
|
|
|
|
2020-02-25 14:34:56 +00:00
|
|
|
@spec by_author(query, User.t()) :: query
|
2020-01-20 18:47:44 +00:00
|
|
|
def by_author(query \\ Activity, %User{ap_id: ap_id}) do
|
|
|
|
from(a in query, where: a.actor == ^ap_id)
|
|
|
|
end
|
|
|
|
|
2020-03-24 19:14:26 +00:00
|
|
|
def find_by_object_ap_id(activities, object_ap_id) do
|
|
|
|
Enum.find(
|
|
|
|
activities,
|
|
|
|
&(object_ap_id in [is_map(&1.data["object"]) && &1.data["object"]["id"], &1.data["object"]])
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2019-09-03 14:58:30 +00:00
|
|
|
@spec by_object_id(query, String.t() | [String.t()]) :: query
|
|
|
|
def by_object_id(query \\ Activity, object_id)
|
|
|
|
|
|
|
|
def by_object_id(query, object_ids) when is_list(object_ids) do
|
|
|
|
from(
|
|
|
|
activity in query,
|
|
|
|
where:
|
|
|
|
fragment(
|
|
|
|
"coalesce((?)->'object'->>'id', (?)->>'object') = ANY(?)",
|
|
|
|
activity.data,
|
|
|
|
activity.data,
|
|
|
|
^object_ids
|
|
|
|
)
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
def by_object_id(query, object_id) when is_binary(object_id) do
|
2019-08-27 13:21:03 +00:00
|
|
|
from(activity in query,
|
|
|
|
where:
|
|
|
|
fragment(
|
|
|
|
"coalesce((?)->'object'->>'id', (?)->>'object') = ?",
|
|
|
|
activity.data,
|
|
|
|
activity.data,
|
|
|
|
^object_id
|
|
|
|
)
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2020-02-20 11:48:46 +00:00
|
|
|
@spec by_object_in_reply_to_id(query, String.t(), keyword()) :: query
|
2020-01-22 18:10:17 +00:00
|
|
|
def by_object_in_reply_to_id(query, in_reply_to_id, opts \\ []) do
|
|
|
|
query =
|
|
|
|
if opts[:skip_preloading] do
|
|
|
|
Activity.with_joined_object(query)
|
|
|
|
else
|
|
|
|
Activity.with_preloaded_object(query)
|
|
|
|
end
|
|
|
|
|
|
|
|
where(
|
|
|
|
query,
|
|
|
|
[activity, object: o],
|
|
|
|
fragment("(?)->>'inReplyTo' = ?", o.data, ^to_string(in_reply_to_id))
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2019-08-27 13:21:03 +00:00
|
|
|
@spec by_type(query, String.t()) :: query
|
|
|
|
def by_type(query \\ Activity, activity_type) do
|
|
|
|
from(
|
|
|
|
activity in query,
|
|
|
|
where: fragment("(?)->>'type' = ?", activity.data, ^activity_type)
|
|
|
|
)
|
|
|
|
end
|
2019-12-04 23:50:38 +00:00
|
|
|
|
|
|
|
@spec exclude_type(query, String.t()) :: query
|
|
|
|
def exclude_type(query \\ Activity, activity_type) do
|
|
|
|
from(
|
|
|
|
activity in query,
|
|
|
|
where: fragment("(?)->>'type' != ?", activity.data, ^activity_type)
|
|
|
|
)
|
|
|
|
end
|
2020-01-20 18:47:44 +00:00
|
|
|
|
|
|
|
def exclude_authors(query \\ Activity, actors) do
|
|
|
|
from(activity in query, where: activity.actor not in ^actors)
|
|
|
|
end
|
2019-08-27 13:21:03 +00:00
|
|
|
end
|