2018-10-11 17:37:39 +02:00
|
|
|
defmodule Mobilizon.Actors do
|
2018-05-18 09:56:21 +02:00
|
|
|
@moduledoc """
|
|
|
|
The Actors context.
|
|
|
|
"""
|
|
|
|
|
2019-09-08 01:49:56 +02:00
|
|
|
import Ecto.Query
|
2019-09-09 00:52:49 +02:00
|
|
|
import EctoEnum
|
2020-08-05 16:44:08 +02:00
|
|
|
import Geo.PostGIS, only: [st_dwithin_in_meters: 3]
|
|
|
|
import Mobilizon.Service.Guards
|
2018-12-14 11:23:36 +01:00
|
|
|
|
2019-09-08 01:49:56 +02:00
|
|
|
alias Ecto.Multi
|
2018-05-18 09:56:21 +02:00
|
|
|
|
2019-09-09 00:52:49 +02:00
|
|
|
alias Mobilizon.Actors.{Actor, Bot, Follower, Member}
|
2020-08-05 16:44:08 +02:00
|
|
|
alias Mobilizon.Addresses.Address
|
2021-09-10 11:36:05 +02:00
|
|
|
alias Mobilizon.Crypto
|
2020-12-15 17:17:42 +01:00
|
|
|
alias Mobilizon.Events.FeedToken
|
2021-12-15 12:58:31 +01:00
|
|
|
alias Mobilizon.Medias
|
2020-01-23 00:55:07 +01:00
|
|
|
alias Mobilizon.Service.Workers
|
2020-01-22 02:14:42 +01:00
|
|
|
alias Mobilizon.Storage.{Page, Repo}
|
2020-06-27 19:12:45 +02:00
|
|
|
alias Mobilizon.Users
|
2020-12-15 17:17:42 +01:00
|
|
|
alias Mobilizon.Users.User
|
2020-01-28 19:18:33 +01:00
|
|
|
|
2019-05-22 14:12:11 +02:00
|
|
|
require Logger
|
2018-05-18 09:56:21 +02:00
|
|
|
|
2019-09-09 00:52:49 +02:00
|
|
|
defenum(ActorType, :actor_type, [
|
|
|
|
:Person,
|
|
|
|
:Application,
|
|
|
|
:Group,
|
|
|
|
:Organization,
|
|
|
|
:Service
|
|
|
|
])
|
|
|
|
|
|
|
|
defenum(ActorOpenness, :actor_openness, [
|
|
|
|
:invite_only,
|
|
|
|
:moderated,
|
|
|
|
:open
|
|
|
|
])
|
|
|
|
|
|
|
|
defenum(ActorVisibility, :actor_visibility, [
|
|
|
|
:public,
|
|
|
|
:unlisted,
|
|
|
|
# Probably unused
|
|
|
|
:restricted,
|
|
|
|
:private
|
|
|
|
])
|
|
|
|
|
|
|
|
defenum(MemberRole, :member_role, [
|
2020-02-18 08:57:00 +01:00
|
|
|
:invited,
|
2019-09-09 00:52:49 +02:00
|
|
|
:not_approved,
|
|
|
|
:member,
|
|
|
|
:moderator,
|
|
|
|
:administrator,
|
2020-02-18 08:57:00 +01:00
|
|
|
:creator,
|
|
|
|
:rejected
|
2019-09-09 00:52:49 +02:00
|
|
|
])
|
|
|
|
|
2019-09-11 03:16:37 +02:00
|
|
|
@administrator_roles [:creator, :administrator]
|
2020-07-09 17:24:28 +02:00
|
|
|
@moderator_roles [:moderator] ++ @administrator_roles
|
|
|
|
@member_roles [:member] ++ @moderator_roles
|
2019-09-11 03:16:37 +02:00
|
|
|
|
2022-04-01 10:03:43 +02:00
|
|
|
@associations_to_preload [:organized_events, :followers, :followings, :user, :physical_address]
|
|
|
|
|
2018-05-18 09:56:21 +02:00
|
|
|
@doc """
|
2019-09-09 00:52:49 +02:00
|
|
|
Gets a single actor.
|
2018-05-18 09:56:21 +02:00
|
|
|
"""
|
2019-09-09 00:52:49 +02:00
|
|
|
@spec get_actor(integer | String.t()) :: Actor.t() | nil
|
2021-02-24 19:06:48 +01:00
|
|
|
def get_actor(nil), do: nil
|
2019-09-09 00:52:49 +02:00
|
|
|
def get_actor(id), do: Repo.get(Actor, id)
|
2018-05-18 09:56:21 +02:00
|
|
|
|
|
|
|
@doc """
|
|
|
|
Gets a single actor.
|
2019-09-11 03:16:37 +02:00
|
|
|
Raises `Ecto.NoResultsError` if the actor does not exist.
|
2019-09-09 00:52:49 +02:00
|
|
|
"""
|
|
|
|
@spec get_actor!(integer | String.t()) :: Actor.t()
|
|
|
|
def get_actor!(id), do: Repo.get!(Actor, id)
|
2018-05-18 09:56:21 +02:00
|
|
|
|
2019-09-09 00:52:49 +02:00
|
|
|
@doc """
|
|
|
|
Gets an actor with preloaded relations.
|
|
|
|
"""
|
2021-09-24 16:46:42 +02:00
|
|
|
@spec get_actor_with_preload(integer | String.t(), boolean) :: Actor.t() | nil
|
2020-06-11 19:13:21 +02:00
|
|
|
def get_actor_with_preload(id, include_suspended \\ false) do
|
2019-09-09 00:52:49 +02:00
|
|
|
id
|
2020-06-11 19:13:21 +02:00
|
|
|
|> actor_with_preload_query(include_suspended)
|
2019-09-09 00:52:49 +02:00
|
|
|
|> Repo.one()
|
|
|
|
end
|
2018-05-18 09:56:21 +02:00
|
|
|
|
2021-10-05 15:29:06 +02:00
|
|
|
@spec get_actor_with_preload!(integer | String.t()) :: Actor.t()
|
|
|
|
def get_actor_with_preload!(id) do
|
2021-09-24 16:46:42 +02:00
|
|
|
id
|
2021-10-05 15:29:06 +02:00
|
|
|
|> actor_with_preload_query(false)
|
2021-09-24 16:46:42 +02:00
|
|
|
|> Repo.one!()
|
|
|
|
end
|
|
|
|
|
2019-09-09 00:52:49 +02:00
|
|
|
@doc """
|
|
|
|
Gets a local actor with preloaded relations.
|
2018-05-18 09:56:21 +02:00
|
|
|
"""
|
2019-09-09 00:52:49 +02:00
|
|
|
@spec get_local_actor_with_preload(integer | String.t()) :: Actor.t() | nil
|
|
|
|
def get_local_actor_with_preload(id) do
|
|
|
|
id
|
|
|
|
|> actor_with_preload_query()
|
|
|
|
|> filter_local()
|
|
|
|
|> Repo.one()
|
2018-05-18 09:56:21 +02:00
|
|
|
end
|
|
|
|
|
2019-09-09 00:52:49 +02:00
|
|
|
@doc """
|
|
|
|
Gets an actor by its URL (ActivityPub ID). The `:preload` option allows to
|
|
|
|
preload the followers relation.
|
|
|
|
"""
|
|
|
|
@spec get_actor_by_url(String.t(), boolean) ::
|
|
|
|
{:ok, Actor.t()} | {:error, :actor_not_found}
|
2019-10-25 17:43:37 +02:00
|
|
|
def get_actor_by_url(url, preload \\ false)
|
|
|
|
def get_actor_by_url(nil, _preload), do: {:error, :actor_not_found}
|
|
|
|
|
|
|
|
def get_actor_by_url(url, preload) do
|
2019-09-09 00:52:49 +02:00
|
|
|
case Repo.get_by(Actor, url: url) do
|
|
|
|
nil ->
|
|
|
|
{:error, :actor_not_found}
|
|
|
|
|
|
|
|
actor ->
|
|
|
|
{:ok, preload_followers(actor, preload)}
|
|
|
|
end
|
2019-08-14 17:45:11 +02:00
|
|
|
end
|
|
|
|
|
2020-07-09 17:24:28 +02:00
|
|
|
@doc """
|
|
|
|
New function to replace `Mobilizon.Actors.get_actor_by_url/1` with
|
|
|
|
better signature
|
|
|
|
"""
|
2021-10-05 15:29:06 +02:00
|
|
|
@spec get_actor_by_url_2(String.t()) :: Actor.t() | nil
|
|
|
|
def get_actor_by_url_2(url) do
|
|
|
|
Repo.get_by(Actor, url: url)
|
2020-07-09 17:24:28 +02:00
|
|
|
end
|
|
|
|
|
2019-09-09 00:52:49 +02:00
|
|
|
@doc """
|
|
|
|
Gets an actor by its URL (ActivityPub ID). The `:preload` option allows to
|
|
|
|
preload the followers relation.
|
|
|
|
Raises `Ecto.NoResultsError` if the actor does not exist.
|
|
|
|
"""
|
|
|
|
@spec get_actor_by_url!(String.t(), boolean) :: Actor.t()
|
|
|
|
def get_actor_by_url!(url, preload \\ false) do
|
|
|
|
Actor
|
|
|
|
|> Repo.get_by!(url: url)
|
|
|
|
|> preload_followers(preload)
|
2019-01-21 15:08:22 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
@doc """
|
2019-09-09 00:52:49 +02:00
|
|
|
Gets an actor by name.
|
2019-01-21 15:08:22 +01:00
|
|
|
"""
|
2022-04-18 14:38:57 +02:00
|
|
|
@spec get_actor_by_name(String.t(), atom() | nil) :: Actor.t() | nil
|
2019-09-09 00:52:49 +02:00
|
|
|
def get_actor_by_name(name, type \\ nil) do
|
2021-09-24 16:46:42 +02:00
|
|
|
Actor
|
2019-09-09 00:52:49 +02:00
|
|
|
|> filter_by_type(type)
|
2021-04-09 12:12:06 +02:00
|
|
|
|> filter_by_name(name |> String.trim() |> String.trim_leading("@") |> String.split("@"))
|
2019-01-21 15:08:22 +01:00
|
|
|
|> Repo.one()
|
|
|
|
end
|
|
|
|
|
|
|
|
@doc """
|
2019-09-09 00:52:49 +02:00
|
|
|
Gets a local actor by its preferred username.
|
2019-01-21 15:08:22 +01:00
|
|
|
"""
|
2019-09-09 00:52:49 +02:00
|
|
|
@spec get_local_actor_by_name(String.t()) :: Actor.t() | nil
|
|
|
|
def get_local_actor_by_name(name) do
|
2021-09-24 16:46:42 +02:00
|
|
|
Actor
|
2019-09-09 00:52:49 +02:00
|
|
|
|> filter_by_name([name])
|
2019-01-21 15:08:22 +01:00
|
|
|
|> Repo.one()
|
2018-05-18 09:56:21 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
@doc """
|
2019-09-09 00:52:49 +02:00
|
|
|
Gets a local actor by its preferred username and preloaded relations
|
|
|
|
(organized events, followers and followings).
|
|
|
|
"""
|
|
|
|
@spec get_local_actor_by_name_with_preload(String.t()) :: Actor.t() | nil
|
|
|
|
def get_local_actor_by_name_with_preload(name) do
|
|
|
|
name
|
|
|
|
|> get_local_actor_by_name()
|
2022-04-01 10:03:43 +02:00
|
|
|
|> Repo.preload(@associations_to_preload)
|
2019-09-09 00:52:49 +02:00
|
|
|
end
|
2018-05-18 09:56:21 +02:00
|
|
|
|
2019-09-09 00:52:49 +02:00
|
|
|
@doc """
|
|
|
|
Gets an actor by name and preloads the organized events.
|
|
|
|
"""
|
|
|
|
@spec get_actor_by_name_with_preload(String.t(), atom() | nil) :: Actor.t() | nil
|
|
|
|
def get_actor_by_name_with_preload(name, type \\ nil) do
|
|
|
|
name
|
|
|
|
|> get_actor_by_name(type)
|
2022-04-01 10:03:43 +02:00
|
|
|
|> Repo.preload(@associations_to_preload)
|
2019-09-09 00:52:49 +02:00
|
|
|
end
|
2018-05-18 09:56:21 +02:00
|
|
|
|
2019-09-09 00:52:49 +02:00
|
|
|
@doc """
|
|
|
|
Creates an actor.
|
|
|
|
"""
|
|
|
|
@spec create_actor(map) :: {:ok, Actor.t()} | {:error, Ecto.Changeset.t()}
|
2018-05-18 09:56:21 +02:00
|
|
|
def create_actor(attrs \\ %{}) do
|
2020-07-09 17:24:28 +02:00
|
|
|
type = Map.get(attrs, :type, :Person)
|
|
|
|
|
|
|
|
case type do
|
|
|
|
:Person ->
|
|
|
|
%Actor{}
|
|
|
|
|> Actor.changeset(attrs)
|
|
|
|
|> Repo.insert()
|
|
|
|
|
|
|
|
:Group ->
|
|
|
|
create_group(attrs)
|
|
|
|
end
|
2018-05-18 09:56:21 +02:00
|
|
|
end
|
|
|
|
|
2019-09-11 03:16:37 +02:00
|
|
|
@doc """
|
|
|
|
Creates a new person actor.
|
|
|
|
"""
|
2021-09-24 16:46:42 +02:00
|
|
|
@spec new_person(map, default_actor :: boolean()) ::
|
|
|
|
{:ok, Actor.t()} | {:error, Ecto.Changeset.t()}
|
2020-06-27 19:12:45 +02:00
|
|
|
def new_person(args, default_actor \\ false) do
|
2019-09-11 03:16:37 +02:00
|
|
|
args = Map.put(args, :keys, Crypto.generate_rsa_2048_private_key())
|
|
|
|
|
2020-12-15 17:17:42 +01:00
|
|
|
multi =
|
|
|
|
Multi.new()
|
|
|
|
|> Multi.insert(:person, Actor.registration_changeset(%Actor{}, args))
|
|
|
|
|> Multi.insert(:token, fn %{person: person} ->
|
|
|
|
FeedToken.changeset(%FeedToken{}, %{
|
|
|
|
user_id: args.user_id,
|
|
|
|
actor_id: person.id,
|
|
|
|
token: Ecto.UUID.generate()
|
|
|
|
})
|
|
|
|
end)
|
|
|
|
|
|
|
|
multi =
|
2020-06-27 19:12:45 +02:00
|
|
|
if default_actor do
|
|
|
|
user = Users.get_user!(args.user_id)
|
2020-12-15 17:17:42 +01:00
|
|
|
|
|
|
|
Multi.update(multi, :user, fn %{person: person} ->
|
|
|
|
User.changeset(user, %{default_actor_id: person.id})
|
|
|
|
end)
|
|
|
|
else
|
|
|
|
multi
|
2020-06-27 19:12:45 +02:00
|
|
|
end
|
2019-09-11 03:16:37 +02:00
|
|
|
|
2020-12-15 17:17:42 +01:00
|
|
|
case Repo.transaction(multi) do
|
|
|
|
{:ok, %{person: %Actor{} = person}} ->
|
|
|
|
{:ok, person}
|
|
|
|
|
|
|
|
{:error, _step, err, _} ->
|
|
|
|
Logger.error("Error while creating a new person")
|
|
|
|
{:error, err}
|
2019-09-11 03:16:37 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-05-18 09:56:21 +02:00
|
|
|
@doc """
|
2019-09-09 00:52:49 +02:00
|
|
|
Updates an actor.
|
2018-05-18 09:56:21 +02:00
|
|
|
"""
|
2019-09-09 00:52:49 +02:00
|
|
|
@spec update_actor(Actor.t(), map) :: {:ok, Actor.t()} | {:error, Ecto.Changeset.t()}
|
2018-05-18 09:56:21 +02:00
|
|
|
def update_actor(%Actor{} = actor, attrs) do
|
|
|
|
actor
|
2022-04-01 10:03:43 +02:00
|
|
|
|> Repo.preload(@associations_to_preload)
|
2019-09-04 18:24:31 +02:00
|
|
|
|> Actor.update_changeset(attrs)
|
2019-06-05 18:29:39 +02:00
|
|
|
|> delete_files_if_media_changed()
|
2018-05-18 09:56:21 +02:00
|
|
|
|> Repo.update()
|
|
|
|
end
|
|
|
|
|
|
|
|
@doc """
|
2019-09-09 00:52:49 +02:00
|
|
|
Upserts an actor.
|
|
|
|
Conflicts on actor's URL/AP ID, replaces keys, avatar and banner, name and summary.
|
|
|
|
"""
|
|
|
|
@spec upsert_actor(map, boolean) :: {:ok, Actor.t()} | {:error, Ecto.Changeset.t()}
|
2020-06-10 14:28:27 +02:00
|
|
|
def upsert_actor(
|
2020-08-27 11:53:24 +02:00
|
|
|
data,
|
2020-06-10 14:28:27 +02:00
|
|
|
preload \\ false
|
|
|
|
) do
|
2019-09-09 00:52:49 +02:00
|
|
|
insert =
|
|
|
|
data
|
|
|
|
|> Actor.remote_actor_creation_changeset()
|
|
|
|
|> Repo.insert(
|
2020-08-27 11:53:24 +02:00
|
|
|
on_conflict: {:replace_all_except, [:id, :url, :preferred_username, :domain]},
|
2019-09-09 00:52:49 +02:00
|
|
|
conflict_target: [:url]
|
|
|
|
)
|
2018-05-18 09:56:21 +02:00
|
|
|
|
2019-09-09 00:52:49 +02:00
|
|
|
case insert do
|
|
|
|
{:ok, actor} ->
|
2022-04-01 10:03:43 +02:00
|
|
|
actor = if preload, do: Repo.preload(actor, @associations_to_preload), else: actor
|
2018-05-18 09:56:21 +02:00
|
|
|
|
2019-09-09 00:52:49 +02:00
|
|
|
{:ok, actor}
|
2018-05-18 09:56:21 +02:00
|
|
|
|
2019-09-09 00:52:49 +02:00
|
|
|
error ->
|
|
|
|
Logger.debug(inspect(error))
|
2018-05-18 09:56:21 +02:00
|
|
|
|
2019-09-09 00:52:49 +02:00
|
|
|
{:error, error}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-08-27 11:53:24 +02:00
|
|
|
@delete_actor_default_options [reserve_username: true, suspension: false]
|
2020-06-11 19:13:21 +02:00
|
|
|
|
2021-09-28 19:40:37 +02:00
|
|
|
@spec delete_actor(Actor.t(), Keyword.t()) :: {:error, Ecto.Changeset.t()} | {:ok, Oban.Job.t()}
|
2020-06-11 19:13:21 +02:00
|
|
|
def delete_actor(%Actor{} = actor, options \\ @delete_actor_default_options) do
|
|
|
|
delete_actor_options = Keyword.merge(@delete_actor_default_options, options)
|
|
|
|
|
|
|
|
Workers.Background.enqueue("delete_actor", %{
|
|
|
|
"actor_id" => actor.id,
|
2020-08-27 11:53:24 +02:00
|
|
|
"author_id" => Keyword.get(delete_actor_options, :author_id),
|
|
|
|
"reserve_username" => Keyword.get(delete_actor_options, :reserve_username, true),
|
|
|
|
"suspension" => Keyword.get(delete_actor_options, :suspension, false)
|
2020-06-11 19:13:21 +02:00
|
|
|
})
|
2019-12-03 11:29:51 +01:00
|
|
|
end
|
|
|
|
|
2020-02-14 17:56:36 +01:00
|
|
|
@spec actor_key_rotation(Actor.t()) :: {:ok, Actor.t()} | {:error, Ecto.Changeset.t()}
|
|
|
|
def actor_key_rotation(%Actor{} = actor) do
|
|
|
|
actor
|
|
|
|
|> Actor.changeset(%{keys: Crypto.generate_rsa_2048_private_key()})
|
|
|
|
|> Repo.update()
|
|
|
|
end
|
|
|
|
|
2019-03-01 17:11:28 +01:00
|
|
|
@doc """
|
2019-09-09 00:52:49 +02:00
|
|
|
Returns the list of actors.
|
2019-03-01 17:11:28 +01:00
|
|
|
"""
|
2020-08-27 11:53:24 +02:00
|
|
|
@spec list_actors(
|
|
|
|
atom(),
|
|
|
|
String.t(),
|
|
|
|
String.t(),
|
|
|
|
String.t(),
|
|
|
|
boolean,
|
|
|
|
boolean,
|
2021-09-24 16:46:42 +02:00
|
|
|
integer | nil,
|
|
|
|
integer | nil
|
2022-04-07 18:37:44 +02:00
|
|
|
) :: Page.t(Actor.t())
|
2020-06-11 19:13:21 +02:00
|
|
|
def list_actors(
|
|
|
|
type \\ :Person,
|
|
|
|
preferred_username \\ "",
|
|
|
|
name \\ "",
|
|
|
|
domain \\ "",
|
|
|
|
local \\ true,
|
|
|
|
suspended \\ false,
|
|
|
|
page \\ nil,
|
|
|
|
limit \\ nil
|
|
|
|
)
|
|
|
|
|
|
|
|
def list_actors(
|
|
|
|
:Person,
|
|
|
|
preferred_username,
|
|
|
|
name,
|
|
|
|
domain,
|
|
|
|
local,
|
|
|
|
suspended,
|
|
|
|
page,
|
|
|
|
limit
|
|
|
|
) do
|
|
|
|
person_query()
|
2020-08-27 11:53:24 +02:00
|
|
|
|> filter_actors(preferred_username, name, domain, local, suspended)
|
|
|
|
|> Page.build_page(page, limit)
|
|
|
|
end
|
|
|
|
|
|
|
|
def list_actors(
|
|
|
|
:Group,
|
|
|
|
preferred_username,
|
|
|
|
name,
|
|
|
|
domain,
|
|
|
|
local,
|
|
|
|
suspended,
|
|
|
|
page,
|
|
|
|
limit
|
|
|
|
) do
|
|
|
|
group_query()
|
|
|
|
|> filter_actors(preferred_username, name, domain, local, suspended)
|
|
|
|
|> Page.build_page(page, limit)
|
|
|
|
end
|
|
|
|
|
2021-09-24 16:46:42 +02:00
|
|
|
@spec list_suspended_actors_to_purge(Keyword.t()) :: list(Actors.t())
|
|
|
|
def list_suspended_actors_to_purge(options) do
|
|
|
|
suspension_days = Keyword.get(options, :suspension, 30)
|
|
|
|
|
|
|
|
Actor
|
|
|
|
|> filter_suspended_days(suspension_days)
|
|
|
|
|> Repo.all()
|
|
|
|
end
|
|
|
|
|
|
|
|
@spec filter_actors(
|
|
|
|
Ecto.Queryable.t(),
|
|
|
|
String.t(),
|
|
|
|
String.t(),
|
|
|
|
String.t(),
|
|
|
|
boolean(),
|
|
|
|
boolean()
|
|
|
|
) ::
|
2020-08-27 11:53:24 +02:00
|
|
|
Ecto.Query.t()
|
|
|
|
defp filter_actors(
|
|
|
|
query,
|
|
|
|
preferred_username,
|
|
|
|
name,
|
|
|
|
domain,
|
|
|
|
local,
|
|
|
|
suspended
|
|
|
|
) do
|
|
|
|
query
|
2020-06-11 19:13:21 +02:00
|
|
|
|> filter_suspended(suspended)
|
|
|
|
|> filter_preferred_username(preferred_username)
|
|
|
|
|> filter_name(name)
|
|
|
|
|> filter_domain(domain)
|
|
|
|
|> filter_remote(local)
|
|
|
|
end
|
|
|
|
|
|
|
|
defp filter_preferred_username(query, ""), do: query
|
|
|
|
|
|
|
|
defp filter_preferred_username(query, preferred_username),
|
|
|
|
do: where(query, [a], ilike(a.preferred_username, ^"%#{preferred_username}%"))
|
|
|
|
|
|
|
|
defp filter_name(query, ""), do: query
|
|
|
|
|
|
|
|
defp filter_name(query, name),
|
|
|
|
do: where(query, [a], ilike(a.name, ^"%#{name}%"))
|
|
|
|
|
|
|
|
defp filter_domain(query, ""), do: query
|
|
|
|
|
|
|
|
defp filter_domain(query, domain),
|
|
|
|
do: where(query, [a], ilike(a.domain, ^"%#{domain}%"))
|
|
|
|
|
|
|
|
defp filter_remote(query, true), do: filter_local(query)
|
|
|
|
defp filter_remote(query, false), do: filter_external(query)
|
|
|
|
|
2021-09-24 16:46:42 +02:00
|
|
|
@spec filter_suspended(Ecto.Queryable.t(), boolean()) :: Ecto.Query.t()
|
2020-06-11 19:13:21 +02:00
|
|
|
defp filter_suspended(query, true), do: where(query, [a], a.suspended)
|
|
|
|
defp filter_suspended(query, false), do: where(query, [a], not a.suspended)
|
2019-03-01 17:11:28 +01:00
|
|
|
|
2021-09-24 16:46:42 +02:00
|
|
|
@spec filter_out_anonymous_actor_id(Ecto.Queryable.t(), integer() | String.t()) ::
|
|
|
|
Ecto.Query.t()
|
2021-05-02 19:27:34 +02:00
|
|
|
defp filter_out_anonymous_actor_id(query, anonymous_actor_id),
|
|
|
|
do: where(query, [a], a.id != ^anonymous_actor_id)
|
|
|
|
|
2021-09-24 16:46:42 +02:00
|
|
|
@spec filter_suspended_days(Ecto.Queryable.t(), integer()) :: Ecto.Query.t()
|
|
|
|
defp filter_suspended_days(query, suspended_days) do
|
|
|
|
expiration_date = DateTime.add(DateTime.utc_now(), suspended_days * 24 * -3600)
|
|
|
|
|
|
|
|
where(
|
|
|
|
query,
|
|
|
|
[a],
|
|
|
|
a.suspended and
|
|
|
|
a.updated_at > ^expiration_date
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2019-09-11 03:16:37 +02:00
|
|
|
@doc """
|
|
|
|
Returns the list of local actors by their username.
|
|
|
|
"""
|
|
|
|
@spec list_local_actor_by_username(String.t()) :: [Actor.t()]
|
|
|
|
def list_local_actor_by_username(username) do
|
|
|
|
username
|
|
|
|
|> actor_by_username_query()
|
|
|
|
|> filter_local()
|
|
|
|
|> Repo.all()
|
|
|
|
|> Repo.preload(:organized_events)
|
|
|
|
end
|
|
|
|
|
2022-04-22 12:02:14 +02:00
|
|
|
@spec last_group_created :: Actor.t() | nil
|
|
|
|
def last_group_created do
|
|
|
|
Actor
|
|
|
|
|> where(type: :Group, suspended: false)
|
|
|
|
|> order_by(desc: :inserted_at)
|
|
|
|
|> limit(1)
|
|
|
|
|> Repo.one()
|
|
|
|
end
|
|
|
|
|
2019-09-11 03:16:37 +02:00
|
|
|
@doc """
|
|
|
|
Builds a page struct for actors by their name or displayed name.
|
|
|
|
"""
|
2021-11-06 10:09:54 +01:00
|
|
|
@spec search_actors(
|
2020-12-17 15:25:58 +01:00
|
|
|
String.t(),
|
|
|
|
Keyword.t(),
|
2019-09-11 03:16:37 +02:00
|
|
|
integer | nil,
|
|
|
|
integer | nil
|
2022-04-07 18:37:44 +02:00
|
|
|
) :: Page.t(Actor.t())
|
2021-11-06 10:09:54 +01:00
|
|
|
def search_actors(
|
2020-12-17 15:25:58 +01:00
|
|
|
term,
|
|
|
|
options \\ [],
|
2020-08-05 16:44:08 +02:00
|
|
|
page \\ nil,
|
|
|
|
limit \\ nil
|
|
|
|
) do
|
2021-11-06 10:09:54 +01:00
|
|
|
term
|
|
|
|
|> build_actors_by_username_or_name_page_query(options)
|
2022-04-01 16:48:46 +02:00
|
|
|
|> maybe_exclude_stale_actors(Keyword.get(options, :exclude_stale_actors, false))
|
2021-11-06 10:09:54 +01:00
|
|
|
|> maybe_exclude_my_groups(
|
|
|
|
Keyword.get(options, :exclude_my_groups, false),
|
|
|
|
Keyword.get(options, :current_actor_id)
|
|
|
|
)
|
|
|
|
|> Page.build_page(page, limit)
|
|
|
|
end
|
|
|
|
|
|
|
|
defp maybe_exclude_my_groups(query, true, current_actor_id) when current_actor_id != nil do
|
|
|
|
query
|
|
|
|
|> join(:left, [a], m in Member, on: a.id == m.parent_id)
|
|
|
|
|> join(:left, [a], f in Follower, on: a.id == f.target_actor_id)
|
|
|
|
|> where([_a, ..., m, f], m.actor_id != ^current_actor_id and f.actor_id != ^current_actor_id)
|
|
|
|
end
|
|
|
|
|
|
|
|
defp maybe_exclude_my_groups(query, _, _), do: query
|
|
|
|
|
2022-04-01 16:48:46 +02:00
|
|
|
@spec maybe_exclude_stale_actors(Ecto.Queryable.t(), boolean()) :: Ecto.Query.t()
|
|
|
|
defp maybe_exclude_stale_actors(query, true) do
|
|
|
|
actor_stale_period =
|
|
|
|
Application.get_env(:mobilizon, :activitypub)[:stale_actor_search_exclusion_after]
|
|
|
|
|
|
|
|
stale_date = DateTime.utc_now() |> DateTime.add(-actor_stale_period)
|
|
|
|
where(query, [a], is_nil(a.domain) or a.last_refreshed_at >= ^stale_date)
|
|
|
|
end
|
|
|
|
|
|
|
|
defp maybe_exclude_stale_actors(query, false), do: query
|
|
|
|
|
2021-11-06 10:09:54 +01:00
|
|
|
@spec build_actors_by_username_or_name_page_query(
|
|
|
|
String.t(),
|
|
|
|
Keyword.t()
|
|
|
|
) :: Ecto.Query.t()
|
|
|
|
defp build_actors_by_username_or_name_page_query(
|
|
|
|
term,
|
|
|
|
options
|
|
|
|
) do
|
2021-05-02 19:27:34 +02:00
|
|
|
anonymous_actor_id = Mobilizon.Config.anonymous_actor_id()
|
2021-09-10 11:36:05 +02:00
|
|
|
query = from(a in Actor)
|
2021-05-02 19:27:34 +02:00
|
|
|
|
2021-09-10 11:36:05 +02:00
|
|
|
query
|
2021-11-06 10:09:54 +01:00
|
|
|
|> distinct([q], q.id)
|
2020-08-05 16:44:08 +02:00
|
|
|
|> actor_by_username_or_name_query(term)
|
2020-12-17 15:25:58 +01:00
|
|
|
|> actors_for_location(Keyword.get(options, :location), Keyword.get(options, :radius))
|
2021-11-06 10:09:54 +01:00
|
|
|
|> filter_by_type(Keyword.get(options, :actor_type, :Group))
|
2020-12-17 15:25:58 +01:00
|
|
|
|> filter_by_minimum_visibility(Keyword.get(options, :minimum_visibility, :public))
|
2020-08-27 11:53:24 +02:00
|
|
|
|> filter_suspended(false)
|
2021-05-02 19:27:34 +02:00
|
|
|
|> filter_out_anonymous_actor_id(anonymous_actor_id)
|
2019-09-11 03:16:37 +02:00
|
|
|
end
|
|
|
|
|
2018-12-14 17:41:55 +01:00
|
|
|
@doc """
|
2019-09-09 00:52:49 +02:00
|
|
|
Gets a group by its title.
|
2018-12-14 17:41:55 +01:00
|
|
|
"""
|
|
|
|
@spec get_group_by_title(String.t()) :: Actor.t() | nil
|
|
|
|
def get_group_by_title(title) do
|
2019-09-09 00:52:49 +02:00
|
|
|
group_query()
|
|
|
|
|> filter_by_name(String.split(title, "@"))
|
|
|
|
|> Repo.one()
|
2018-06-06 17:42:53 +02:00
|
|
|
end
|
|
|
|
|
2019-03-19 11:16:03 +01:00
|
|
|
@doc """
|
2019-09-11 03:16:37 +02:00
|
|
|
Gets a group by its actor id.
|
2019-03-19 11:16:03 +01:00
|
|
|
"""
|
2019-09-11 03:16:37 +02:00
|
|
|
@spec get_group_by_actor_id(integer | String.t()) ::
|
|
|
|
{:ok, Actor.t()} | {:error, :group_not_found}
|
|
|
|
def get_group_by_actor_id(actor_id) do
|
|
|
|
case Repo.get_by(Actor, id: actor_id, type: :Group) do
|
|
|
|
nil ->
|
|
|
|
{:error, :group_not_found}
|
2018-05-18 09:56:21 +02:00
|
|
|
|
2019-09-11 03:16:37 +02:00
|
|
|
actor ->
|
|
|
|
{:ok, actor}
|
|
|
|
end
|
2018-05-19 10:19:21 +02:00
|
|
|
end
|
|
|
|
|
2021-09-24 16:46:42 +02:00
|
|
|
@spec get_local_group_by_url(String.t()) :: Actor.t() | nil
|
2020-07-09 17:24:28 +02:00
|
|
|
def get_local_group_by_url(group_url) do
|
|
|
|
group_query()
|
|
|
|
|> where([q], q.url == ^group_url and is_nil(q.domain))
|
|
|
|
|> Repo.one()
|
|
|
|
end
|
|
|
|
|
2021-09-24 16:46:42 +02:00
|
|
|
@spec get_group_by_members_url(String.t()) :: Actor.t() | nil
|
2020-07-09 17:24:28 +02:00
|
|
|
def get_group_by_members_url(members_url) do
|
|
|
|
group_query()
|
|
|
|
|> where([q], q.members_url == ^members_url)
|
|
|
|
|> Repo.one()
|
|
|
|
end
|
|
|
|
|
2021-09-28 19:40:37 +02:00
|
|
|
@spec get_actor_by_followers_url(String.t()) :: Actor.t() | nil
|
2020-09-02 08:59:59 +02:00
|
|
|
def get_actor_by_followers_url(followers_url) do
|
|
|
|
Actor
|
2020-08-27 11:53:24 +02:00
|
|
|
|> where([q], q.followers_url == ^followers_url)
|
|
|
|
|> Repo.one()
|
|
|
|
end
|
|
|
|
|
2019-03-19 11:16:03 +01:00
|
|
|
@doc """
|
2019-09-09 00:52:49 +02:00
|
|
|
Creates a group.
|
2020-07-09 17:24:28 +02:00
|
|
|
|
|
|
|
If the group is local, creates an admin actor as well from `creator_actor_id`.
|
2019-03-19 11:16:03 +01:00
|
|
|
"""
|
2019-09-11 03:16:37 +02:00
|
|
|
@spec create_group(map) :: {:ok, Actor.t()} | {:error, Ecto.Changeset.t()}
|
2019-09-09 00:52:49 +02:00
|
|
|
def create_group(attrs \\ %{}) do
|
2021-09-10 11:36:05 +02:00
|
|
|
if Map.get(attrs, :local, true) do
|
|
|
|
multi =
|
|
|
|
Multi.new()
|
|
|
|
|> Multi.insert(:insert_group, Actor.group_creation_changeset(%Actor{}, attrs))
|
|
|
|
|> Multi.insert(:add_admin_member, fn %{insert_group: group} ->
|
|
|
|
Member.changeset(%Member{}, %{
|
|
|
|
parent_id: group.id,
|
|
|
|
actor_id: attrs.creator_actor_id,
|
|
|
|
role: :administrator
|
|
|
|
})
|
|
|
|
end)
|
|
|
|
|> Repo.transaction()
|
|
|
|
|
|
|
|
case multi do
|
|
|
|
{:ok, %{insert_group: %Actor{} = group, add_admin_member: %Member{} = _admin_member}} ->
|
|
|
|
{:ok, group}
|
|
|
|
|
2021-09-24 16:46:42 +02:00
|
|
|
{:error, _err, %Ecto.Changeset{} = err, _} ->
|
2021-09-10 11:36:05 +02:00
|
|
|
{:error, err}
|
2020-07-09 17:24:28 +02:00
|
|
|
end
|
|
|
|
else
|
|
|
|
%Actor{}
|
|
|
|
|> Actor.group_creation_changeset(attrs)
|
|
|
|
|> Repo.insert()
|
2020-02-18 08:57:00 +01:00
|
|
|
end
|
2018-05-18 09:56:21 +02:00
|
|
|
end
|
|
|
|
|
2020-09-30 10:42:19 +02:00
|
|
|
@doc """
|
|
|
|
Counts the local groups
|
|
|
|
"""
|
|
|
|
@spec count_local_groups :: integer()
|
|
|
|
def count_local_groups do
|
|
|
|
groups_query()
|
|
|
|
|> filter_local()
|
|
|
|
|> Repo.aggregate(:count)
|
|
|
|
end
|
|
|
|
|
|
|
|
@doc """
|
|
|
|
Counts all the groups
|
|
|
|
"""
|
|
|
|
@spec count_groups :: integer()
|
|
|
|
def count_groups do
|
|
|
|
groups_query()
|
|
|
|
|> Repo.aggregate(:count)
|
|
|
|
end
|
|
|
|
|
2020-08-12 16:05:34 +02:00
|
|
|
@doc """
|
|
|
|
Lists the groups.
|
|
|
|
"""
|
|
|
|
@spec list_groups_for_stream :: Enum.t()
|
|
|
|
def list_groups_for_stream do
|
|
|
|
groups_query()
|
|
|
|
|> Repo.stream()
|
|
|
|
end
|
|
|
|
|
2020-11-06 11:34:32 +01:00
|
|
|
@doc """
|
|
|
|
Lists the groups.
|
|
|
|
"""
|
2021-10-05 15:29:06 +02:00
|
|
|
@spec list_external_groups :: list(Actor.t())
|
|
|
|
def list_external_groups do
|
2020-11-06 11:34:32 +01:00
|
|
|
external_groups_query()
|
2021-10-05 15:29:06 +02:00
|
|
|
|> limit(100)
|
2021-04-21 18:57:23 +02:00
|
|
|
|> Repo.all()
|
2020-11-06 11:34:32 +01:00
|
|
|
end
|
|
|
|
|
2019-01-21 15:08:22 +01:00
|
|
|
@doc """
|
2019-09-11 03:16:37 +02:00
|
|
|
Returns the list of groups an actor is member of.
|
2019-01-21 15:08:22 +01:00
|
|
|
"""
|
2019-09-11 03:16:37 +02:00
|
|
|
@spec list_groups_member_of(Actor.t()) :: [Actor.t()]
|
|
|
|
def list_groups_member_of(%Actor{id: actor_id}) do
|
|
|
|
actor_id
|
|
|
|
|> groups_member_of_query()
|
|
|
|
|> Repo.all()
|
2019-01-21 15:08:22 +01:00
|
|
|
end
|
|
|
|
|
2020-02-18 08:57:00 +01:00
|
|
|
@doc """
|
|
|
|
Gets a single member.
|
|
|
|
"""
|
2021-09-24 16:46:42 +02:00
|
|
|
@spec get_member(integer | String.t()) :: Member.t() | nil
|
2020-02-18 08:57:00 +01:00
|
|
|
def get_member(id) do
|
|
|
|
Member
|
|
|
|
|> Repo.get(id)
|
|
|
|
|> Repo.preload([:actor, :parent, :invited_by])
|
|
|
|
end
|
|
|
|
|
2019-03-19 11:16:03 +01:00
|
|
|
@doc """
|
2019-09-11 03:16:37 +02:00
|
|
|
Gets a single member.
|
|
|
|
Raises `Ecto.NoResultsError` if the member does not exist.
|
2019-03-19 11:16:03 +01:00
|
|
|
"""
|
2019-09-11 03:16:37 +02:00
|
|
|
@spec get_member!(integer | String.t()) :: Member.t()
|
|
|
|
def get_member!(id), do: Repo.get!(Member, id)
|
2019-07-30 16:40:59 +02:00
|
|
|
|
2019-09-09 00:52:49 +02:00
|
|
|
@doc """
|
2019-09-11 03:16:37 +02:00
|
|
|
Gets a single member of an actor (for example a group).
|
2019-09-09 00:52:49 +02:00
|
|
|
"""
|
2021-09-24 16:46:42 +02:00
|
|
|
@spec get_member(actor_id :: integer | String.t(), parent_id :: integer | String.t()) ::
|
2019-09-11 03:16:37 +02:00
|
|
|
{:ok, Member.t()} | {:error, :member_not_found}
|
2019-09-09 00:52:49 +02:00
|
|
|
def get_member(actor_id, parent_id) do
|
|
|
|
case Repo.get_by(Member, actor_id: actor_id, parent_id: parent_id) do
|
2019-09-11 03:16:37 +02:00
|
|
|
nil ->
|
|
|
|
{:error, :member_not_found}
|
|
|
|
|
|
|
|
member ->
|
|
|
|
{:ok, member}
|
2019-09-09 00:52:49 +02:00
|
|
|
end
|
|
|
|
end
|
2018-05-18 09:56:21 +02:00
|
|
|
|
2020-02-18 08:57:00 +01:00
|
|
|
@spec get_member(integer | String.t(), integer | String.t(), list()) ::
|
|
|
|
{:ok, Member.t()} | {:error, :member_not_found}
|
|
|
|
def get_member(actor_id, parent_id, roles) do
|
|
|
|
case Member
|
|
|
|
|> where([m], m.actor_id == ^actor_id and m.parent_id == ^parent_id and m.role in ^roles)
|
|
|
|
|> Repo.one() do
|
|
|
|
nil ->
|
|
|
|
{:error, :member_not_found}
|
|
|
|
|
|
|
|
member ->
|
|
|
|
{:ok, member}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-07-20 18:22:18 +02:00
|
|
|
@doc """
|
|
|
|
Returns whether the `actor_id` is a confirmed member for the group `parent_id`
|
|
|
|
"""
|
2020-02-18 08:57:00 +01:00
|
|
|
@spec is_member?(integer | String.t(), integer | String.t()) :: boolean()
|
|
|
|
def is_member?(actor_id, parent_id) do
|
|
|
|
match?(
|
|
|
|
{:ok, %Member{}},
|
2020-07-09 17:24:28 +02:00
|
|
|
get_member(actor_id, parent_id, @member_roles)
|
2020-02-18 08:57:00 +01:00
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2021-07-20 18:22:18 +02:00
|
|
|
@doc """
|
|
|
|
Returns whether the `actor_id` is a moderator for the group `parent_id`
|
|
|
|
"""
|
2020-10-09 15:26:37 +02:00
|
|
|
@spec is_moderator?(integer | String.t(), integer | String.t()) :: boolean()
|
|
|
|
def is_moderator?(actor_id, parent_id) do
|
|
|
|
match?(
|
|
|
|
{:ok, %Member{}},
|
|
|
|
get_member(actor_id, parent_id, @moderator_roles)
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2021-07-20 18:22:18 +02:00
|
|
|
@doc """
|
|
|
|
Returns whether the `actor_id` is an administrator for the group `parent_id`
|
|
|
|
"""
|
2020-10-09 15:26:37 +02:00
|
|
|
@spec is_administrator?(integer | String.t(), integer | String.t()) :: boolean()
|
|
|
|
def is_administrator?(actor_id, parent_id) do
|
|
|
|
match?(
|
|
|
|
{:ok, %Member{}},
|
|
|
|
get_member(actor_id, parent_id, @administrator_roles)
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2020-11-06 11:34:32 +01:00
|
|
|
@doc """
|
|
|
|
Gets the default member role depending on the event join options.
|
|
|
|
"""
|
|
|
|
@spec get_default_member_role(Actor.t()) :: :member | :not_approved
|
|
|
|
def get_default_member_role(%Actor{openness: :open}), do: :member
|
|
|
|
def get_default_member_role(%Actor{openness: _}), do: :not_approved
|
|
|
|
|
2020-02-18 08:57:00 +01:00
|
|
|
@doc """
|
|
|
|
Gets a single member of an actor (for example a group).
|
|
|
|
"""
|
|
|
|
@spec get_member_by_url(String.t()) :: Member.t() | nil
|
|
|
|
def get_member_by_url(url) do
|
|
|
|
Member
|
|
|
|
|> where(url: ^url)
|
|
|
|
|> preload([:actor, :parent, :invited_by])
|
|
|
|
|> Repo.one()
|
|
|
|
end
|
|
|
|
|
2020-07-09 17:24:28 +02:00
|
|
|
@spec get_single_group_member_actor(integer() | String.t()) :: Actor.t() | nil
|
|
|
|
def get_single_group_member_actor(group_id) do
|
2021-07-30 17:30:00 +02:00
|
|
|
do_get_single_group_member_actor(group_id, [:member, :moderator, :administrator, :creator])
|
|
|
|
end
|
|
|
|
|
|
|
|
@spec get_single_group_moderator_actor(integer() | String.t()) :: Actor.t() | nil
|
|
|
|
def get_single_group_moderator_actor(group_id) do
|
|
|
|
do_get_single_group_member_actor(group_id, [:moderator, :administrator, :creator])
|
|
|
|
end
|
|
|
|
|
|
|
|
@spec do_get_single_group_member_actor(integer() | String.t(), list(atom())) :: Actor.t() | nil
|
|
|
|
defp do_get_single_group_member_actor(group_id, roles) do
|
2020-07-09 17:24:28 +02:00
|
|
|
Member
|
2021-07-30 17:30:00 +02:00
|
|
|
|> where([m], m.parent_id == ^group_id and m.role in ^roles)
|
2020-07-09 17:24:28 +02:00
|
|
|
|> join(:inner, [m], a in Actor, on: m.actor_id == a.id)
|
|
|
|
|> where([_m, a], is_nil(a.domain))
|
|
|
|
|> limit(1)
|
|
|
|
|> select([_m, a], a)
|
|
|
|
|> Repo.one()
|
|
|
|
end
|
|
|
|
|
2018-05-18 09:56:21 +02:00
|
|
|
@doc """
|
|
|
|
Creates a member.
|
|
|
|
"""
|
2019-09-11 03:16:37 +02:00
|
|
|
@spec create_member(map) :: {:ok, Member.t()} | {:error, Ecto.Changeset.t()}
|
2018-05-18 09:56:21 +02:00
|
|
|
def create_member(attrs \\ %{}) do
|
2021-09-10 11:36:05 +02:00
|
|
|
case %Member{}
|
|
|
|
|> Member.changeset(attrs)
|
|
|
|
|> Repo.insert(
|
|
|
|
on_conflict: {:replace_all_except, [:id, :url, :actor_id, :parent_id]},
|
|
|
|
conflict_target: [:actor_id, :parent_id],
|
|
|
|
# See https://hexdocs.pm/ecto/Ecto.Repo.html#c:insert/2-upserts,
|
|
|
|
# when doing an upsert with on_conflict, PG doesn't return whether it's an insert or upsert
|
|
|
|
# so we need to refresh the fields
|
|
|
|
returning: true
|
|
|
|
) do
|
|
|
|
{:ok, %Member{} = member} ->
|
|
|
|
{:ok, Repo.preload(member, [:actor, :parent, :invited_by])}
|
|
|
|
|
|
|
|
{:error, %Ecto.Changeset{} = err} ->
|
|
|
|
{:error, err}
|
2018-08-24 11:34:00 +02:00
|
|
|
end
|
2018-05-18 09:56:21 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
@doc """
|
|
|
|
Updates a member.
|
|
|
|
"""
|
2019-09-11 03:16:37 +02:00
|
|
|
@spec update_member(Member.t(), map) :: {:ok, Member.t()} | {:error, Ecto.Changeset.t()}
|
2018-05-18 09:56:21 +02:00
|
|
|
def update_member(%Member{} = member, attrs) do
|
|
|
|
member
|
|
|
|
|> Member.changeset(attrs)
|
|
|
|
|> Repo.update()
|
|
|
|
end
|
|
|
|
|
|
|
|
@doc """
|
2019-09-11 03:16:37 +02:00
|
|
|
Deletes a member.
|
|
|
|
"""
|
|
|
|
@spec delete_member(Member.t()) :: {:ok, Member.t()} | {:error, Ecto.Changeset.t()}
|
|
|
|
def delete_member(%Member{} = member), do: Repo.delete(member)
|
2018-05-18 09:56:21 +02:00
|
|
|
|
2020-02-18 08:57:00 +01:00
|
|
|
@doc """
|
|
|
|
Returns the list of memberships for an user.
|
|
|
|
|
|
|
|
Default behaviour is to not return :not_approved memberships
|
|
|
|
|
|
|
|
## Examples
|
|
|
|
|
|
|
|
iex> list_event_participations_for_user(5)
|
|
|
|
%Page{total: 3, elements: [%Participant{}, ...]}
|
|
|
|
|
|
|
|
"""
|
|
|
|
@spec list_memberships_for_user(
|
|
|
|
integer,
|
2021-12-13 17:02:10 +01:00
|
|
|
String.t() | nil,
|
2020-02-18 08:57:00 +01:00
|
|
|
integer | nil,
|
|
|
|
integer | nil
|
2021-12-13 17:02:10 +01:00
|
|
|
) :: Page.t(Member.t())
|
|
|
|
def list_memberships_for_user(user_id, name, page, limit) do
|
2020-02-18 08:57:00 +01:00
|
|
|
user_id
|
|
|
|
|> list_members_for_user_query()
|
2021-12-13 17:02:10 +01:00
|
|
|
|> filter_members_by_group_name(name)
|
2020-02-18 08:57:00 +01:00
|
|
|
|> Page.build_page(page, limit)
|
|
|
|
end
|
|
|
|
|
2019-09-11 03:16:37 +02:00
|
|
|
@doc """
|
|
|
|
Returns the list of members for an actor.
|
|
|
|
"""
|
2021-09-30 09:26:57 +02:00
|
|
|
@spec list_members_for_actor(Actor.t(), integer | nil, integer | nil) :: Page.t(Member.t())
|
2020-02-18 08:57:00 +01:00
|
|
|
def list_members_for_actor(%Actor{id: actor_id}, page \\ nil, limit \\ nil) do
|
2019-09-11 03:16:37 +02:00
|
|
|
actor_id
|
|
|
|
|> members_for_actor_query()
|
2020-02-18 08:57:00 +01:00
|
|
|
|> Page.build_page(page, limit)
|
2019-09-11 03:16:37 +02:00
|
|
|
end
|
2018-05-18 09:56:21 +02:00
|
|
|
|
2021-09-30 09:26:57 +02:00
|
|
|
@spec list_all_local_members_for_group(Actor.t()) :: list(Member.t())
|
2021-09-10 11:36:05 +02:00
|
|
|
def list_all_local_members_for_group(%Actor{id: group_id, type: :Group} = _group) do
|
|
|
|
group_id
|
|
|
|
|> group_internal_member_query()
|
|
|
|
|> Repo.all()
|
|
|
|
end
|
|
|
|
|
2019-09-11 03:16:37 +02:00
|
|
|
@doc """
|
2021-02-24 19:06:48 +01:00
|
|
|
Returns a paginated list of members for a group.
|
2018-05-18 09:56:21 +02:00
|
|
|
"""
|
2022-05-03 12:23:09 +02:00
|
|
|
@spec list_members_for_group(
|
|
|
|
Actor.t(),
|
|
|
|
String.t() | nil,
|
|
|
|
list(atom()),
|
|
|
|
integer | nil,
|
|
|
|
integer | nil
|
|
|
|
) ::
|
2021-09-30 09:26:57 +02:00
|
|
|
Page.t(Member.t())
|
2020-07-09 17:24:28 +02:00
|
|
|
def list_members_for_group(
|
2020-02-18 08:57:00 +01:00
|
|
|
%Actor{id: group_id, type: :Group},
|
2021-12-13 17:02:10 +01:00
|
|
|
name \\ nil,
|
2020-07-09 17:24:28 +02:00
|
|
|
roles \\ [],
|
2020-02-18 08:57:00 +01:00
|
|
|
page \\ nil,
|
|
|
|
limit \\ nil
|
|
|
|
) do
|
|
|
|
group_id
|
|
|
|
|> members_for_group_query()
|
2021-12-13 17:02:10 +01:00
|
|
|
|> join_members_actor()
|
|
|
|
|> filter_members_by_actor_name(name)
|
2020-07-09 17:24:28 +02:00
|
|
|
|> filter_member_role(roles)
|
2020-02-18 08:57:00 +01:00
|
|
|
|> Page.build_page(page, limit)
|
2018-05-18 09:56:21 +02:00
|
|
|
end
|
|
|
|
|
2020-07-09 17:24:28 +02:00
|
|
|
@spec list_external_actors_members_for_group(Actor.t()) :: list(Actor.t())
|
|
|
|
def list_external_actors_members_for_group(%Actor{id: group_id, type: :Group}) do
|
|
|
|
group_id
|
|
|
|
|> group_external_member_actor_query()
|
|
|
|
|> Repo.all()
|
|
|
|
end
|
|
|
|
|
2021-02-24 19:06:48 +01:00
|
|
|
@spec list_internal_actors_members_for_group(Actor.t(), list()) :: list(Actor.t())
|
|
|
|
def list_internal_actors_members_for_group(%Actor{id: group_id, type: :Group}, roles \\ []) do
|
|
|
|
group_id
|
|
|
|
|> group_internal_member_actor_query(roles)
|
|
|
|
|> Repo.all()
|
|
|
|
end
|
|
|
|
|
2020-11-06 11:34:32 +01:00
|
|
|
@doc """
|
|
|
|
Returns the complete list of administrator members for a group.
|
|
|
|
"""
|
|
|
|
@spec list_all_administrator_members_for_group(integer | String.t()) :: [Member.t()]
|
|
|
|
def list_all_administrator_members_for_group(id) do
|
|
|
|
id
|
|
|
|
|> administrator_members_for_group_query()
|
|
|
|
|> Repo.all()
|
|
|
|
end
|
|
|
|
|
2019-09-09 00:52:49 +02:00
|
|
|
@doc """
|
2019-09-11 03:16:37 +02:00
|
|
|
Returns the list of all group ids where the actor_id is the last administrator.
|
2018-05-18 09:56:21 +02:00
|
|
|
"""
|
2019-09-11 03:16:37 +02:00
|
|
|
@spec list_group_ids_where_last_administrator(integer | String.t()) :: [integer]
|
|
|
|
def list_group_ids_where_last_administrator(actor_id) do
|
|
|
|
actor_id
|
|
|
|
|> group_ids_where_last_administrator_query()
|
|
|
|
|> Repo.all()
|
2018-05-18 09:56:21 +02:00
|
|
|
end
|
|
|
|
|
2020-08-14 11:32:23 +02:00
|
|
|
@doc """
|
|
|
|
Returns whether the member is the last administrator for a group
|
|
|
|
"""
|
|
|
|
@spec is_only_administrator?(integer | String.t(), integer | String.t()) :: boolean()
|
|
|
|
def is_only_administrator?(member_id, group_id) do
|
|
|
|
Member
|
|
|
|
|> where(
|
|
|
|
[m],
|
|
|
|
m.parent_id == ^group_id and m.id != ^member_id and m.role in ^@administrator_roles
|
|
|
|
)
|
|
|
|
|> Repo.aggregate(:count)
|
|
|
|
|> (&(&1 == 0)).()
|
|
|
|
end
|
|
|
|
|
2022-03-24 14:19:24 +01:00
|
|
|
@doc """
|
2022-05-09 18:27:29 +02:00
|
|
|
Returns the number of members for a group
|
2022-03-24 14:19:24 +01:00
|
|
|
"""
|
|
|
|
@spec count_members_for_group(Actor.t()) :: integer()
|
|
|
|
def count_members_for_group(%Actor{id: actor_id}) do
|
|
|
|
actor_id
|
|
|
|
|> members_for_group_query()
|
|
|
|
# |> where([m], m.role in @member_roles)
|
|
|
|
|> Repo.aggregate(:count)
|
|
|
|
end
|
|
|
|
|
2019-03-19 11:16:03 +01:00
|
|
|
@doc """
|
2019-09-11 03:16:37 +02:00
|
|
|
Gets a single bot.
|
|
|
|
Raises `Ecto.NoResultsError` if the bot does not exist.
|
2019-03-19 11:16:03 +01:00
|
|
|
"""
|
2019-09-11 03:16:37 +02:00
|
|
|
def get_bot!(id), do: Repo.get!(Bot, id)
|
2018-06-06 17:42:53 +02:00
|
|
|
|
2019-03-19 11:16:03 +01:00
|
|
|
@doc """
|
2019-09-11 03:16:37 +02:00
|
|
|
Gets the bot associated to an actor.
|
2019-03-19 11:16:03 +01:00
|
|
|
"""
|
2019-09-11 03:16:37 +02:00
|
|
|
@spec get_bot_for_actor(Actor.t()) :: Bot.t()
|
|
|
|
def get_bot_for_actor(%Actor{id: actor_id}) do
|
|
|
|
Repo.get_by!(Bot, actor_id: actor_id)
|
2018-05-30 14:27:21 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
@doc """
|
|
|
|
Creates a bot.
|
|
|
|
"""
|
2021-09-10 11:36:05 +02:00
|
|
|
@spec create_bot(attrs :: map) :: {:ok, Bot.t()} | {:error, Ecto.Changeset.t()}
|
2018-05-30 14:27:21 +02:00
|
|
|
def create_bot(attrs \\ %{}) do
|
|
|
|
%Bot{}
|
|
|
|
|> Bot.changeset(attrs)
|
|
|
|
|> Repo.insert()
|
|
|
|
end
|
|
|
|
|
|
|
|
@doc """
|
2019-09-11 03:16:37 +02:00
|
|
|
Registers a new bot.
|
|
|
|
"""
|
2021-09-10 11:36:05 +02:00
|
|
|
@spec register_bot(%{name: String.t(), summary: String.t()}) ::
|
|
|
|
{:ok, Actor.t()} | {:error, Ecto.Changeset.t()}
|
2019-09-11 03:16:37 +02:00
|
|
|
def register_bot(%{name: name, summary: summary}) do
|
|
|
|
attrs = %{
|
|
|
|
preferred_username: name,
|
|
|
|
domain: nil,
|
|
|
|
keys: Crypto.generate_rsa_2048_private_key(),
|
|
|
|
summary: summary,
|
|
|
|
type: :Service
|
|
|
|
}
|
2018-05-30 14:27:21 +02:00
|
|
|
|
2019-09-11 03:16:37 +02:00
|
|
|
%Actor{}
|
|
|
|
|> Actor.registration_changeset(attrs)
|
|
|
|
|> Repo.insert()
|
|
|
|
end
|
2018-05-30 14:27:21 +02:00
|
|
|
|
2021-09-10 11:36:05 +02:00
|
|
|
@spec get_or_create_internal_actor(String.t()) ::
|
|
|
|
{:ok, Actor.t()} | {:error, Ecto.Changeset.t()}
|
2019-12-20 13:04:34 +01:00
|
|
|
def get_or_create_internal_actor(username) do
|
|
|
|
case username |> Actor.build_url(:page) |> get_actor_by_url() do
|
2019-09-11 03:16:37 +02:00
|
|
|
{:ok, %Actor{} = actor} ->
|
|
|
|
{:ok, actor}
|
2018-05-30 14:27:21 +02:00
|
|
|
|
2019-09-11 03:16:37 +02:00
|
|
|
_ ->
|
2019-12-20 13:04:34 +01:00
|
|
|
case username do
|
|
|
|
"anonymous" ->
|
|
|
|
Actor.build_anonymous_actor_creation_attrs()
|
|
|
|
|> Repo.insert()
|
|
|
|
|
|
|
|
"relay" ->
|
|
|
|
Actor.build_relay_creation_attrs()
|
|
|
|
|> Repo.insert()
|
|
|
|
end
|
2019-09-11 03:16:37 +02:00
|
|
|
end
|
|
|
|
end
|
2018-05-30 14:27:21 +02:00
|
|
|
|
2019-09-11 03:16:37 +02:00
|
|
|
@doc """
|
|
|
|
Updates a bot.
|
2018-05-30 14:27:21 +02:00
|
|
|
"""
|
2019-09-11 03:16:37 +02:00
|
|
|
@spec update_bot(Bot.t(), map) :: {:ok, Bot.t()} | {:error, Ecto.Changeset.t()}
|
2018-05-30 14:27:21 +02:00
|
|
|
def update_bot(%Bot{} = bot, attrs) do
|
|
|
|
bot
|
|
|
|
|> Bot.changeset(attrs)
|
|
|
|
|> Repo.update()
|
|
|
|
end
|
|
|
|
|
|
|
|
@doc """
|
2019-09-11 03:16:37 +02:00
|
|
|
Deletes a bot.
|
2018-05-30 14:27:21 +02:00
|
|
|
"""
|
2019-09-11 03:16:37 +02:00
|
|
|
@spec delete_bot(Bot.t()) :: {:ok, Bot.t()} | {:error, Ecto.Changeset.t()}
|
|
|
|
def delete_bot(%Bot{} = bot), do: Repo.delete(bot)
|
2018-05-30 14:27:21 +02:00
|
|
|
|
2021-01-20 18:16:44 +01:00
|
|
|
@doc """
|
|
|
|
Gets a single follower.
|
|
|
|
"""
|
|
|
|
@spec get_follower(integer | String.t()) :: Follower.t() | nil
|
|
|
|
def get_follower(id) do
|
|
|
|
Follower
|
|
|
|
|> Repo.get(id)
|
|
|
|
|> Repo.preload([:actor, :target_actor])
|
|
|
|
end
|
|
|
|
|
2018-08-01 14:45:18 +02:00
|
|
|
@doc """
|
|
|
|
Gets a single follower.
|
2019-09-11 03:16:37 +02:00
|
|
|
Raises `Ecto.NoResultsError` if the follower does not exist.
|
2018-08-01 14:45:18 +02:00
|
|
|
"""
|
2019-09-11 03:16:37 +02:00
|
|
|
@spec get_follower!(integer | String.t()) :: Follower.t()
|
2018-08-01 14:45:18 +02:00
|
|
|
def get_follower!(id) do
|
2019-09-11 03:16:37 +02:00
|
|
|
Follower
|
|
|
|
|> Repo.get!(id)
|
2018-08-01 14:45:18 +02:00
|
|
|
|> Repo.preload([:actor, :target_actor])
|
|
|
|
end
|
|
|
|
|
2019-03-19 11:16:03 +01:00
|
|
|
@doc """
|
2019-09-11 03:16:37 +02:00
|
|
|
Get a follower by the url.
|
2019-03-19 11:16:03 +01:00
|
|
|
"""
|
2019-09-11 03:16:37 +02:00
|
|
|
@spec get_follower_by_url(String.t()) :: Follower.t()
|
|
|
|
def get_follower_by_url(url) do
|
|
|
|
url
|
|
|
|
|> follower_by_url()
|
|
|
|
|> Repo.one()
|
2019-09-09 00:52:49 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
@doc """
|
2019-09-11 03:16:37 +02:00
|
|
|
Gets a follower by the followed actor and following actor
|
2019-09-09 00:52:49 +02:00
|
|
|
"""
|
2019-09-11 03:16:37 +02:00
|
|
|
@spec get_follower_by_followed_and_following(Actor.t(), Actor.t()) :: Follower.t() | nil
|
|
|
|
def get_follower_by_followed_and_following(%Actor{id: followed_id}, %Actor{id: following_id}) do
|
|
|
|
followed_id
|
|
|
|
|> follower_by_followed_and_following_query(following_id)
|
|
|
|
|> Repo.one()
|
2019-09-09 00:52:49 +02:00
|
|
|
end
|
|
|
|
|
2018-08-01 14:45:18 +02:00
|
|
|
@doc """
|
|
|
|
Creates a follower.
|
|
|
|
"""
|
2021-09-10 11:36:05 +02:00
|
|
|
@spec create_follower(attrs :: map) :: {:ok, Follower.t()} | {:error, Ecto.Changeset.t()}
|
2018-08-01 14:45:18 +02:00
|
|
|
def create_follower(attrs \\ %{}) do
|
2021-09-10 11:36:05 +02:00
|
|
|
case %Follower{}
|
|
|
|
|> Follower.changeset(attrs)
|
|
|
|
|> Repo.insert() do
|
|
|
|
{:ok, %Follower{} = follower} ->
|
|
|
|
{:ok, Repo.preload(follower, [:actor, :target_actor])}
|
|
|
|
|
|
|
|
{:error, %Ecto.Changeset{} = err} ->
|
|
|
|
{:error, err}
|
2018-11-27 14:02:51 +01:00
|
|
|
end
|
2018-08-01 14:45:18 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
@doc """
|
|
|
|
Updates a follower.
|
|
|
|
"""
|
2019-09-11 03:16:37 +02:00
|
|
|
@spec update_follower(Follower.t(), map) :: {:ok, Follower.t()} | {:error, Ecto.Changeset.t()}
|
2018-08-01 14:45:18 +02:00
|
|
|
def update_follower(%Follower{} = follower, attrs) do
|
2019-12-03 11:29:51 +01:00
|
|
|
with {:ok, %Follower{} = follower} <-
|
|
|
|
follower
|
|
|
|
|> Follower.changeset(attrs)
|
|
|
|
|> Repo.update() do
|
|
|
|
{:ok, Repo.preload(follower, [:actor, :target_actor])}
|
|
|
|
end
|
2018-08-01 14:45:18 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
@doc """
|
2019-09-11 03:16:37 +02:00
|
|
|
Deletes a follower.
|
|
|
|
"""
|
|
|
|
@spec delete_follower(Follower.t()) :: {:ok, Follower.t()} | {:error, Ecto.Changeset.t()}
|
|
|
|
def delete_follower(%Follower{} = follower), do: Repo.delete(follower)
|
2018-08-01 14:45:18 +02:00
|
|
|
|
2019-09-11 03:16:37 +02:00
|
|
|
@doc """
|
|
|
|
Deletes a follower by followed and following actors.
|
2018-08-01 14:45:18 +02:00
|
|
|
"""
|
2019-09-11 03:16:37 +02:00
|
|
|
@spec delete_follower_by_followed_and_following(Actor.t(), Actor.t()) ::
|
|
|
|
{:ok, Follower.t()} | {:error, Ecto.Changeset.t()}
|
|
|
|
def delete_follower_by_followed_and_following(%Actor{} = followed, %Actor{} = following) do
|
|
|
|
followed
|
|
|
|
|> get_follower_by_followed_and_following(following)
|
|
|
|
|> Repo.delete()
|
2018-08-01 14:45:18 +02:00
|
|
|
end
|
|
|
|
|
2021-10-25 13:18:46 +02:00
|
|
|
@spec list_paginated_follows_for_actor(Actor.t(), integer | nil, integer | nil) ::
|
|
|
|
Page.t(Follower.t())
|
|
|
|
def list_paginated_follows_for_actor(%Actor{id: actor_id}, page \\ nil, limit \\ nil) do
|
|
|
|
actor_id
|
|
|
|
|> followings_for_actor_query
|
|
|
|
|> Page.build_page(page, limit)
|
|
|
|
end
|
|
|
|
|
2019-09-11 03:16:37 +02:00
|
|
|
@doc """
|
|
|
|
Returns the list of external followers for an actor.
|
|
|
|
"""
|
|
|
|
@spec list_external_followers_for_actor(Actor.t()) :: [Follower.t()]
|
|
|
|
def list_external_followers_for_actor(%Actor{id: actor_id}) do
|
|
|
|
actor_id
|
2019-12-03 11:29:51 +01:00
|
|
|
|> list_external_follower_actors_for_actor_query()
|
2019-09-11 03:16:37 +02:00
|
|
|
|> Repo.all()
|
|
|
|
end
|
2018-12-14 17:41:55 +01:00
|
|
|
|
2019-12-03 11:29:51 +01:00
|
|
|
@doc """
|
|
|
|
Returns the paginated list of external followers for an actor.
|
|
|
|
"""
|
|
|
|
@spec list_external_followers_for_actor_paginated(Actor.t(), integer | nil, integer | nil) ::
|
2022-04-07 18:37:44 +02:00
|
|
|
Page.t(Actor.t())
|
2019-12-03 11:29:51 +01:00
|
|
|
def list_external_followers_for_actor_paginated(%Actor{id: actor_id}, page \\ nil, limit \\ nil) do
|
|
|
|
actor_id
|
|
|
|
|> list_external_followers_for_actor_query()
|
|
|
|
|> Page.build_page(page, limit)
|
|
|
|
end
|
|
|
|
|
2019-09-11 03:16:37 +02:00
|
|
|
@doc """
|
|
|
|
Build a page struct for followers of an actor.
|
|
|
|
"""
|
2022-04-07 18:37:44 +02:00
|
|
|
@spec build_followers_for_actor(Actor.t(), integer | nil, integer | nil) :: Page.t(Follower.t())
|
2019-09-11 03:16:37 +02:00
|
|
|
def build_followers_for_actor(%Actor{id: actor_id}, page \\ nil, limit \\ nil) do
|
|
|
|
actor_id
|
2019-12-03 11:29:51 +01:00
|
|
|
|> follower_actors_for_actor_query()
|
2019-09-11 03:16:37 +02:00
|
|
|
|> Page.build_page(page, limit)
|
|
|
|
end
|
2018-12-14 17:41:55 +01:00
|
|
|
|
2020-10-07 10:05:07 +02:00
|
|
|
@doc """
|
|
|
|
Returns the number of followers for an actor
|
|
|
|
"""
|
|
|
|
@spec count_followers_for_actor(Actor.t()) :: integer()
|
|
|
|
def count_followers_for_actor(%Actor{id: actor_id}) do
|
|
|
|
actor_id
|
|
|
|
|> follower_for_actor_query()
|
|
|
|
|> where(approved: true)
|
|
|
|
|> Repo.aggregate(:count)
|
|
|
|
end
|
|
|
|
|
2021-01-20 18:16:44 +01:00
|
|
|
@doc """
|
|
|
|
Returns a paginated list of followers for an actor.
|
|
|
|
"""
|
|
|
|
@spec list_paginated_followers_for_actor(Actor.t(), boolean | nil, integer | nil, integer | nil) ::
|
2022-04-07 18:37:44 +02:00
|
|
|
Page.t(Follower.t())
|
2021-01-20 18:16:44 +01:00
|
|
|
def list_paginated_followers_for_actor(
|
|
|
|
%Actor{id: actor_id},
|
|
|
|
approved \\ nil,
|
|
|
|
page \\ nil,
|
|
|
|
limit \\ nil
|
|
|
|
) do
|
|
|
|
actor_id
|
|
|
|
|> follower_for_actor_query()
|
|
|
|
|> filter_followed_by_approved_status(approved)
|
|
|
|
|> order_by(desc: :updated_at)
|
|
|
|
|> preload([:actor, :target_actor])
|
|
|
|
|> Page.build_page(page, limit)
|
|
|
|
end
|
|
|
|
|
2020-10-07 10:05:07 +02:00
|
|
|
@doc """
|
|
|
|
Returns the number of followings for an actor
|
|
|
|
"""
|
|
|
|
@spec count_followings_for_actor(Actor.t()) :: integer()
|
|
|
|
def count_followings_for_actor(%Actor{id: actor_id}) do
|
|
|
|
actor_id
|
|
|
|
|> followings_for_actor_query()
|
|
|
|
|> where(approved: true)
|
|
|
|
|> Repo.aggregate(:count)
|
|
|
|
end
|
|
|
|
|
2019-12-03 11:29:51 +01:00
|
|
|
@doc """
|
|
|
|
Returns the list of external followings for an actor.
|
|
|
|
"""
|
|
|
|
@spec list_external_followings_for_actor_paginated(Actor.t(), integer | nil, integer | nil) ::
|
2022-04-07 18:37:44 +02:00
|
|
|
Page.t(Follower.t())
|
2019-12-03 11:29:51 +01:00
|
|
|
def list_external_followings_for_actor_paginated(
|
|
|
|
%Actor{id: actor_id},
|
|
|
|
page \\ nil,
|
|
|
|
limit \\ nil
|
|
|
|
) do
|
|
|
|
actor_id
|
|
|
|
|> list_external_followings_for_actor_query()
|
|
|
|
|> Page.build_page(page, limit)
|
|
|
|
end
|
|
|
|
|
2019-09-11 03:16:37 +02:00
|
|
|
@doc """
|
|
|
|
Build a page struct for followings of an actor.
|
2018-12-14 17:41:55 +01:00
|
|
|
"""
|
2022-04-07 18:37:44 +02:00
|
|
|
@spec build_followings_for_actor(Actor.t(), integer | nil, integer | nil) ::
|
|
|
|
Page.t(Follower.t())
|
2019-09-11 03:16:37 +02:00
|
|
|
def build_followings_for_actor(%Actor{id: actor_id}, page \\ nil, limit \\ nil) do
|
|
|
|
actor_id
|
2019-12-03 11:29:51 +01:00
|
|
|
|> followings_actors_for_actor_query()
|
2019-09-11 03:16:37 +02:00
|
|
|
|> Page.build_page(page, limit)
|
2018-12-14 17:41:55 +01:00
|
|
|
end
|
|
|
|
|
2018-08-01 14:45:18 +02:00
|
|
|
@doc """
|
2019-09-11 03:16:37 +02:00
|
|
|
Makes an actor following another actor.
|
2019-09-09 00:52:49 +02:00
|
|
|
"""
|
2021-09-24 16:46:42 +02:00
|
|
|
@spec follow(
|
|
|
|
followed :: Actor.t(),
|
|
|
|
follower :: Actor.t(),
|
|
|
|
url :: String.t() | nil,
|
|
|
|
approved :: boolean | nil
|
|
|
|
) ::
|
|
|
|
{:ok, Follower.t()}
|
|
|
|
| {:error, :already_following | :followed_suspended | Ecto.Changeset.t()}
|
2019-09-09 00:52:49 +02:00
|
|
|
def follow(%Actor{} = followed, %Actor{} = follower, url \\ nil, approved \\ true) do
|
2021-09-24 16:46:42 +02:00
|
|
|
if followed.suspended do
|
|
|
|
{:error, :followed_suspended}
|
2019-09-09 00:52:49 +02:00
|
|
|
else
|
2021-12-29 14:59:33 +01:00
|
|
|
case check_follow(follower, followed) do
|
2021-09-24 16:46:42 +02:00
|
|
|
%Follower{} ->
|
|
|
|
{:error, :already_following}
|
|
|
|
|
|
|
|
nil ->
|
|
|
|
Logger.info(
|
|
|
|
"Making #{Actor.preferred_username_and_domain(follower)} follow #{Actor.preferred_username_and_domain(followed)} " <>
|
|
|
|
"(approved: #{approved})"
|
|
|
|
)
|
|
|
|
|
|
|
|
create_follower(%{
|
|
|
|
"actor_id" => follower.id,
|
|
|
|
"target_actor_id" => followed.id,
|
|
|
|
"approved" => approved,
|
|
|
|
"url" => url
|
|
|
|
})
|
|
|
|
end
|
2019-09-09 00:52:49 +02:00
|
|
|
end
|
|
|
|
end
|
2018-08-01 14:45:18 +02:00
|
|
|
|
2019-09-09 00:52:49 +02:00
|
|
|
@doc """
|
2019-09-11 03:16:37 +02:00
|
|
|
Unfollows an actor (removes a Follower record).
|
2019-09-09 00:52:49 +02:00
|
|
|
"""
|
2019-09-11 03:16:37 +02:00
|
|
|
@spec unfollow(Actor.t(), Actor.t()) ::
|
|
|
|
{:ok, Follower.t()} | {:error, Ecto.Changeset.t() | String.t()}
|
2019-09-09 00:52:49 +02:00
|
|
|
def unfollow(%Actor{} = followed, %Actor{} = follower) do
|
2021-12-29 14:59:33 +01:00
|
|
|
case {:already_following, check_follow(follower, followed)} do
|
2019-09-09 00:52:49 +02:00
|
|
|
{:already_following, %Follower{} = follow} ->
|
|
|
|
delete_follower(follow)
|
|
|
|
|
2019-09-11 03:16:37 +02:00
|
|
|
{:already_following, nil} ->
|
2019-09-09 00:52:49 +02:00
|
|
|
{:error, "Could not unfollow actor: you are not following #{followed.preferred_username}"}
|
|
|
|
end
|
|
|
|
end
|
2018-08-01 14:45:18 +02:00
|
|
|
|
2019-09-09 00:52:49 +02:00
|
|
|
@doc """
|
2019-09-11 03:16:37 +02:00
|
|
|
Checks whether an actor is following another actor.
|
2018-08-01 14:45:18 +02:00
|
|
|
"""
|
2021-12-29 14:59:33 +01:00
|
|
|
@spec check_follow(Actor.t(), Actor.t()) :: Follower.t() | nil
|
|
|
|
def check_follow(%Actor{} = follower_actor, %Actor{} = followed_actor) do
|
2019-09-11 03:16:37 +02:00
|
|
|
get_follower_by_followed_and_following(followed_actor, follower_actor)
|
2018-08-01 14:45:18 +02:00
|
|
|
end
|
2019-06-17 17:15:27 +02:00
|
|
|
|
2020-02-14 17:56:36 +01:00
|
|
|
@doc """
|
|
|
|
Whether the actor needs to be updated.
|
|
|
|
|
2021-04-20 16:55:55 +02:00
|
|
|
Local actors obviously don't need to be updated, neither do suspended ones
|
2020-02-14 17:56:36 +01:00
|
|
|
"""
|
|
|
|
@spec needs_update?(Actor.t()) :: boolean
|
|
|
|
def needs_update?(%Actor{domain: nil}), do: false
|
|
|
|
|
2021-04-20 16:55:55 +02:00
|
|
|
def needs_update?(%Actor{suspended: true}), do: false
|
|
|
|
|
2020-02-14 17:56:36 +01:00
|
|
|
def needs_update?(%Actor{last_refreshed_at: nil, domain: domain}) when not is_nil(domain),
|
|
|
|
do: true
|
|
|
|
|
|
|
|
def needs_update?(%Actor{domain: domain} = actor) when not is_nil(domain) do
|
|
|
|
DateTime.diff(DateTime.utc_now(), actor.last_refreshed_at) >=
|
|
|
|
Application.get_env(:mobilizon, :activitypub)[:actor_stale_period]
|
|
|
|
end
|
|
|
|
|
|
|
|
def needs_update?(_), do: true
|
|
|
|
|
|
|
|
@spec should_rotate_actor_key(Actor.t()) :: boolean
|
|
|
|
def should_rotate_actor_key(%Actor{id: actor_id}) do
|
|
|
|
with {:ok, value} when is_boolean(value) <- Cachex.exists?(:actor_key_rotation, actor_id) do
|
|
|
|
value
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-09-10 11:36:05 +02:00
|
|
|
# TODO: Move me otherwhere
|
|
|
|
@spec schedule_key_rotation(Actor.t(), integer()) :: :ok
|
2020-02-14 17:56:36 +01:00
|
|
|
def schedule_key_rotation(%Actor{id: actor_id} = actor, delay) do
|
|
|
|
Cachex.put(:actor_key_rotation, actor_id, true)
|
|
|
|
|
|
|
|
Workers.Background.enqueue("actor_key_rotation", %{"actor_id" => actor.id}, schedule_in: delay)
|
|
|
|
|
|
|
|
:ok
|
|
|
|
end
|
|
|
|
|
2022-05-06 16:58:33 +02:00
|
|
|
@doc """
|
|
|
|
Returns a relay actor, either `relay@domain` (Mobilizon) or `domain@domain` (Mastodon)
|
|
|
|
"""
|
|
|
|
@spec get_relay(String.t()) :: Actor.t() | nil
|
|
|
|
def get_relay(domain) do
|
|
|
|
get_actor_by_name("relay@#{domain}") || get_actor_by_name("#{domain}@#{domain}")
|
|
|
|
end
|
|
|
|
|
2019-09-09 00:52:49 +02:00
|
|
|
@spec delete_files_if_media_changed(Ecto.Changeset.t()) :: Ecto.Changeset.t()
|
|
|
|
defp delete_files_if_media_changed(%Ecto.Changeset{changes: changes, data: data} = changeset) do
|
|
|
|
Enum.each([:avatar, :banner], fn key ->
|
|
|
|
if Map.has_key?(changes, key) do
|
|
|
|
with %Ecto.Changeset{changes: %{url: new_url}} <- changes[key],
|
|
|
|
%{url: old_url} <- data |> Map.from_struct() |> Map.get(key),
|
|
|
|
false <- new_url == old_url do
|
2021-12-15 12:58:31 +01:00
|
|
|
Medias.delete_user_profile_media_by_url(old_url)
|
2019-09-09 00:52:49 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end)
|
|
|
|
|
|
|
|
changeset
|
|
|
|
end
|
2019-09-11 03:16:37 +02:00
|
|
|
|
2020-06-11 19:13:21 +02:00
|
|
|
@spec actor_with_preload_query(integer | String.t(), boolean()) :: Ecto.Query.t()
|
|
|
|
defp actor_with_preload_query(actor_id, include_suspended \\ false)
|
|
|
|
|
|
|
|
defp actor_with_preload_query(actor_id, false) do
|
|
|
|
actor_id
|
|
|
|
|> actor_with_preload_query(true)
|
|
|
|
|> where([a], not a.suspended)
|
|
|
|
end
|
|
|
|
|
|
|
|
defp actor_with_preload_query(actor_id, true) do
|
|
|
|
Actor
|
|
|
|
|> where([a], a.id == ^actor_id)
|
2022-04-01 10:03:43 +02:00
|
|
|
|> preload([a], ^@associations_to_preload)
|
2019-09-11 03:16:37 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
@spec actor_by_username_query(String.t()) :: Ecto.Query.t()
|
|
|
|
defp actor_by_username_query(username) do
|
|
|
|
from(
|
|
|
|
a in Actor,
|
|
|
|
where:
|
|
|
|
fragment(
|
|
|
|
"f_unaccent(?) <% f_unaccent(?) or f_unaccent(coalesce(?, '')) <% f_unaccent(?)",
|
|
|
|
a.preferred_username,
|
|
|
|
^username,
|
|
|
|
a.name,
|
|
|
|
^username
|
|
|
|
),
|
|
|
|
order_by:
|
|
|
|
fragment(
|
|
|
|
"word_similarity(?, ?) + word_similarity(coalesce(?, ''), ?) desc",
|
|
|
|
a.preferred_username,
|
|
|
|
^username,
|
|
|
|
a.name,
|
|
|
|
^username
|
|
|
|
)
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2021-09-24 16:46:42 +02:00
|
|
|
@spec actor_by_username_or_name_query(Ecto.Queryable.t(), String.t()) :: Ecto.Query.t()
|
2020-08-05 16:44:08 +02:00
|
|
|
defp actor_by_username_or_name_query(query, ""), do: query
|
|
|
|
|
|
|
|
defp actor_by_username_or_name_query(query, username) do
|
|
|
|
query
|
|
|
|
|> where(
|
|
|
|
[a],
|
|
|
|
fragment(
|
|
|
|
"f_unaccent(?) %> f_unaccent(?) or f_unaccent(coalesce(?, '')) %> f_unaccent(?)",
|
|
|
|
a.preferred_username,
|
|
|
|
^username,
|
|
|
|
a.name,
|
|
|
|
^username
|
|
|
|
)
|
2019-09-11 03:16:37 +02:00
|
|
|
)
|
2020-08-05 16:44:08 +02:00
|
|
|
|> order_by(
|
|
|
|
[a],
|
|
|
|
fragment(
|
|
|
|
"word_similarity(?, ?) + word_similarity(coalesce(?, ''), ?) desc",
|
|
|
|
a.preferred_username,
|
|
|
|
^username,
|
|
|
|
a.name,
|
|
|
|
^username
|
|
|
|
)
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2021-09-24 16:46:42 +02:00
|
|
|
@spec actors_for_location(Ecto.Queryable.t(), String.t(), integer()) :: Ecto.Query.t()
|
2020-12-17 15:25:58 +01:00
|
|
|
defp actors_for_location(query, location, radius)
|
2021-09-10 11:27:59 +02:00
|
|
|
when is_valid_string(location) and not is_nil(radius) do
|
2021-11-06 10:09:54 +01:00
|
|
|
{lon, lat} = Geohax.decode(location)
|
|
|
|
point = Geo.WKT.decode!("SRID=4326;POINT(#{lon} #{lat})")
|
|
|
|
|
|
|
|
query
|
|
|
|
|> join(:inner, [q], a in Address, on: a.id == q.physical_address_id, as: :address)
|
|
|
|
|> where(
|
|
|
|
[q],
|
|
|
|
st_dwithin_in_meters(^point, as(:address).geom, ^(radius * 1000))
|
|
|
|
)
|
2019-09-11 03:16:37 +02:00
|
|
|
end
|
|
|
|
|
2020-12-17 15:25:58 +01:00
|
|
|
defp actors_for_location(query, _location, _radius), do: query
|
2020-08-05 16:44:08 +02:00
|
|
|
|
2020-06-11 19:13:21 +02:00
|
|
|
@spec person_query :: Ecto.Query.t()
|
|
|
|
defp person_query do
|
|
|
|
from(a in Actor, where: a.type == ^:Person)
|
|
|
|
end
|
|
|
|
|
2019-09-11 03:16:37 +02:00
|
|
|
@spec group_query :: Ecto.Query.t()
|
|
|
|
defp group_query do
|
|
|
|
from(a in Actor, where: a.type == ^:Group)
|
|
|
|
end
|
|
|
|
|
|
|
|
@spec groups_member_of_query(integer | String.t()) :: Ecto.Query.t()
|
|
|
|
defp groups_member_of_query(actor_id) do
|
2021-06-26 15:23:22 +02:00
|
|
|
Actor
|
|
|
|
|> join(:inner, [a], m in Member, on: a.id == m.parent_id)
|
|
|
|
|> where([a, m], m.actor_id == ^actor_id and m.role in ^@member_roles)
|
2019-09-11 03:16:37 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
@spec groups_query :: Ecto.Query.t()
|
|
|
|
defp groups_query do
|
|
|
|
from(
|
|
|
|
a in Actor,
|
|
|
|
where: a.type == ^:Group,
|
2020-08-12 16:05:34 +02:00
|
|
|
where: a.visibility == ^:public
|
2019-09-11 03:16:37 +02:00
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2020-11-06 11:34:32 +01:00
|
|
|
@spec external_groups_query :: Ecto.Query.t()
|
|
|
|
defp external_groups_query do
|
|
|
|
where(Actor, [a], a.type == ^:Group and not is_nil(a.domain))
|
|
|
|
end
|
|
|
|
|
2020-02-18 08:57:00 +01:00
|
|
|
@spec list_members_for_user_query(integer()) :: Ecto.Query.t()
|
|
|
|
defp list_members_for_user_query(user_id) do
|
2021-12-13 17:02:10 +01:00
|
|
|
Member
|
|
|
|
|> join_members_actor()
|
|
|
|
|> where([m, a], a.user_id == ^user_id and m.role != ^:not_approved)
|
|
|
|
|> preload([:parent, :actor, :invited_by])
|
2020-02-18 08:57:00 +01:00
|
|
|
end
|
|
|
|
|
2019-09-11 03:16:37 +02:00
|
|
|
@spec members_for_actor_query(integer | String.t()) :: Ecto.Query.t()
|
|
|
|
defp members_for_actor_query(actor_id) do
|
|
|
|
from(
|
|
|
|
m in Member,
|
|
|
|
where: m.actor_id == ^actor_id,
|
2020-08-14 11:32:23 +02:00
|
|
|
preload: [:parent, :invited_by]
|
2019-09-11 03:16:37 +02:00
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
@spec members_for_group_query(integer | String.t()) :: Ecto.Query.t()
|
|
|
|
defp members_for_group_query(group_id) do
|
2020-11-06 11:34:32 +01:00
|
|
|
Member
|
|
|
|
|> where(parent_id: ^group_id)
|
|
|
|
|> order_by(desc: :updated_at)
|
|
|
|
|> preload([:parent, :actor])
|
2019-09-11 03:16:37 +02:00
|
|
|
end
|
|
|
|
|
2020-07-09 17:24:28 +02:00
|
|
|
@spec group_external_member_actor_query(integer()) :: Ecto.Query.t()
|
|
|
|
defp group_external_member_actor_query(group_id) do
|
|
|
|
Member
|
|
|
|
|> where([m], m.parent_id == ^group_id)
|
|
|
|
|> join(:inner, [m], a in Actor, on: m.actor_id == a.id)
|
|
|
|
|> where([_m, a], not is_nil(a.domain))
|
|
|
|
|> select([_m, a], a)
|
|
|
|
end
|
|
|
|
|
2021-02-24 19:06:48 +01:00
|
|
|
@spec group_internal_member_actor_query(integer(), list()) :: Ecto.Query.t()
|
|
|
|
defp group_internal_member_actor_query(group_id, role) do
|
|
|
|
Member
|
|
|
|
|> where([m], m.parent_id == ^group_id)
|
|
|
|
|> filter_member_role(role)
|
|
|
|
|> join(:inner, [m], a in Actor, on: m.actor_id == a.id)
|
|
|
|
|> where([_m, a], is_nil(a.domain))
|
|
|
|
|> select([_m, a], a)
|
|
|
|
end
|
|
|
|
|
2020-08-27 11:53:24 +02:00
|
|
|
@spec group_internal_member_query(integer()) :: Ecto.Query.t()
|
|
|
|
defp group_internal_member_query(group_id) do
|
|
|
|
Member
|
|
|
|
|> where([m], m.parent_id == ^group_id)
|
|
|
|
|> join(:inner, [m], a in Actor, on: m.actor_id == a.id)
|
|
|
|
|> where([_m, a], is_nil(a.domain))
|
|
|
|
|> preload([m], [:parent, :actor])
|
|
|
|
|> select([m, _a], m)
|
|
|
|
end
|
|
|
|
|
2021-09-24 16:46:42 +02:00
|
|
|
@spec filter_member_role(Ecto.Queryable.t(), list(atom()) | atom()) :: Ecto.Query.t()
|
2021-02-24 19:06:48 +01:00
|
|
|
defp filter_member_role(query, []), do: query
|
2020-07-09 17:24:28 +02:00
|
|
|
|
2021-02-24 19:06:48 +01:00
|
|
|
defp filter_member_role(query, roles) when is_list(roles) do
|
2020-07-09 17:24:28 +02:00
|
|
|
where(query, [m], m.role in ^roles)
|
|
|
|
end
|
|
|
|
|
2021-02-24 19:06:48 +01:00
|
|
|
defp filter_member_role(query, role) when is_atom(role) do
|
2020-07-09 17:24:28 +02:00
|
|
|
from(m in query, where: m.role == ^role)
|
|
|
|
end
|
|
|
|
|
2021-12-13 17:02:10 +01:00
|
|
|
@spec filter_members_by_actor_name(Ecto.Query.t(), String.t() | nil) :: Ecto.Query.t()
|
|
|
|
defp filter_members_by_actor_name(query, nil), do: query
|
|
|
|
defp filter_members_by_actor_name(query, ""), do: query
|
|
|
|
|
|
|
|
defp filter_members_by_actor_name(query, name) when is_binary(name) do
|
|
|
|
where(query, [_q, a], like(a.name, ^"%#{name}%") or like(a.preferred_username, ^"%#{name}%"))
|
|
|
|
end
|
|
|
|
|
|
|
|
defp filter_members_by_group_name(query, nil), do: query
|
|
|
|
defp filter_members_by_group_name(query, ""), do: query
|
|
|
|
|
|
|
|
defp filter_members_by_group_name(query, name) when is_binary(name) do
|
|
|
|
query
|
|
|
|
|> join(:inner, [q], a in Actor, on: q.parent_id == a.id)
|
|
|
|
|> where([_q, ..., a], like(a.name, ^"%#{name}%") or like(a.preferred_username, ^"%#{name}%"))
|
|
|
|
end
|
|
|
|
|
2022-01-06 18:48:48 +01:00
|
|
|
@spec join_members_actor(Ecto.Queryable.t()) :: Ecto.Query.t()
|
2021-12-13 17:02:10 +01:00
|
|
|
defp join_members_actor(query) do
|
|
|
|
join(query, :inner, [q], a in Actor, on: q.actor_id == a.id)
|
|
|
|
end
|
|
|
|
|
2019-09-11 03:16:37 +02:00
|
|
|
@spec administrator_members_for_group_query(integer | String.t()) :: Ecto.Query.t()
|
|
|
|
defp administrator_members_for_group_query(group_id) do
|
|
|
|
from(
|
|
|
|
m in Member,
|
|
|
|
where: m.parent_id == ^group_id and m.role in ^@administrator_roles,
|
|
|
|
preload: [:actor]
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
@spec administrator_members_for_actor_query(integer | String.t()) :: Ecto.Query.t()
|
|
|
|
defp administrator_members_for_actor_query(actor_id) do
|
|
|
|
from(
|
|
|
|
m in Member,
|
|
|
|
where: m.actor_id == ^actor_id and m.role in ^@administrator_roles,
|
|
|
|
select: m.parent_id
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
@spec group_ids_where_last_administrator_query(integer | String.t()) :: Ecto.Query.t()
|
|
|
|
defp group_ids_where_last_administrator_query(actor_id) do
|
|
|
|
from(
|
|
|
|
m in Member,
|
|
|
|
where: m.role in ^@administrator_roles,
|
|
|
|
join: m2 in subquery(administrator_members_for_actor_query(actor_id)),
|
|
|
|
on: m.parent_id == m2.parent_id,
|
|
|
|
group_by: m.parent_id,
|
|
|
|
select: m.parent_id,
|
|
|
|
having: count(m.actor_id) == 1
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
@spec follower_by_url(String.t()) :: Ecto.Query.t()
|
|
|
|
defp follower_by_url(url) do
|
|
|
|
from(
|
|
|
|
f in Follower,
|
|
|
|
where: f.url == ^url,
|
|
|
|
preload: [:actor, :target_actor]
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
@spec follower_by_followed_and_following_query(integer | String.t(), integer | String.t()) ::
|
|
|
|
Ecto.Query.t()
|
|
|
|
defp follower_by_followed_and_following_query(followed_id, follower_id) do
|
|
|
|
from(
|
|
|
|
f in Follower,
|
2019-12-03 11:29:51 +01:00
|
|
|
where: f.target_actor_id == ^followed_id and f.actor_id == ^follower_id,
|
|
|
|
preload: [:actor, :target_actor]
|
2019-09-11 03:16:37 +02:00
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2019-12-03 11:29:51 +01:00
|
|
|
@spec follower_actors_for_actor_query(integer | String.t()) :: Ecto.Query.t()
|
|
|
|
defp follower_actors_for_actor_query(actor_id) do
|
2019-09-11 03:16:37 +02:00
|
|
|
from(
|
|
|
|
a in Actor,
|
|
|
|
join: f in Follower,
|
|
|
|
on: a.id == f.actor_id,
|
2019-12-19 13:41:25 +01:00
|
|
|
where: f.target_actor_id == ^actor_id and f.approved == true
|
2019-09-11 03:16:37 +02:00
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2019-12-03 11:29:51 +01:00
|
|
|
@spec follower_for_actor_query(integer | String.t()) :: Ecto.Query.t()
|
|
|
|
defp follower_for_actor_query(actor_id) do
|
|
|
|
from(
|
|
|
|
f in Follower,
|
|
|
|
join: a in Actor,
|
|
|
|
on: a.id == f.actor_id,
|
|
|
|
where: f.target_actor_id == ^actor_id
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
@spec followings_actors_for_actor_query(integer | String.t()) :: Ecto.Query.t()
|
|
|
|
defp followings_actors_for_actor_query(actor_id) do
|
2019-09-11 03:16:37 +02:00
|
|
|
from(
|
|
|
|
a in Actor,
|
|
|
|
join: f in Follower,
|
|
|
|
on: a.id == f.target_actor_id,
|
|
|
|
where: f.actor_id == ^actor_id
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2019-12-03 11:29:51 +01:00
|
|
|
@spec followings_for_actor_query(integer | String.t()) :: Ecto.Query.t()
|
|
|
|
defp followings_for_actor_query(actor_id) do
|
|
|
|
from(
|
|
|
|
f in Follower,
|
|
|
|
join: a in Actor,
|
|
|
|
on: a.id == f.target_actor_id,
|
|
|
|
where: f.actor_id == ^actor_id
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
@spec list_external_follower_actors_for_actor_query(integer) :: Ecto.Query.t()
|
|
|
|
defp list_external_follower_actors_for_actor_query(actor_id) do
|
|
|
|
actor_id
|
|
|
|
|> follower_actors_for_actor_query()
|
|
|
|
|> filter_external()
|
|
|
|
end
|
|
|
|
|
|
|
|
@spec list_external_followers_for_actor_query(integer) :: Ecto.Query.t()
|
|
|
|
defp list_external_followers_for_actor_query(actor_id) do
|
|
|
|
actor_id
|
|
|
|
|> follower_for_actor_query()
|
|
|
|
|> filter_follower_actors_external()
|
|
|
|
end
|
|
|
|
|
|
|
|
@spec list_external_followings_for_actor_query(integer) :: Ecto.Query.t()
|
|
|
|
defp list_external_followings_for_actor_query(actor_id) do
|
|
|
|
actor_id
|
|
|
|
|> followings_for_actor_query()
|
|
|
|
|> filter_follower_actors_external()
|
|
|
|
|> order_by(desc: :updated_at)
|
|
|
|
end
|
|
|
|
|
2021-09-24 16:46:42 +02:00
|
|
|
@spec filter_local(Ecto.Queryable.t()) :: Ecto.Query.t()
|
2019-09-11 03:16:37 +02:00
|
|
|
defp filter_local(query) do
|
|
|
|
from(a in query, where: is_nil(a.domain))
|
|
|
|
end
|
|
|
|
|
2021-09-24 16:46:42 +02:00
|
|
|
@spec filter_external(Ecto.Queryable.t()) :: Ecto.Query.t()
|
2019-09-11 03:16:37 +02:00
|
|
|
defp filter_external(query) do
|
|
|
|
from(a in query, where: not is_nil(a.domain))
|
|
|
|
end
|
|
|
|
|
2021-09-24 16:46:42 +02:00
|
|
|
@spec filter_follower_actors_external(Ecto.Queryable.t()) :: Ecto.Query.t()
|
2019-12-03 11:29:51 +01:00
|
|
|
defp filter_follower_actors_external(query) do
|
|
|
|
query
|
|
|
|
|> where([_f, a], not is_nil(a.domain))
|
|
|
|
|> preload([f, a], [:target_actor, :actor])
|
|
|
|
end
|
|
|
|
|
2022-04-18 14:38:57 +02:00
|
|
|
@spec filter_by_type(Ecto.Queryable.t(), atom() | nil) :: Ecto.Queryable.t()
|
2019-12-03 11:29:51 +01:00
|
|
|
defp filter_by_type(query, type)
|
|
|
|
when type in [:Person, :Group, :Application, :Service, :Organisation] do
|
2019-09-11 03:16:37 +02:00
|
|
|
from(a in query, where: a.type == ^type)
|
|
|
|
end
|
|
|
|
|
|
|
|
defp filter_by_type(query, _type), do: query
|
|
|
|
|
2021-09-24 16:46:42 +02:00
|
|
|
@spec filter_by_minimum_visibility(Ecto.Queryable.t(), atom()) :: Ecto.Query.t()
|
2020-12-17 15:25:58 +01:00
|
|
|
defp filter_by_minimum_visibility(query, :private), do: query
|
|
|
|
|
|
|
|
defp filter_by_minimum_visibility(query, :restricted) do
|
|
|
|
from(a in query, where: a.visibility in ^[:public, :unlisted, :restricted])
|
|
|
|
end
|
|
|
|
|
|
|
|
defp filter_by_minimum_visibility(query, :unlisted) do
|
|
|
|
from(a in query, where: a.visibility in ^[:public, :unlisted])
|
|
|
|
end
|
|
|
|
|
|
|
|
defp filter_by_minimum_visibility(query, :public) do
|
|
|
|
from(a in query, where: a.visibility == ^:public)
|
|
|
|
end
|
|
|
|
|
2021-09-24 16:46:42 +02:00
|
|
|
@spec filter_by_name(query :: Ecto.Queryable.t(), [String.t()]) :: Ecto.Query.t()
|
2019-09-11 03:16:37 +02:00
|
|
|
defp filter_by_name(query, [name]) do
|
2021-09-10 11:36:05 +02:00
|
|
|
where(query, [a], a.preferred_username == ^name and is_nil(a.domain))
|
2019-09-11 03:16:37 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
defp filter_by_name(query, [name, domain]) do
|
2021-06-07 10:24:57 +02:00
|
|
|
if domain == Mobilizon.Config.instance_hostname() do
|
|
|
|
filter_by_name(query, [name])
|
|
|
|
else
|
|
|
|
where(query, [a], a.preferred_username == ^name and a.domain == ^domain)
|
|
|
|
end
|
2019-09-11 03:16:37 +02:00
|
|
|
end
|
|
|
|
|
2021-09-24 16:46:42 +02:00
|
|
|
@spec filter_followed_by_approved_status(Ecto.Queryable.t(), boolean() | nil) :: Ecto.Query.t()
|
2021-01-20 18:16:44 +01:00
|
|
|
defp filter_followed_by_approved_status(query, nil), do: query
|
|
|
|
|
|
|
|
defp filter_followed_by_approved_status(query, approved) do
|
|
|
|
from(f in query, where: f.approved == ^approved)
|
|
|
|
end
|
|
|
|
|
2019-09-11 03:16:37 +02:00
|
|
|
@spec preload_followers(Actor.t(), boolean) :: Actor.t()
|
|
|
|
defp preload_followers(actor, true), do: Repo.preload(actor, [:followers])
|
|
|
|
defp preload_followers(actor, false), do: actor
|
2021-11-10 16:36:32 +01:00
|
|
|
|
|
|
|
@spec list_actors_to_notify_from_group_event(Actor.t()) :: Follower.t()
|
|
|
|
def list_actors_to_notify_from_group_event(%Actor{id: actor_id}) do
|
|
|
|
Actor
|
|
|
|
|> join(:left, [a], f in Follower, on: a.id == f.actor_id)
|
|
|
|
|> join(:left, [a], m in Member, on: a.id == m.actor_id)
|
|
|
|
|> where(
|
|
|
|
[a, f],
|
|
|
|
is_nil(a.domain) and f.target_actor_id == ^actor_id and f.approved == true and
|
|
|
|
f.notify == true
|
|
|
|
)
|
|
|
|
|> or_where(
|
|
|
|
[a, _f, m],
|
|
|
|
is_nil(a.domain) and m.parent_id == ^actor_id and m.role in ^@member_roles
|
|
|
|
)
|
|
|
|
|> Repo.all()
|
|
|
|
end
|
2018-05-18 09:56:21 +02:00
|
|
|
end
|