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
|
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}
|
2019-09-11 03:16:37 +02:00
|
|
|
alias Mobilizon.{Crypto, Events}
|
2019-06-05 18:29:39 +02:00
|
|
|
alias Mobilizon.Media.File
|
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}
|
|
|
|
|
|
|
|
alias Mobilizon.Federation.ActivityPub
|
2019-09-08 01:49:56 +02:00
|
|
|
|
2020-01-28 19:18:33 +01:00
|
|
|
alias Mobilizon.Web.Upload
|
|
|
|
|
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-16 02:07:44 +02:00
|
|
|
@public_visibility [:public, :unlisted]
|
2019-09-11 03:16:37 +02:00
|
|
|
@administrator_roles [:creator, :administrator]
|
2019-12-03 11:29:51 +01:00
|
|
|
@actor_preloads [:user, :organized_events, :comments]
|
2019-09-11 03:16:37 +02:00
|
|
|
|
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
|
|
|
|
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.
|
|
|
|
"""
|
|
|
|
@spec get_actor_with_preload(integer | String.t()) :: 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
|
|
|
|
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
|
|
|
|
|
2020-06-11 19:13:21 +02:00
|
|
|
@spec get_remote_actor_with_preload(integer | String.t(), boolean()) :: Actor.t() | nil
|
|
|
|
def get_remote_actor_with_preload(id, include_suspended \\ false) do
|
|
|
|
id
|
|
|
|
|> actor_with_preload_query(include_suspended)
|
|
|
|
|> filter_external()
|
|
|
|
|> Repo.one()
|
|
|
|
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
|
|
|
|
|
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
|
|
|
"""
|
2019-09-09 00:52:49 +02:00
|
|
|
@spec get_actor_by_name(String.t(), atom | nil) :: Actor.t() | nil
|
|
|
|
def get_actor_by_name(name, type \\ nil) do
|
2019-09-18 00:37:31 +02:00
|
|
|
query = from(a in Actor)
|
|
|
|
|
|
|
|
query
|
2019-09-09 00:52:49 +02:00
|
|
|
|> filter_by_type(type)
|
|
|
|
|> filter_by_name(String.split(name, "@"))
|
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
|
2019-09-18 00:37:31 +02:00
|
|
|
query = from(a in Actor)
|
|
|
|
|
|
|
|
query
|
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()
|
|
|
|
|> Repo.preload([:organized_events, :followers, :followings])
|
|
|
|
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)
|
2019-11-21 15:51:13 +01:00
|
|
|
|> Repo.preload([:organized_events, :user])
|
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
|
|
|
|
%Actor{}
|
|
|
|
|> Actor.changeset(attrs)
|
|
|
|
|> Repo.insert()
|
|
|
|
end
|
|
|
|
|
2019-09-11 03:16:37 +02:00
|
|
|
@doc """
|
|
|
|
Creates a new person actor.
|
|
|
|
"""
|
|
|
|
@spec new_person(map) :: {:ok, Actor.t()} | {:error, Ecto.Changeset.t()}
|
|
|
|
def new_person(args) do
|
|
|
|
args = Map.put(args, :keys, Crypto.generate_rsa_2048_private_key())
|
|
|
|
|
|
|
|
with {:ok, %Actor{} = person} <-
|
|
|
|
%Actor{}
|
|
|
|
|> Actor.registration_changeset(args)
|
|
|
|
|> Repo.insert() do
|
2020-02-13 15:48:12 +01:00
|
|
|
Events.create_feed_token(%{user_id: args["user_id"], actor_id: person.id})
|
2019-09-11 03:16:37 +02:00
|
|
|
|
|
|
|
{:ok, person}
|
|
|
|
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
|
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(
|
|
|
|
%{keys: keys, name: name, summary: summary, avatar: avatar, banner: banner} = data,
|
|
|
|
preload \\ false
|
|
|
|
) do
|
2019-09-09 00:52:49 +02:00
|
|
|
insert =
|
|
|
|
data
|
|
|
|
|> Actor.remote_actor_creation_changeset()
|
|
|
|
|> Repo.insert(
|
2020-06-10 14:28:27 +02:00
|
|
|
on_conflict: [
|
|
|
|
set: [
|
|
|
|
keys: keys,
|
|
|
|
name: name,
|
|
|
|
summary: summary,
|
|
|
|
avatar: transform_media_file(avatar),
|
|
|
|
banner: transform_media_file(banner)
|
|
|
|
]
|
|
|
|
],
|
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} ->
|
|
|
|
actor = if preload, do: Repo.preload(actor, [:followers]), 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-06-10 14:28:27 +02:00
|
|
|
defp transform_media_file(nil), do: nil
|
|
|
|
|
|
|
|
defp transform_media_file(file) do
|
|
|
|
file = for({key, val} <- file, into: %{}, do: {String.to_atom(key), val})
|
|
|
|
|
|
|
|
if is_nil(file) do
|
|
|
|
nil
|
|
|
|
else
|
|
|
|
struct(Mobilizon.Media.File, file)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-06-11 19:13:21 +02:00
|
|
|
@delete_actor_default_options [reserve_username: true]
|
|
|
|
|
|
|
|
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,
|
|
|
|
"reserve_username" => Keyword.get(delete_actor_options, :reserve_username, true)
|
|
|
|
})
|
2019-12-03 11:29:51 +01:00
|
|
|
end
|
|
|
|
|
2019-09-09 00:52:49 +02:00
|
|
|
@doc """
|
|
|
|
Deletes an actor.
|
2018-05-18 09:56:21 +02:00
|
|
|
"""
|
2019-12-03 11:29:51 +01:00
|
|
|
@spec perform(atom(), Actor.t()) :: {:ok, Actor.t()} | {:error, Ecto.Changeset.t()}
|
2020-06-11 19:13:21 +02:00
|
|
|
def perform(:delete_actor, %Actor{} = actor, options \\ @delete_actor_default_options) do
|
2019-12-03 11:29:51 +01:00
|
|
|
actor = Repo.preload(actor, @actor_preloads)
|
|
|
|
|
2020-06-11 19:13:21 +02:00
|
|
|
delete_actor_options = Keyword.merge(@delete_actor_default_options, options)
|
|
|
|
|
|
|
|
multi =
|
2019-09-09 00:52:49 +02:00
|
|
|
Multi.new()
|
2019-12-03 11:29:51 +01:00
|
|
|
|> Multi.run(:delete_organized_events, fn _, _ -> delete_actor_organized_events(actor) end)
|
|
|
|
|> Multi.run(:empty_comments, fn _, _ -> delete_actor_empty_comments(actor) end)
|
|
|
|
|> Multi.run(:remove_banner, fn _, _ -> remove_banner(actor) end)
|
|
|
|
|> Multi.run(:remove_avatar, fn _, _ -> remove_avatar(actor) end)
|
2019-09-09 00:52:49 +02:00
|
|
|
|
2020-06-11 19:13:21 +02:00
|
|
|
multi =
|
|
|
|
if Keyword.get(delete_actor_options, :reserve_username, true) do
|
|
|
|
Multi.update(multi, :actor, Actor.delete_changeset(actor))
|
|
|
|
else
|
|
|
|
Multi.delete(multi, :actor, actor)
|
|
|
|
end
|
|
|
|
|
|
|
|
case Repo.transaction(multi) do
|
2019-06-05 18:29:39 +02:00
|
|
|
{:ok, %{actor: %Actor{} = actor}} ->
|
2019-12-16 11:47:31 +01:00
|
|
|
{:ok, true} = Cachex.del(:activity_pub, "actor_#{actor.preferred_username}")
|
2019-06-05 18:29:39 +02:00
|
|
|
{:ok, actor}
|
|
|
|
|
|
|
|
{:error, remove, error, _} when remove in [:remove_banner, :remove_avatar] ->
|
|
|
|
{:error, error}
|
|
|
|
end
|
|
|
|
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-06-11 19:13:21 +02:00
|
|
|
@spec list_actors(String.t(), String.t(), boolean, boolean, integer, integer) :: Page.t()
|
|
|
|
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()
|
|
|
|
|> filter_suspended(suspended)
|
|
|
|
|> filter_preferred_username(preferred_username)
|
|
|
|
|> filter_name(name)
|
|
|
|
|> filter_domain(domain)
|
|
|
|
|> filter_remote(local)
|
|
|
|
|> Page.build_page(page, limit)
|
|
|
|
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)
|
|
|
|
|
|
|
|
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
|
|
|
|
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
|
|
|
|
|
|
|
|
@doc """
|
|
|
|
Builds a page struct for actors by their name or displayed name.
|
|
|
|
"""
|
|
|
|
@spec build_actors_by_username_or_name_page(
|
|
|
|
String.t(),
|
|
|
|
[ActorType.t()],
|
|
|
|
integer | nil,
|
|
|
|
integer | nil
|
|
|
|
) :: Page.t()
|
|
|
|
def build_actors_by_username_or_name_page(username, types, page \\ nil, limit \\ nil) do
|
|
|
|
username
|
|
|
|
|> actor_by_username_or_name_query()
|
|
|
|
|> filter_by_types(types)
|
|
|
|
|> Page.build_page(page, limit)
|
|
|
|
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
|
|
|
|
|
2018-12-14 17:41:55 +01:00
|
|
|
@doc """
|
2019-09-09 00:52:49 +02:00
|
|
|
Gets a local group by its title.
|
2018-12-14 17:41:55 +01:00
|
|
|
"""
|
|
|
|
@spec get_local_group_by_title(String.t()) :: Actor.t() | nil
|
|
|
|
def get_local_group_by_title(title) do
|
2019-09-09 00:52:49 +02:00
|
|
|
group_query()
|
|
|
|
|> filter_by_name([title])
|
2018-12-14 17:41:55 +01:00
|
|
|
|> Repo.one()
|
|
|
|
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
|
|
|
|
|
2019-03-19 11:16:03 +01:00
|
|
|
@doc """
|
2019-09-09 00:52:49 +02:00
|
|
|
Creates a group.
|
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
|
2020-02-18 08:57:00 +01:00
|
|
|
with {:ok, %{insert_group: %Actor{} = group, add_admin_member: %Member{} = _admin_member}} <-
|
|
|
|
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() do
|
|
|
|
{:ok, group}
|
|
|
|
end
|
2018-05-18 09:56:21 +02:00
|
|
|
end
|
|
|
|
|
2018-05-30 14:27:21 +02:00
|
|
|
@doc """
|
2019-09-11 03:16:37 +02:00
|
|
|
Deletes a group.
|
2018-05-30 14:27:21 +02:00
|
|
|
"""
|
2019-09-11 03:16:37 +02:00
|
|
|
def delete_group!(%Actor{type: :Group} = group), do: Repo.delete!(group)
|
2018-05-30 14:27:21 +02:00
|
|
|
|
|
|
|
@doc """
|
2019-09-11 03:16:37 +02:00
|
|
|
Lists the groups.
|
2018-05-30 14:27:21 +02:00
|
|
|
"""
|
2020-02-18 08:57:00 +01:00
|
|
|
@spec list_groups(integer | nil, integer | nil) :: Page.t()
|
2019-09-09 00:52:49 +02:00
|
|
|
def list_groups(page \\ nil, limit \\ nil) do
|
2019-09-11 03:16:37 +02:00
|
|
|
groups_query()
|
2020-02-18 08:57:00 +01:00
|
|
|
|> Page.build_page(page, limit)
|
2019-01-25 09:23:44 +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.
|
|
|
|
"""
|
|
|
|
@spec get_member(integer | String.t()) :: Member.t() | nil
|
|
|
|
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
|
|
|
"""
|
2019-09-11 03:16:37 +02:00
|
|
|
@spec get_member(integer | String.t(), integer | String.t()) ::
|
|
|
|
{: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
|
|
|
|
|
|
|
|
@spec is_member?(integer | String.t(), integer | String.t()) :: boolean()
|
|
|
|
def is_member?(actor_id, parent_id) do
|
|
|
|
match?(
|
|
|
|
{:ok, %Member{}},
|
|
|
|
get_member(actor_id, parent_id, [
|
|
|
|
:member,
|
|
|
|
:moderator,
|
|
|
|
:administrator,
|
|
|
|
:creator
|
|
|
|
])
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
@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
|
|
|
|
|
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
|
2018-08-24 11:34:00 +02:00
|
|
|
with {:ok, %Member{} = member} <-
|
|
|
|
%Member{}
|
|
|
|
|> Member.changeset(attrs)
|
|
|
|
|> Repo.insert() do
|
2020-02-18 08:57:00 +01:00
|
|
|
{:ok, Repo.preload(member, [:actor, :parent, :invited_by])}
|
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,
|
|
|
|
integer | nil,
|
|
|
|
integer | nil
|
|
|
|
) :: Page.t()
|
|
|
|
def list_memberships_for_user(user_id, page, limit) do
|
|
|
|
user_id
|
|
|
|
|> list_members_for_user_query()
|
|
|
|
|> Page.build_page(page, limit)
|
|
|
|
end
|
|
|
|
|
2019-09-11 03:16:37 +02:00
|
|
|
@doc """
|
|
|
|
Returns the list of members for an actor.
|
|
|
|
"""
|
2020-02-18 08:57:00 +01:00
|
|
|
@spec list_members_for_actor(Actor.t(), integer | nil, integer | nil) :: Page.t()
|
|
|
|
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
|
|
|
|
2019-09-11 03:16:37 +02:00
|
|
|
@doc """
|
|
|
|
Returns the list of members for a group.
|
2018-05-18 09:56:21 +02:00
|
|
|
"""
|
2020-02-18 08:57:00 +01:00
|
|
|
@spec list_members_for_group(Actor.t(), integer | nil, integer | nil) :: Page.t()
|
|
|
|
def list_members_for_group(%Actor{id: group_id, type: :Group}, page \\ nil, limit \\ nil) do
|
2019-09-11 03:16:37 +02:00
|
|
|
group_id
|
|
|
|
|> members_for_group_query()
|
2020-02-18 08:57:00 +01:00
|
|
|
|> Page.build_page(page, limit)
|
|
|
|
end
|
|
|
|
|
|
|
|
@spec list_external_members_for_group(Actor.t(), integer | nil, integer | nil) :: Page.t()
|
|
|
|
def list_external_members_for_group(
|
|
|
|
%Actor{id: group_id, type: :Group},
|
|
|
|
page \\ nil,
|
|
|
|
limit \\ nil
|
|
|
|
) do
|
|
|
|
group_id
|
|
|
|
|> members_for_group_query()
|
|
|
|
|> filter_external()
|
|
|
|
|> Page.build_page(page, limit)
|
2018-05-18 09:56:21 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
@doc """
|
2019-09-09 00:52:49 +02:00
|
|
|
Returns the list of administrator members for a group.
|
|
|
|
"""
|
2019-09-11 03:16:37 +02:00
|
|
|
@spec list_administrator_members_for_group(integer | String.t(), integer | nil, integer | nil) ::
|
2020-02-18 08:57:00 +01:00
|
|
|
Page.t()
|
2019-09-09 00:52:49 +02:00
|
|
|
def list_administrator_members_for_group(id, page \\ nil, limit \\ nil) do
|
2019-09-11 03:16:37 +02:00
|
|
|
id
|
|
|
|
|> administrator_members_for_group_query()
|
2020-02-18 08:57:00 +01:00
|
|
|
|> Page.build_page(page, limit)
|
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 """
|
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
|
|
|
|
|
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.
|
|
|
|
"""
|
2019-09-11 03:16:37 +02:00
|
|
|
@spec create_bot(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.
|
|
|
|
"""
|
|
|
|
@spec register_bot(map) :: {:ok, Actor.t()} | {:error, Ecto.Changeset.t()}
|
|
|
|
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
|
|
|
|
2019-12-20 13:04:34 +01:00
|
|
|
@spec get_or_create_internal_actor(String.t()) :: {:ok, Actor.t()}
|
|
|
|
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
|
|
|
|
|
|
|
@doc """
|
2019-09-11 03:16:37 +02:00
|
|
|
Returns the list of bots.
|
2018-05-30 14:27:21 +02:00
|
|
|
"""
|
2019-09-11 03:16:37 +02:00
|
|
|
@spec list_bots :: [Bot.t()]
|
|
|
|
def list_bots, do: Repo.all(Bot)
|
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.
|
|
|
|
"""
|
2019-09-11 03:16:37 +02:00
|
|
|
@spec create_follower(map) :: {:ok, Follower.t()} | {:error, Ecto.Changeset.t()}
|
2018-08-01 14:45:18 +02:00
|
|
|
def create_follower(attrs \\ %{}) do
|
2018-11-27 14:02:51 +01:00
|
|
|
with {:ok, %Follower{} = follower} <-
|
|
|
|
%Follower{}
|
|
|
|
|> Follower.changeset(attrs)
|
|
|
|
|> Repo.insert() do
|
|
|
|
{:ok, Repo.preload(follower, [:actor, :target_actor])}
|
|
|
|
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
|
|
|
|
|
2018-12-14 17:41:55 +01:00
|
|
|
@doc """
|
2019-09-11 03:16:37 +02:00
|
|
|
Returns the list of followers for an actor.
|
|
|
|
If actor A and C both follow actor B, actor B's followers are A and C.
|
|
|
|
"""
|
2019-12-03 11:29:51 +01:00
|
|
|
@spec list_followers_actors_for_actor(Actor.t()) :: [Actor.t()]
|
|
|
|
def list_followers_actors_for_actor(%Actor{id: actor_id}) do
|
2019-09-11 03:16:37 +02:00
|
|
|
actor_id
|
2019-12-03 11:29:51 +01:00
|
|
|
|> 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-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) ::
|
|
|
|
Page.t()
|
|
|
|
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.
|
|
|
|
"""
|
|
|
|
@spec build_followers_for_actor(Actor.t(), integer | nil, integer | nil) :: Page.t()
|
|
|
|
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
|
|
|
|
2019-09-11 03:16:37 +02:00
|
|
|
@doc """
|
|
|
|
Returns the list of followings for an actor.
|
|
|
|
If actor A follows actor B and C, actor A's followings are B and C.
|
|
|
|
"""
|
|
|
|
@spec list_followings_for_actor(Actor.t()) :: [Follower.t()]
|
|
|
|
def list_followings_for_actor(%Actor{id: actor_id}) do
|
|
|
|
actor_id
|
2019-12-03 11:29:51 +01:00
|
|
|
|> followings_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 list of external followings for an actor.
|
|
|
|
"""
|
|
|
|
@spec list_external_followings_for_actor_paginated(Actor.t(), integer | nil, integer | nil) ::
|
|
|
|
Page.t()
|
|
|
|
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
|
|
|
"""
|
2019-09-11 03:16:37 +02:00
|
|
|
@spec build_followings_for_actor(Actor.t(), integer | nil, integer | nil) :: Page.t()
|
|
|
|
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
|
|
|
"""
|
2019-09-11 03:16:37 +02:00
|
|
|
@spec follow(Actor.t(), Actor.t(), String.t() | nil, boolean | nil) ::
|
|
|
|
{:ok, Follower.t()} | {:error, atom, String.t()}
|
2019-09-09 00:52:49 +02:00
|
|
|
def follow(%Actor{} = followed, %Actor{} = follower, url \\ nil, approved \\ true) do
|
|
|
|
with {:suspended, false} <- {:suspended, followed.suspended},
|
|
|
|
# Check if followed has blocked follower
|
2019-09-11 03:16:37 +02:00
|
|
|
{:already_following, nil} <- {:already_following, is_following(follower, followed)} do
|
|
|
|
Logger.info(
|
|
|
|
"Making #{follower.preferred_username} follow #{followed.preferred_username} " <>
|
|
|
|
"(approved: #{approved})"
|
|
|
|
)
|
|
|
|
|
|
|
|
create_follower(%{
|
|
|
|
"actor_id" => follower.id,
|
|
|
|
"target_actor_id" => followed.id,
|
|
|
|
"approved" => approved,
|
|
|
|
"url" => url
|
|
|
|
})
|
2019-09-09 00:52:49 +02:00
|
|
|
else
|
|
|
|
{:already_following, %Follower{}} ->
|
|
|
|
{:error, :already_following,
|
|
|
|
"Could not follow actor: you are already following #{followed.preferred_username}"}
|
|
|
|
|
|
|
|
{:suspended, _} ->
|
|
|
|
{:error, :suspended,
|
|
|
|
"Could not follow actor: #{followed.preferred_username} has been suspended"}
|
|
|
|
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
|
2019-09-11 03:16:37 +02:00
|
|
|
case {:already_following, is_following(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
|
|
|
"""
|
2019-09-11 03:16:37 +02:00
|
|
|
@spec is_following(Actor.t(), Actor.t()) :: Follower.t() | nil
|
|
|
|
def is_following(%Actor{} = follower_actor, %Actor{} = followed_actor) do
|
|
|
|
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.
|
|
|
|
|
|
|
|
Local actors obviously don't need to be updated
|
|
|
|
"""
|
|
|
|
@spec needs_update?(Actor.t()) :: boolean
|
|
|
|
def needs_update?(%Actor{domain: nil}), do: false
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
@spec schedule_key_rotation(Actor.t(), integer()) :: nil
|
|
|
|
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
|
|
|
|
|
2019-09-11 03:16:37 +02:00
|
|
|
@spec remove_banner(Actor.t()) :: {:ok, Actor.t()}
|
2019-06-17 17:15:27 +02:00
|
|
|
defp remove_banner(%Actor{banner: nil} = actor), do: {:ok, actor}
|
|
|
|
|
|
|
|
defp remove_banner(%Actor{banner: %File{url: url}} = actor) do
|
|
|
|
safe_remove_file(url, actor)
|
|
|
|
end
|
|
|
|
|
2019-09-11 03:16:37 +02:00
|
|
|
@spec remove_avatar(Actor.t()) :: {:ok, Actor.t()}
|
2019-06-17 17:15:27 +02:00
|
|
|
defp remove_avatar(%Actor{avatar: nil} = actor), do: {:ok, actor}
|
|
|
|
|
|
|
|
defp remove_avatar(%Actor{avatar: %File{url: url}} = actor) do
|
|
|
|
safe_remove_file(url, actor)
|
|
|
|
end
|
|
|
|
|
2019-09-11 03:16:37 +02:00
|
|
|
@spec safe_remove_file(String.t(), Actor.t()) :: {:ok, Actor.t()}
|
2019-06-17 17:15:27 +02:00
|
|
|
defp safe_remove_file(url, %Actor{} = actor) do
|
2020-01-28 19:18:33 +01:00
|
|
|
case Upload.remove(url) do
|
2019-07-23 18:06:22 +02:00
|
|
|
{:ok, _value} ->
|
|
|
|
{:ok, actor}
|
|
|
|
|
2019-06-17 17:15:27 +02:00
|
|
|
{:error, error} ->
|
|
|
|
Logger.error("Error while removing an upload file")
|
2019-07-30 16:40:59 +02:00
|
|
|
Logger.debug(inspect(error))
|
2019-09-11 03:16:37 +02:00
|
|
|
|
2019-06-17 17:15:27 +02:00
|
|
|
{:ok, actor}
|
|
|
|
end
|
|
|
|
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
|
2020-01-28 19:18:33 +01:00
|
|
|
Upload.remove(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)
|
|
|
|
|> preload([a], [:organized_events, :followers, :followings])
|
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
|
|
|
|
|
|
|
|
@spec actor_by_username_or_name_query(String.t()) :: Ecto.Query.t()
|
|
|
|
defp actor_by_username_or_name_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
|
|
|
|
|
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
|
|
|
|
from(
|
|
|
|
a in Actor,
|
|
|
|
join: m in Member,
|
|
|
|
on: a.id == m.parent_id,
|
|
|
|
where: m.actor_id == ^actor_id
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
@spec groups_query :: Ecto.Query.t()
|
|
|
|
defp groups_query do
|
|
|
|
from(
|
|
|
|
a in Actor,
|
|
|
|
where: a.type == ^:Group,
|
2019-09-16 02:07:44 +02:00
|
|
|
where: a.visibility in ^@public_visibility
|
2019-09-11 03:16:37 +02:00
|
|
|
)
|
|
|
|
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
|
|
|
|
from(
|
|
|
|
m in Member,
|
|
|
|
join: a in Actor,
|
|
|
|
on: m.actor_id == a.id,
|
|
|
|
where: a.user_id == ^user_id and m.role != ^:not_approved,
|
|
|
|
preload: [:parent, :actor, :invited_by]
|
|
|
|
)
|
|
|
|
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,
|
|
|
|
preload: [:parent]
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
@spec members_for_group_query(integer | String.t()) :: Ecto.Query.t()
|
|
|
|
defp members_for_group_query(group_id) do
|
|
|
|
from(
|
|
|
|
m in Member,
|
|
|
|
where: m.parent_id == ^group_id,
|
|
|
|
preload: [:parent, :actor]
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
@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
|
|
|
|
|
2019-09-11 03:16:37 +02:00
|
|
|
@spec filter_local(Ecto.Query.t()) :: Ecto.Query.t()
|
|
|
|
defp filter_local(query) do
|
|
|
|
from(a in query, where: is_nil(a.domain))
|
|
|
|
end
|
|
|
|
|
|
|
|
@spec filter_external(Ecto.Query.t()) :: Ecto.Query.t()
|
|
|
|
defp filter_external(query) do
|
|
|
|
from(a in query, where: not is_nil(a.domain))
|
|
|
|
end
|
|
|
|
|
2019-12-03 11:29:51 +01:00
|
|
|
@spec filter_follower_actors_external(Ecto.Query.t()) :: Ecto.Query.t()
|
|
|
|
defp filter_follower_actors_external(query) do
|
|
|
|
query
|
|
|
|
|> where([_f, a], not is_nil(a.domain))
|
|
|
|
|> preload([f, a], [:target_actor, :actor])
|
|
|
|
end
|
|
|
|
|
2019-09-11 03:16:37 +02:00
|
|
|
@spec filter_by_type(Ecto.Query.t(), ActorType.t()) :: Ecto.Query.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
|
|
|
|
|
|
|
|
@spec filter_by_types(Ecto.Query.t(), [ActorType.t()]) :: Ecto.Query.t()
|
|
|
|
defp filter_by_types(query, types) do
|
|
|
|
from(a in query, where: a.type in ^types)
|
|
|
|
end
|
|
|
|
|
|
|
|
@spec filter_by_name(Ecto.Query.t(), [String.t()]) :: Ecto.Query.t()
|
|
|
|
defp filter_by_name(query, [name]) do
|
|
|
|
from(a in query, where: a.preferred_username == ^name and is_nil(a.domain))
|
|
|
|
end
|
|
|
|
|
|
|
|
defp filter_by_name(query, [name, domain]) do
|
|
|
|
from(a in query, where: a.preferred_username == ^name and a.domain == ^domain)
|
|
|
|
end
|
|
|
|
|
|
|
|
@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
|
2019-12-03 11:29:51 +01:00
|
|
|
|
|
|
|
defp delete_actor_organized_events(%Actor{organized_events: organized_events}) do
|
|
|
|
res =
|
|
|
|
Enum.map(organized_events, fn event ->
|
|
|
|
event =
|
|
|
|
Repo.preload(event, [:organizer_actor, :participants, :picture, :mentions, :comments])
|
|
|
|
|
|
|
|
ActivityPub.delete(event, false)
|
|
|
|
end)
|
|
|
|
|
|
|
|
if Enum.all?(res, fn {status, _, _} -> status == :ok end) do
|
|
|
|
{:ok, res}
|
|
|
|
else
|
|
|
|
{:error, res}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
defp delete_actor_empty_comments(%Actor{comments: comments}) do
|
|
|
|
res =
|
|
|
|
Enum.map(comments, fn comment ->
|
|
|
|
comment =
|
|
|
|
Repo.preload(comment, [:actor, :mentions, :event, :in_reply_to_comment, :origin_comment])
|
|
|
|
|
|
|
|
ActivityPub.delete(comment, false)
|
|
|
|
end)
|
|
|
|
|
|
|
|
if Enum.all?(res, fn {status, _, _} -> status == :ok end) do
|
|
|
|
{:ok, res}
|
|
|
|
else
|
|
|
|
{:error, res}
|
|
|
|
end
|
|
|
|
end
|
2018-05-18 09:56:21 +02:00
|
|
|
end
|