2018-05-18 09:56:21 +02:00
|
|
|
import EctoEnum
|
|
|
|
|
2018-10-11 17:37:39 +02:00
|
|
|
defenum(Mobilizon.Actors.ActorTypeEnum, :actor_type, [
|
2018-07-27 10:45:35 +02:00
|
|
|
:Person,
|
|
|
|
:Application,
|
|
|
|
:Group,
|
|
|
|
:Organization,
|
|
|
|
:Service
|
|
|
|
])
|
2018-05-18 09:56:21 +02:00
|
|
|
|
2019-03-01 17:11:28 +01:00
|
|
|
defenum(Mobilizon.Actors.ActorOpennessEnum, :actor_openness, [
|
2018-11-23 15:03:53 +01:00
|
|
|
:invite_only,
|
|
|
|
:moderated,
|
|
|
|
:open
|
|
|
|
])
|
|
|
|
|
2019-04-25 19:05:05 +02:00
|
|
|
defenum(Mobilizon.Actors.ActorVisibilityEnum, :actor_visibility_type, [
|
|
|
|
:public,
|
|
|
|
:unlisted,
|
|
|
|
# Probably unused
|
|
|
|
:restricted,
|
|
|
|
:private
|
|
|
|
])
|
|
|
|
|
2018-10-11 17:37:39 +02:00
|
|
|
defmodule Mobilizon.Actors.Actor do
|
2018-05-18 09:56:21 +02:00
|
|
|
@moduledoc """
|
|
|
|
Represents an actor (local and remote actors)
|
|
|
|
"""
|
|
|
|
use Ecto.Schema
|
|
|
|
import Ecto.Changeset
|
2018-12-14 11:23:36 +01:00
|
|
|
|
2018-10-11 17:37:39 +02:00
|
|
|
alias Mobilizon.Actors
|
2019-03-05 17:23:05 +01:00
|
|
|
alias Mobilizon.Users.User
|
|
|
|
alias Mobilizon.Actors.{Actor, Follower, Member}
|
2019-03-08 12:25:06 +01:00
|
|
|
alias Mobilizon.Events.{Event, FeedToken}
|
2019-05-22 14:12:11 +02:00
|
|
|
alias Mobilizon.Media.File
|
2018-05-18 09:56:21 +02:00
|
|
|
|
2019-04-25 19:05:05 +02:00
|
|
|
alias MobilizonWeb.Router.Helpers, as: Routes
|
|
|
|
alias MobilizonWeb.Endpoint
|
|
|
|
|
2018-05-18 09:56:21 +02:00
|
|
|
import Ecto.Query
|
2018-12-14 11:23:36 +01:00
|
|
|
import Mobilizon.Ecto
|
2018-10-11 17:37:39 +02:00
|
|
|
alias Mobilizon.Repo
|
2018-05-18 09:56:21 +02:00
|
|
|
|
2018-11-08 16:11:23 +01:00
|
|
|
require Logger
|
2018-05-18 09:56:21 +02:00
|
|
|
|
2018-07-27 10:45:35 +02:00
|
|
|
# @type t :: %Actor{description: String.t, id: integer(), inserted_at: DateTime.t, updated_at: DateTime.t, display_name: String.t, domain: String.t, keys: String.t, suspended: boolean(), url: String.t, username: String.t, organized_events: list(), groups: list(), group_request: list(), user: User.t, field: ActorTypeEnum.t}
|
2018-05-18 09:56:21 +02:00
|
|
|
|
|
|
|
schema "actors" do
|
2018-07-27 10:45:35 +02:00
|
|
|
field(:url, :string)
|
|
|
|
field(:outbox_url, :string)
|
|
|
|
field(:inbox_url, :string)
|
|
|
|
field(:following_url, :string)
|
|
|
|
field(:followers_url, :string)
|
|
|
|
field(:shared_inbox_url, :string)
|
2018-10-11 17:37:39 +02:00
|
|
|
field(:type, Mobilizon.Actors.ActorTypeEnum, default: :Person)
|
2018-07-27 10:45:35 +02:00
|
|
|
field(:name, :string)
|
2019-01-21 15:08:22 +01:00
|
|
|
field(:domain, :string, default: nil)
|
2018-07-27 10:45:35 +02:00
|
|
|
field(:summary, :string)
|
|
|
|
field(:preferred_username, :string)
|
|
|
|
field(:keys, :string)
|
|
|
|
field(:manually_approves_followers, :boolean, default: false)
|
2019-03-01 17:11:28 +01:00
|
|
|
field(:openness, Mobilizon.Actors.ActorOpennessEnum, default: :moderated)
|
2019-04-25 19:05:05 +02:00
|
|
|
field(:visibility, Mobilizon.Actors.ActorVisibilityEnum, default: :private)
|
2018-07-27 10:45:35 +02:00
|
|
|
field(:suspended, :boolean, default: false)
|
2019-02-25 18:35:00 +01:00
|
|
|
# field(:openness, Mobilizon.Actors.ActorOpennessEnum, default: :moderated)
|
2018-11-27 14:02:51 +01:00
|
|
|
has_many(:followers, Follower, foreign_key: :target_actor_id)
|
|
|
|
has_many(:followings, Follower, foreign_key: :actor_id)
|
2018-07-27 10:45:35 +02:00
|
|
|
has_many(:organized_events, Event, foreign_key: :organizer_actor_id)
|
|
|
|
many_to_many(:memberships, Actor, join_through: Member)
|
|
|
|
belongs_to(:user, User)
|
2019-03-08 12:25:06 +01:00
|
|
|
has_many(:feed_tokens, FeedToken, foreign_key: :actor_id)
|
2019-05-22 14:12:11 +02:00
|
|
|
embeds_one(:avatar, File)
|
|
|
|
embeds_one(:banner, File)
|
2018-05-18 09:56:21 +02:00
|
|
|
|
|
|
|
timestamps()
|
|
|
|
end
|
|
|
|
|
|
|
|
@doc false
|
|
|
|
def changeset(%Actor{} = actor, attrs) do
|
|
|
|
actor
|
2018-07-27 10:45:35 +02:00
|
|
|
|> Ecto.Changeset.cast(attrs, [
|
|
|
|
:url,
|
|
|
|
:outbox_url,
|
|
|
|
:inbox_url,
|
|
|
|
:shared_inbox_url,
|
|
|
|
:following_url,
|
|
|
|
:followers_url,
|
|
|
|
:type,
|
|
|
|
:name,
|
|
|
|
:domain,
|
|
|
|
:summary,
|
|
|
|
:preferred_username,
|
|
|
|
:keys,
|
|
|
|
:manually_approves_followers,
|
|
|
|
:suspended,
|
|
|
|
:user_id
|
|
|
|
])
|
2018-12-03 11:58:57 +01:00
|
|
|
|> build_urls()
|
2019-05-22 14:12:11 +02:00
|
|
|
|> cast_embed(:avatar)
|
|
|
|
|> cast_embed(:banner)
|
2019-02-25 18:35:00 +01:00
|
|
|
|> unique_username_validator()
|
2018-06-14 17:25:55 +02:00
|
|
|
|> validate_required([:preferred_username, :keys, :suspended, :url])
|
2018-12-03 11:58:57 +01:00
|
|
|
|> unique_constraint(:preferred_username, name: :actors_preferred_username_domain_type_index)
|
|
|
|
|> unique_constraint(:url, name: :actors_url_index)
|
2018-05-18 09:56:21 +02:00
|
|
|
end
|
|
|
|
|
2019-03-19 11:16:03 +01:00
|
|
|
@doc """
|
|
|
|
Changeset for person registration
|
|
|
|
"""
|
|
|
|
@spec registration_changeset(struct(), map()) :: Ecto.Changeset.t()
|
2018-05-18 09:56:21 +02:00
|
|
|
def registration_changeset(%Actor{} = actor, attrs) do
|
|
|
|
actor
|
2018-07-27 10:45:35 +02:00
|
|
|
|> Ecto.Changeset.cast(attrs, [
|
|
|
|
:preferred_username,
|
|
|
|
:domain,
|
|
|
|
:name,
|
|
|
|
:summary,
|
|
|
|
:keys,
|
|
|
|
:suspended,
|
|
|
|
:url,
|
|
|
|
:type,
|
|
|
|
:user_id
|
|
|
|
])
|
2018-12-03 11:58:57 +01:00
|
|
|
|> build_urls()
|
2019-05-22 14:12:11 +02:00
|
|
|
|> cast_embed(:avatar)
|
|
|
|
|> cast_embed(:banner)
|
2019-01-29 11:02:32 +01:00
|
|
|
# Needed because following constraint can't work for domain null values (local)
|
|
|
|
|> unique_username_validator()
|
2018-12-03 11:58:57 +01:00
|
|
|
|> unique_constraint(:preferred_username, name: :actors_preferred_username_domain_type_index)
|
|
|
|
|> unique_constraint(:url, name: :actors_url_index)
|
2018-06-14 17:25:55 +02:00
|
|
|
|> validate_required([:preferred_username, :keys, :suspended, :url, :type])
|
2018-05-18 09:56:21 +02:00
|
|
|
end
|
|
|
|
|
2018-08-01 14:45:18 +02:00
|
|
|
# TODO : Use me !
|
2018-11-12 23:30:47 +01:00
|
|
|
# @email_regex ~r/^[a-zA-Z0-9.!#$%&'*+\/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$/
|
2019-03-19 11:16:03 +01:00
|
|
|
@doc """
|
|
|
|
Changeset for remote actor creation
|
|
|
|
"""
|
|
|
|
@spec remote_actor_creation(map()) :: Ecto.Changeset.t()
|
2018-05-18 09:56:21 +02:00
|
|
|
def remote_actor_creation(params) do
|
|
|
|
changes =
|
|
|
|
%Actor{}
|
2018-07-27 10:45:35 +02:00
|
|
|
|> Ecto.Changeset.cast(params, [
|
|
|
|
:url,
|
|
|
|
:outbox_url,
|
|
|
|
:inbox_url,
|
|
|
|
:shared_inbox_url,
|
|
|
|
:following_url,
|
|
|
|
:followers_url,
|
|
|
|
:type,
|
|
|
|
:name,
|
|
|
|
:domain,
|
|
|
|
:summary,
|
|
|
|
:preferred_username,
|
|
|
|
:keys,
|
2019-05-22 14:12:11 +02:00
|
|
|
:manually_approves_followers
|
2018-07-27 10:45:35 +02:00
|
|
|
])
|
|
|
|
|> validate_required([
|
|
|
|
:url,
|
|
|
|
:outbox_url,
|
|
|
|
:inbox_url,
|
|
|
|
:type,
|
|
|
|
:domain,
|
|
|
|
:preferred_username,
|
|
|
|
:keys
|
|
|
|
])
|
2019-05-22 14:12:11 +02:00
|
|
|
|> cast_embed(:avatar)
|
|
|
|
|> cast_embed(:banner)
|
2019-02-25 18:35:00 +01:00
|
|
|
# Needed because following constraint can't work for domain null values (local)
|
|
|
|
|> unique_username_validator()
|
2018-12-03 11:58:57 +01:00
|
|
|
|> unique_constraint(:preferred_username, name: :actors_preferred_username_domain_type_index)
|
|
|
|
|> unique_constraint(:url, name: :actors_url_index)
|
2018-05-18 09:56:21 +02:00
|
|
|
|> validate_length(:summary, max: 5000)
|
|
|
|
|> validate_length(:preferred_username, max: 100)
|
|
|
|
|> put_change(:local, false)
|
|
|
|
|
|
|
|
Logger.debug("Remote actor creation")
|
2018-07-27 10:45:35 +02:00
|
|
|
Logger.debug(inspect(changes))
|
2018-05-18 09:56:21 +02:00
|
|
|
changes
|
|
|
|
end
|
|
|
|
|
2019-03-19 11:16:03 +01:00
|
|
|
@doc """
|
|
|
|
Changeset for group creation
|
|
|
|
"""
|
|
|
|
@spec group_creation(struct(), map()) :: Ecto.Changeset.t()
|
2018-05-30 18:59:13 +02:00
|
|
|
def group_creation(%Actor{} = actor, params) do
|
|
|
|
actor
|
2018-07-27 10:45:35 +02:00
|
|
|
|> Ecto.Changeset.cast(params, [
|
|
|
|
:url,
|
|
|
|
:outbox_url,
|
|
|
|
:inbox_url,
|
|
|
|
:shared_inbox_url,
|
|
|
|
:type,
|
|
|
|
:name,
|
|
|
|
:domain,
|
|
|
|
:summary,
|
2019-05-22 14:12:11 +02:00
|
|
|
:preferred_username
|
2018-07-27 10:45:35 +02:00
|
|
|
])
|
2019-05-22 14:12:11 +02:00
|
|
|
|> cast_embed(:avatar)
|
|
|
|
|> cast_embed(:banner)
|
2018-12-03 11:58:57 +01:00
|
|
|
|> build_urls(:Group)
|
|
|
|
|> put_change(:domain, nil)
|
2018-12-14 17:41:55 +01:00
|
|
|
|> put_change(:keys, Actors.create_keys())
|
2018-12-03 11:58:57 +01:00
|
|
|
|> put_change(:type, :Group)
|
2019-02-25 18:35:00 +01:00
|
|
|
|> unique_username_validator()
|
2018-12-03 11:58:57 +01:00
|
|
|
|> validate_required([:url, :outbox_url, :inbox_url, :type, :preferred_username])
|
|
|
|
|> unique_constraint(:preferred_username, name: :actors_preferred_username_domain_type_index)
|
|
|
|
|> unique_constraint(:url, name: :actors_url_index)
|
|
|
|
|> validate_length(:summary, max: 5000)
|
|
|
|
|> validate_length(:preferred_username, max: 100)
|
|
|
|
|> put_change(:local, true)
|
|
|
|
end
|
|
|
|
|
2019-02-25 18:35:00 +01:00
|
|
|
defp unique_username_validator(
|
|
|
|
%Ecto.Changeset{changes: %{preferred_username: username} = changes} = changeset
|
|
|
|
) do
|
|
|
|
with nil <- Map.get(changes, :domain, nil),
|
|
|
|
%Actor{preferred_username: _username} <- Actors.get_local_actor_by_name(username) do
|
2019-01-29 11:02:32 +01:00
|
|
|
changeset |> add_error(:preferred_username, "Username is already taken")
|
|
|
|
else
|
2019-02-25 18:35:00 +01:00
|
|
|
_ -> changeset
|
2019-01-29 11:02:32 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-02-25 18:35:00 +01:00
|
|
|
# When we don't even have any preferred_username, don't even try validating preferred_username
|
|
|
|
defp unique_username_validator(changeset) do
|
|
|
|
changeset
|
|
|
|
end
|
|
|
|
|
2018-12-03 12:08:18 +01:00
|
|
|
@spec build_urls(Ecto.Changeset.t(), atom()) :: Ecto.Changeset.t()
|
2018-12-03 11:58:57 +01:00
|
|
|
defp build_urls(changeset, type \\ :Person)
|
2018-12-03 12:08:18 +01:00
|
|
|
|
2019-04-25 19:05:05 +02:00
|
|
|
defp build_urls(%Ecto.Changeset{changes: %{preferred_username: username}} = changeset, _type) do
|
2018-12-03 11:58:57 +01:00
|
|
|
changeset
|
2018-07-27 10:45:35 +02:00
|
|
|
|> put_change(
|
|
|
|
:outbox_url,
|
2019-04-25 19:05:05 +02:00
|
|
|
build_url(username, :outbox)
|
2018-07-27 10:45:35 +02:00
|
|
|
)
|
|
|
|
|> put_change(
|
|
|
|
:inbox_url,
|
2019-04-25 19:05:05 +02:00
|
|
|
build_url(username, :inbox)
|
2018-07-27 10:45:35 +02:00
|
|
|
)
|
2018-10-11 17:37:39 +02:00
|
|
|
|> put_change(:shared_inbox_url, "#{MobilizonWeb.Endpoint.url()}/inbox")
|
2019-04-25 19:05:05 +02:00
|
|
|
|> put_change(:url, build_url(username, :page))
|
2018-05-30 18:59:13 +02:00
|
|
|
end
|
|
|
|
|
2018-12-03 11:58:57 +01:00
|
|
|
defp build_urls(%Ecto.Changeset{} = changeset, _type), do: changeset
|
|
|
|
|
2019-04-25 19:05:05 +02:00
|
|
|
@doc """
|
|
|
|
Build an AP URL for an actor
|
|
|
|
"""
|
|
|
|
@spec build_url(String.t(), atom()) :: String.t()
|
|
|
|
def build_url(preferred_username, endpoint, args \\ [])
|
|
|
|
|
|
|
|
def build_url(preferred_username, :page, args) do
|
|
|
|
Endpoint
|
|
|
|
|> Routes.page_url(:actor, preferred_username, args)
|
|
|
|
|> URI.decode()
|
|
|
|
end
|
|
|
|
|
|
|
|
def build_url(username, :inbox, _args), do: "#{build_url(username, :page)}/inbox"
|
|
|
|
|
|
|
|
def build_url(preferred_username, endpoint, args)
|
|
|
|
when endpoint in [:outbox, :following, :followers] do
|
|
|
|
Endpoint
|
|
|
|
|> Routes.activity_pub_url(endpoint, preferred_username, args)
|
|
|
|
|> URI.decode()
|
|
|
|
end
|
|
|
|
|
2018-11-12 18:17:53 +01:00
|
|
|
@doc """
|
|
|
|
Get a public key for a given ActivityPub actor ID (url)
|
|
|
|
"""
|
2019-02-22 16:11:57 +01:00
|
|
|
@spec get_public_key_for_url(String.t()) :: {:ok, String.t()} | {:error, atom()}
|
2018-05-18 09:56:21 +02:00
|
|
|
def get_public_key_for_url(url) do
|
2018-11-12 18:17:53 +01:00
|
|
|
with {:ok, %Actor{keys: keys}} <- Actors.get_or_fetch_by_url(url),
|
|
|
|
{:ok, public_key} <- prepare_public_key(keys) do
|
|
|
|
{:ok, public_key}
|
2018-05-18 09:56:21 +02:00
|
|
|
else
|
2018-11-12 18:17:53 +01:00
|
|
|
{:error, :pem_decode_error} ->
|
|
|
|
Logger.error("Error while decoding PEM")
|
|
|
|
{:error, :pem_decode_error}
|
|
|
|
|
2018-11-08 16:11:23 +01:00
|
|
|
_ ->
|
|
|
|
Logger.error("Unable to fetch actor, so no keys for you")
|
|
|
|
{:error, :actor_fetch_error}
|
2018-05-18 09:56:21 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-11-12 18:17:53 +01:00
|
|
|
@doc """
|
|
|
|
Convert internal PEM encoded keys to public key format
|
|
|
|
"""
|
|
|
|
@spec prepare_public_key(String.t()) :: {:ok, tuple()} | {:error, :pem_decode_error}
|
|
|
|
def prepare_public_key(public_key_code) do
|
|
|
|
with [public_key_entry] <- :public_key.pem_decode(public_key_code) do
|
|
|
|
{:ok, :public_key.pem_entry_decode(public_key_entry)}
|
|
|
|
else
|
|
|
|
_err ->
|
|
|
|
{:error, :pem_decode_error}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-08-03 10:16:22 +02:00
|
|
|
@doc """
|
|
|
|
Get followers from an actor
|
|
|
|
|
|
|
|
If actor A and C both follow actor B, actor B's followers are A and C
|
|
|
|
"""
|
2019-04-25 19:05:05 +02:00
|
|
|
@spec get_followers(struct(), number(), number()) :: map()
|
2018-12-14 11:23:36 +01:00
|
|
|
def get_followers(%Actor{id: actor_id} = _actor, page \\ nil, limit \\ nil) do
|
2019-04-25 19:05:05 +02:00
|
|
|
query =
|
|
|
|
from(
|
|
|
|
a in Actor,
|
|
|
|
join: f in Follower,
|
|
|
|
on: a.id == f.actor_id,
|
|
|
|
where: f.target_actor_id == ^actor_id
|
|
|
|
)
|
|
|
|
|
|
|
|
total = Task.async(fn -> Repo.aggregate(query, :count, :id) end)
|
|
|
|
elements = Task.async(fn -> Repo.all(paginate(query, page, limit)) end)
|
|
|
|
|
|
|
|
%{total: Task.await(total), elements: Task.await(elements)}
|
|
|
|
end
|
|
|
|
|
|
|
|
@spec get_full_followers(struct()) :: list()
|
|
|
|
def get_full_followers(%Actor{id: actor_id} = _actor) do
|
2018-05-18 09:56:21 +02:00
|
|
|
Repo.all(
|
2018-07-27 10:45:35 +02:00
|
|
|
from(
|
|
|
|
a in Actor,
|
|
|
|
join: f in Follower,
|
|
|
|
on: a.id == f.actor_id,
|
2018-12-14 11:23:36 +01:00
|
|
|
where: f.target_actor_id == ^actor_id
|
2018-07-27 10:45:35 +02:00
|
|
|
)
|
2018-05-18 09:56:21 +02:00
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2018-08-03 10:16:22 +02:00
|
|
|
@doc """
|
|
|
|
Get followings from an actor
|
|
|
|
|
|
|
|
If actor A follows actor B and C, actor A's followings are B and B
|
|
|
|
"""
|
2019-03-19 11:16:03 +01:00
|
|
|
@spec get_followings(struct(), number(), number()) :: list()
|
2018-12-14 11:23:36 +01:00
|
|
|
def get_followings(%Actor{id: actor_id} = _actor, page \\ nil, limit \\ nil) do
|
2019-04-25 19:05:05 +02:00
|
|
|
query =
|
|
|
|
from(
|
|
|
|
a in Actor,
|
|
|
|
join: f in Follower,
|
|
|
|
on: a.id == f.target_actor_id,
|
|
|
|
where: f.actor_id == ^actor_id
|
|
|
|
)
|
|
|
|
|
|
|
|
total = Task.async(fn -> Repo.aggregate(query, :count, :id) end)
|
|
|
|
elements = Task.async(fn -> Repo.all(paginate(query, page, limit)) end)
|
|
|
|
|
|
|
|
%{total: Task.await(total), elements: Task.await(elements)}
|
|
|
|
end
|
|
|
|
|
|
|
|
@spec get_full_followings(struct()) :: list()
|
|
|
|
def get_full_followings(%Actor{id: actor_id} = _actor) do
|
2018-05-18 09:56:21 +02:00
|
|
|
Repo.all(
|
2018-07-27 10:45:35 +02:00
|
|
|
from(
|
|
|
|
a in Actor,
|
|
|
|
join: f in Follower,
|
|
|
|
on: a.id == f.target_actor_id,
|
2018-12-14 11:23:36 +01:00
|
|
|
where: f.actor_id == ^actor_id
|
2018-07-27 10:45:35 +02:00
|
|
|
)
|
2018-05-18 09:56:21 +02:00
|
|
|
)
|
|
|
|
end
|
2018-08-24 11:34:00 +02:00
|
|
|
|
2019-03-19 11:16:03 +01:00
|
|
|
@doc """
|
|
|
|
Returns the groups an actor is member of
|
|
|
|
"""
|
|
|
|
@spec get_groups_member_of(struct()) :: list()
|
2018-08-24 11:34:00 +02:00
|
|
|
def get_groups_member_of(%Actor{id: actor_id}) do
|
|
|
|
Repo.all(
|
|
|
|
from(
|
|
|
|
a in Actor,
|
|
|
|
join: m in Member,
|
|
|
|
on: a.id == m.parent_id,
|
|
|
|
where: m.actor_id == ^actor_id
|
|
|
|
)
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2019-03-19 11:16:03 +01:00
|
|
|
@doc """
|
|
|
|
Returns the members for a group actor
|
|
|
|
"""
|
|
|
|
@spec get_members_for_group(struct()) :: list()
|
2018-08-24 11:34:00 +02:00
|
|
|
def get_members_for_group(%Actor{id: actor_id}) do
|
|
|
|
Repo.all(
|
|
|
|
from(
|
|
|
|
a in Actor,
|
|
|
|
join: m in Member,
|
|
|
|
on: a.id == m.actor_id,
|
|
|
|
where: m.parent_id == ^actor_id
|
|
|
|
)
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2019-03-19 11:16:03 +01:00
|
|
|
@doc """
|
|
|
|
Make an actor follow another
|
|
|
|
"""
|
2018-11-27 14:02:51 +01:00
|
|
|
@spec follow(struct(), struct(), boolean()) :: Follower.t() | {:error, String.t()}
|
2018-11-27 17:54:54 +01:00
|
|
|
def follow(%Actor{} = followed, %Actor{} = follower, approved \\ true) do
|
2018-11-12 09:05:31 +01:00
|
|
|
with {:suspended, false} <- {:suspended, followed.suspended},
|
|
|
|
# Check if followed has blocked follower
|
|
|
|
{:already_following, false} <- {:already_following, following?(follower, followed)} do
|
|
|
|
do_follow(follower, followed, approved)
|
|
|
|
else
|
2018-12-14 17:41:55 +01:00
|
|
|
{:already_following, %Follower{}} ->
|
2018-08-24 11:34:00 +02:00
|
|
|
{:error,
|
|
|
|
"Could not follow actor: you are already following #{followed.preferred_username}"}
|
|
|
|
|
2018-11-12 09:05:31 +01:00
|
|
|
{:suspended, _} ->
|
|
|
|
{:error, "Could not follow actor: #{followed.preferred_username} has been suspended"}
|
2018-08-24 11:34:00 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-03-19 11:16:03 +01:00
|
|
|
@doc """
|
|
|
|
Unfollow an actor (remove a `Mobilizon.Actors.Follower`)
|
|
|
|
"""
|
2018-12-14 17:41:55 +01:00
|
|
|
@spec unfollow(struct(), struct()) :: {:ok, Follower.t()} | {:error, Ecto.Changeset.t()}
|
|
|
|
def unfollow(%Actor{} = followed, %Actor{} = follower) do
|
|
|
|
with {:already_following, %Follower{} = follow} <-
|
|
|
|
{:already_following, following?(follower, followed)} do
|
|
|
|
Actors.delete_follower(follow)
|
|
|
|
else
|
|
|
|
{:already_following, false} ->
|
|
|
|
{:error, "Could not unfollow actor: you are not following #{followed.preferred_username}"}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-03-19 11:16:03 +01:00
|
|
|
@spec do_follow(struct(), struct(), boolean) ::
|
|
|
|
{:ok, Follower.t()} | {:error, Ecto.Changeset.t()}
|
2018-11-12 23:30:47 +01:00
|
|
|
defp do_follow(%Actor{} = follower, %Actor{} = followed, approved) do
|
2018-11-12 09:05:31 +01:00
|
|
|
Actors.create_follower(%{
|
|
|
|
"actor_id" => follower.id,
|
|
|
|
"target_actor_id" => followed.id,
|
|
|
|
"approved" => approved
|
|
|
|
})
|
|
|
|
end
|
|
|
|
|
2019-03-19 11:16:03 +01:00
|
|
|
@doc """
|
|
|
|
Returns whether an actor is following another
|
|
|
|
"""
|
|
|
|
@spec following?(struct(), struct()) :: Follower.t() | false
|
2018-11-27 17:54:54 +01:00
|
|
|
def following?(
|
2018-12-14 17:41:55 +01:00
|
|
|
%Actor{} = follower_actor,
|
|
|
|
%Actor{} = followed_actor
|
2018-11-27 17:54:54 +01:00
|
|
|
) do
|
2018-12-14 17:41:55 +01:00
|
|
|
case Actors.get_follower(followed_actor, follower_actor) do
|
|
|
|
nil -> false
|
|
|
|
%Follower{} = follow -> follow
|
|
|
|
end
|
2018-08-24 11:34:00 +02:00
|
|
|
end
|
2018-12-03 12:29:31 +01:00
|
|
|
|
2019-04-25 19:05:05 +02:00
|
|
|
@spec public_visibility?(struct()) :: boolean()
|
|
|
|
def public_visibility?(%Actor{visibility: visibility}), do: visibility in [:public, :unlisted]
|
|
|
|
|
2019-02-27 16:28:09 +01:00
|
|
|
@doc """
|
|
|
|
Return the preferred_username with the eventual @domain suffix if it's a distant actor
|
|
|
|
"""
|
2018-12-03 12:29:31 +01:00
|
|
|
@spec actor_acct_from_actor(struct()) :: String.t()
|
|
|
|
def actor_acct_from_actor(%Actor{preferred_username: preferred_username, domain: domain}) do
|
|
|
|
if is_nil(domain) do
|
|
|
|
preferred_username
|
|
|
|
else
|
|
|
|
"#{preferred_username}@#{domain}"
|
|
|
|
end
|
|
|
|
end
|
2019-02-27 16:28:09 +01:00
|
|
|
|
|
|
|
@doc """
|
|
|
|
Returns the display name if available, or the preferred_username (with the eventual @domain suffix if it's a distant actor).
|
|
|
|
"""
|
|
|
|
@spec display_name(struct()) :: String.t()
|
|
|
|
def display_name(%Actor{name: name} = actor) do
|
|
|
|
case name do
|
|
|
|
nil -> actor_acct_from_actor(actor)
|
|
|
|
"" -> actor_acct_from_actor(actor)
|
|
|
|
name -> name
|
|
|
|
end
|
|
|
|
end
|
2019-03-01 18:30:46 +01:00
|
|
|
|
2019-03-19 11:16:03 +01:00
|
|
|
@doc """
|
|
|
|
Return display name and username
|
|
|
|
|
|
|
|
## Examples
|
|
|
|
iex> display_name_and_username(%Actor{name: "Thomas C", preferred_username: "tcit", domain: nil})
|
|
|
|
"Thomas (tcit)"
|
|
|
|
|
|
|
|
iex> display_name_and_username(%Actor{name: "Thomas C", preferred_username: "tcit", domain: "framapiaf.org"})
|
|
|
|
"Thomas (tcit@framapiaf.org)"
|
|
|
|
|
|
|
|
iex> display_name_and_username(%Actor{name: nil, preferred_username: "tcit", domain: "framapiaf.org"})
|
|
|
|
"tcit@framapiaf.org"
|
|
|
|
|
|
|
|
"""
|
|
|
|
@spec display_name_and_username(struct()) :: String.t()
|
2019-03-04 17:20:18 +01:00
|
|
|
def display_name_and_username(%Actor{name: nil} = actor), do: actor_acct_from_actor(actor)
|
|
|
|
def display_name_and_username(%Actor{name: ""} = actor), do: actor_acct_from_actor(actor)
|
|
|
|
|
|
|
|
def display_name_and_username(%Actor{name: name} = actor),
|
|
|
|
do: name <> " (" <> actor_acct_from_actor(actor) <> ")"
|
|
|
|
|
2019-03-19 11:16:03 +01:00
|
|
|
@doc """
|
|
|
|
Clear multiple caches for an actor
|
|
|
|
"""
|
|
|
|
@spec clear_cache(struct()) :: {:ok, true}
|
2019-03-01 18:30:46 +01:00
|
|
|
def clear_cache(%Actor{preferred_username: preferred_username, domain: nil}) do
|
|
|
|
Cachex.del(:activity_pub, "actor_" <> preferred_username)
|
2019-03-19 11:16:03 +01:00
|
|
|
Cachex.del(:feed, "actor_" <> preferred_username)
|
|
|
|
Cachex.del(:ics, "actor_" <> preferred_username)
|
2019-03-01 18:30:46 +01:00
|
|
|
end
|
2018-05-18 09:56:21 +02:00
|
|
|
end
|