2018-10-11 17:37:39 +02:00
|
|
|
defmodule Mobilizon.Actors.Actor do
|
2018-05-18 09:56:21 +02:00
|
|
|
@moduledoc """
|
2019-09-09 00:52:49 +02:00
|
|
|
Represents an actor (local and remote).
|
2018-05-18 09:56:21 +02:00
|
|
|
"""
|
2019-09-08 01:49:56 +02:00
|
|
|
|
2018-05-18 09:56:21 +02:00
|
|
|
use Ecto.Schema
|
2019-09-08 01:49:56 +02:00
|
|
|
|
2018-05-18 09:56:21 +02:00
|
|
|
import Ecto.Changeset
|
2018-12-14 11:23:36 +01:00
|
|
|
|
2020-08-05 16:44:08 +02:00
|
|
|
alias Mobilizon.{Actors, Addresses, Config, Crypto, Mention, Share}
|
2019-09-13 01:55:45 +02:00
|
|
|
alias Mobilizon.Actors.{ActorOpenness, ActorType, ActorVisibility, Follower, Member}
|
2020-08-05 16:44:08 +02:00
|
|
|
alias Mobilizon.Addresses.Address
|
2020-07-09 17:24:28 +02:00
|
|
|
alias Mobilizon.Discussions.Comment
|
2020-02-18 08:57:00 +01:00
|
|
|
alias Mobilizon.Events.{Event, FeedToken}
|
2020-11-26 11:41:13 +01:00
|
|
|
alias Mobilizon.Medias.File
|
2019-09-22 16:26:23 +02:00
|
|
|
alias Mobilizon.Reports.{Note, Report}
|
2019-09-08 01:49:56 +02:00
|
|
|
alias Mobilizon.Users.User
|
2019-07-23 13:49:22 +02:00
|
|
|
|
2020-01-26 21:36:50 +01:00
|
|
|
alias Mobilizon.Web.Endpoint
|
|
|
|
alias Mobilizon.Web.Router.Helpers, as: Routes
|
2020-11-17 19:14:55 +01:00
|
|
|
import Mobilizon.Web.Gettext, only: [dgettext: 2]
|
2019-04-25 19:05:05 +02:00
|
|
|
|
2018-11-08 16:11:23 +01:00
|
|
|
require Logger
|
2018-05-18 09:56:21 +02:00
|
|
|
|
2019-09-09 00:52:49 +02:00
|
|
|
@type t :: %__MODULE__{
|
|
|
|
url: String.t(),
|
|
|
|
outbox_url: String.t(),
|
|
|
|
inbox_url: String.t(),
|
|
|
|
following_url: String.t(),
|
|
|
|
followers_url: String.t(),
|
|
|
|
shared_inbox_url: String.t(),
|
2020-07-09 17:24:28 +02:00
|
|
|
resources_url: String.t(),
|
|
|
|
posts_url: String.t(),
|
|
|
|
events_url: String.t(),
|
2019-09-09 00:52:49 +02:00
|
|
|
type: ActorType.t(),
|
|
|
|
name: String.t(),
|
|
|
|
domain: String.t(),
|
|
|
|
summary: String.t(),
|
|
|
|
preferred_username: String.t(),
|
|
|
|
keys: String.t(),
|
|
|
|
manually_approves_followers: boolean,
|
|
|
|
openness: ActorOpenness.t(),
|
|
|
|
visibility: ActorVisibility.t(),
|
|
|
|
suspended: boolean,
|
|
|
|
avatar: File.t(),
|
|
|
|
banner: File.t(),
|
|
|
|
user: User.t(),
|
|
|
|
followers: [Follower.t()],
|
|
|
|
followings: [Follower.t()],
|
|
|
|
organized_events: [Event.t()],
|
2019-12-03 11:29:51 +01:00
|
|
|
comments: [Comment.t()],
|
2019-09-09 00:52:49 +02:00
|
|
|
feed_tokens: [FeedToken.t()],
|
|
|
|
created_reports: [Report.t()],
|
|
|
|
subject_reports: [Report.t()],
|
|
|
|
report_notes: [Note.t()],
|
2019-10-25 17:43:37 +02:00
|
|
|
mentions: [Mention.t()],
|
2019-12-03 11:29:51 +01:00
|
|
|
shares: [Share.t()],
|
|
|
|
owner_shares: [Share.t()],
|
2020-02-14 17:56:36 +01:00
|
|
|
memberships: [t],
|
2020-08-05 16:44:08 +02:00
|
|
|
last_refreshed_at: DateTime.t(),
|
|
|
|
physical_address: Address.t()
|
2019-09-09 00:52:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@required_attrs [:preferred_username, :keys, :suspended, :url]
|
|
|
|
@optional_attrs [
|
|
|
|
:outbox_url,
|
|
|
|
:inbox_url,
|
|
|
|
:shared_inbox_url,
|
|
|
|
:following_url,
|
|
|
|
:followers_url,
|
2020-07-09 17:24:28 +02:00
|
|
|
:posts_url,
|
|
|
|
:events_url,
|
|
|
|
:todos_url,
|
|
|
|
:discussions_url,
|
2019-09-09 00:52:49 +02:00
|
|
|
:type,
|
|
|
|
:name,
|
|
|
|
:domain,
|
|
|
|
:summary,
|
|
|
|
:manually_approves_followers,
|
2020-02-14 17:56:36 +01:00
|
|
|
:last_refreshed_at,
|
2020-02-18 08:57:00 +01:00
|
|
|
:user_id,
|
2020-08-05 16:44:08 +02:00
|
|
|
:physical_address_id,
|
2020-02-18 08:57:00 +01:00
|
|
|
:visibility
|
2019-09-09 00:52:49 +02:00
|
|
|
]
|
|
|
|
@attrs @required_attrs ++ @optional_attrs
|
|
|
|
|
2019-09-09 09:55:08 +02:00
|
|
|
@update_required_attrs @required_attrs -- [:url]
|
2020-11-06 11:34:32 +01:00
|
|
|
@update_optional_attrs [
|
|
|
|
:name,
|
|
|
|
:summary,
|
|
|
|
:manually_approves_followers,
|
|
|
|
:user_id,
|
|
|
|
:visibility,
|
|
|
|
:openness
|
|
|
|
]
|
2019-09-09 00:52:49 +02:00
|
|
|
@update_attrs @update_required_attrs ++ @update_optional_attrs
|
|
|
|
|
|
|
|
@registration_required_attrs [:preferred_username, :keys, :suspended, :url, :type]
|
|
|
|
@registration_optional_attrs [:domain, :name, :summary, :user_id]
|
|
|
|
@registration_attrs @registration_required_attrs ++ @registration_optional_attrs
|
|
|
|
|
|
|
|
@remote_actor_creation_required_attrs [
|
|
|
|
:url,
|
|
|
|
:inbox_url,
|
|
|
|
:type,
|
|
|
|
:domain,
|
|
|
|
:preferred_username,
|
|
|
|
:keys
|
|
|
|
]
|
|
|
|
@remote_actor_creation_optional_attrs [
|
|
|
|
:outbox_url,
|
|
|
|
:shared_inbox_url,
|
|
|
|
:following_url,
|
|
|
|
:followers_url,
|
2020-02-18 08:57:00 +01:00
|
|
|
:members_url,
|
|
|
|
:resources_url,
|
2020-07-09 17:24:28 +02:00
|
|
|
:posts_url,
|
|
|
|
:todos_url,
|
|
|
|
:events_url,
|
|
|
|
:discussions_url,
|
2019-09-09 00:52:49 +02:00
|
|
|
:name,
|
|
|
|
:summary,
|
2020-02-18 08:57:00 +01:00
|
|
|
:manually_approves_followers,
|
2020-11-06 11:34:32 +01:00
|
|
|
:visibility,
|
|
|
|
:openness
|
2019-09-09 00:52:49 +02:00
|
|
|
]
|
|
|
|
@remote_actor_creation_attrs @remote_actor_creation_required_attrs ++
|
|
|
|
@remote_actor_creation_optional_attrs
|
|
|
|
|
2020-02-18 08:57:00 +01:00
|
|
|
@group_creation_required_attrs [
|
|
|
|
:url,
|
|
|
|
:outbox_url,
|
|
|
|
:inbox_url,
|
|
|
|
:type,
|
|
|
|
:preferred_username,
|
|
|
|
:members_url
|
|
|
|
]
|
2020-12-17 15:25:58 +01:00
|
|
|
@group_creation_optional_attrs [
|
|
|
|
:shared_inbox_url,
|
|
|
|
:name,
|
|
|
|
:domain,
|
|
|
|
:summary,
|
|
|
|
:visibility,
|
|
|
|
:openness
|
|
|
|
]
|
2019-09-09 00:52:49 +02:00
|
|
|
@group_creation_attrs @group_creation_required_attrs ++ @group_creation_optional_attrs
|
2018-05-18 09:56:21 +02:00
|
|
|
|
|
|
|
schema "actors" do
|
2018-07-27 10:45:35 +02:00
|
|
|
field(:url, :string)
|
2020-07-09 17:24:28 +02:00
|
|
|
|
2018-07-27 10:45:35 +02:00
|
|
|
field(:outbox_url, :string)
|
|
|
|
field(:inbox_url, :string)
|
|
|
|
field(:following_url, :string)
|
|
|
|
field(:followers_url, :string)
|
|
|
|
field(:shared_inbox_url, :string)
|
2020-02-18 08:57:00 +01:00
|
|
|
field(:members_url, :string)
|
|
|
|
field(:resources_url, :string)
|
2020-07-09 17:24:28 +02:00
|
|
|
field(:posts_url, :string)
|
|
|
|
field(:events_url, :string)
|
2020-02-18 08:57:00 +01:00
|
|
|
field(:todos_url, :string)
|
2020-07-09 17:24:28 +02:00
|
|
|
field(:discussions_url, :string)
|
|
|
|
|
2019-09-09 00:52:49 +02:00
|
|
|
field(:type, ActorType, 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-09-09 00:52:49 +02:00
|
|
|
field(:openness, ActorOpenness, default: :moderated)
|
|
|
|
field(:visibility, ActorVisibility, default: :private)
|
2018-07-27 10:45:35 +02:00
|
|
|
field(:suspended, :boolean, default: false)
|
2020-02-14 17:56:36 +01:00
|
|
|
field(:last_refreshed_at, :utc_datetime)
|
2019-09-09 00:52:49 +02:00
|
|
|
|
|
|
|
embeds_one(:avatar, File, on_replace: :update)
|
|
|
|
embeds_one(:banner, File, on_replace: :update)
|
|
|
|
belongs_to(:user, User)
|
2020-08-05 16:44:08 +02:00
|
|
|
belongs_to(:physical_address, Address, on_replace: :nilify)
|
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)
|
2019-12-03 11:29:51 +01:00
|
|
|
has_many(:comments, Comment, foreign_key: :actor_id)
|
2019-03-08 12:25:06 +01:00
|
|
|
has_many(:feed_tokens, FeedToken, foreign_key: :actor_id)
|
2019-07-23 13:49:22 +02:00
|
|
|
has_many(:created_reports, Report, foreign_key: :reporter_id)
|
|
|
|
has_many(:subject_reports, Report, foreign_key: :reported_id)
|
|
|
|
has_many(:report_notes, Note, foreign_key: :moderator_id)
|
2019-10-25 17:43:37 +02:00
|
|
|
has_many(:mentions, Mention)
|
2019-12-03 11:29:51 +01:00
|
|
|
has_many(:shares, Share, foreign_key: :actor_id)
|
|
|
|
has_many(:owner_shares, Share, foreign_key: :owner_actor_id)
|
2019-09-13 01:55:45 +02:00
|
|
|
many_to_many(:memberships, __MODULE__, join_through: Member)
|
2018-05-18 09:56:21 +02:00
|
|
|
|
|
|
|
timestamps()
|
|
|
|
end
|
|
|
|
|
2019-09-09 00:52:49 +02:00
|
|
|
@doc """
|
|
|
|
Checks whether actor visibility is public.
|
|
|
|
"""
|
2020-09-02 17:42:17 +02:00
|
|
|
@spec is_public_visibility?(t) :: boolean
|
|
|
|
def is_public_visibility?(%__MODULE__{visibility: visibility}) do
|
2019-09-09 00:52:49 +02:00
|
|
|
visibility in [:public, :unlisted]
|
|
|
|
end
|
|
|
|
|
|
|
|
@doc """
|
|
|
|
Returns the display name if available, or the preferred username
|
|
|
|
(with the eventual @domain suffix if it's a distant actor).
|
|
|
|
"""
|
2019-09-13 01:55:45 +02:00
|
|
|
@spec display_name(t) :: String.t()
|
|
|
|
def display_name(%__MODULE__{name: name} = actor) when name in [nil, ""] do
|
2019-09-09 00:52:49 +02:00
|
|
|
preferred_username_and_domain(actor)
|
|
|
|
end
|
|
|
|
|
2019-09-13 01:55:45 +02:00
|
|
|
def display_name(%__MODULE__{name: name}), do: name
|
2019-09-09 00:52:49 +02:00
|
|
|
|
|
|
|
@doc """
|
|
|
|
Returns display name and username.
|
|
|
|
"""
|
2019-09-13 01:55:45 +02:00
|
|
|
@spec display_name_and_username(t) :: String.t()
|
|
|
|
def display_name_and_username(%__MODULE__{name: name} = actor) when name in [nil, ""] do
|
2019-09-09 00:52:49 +02:00
|
|
|
preferred_username_and_domain(actor)
|
|
|
|
end
|
|
|
|
|
2019-09-13 01:55:45 +02:00
|
|
|
def display_name_and_username(%__MODULE__{name: name} = actor) do
|
2020-09-30 15:25:30 +02:00
|
|
|
"#{name} (@#{preferred_username_and_domain(actor)})"
|
2019-09-09 00:52:49 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
@doc """
|
|
|
|
Returns the preferred username with the eventual @domain suffix if it's
|
|
|
|
a distant actor.
|
|
|
|
"""
|
2019-09-13 01:55:45 +02:00
|
|
|
@spec preferred_username_and_domain(t) :: String.t()
|
2019-09-13 02:28:09 +02:00
|
|
|
def preferred_username_and_domain(%__MODULE__{
|
|
|
|
preferred_username: preferred_username,
|
|
|
|
domain: nil
|
|
|
|
}) do
|
2019-09-09 00:52:49 +02:00
|
|
|
preferred_username
|
|
|
|
end
|
|
|
|
|
2019-09-13 02:28:09 +02:00
|
|
|
def preferred_username_and_domain(%__MODULE__{
|
|
|
|
preferred_username: preferred_username,
|
|
|
|
domain: domain
|
|
|
|
}) do
|
2019-09-09 00:52:49 +02:00
|
|
|
"#{preferred_username}@#{domain}"
|
|
|
|
end
|
|
|
|
|
2018-05-18 09:56:21 +02:00
|
|
|
@doc false
|
2019-09-13 01:55:45 +02:00
|
|
|
@spec changeset(t, map) :: Ecto.Changeset.t()
|
|
|
|
def changeset(%__MODULE__{} = actor, attrs) do
|
2018-05-18 09:56:21 +02:00
|
|
|
actor
|
2019-09-09 00:52:49 +02:00
|
|
|
|> cast(attrs, @attrs)
|
2018-12-03 11:58:57 +01:00
|
|
|
|> build_urls()
|
2020-08-05 16:44:08 +02:00
|
|
|
|> common_changeset(attrs)
|
2019-02-25 18:35:00 +01:00
|
|
|
|> unique_username_validator()
|
2019-09-09 00:52:49 +02:00
|
|
|
|> validate_required(@required_attrs)
|
2018-05-18 09:56:21 +02:00
|
|
|
end
|
|
|
|
|
2019-09-04 18:24:31 +02:00
|
|
|
@doc false
|
2019-09-13 01:55:45 +02:00
|
|
|
@spec update_changeset(t, map) :: Ecto.Changeset.t()
|
|
|
|
def update_changeset(%__MODULE__{} = actor, attrs) do
|
2019-09-04 18:24:31 +02:00
|
|
|
actor
|
2019-09-09 00:52:49 +02:00
|
|
|
|> cast(attrs, @update_attrs)
|
2020-08-05 16:44:08 +02:00
|
|
|
|> common_changeset(attrs)
|
2019-09-09 00:52:49 +02:00
|
|
|
|> validate_required(@update_required_attrs)
|
2019-09-04 18:24:31 +02:00
|
|
|
end
|
|
|
|
|
2019-12-03 11:29:51 +01:00
|
|
|
@doc false
|
|
|
|
@spec delete_changeset(t) :: Ecto.Changeset.t()
|
|
|
|
def delete_changeset(%__MODULE__{} = actor) do
|
|
|
|
actor
|
|
|
|
|> change()
|
|
|
|
|> put_change(:name, nil)
|
|
|
|
|> put_change(:summary, nil)
|
|
|
|
|> put_change(:suspended, true)
|
|
|
|
|> put_change(:avatar, nil)
|
|
|
|
|> put_change(:banner, nil)
|
|
|
|
|> put_change(:user_id, nil)
|
|
|
|
end
|
|
|
|
|
2019-03-19 11:16:03 +01:00
|
|
|
@doc """
|
2019-09-09 00:52:49 +02:00
|
|
|
Changeset for person registration.
|
2019-03-19 11:16:03 +01:00
|
|
|
"""
|
2019-09-13 01:55:45 +02:00
|
|
|
@spec registration_changeset(t, map) :: Ecto.Changeset.t()
|
|
|
|
def registration_changeset(%__MODULE__{} = actor, attrs) do
|
2018-05-18 09:56:21 +02:00
|
|
|
actor
|
2019-09-09 00:52:49 +02:00
|
|
|
|> cast(attrs, @registration_attrs)
|
2018-12-03 11:58:57 +01:00
|
|
|
|> build_urls()
|
2020-08-05 16:44:08 +02:00
|
|
|
|> common_changeset(attrs)
|
2019-01-29 11:02:32 +01:00
|
|
|
|> unique_username_validator()
|
2019-09-09 00:52:49 +02:00
|
|
|
|> validate_required(@registration_required_attrs)
|
2018-05-18 09:56:21 +02:00
|
|
|
end
|
|
|
|
|
2019-03-19 11:16:03 +01:00
|
|
|
@doc """
|
2019-09-09 00:52:49 +02:00
|
|
|
Changeset for remote actor creation.
|
2019-03-19 11:16:03 +01:00
|
|
|
"""
|
2019-09-09 00:52:49 +02:00
|
|
|
@spec remote_actor_creation_changeset(map) :: Ecto.Changeset.t()
|
|
|
|
def remote_actor_creation_changeset(attrs) do
|
|
|
|
changeset =
|
2019-09-13 01:55:45 +02:00
|
|
|
%__MODULE__{}
|
2019-09-09 00:52:49 +02:00
|
|
|
|> cast(attrs, @remote_actor_creation_attrs)
|
|
|
|
|> validate_required(@remote_actor_creation_required_attrs)
|
2020-08-05 16:44:08 +02:00
|
|
|
|> common_changeset(attrs)
|
2019-02-25 18:35:00 +01:00
|
|
|
|> unique_username_validator()
|
2020-08-14 11:32:23 +02:00
|
|
|
|> validate_required(:domain)
|
2018-05-18 09:56:21 +02:00
|
|
|
|> validate_length(:summary, max: 5000)
|
|
|
|
|> validate_length(:preferred_username, max: 100)
|
|
|
|
|
2019-09-09 00:52:49 +02:00
|
|
|
Logger.debug("Remote actor creation: #{inspect(changeset)}")
|
2018-05-18 09:56:21 +02:00
|
|
|
|
2019-09-09 00:52:49 +02:00
|
|
|
changeset
|
|
|
|
end
|
2019-07-30 16:40:59 +02:00
|
|
|
|
2020-08-05 16:44:08 +02:00
|
|
|
@spec common_changeset(Ecto.Changeset.t(), map()) :: Ecto.Changeset.t()
|
|
|
|
defp common_changeset(%Ecto.Changeset{} = changeset, attrs) do
|
2019-11-18 17:54:42 +01:00
|
|
|
changeset
|
|
|
|
|> cast_embed(:avatar)
|
|
|
|
|> cast_embed(:banner)
|
2020-08-05 16:44:08 +02:00
|
|
|
|> put_address(attrs)
|
2019-11-18 17:54:42 +01:00
|
|
|
|> unique_constraint(:url, name: :actors_url_index)
|
|
|
|
|> unique_constraint(:preferred_username, name: :actors_preferred_username_domain_type_index)
|
|
|
|
|> validate_format(:preferred_username, ~r/[a-z0-9_]+/)
|
2020-02-14 17:56:36 +01:00
|
|
|
|> put_change(:last_refreshed_at, DateTime.utc_now() |> DateTime.truncate(:second))
|
2019-11-18 17:54:42 +01:00
|
|
|
end
|
|
|
|
|
2019-03-19 11:16:03 +01:00
|
|
|
@doc """
|
|
|
|
Changeset for group creation
|
|
|
|
"""
|
2019-09-13 01:55:45 +02:00
|
|
|
@spec group_creation_changeset(t, map) :: Ecto.Changeset.t()
|
|
|
|
def group_creation_changeset(%__MODULE__{} = actor, params) do
|
2018-05-30 18:59:13 +02:00
|
|
|
actor
|
2019-09-09 00:52:49 +02:00
|
|
|
|> cast(params, @group_creation_attrs)
|
2018-12-03 11:58:57 +01:00
|
|
|
|> build_urls(:Group)
|
2020-08-05 16:44:08 +02:00
|
|
|
|> common_changeset(params)
|
2018-12-03 11:58:57 +01:00
|
|
|
|> put_change(:domain, nil)
|
2019-09-09 00:52:49 +02:00
|
|
|
|> put_change(:keys, Crypto.generate_rsa_2048_private_key())
|
2018-12-03 11:58:57 +01:00
|
|
|
|> put_change(:type, :Group)
|
2019-02-25 18:35:00 +01:00
|
|
|
|> unique_username_validator()
|
2019-09-09 00:52:49 +02:00
|
|
|
|> validate_required(@group_creation_required_attrs)
|
2018-12-03 11:58:57 +01:00
|
|
|
|> validate_length(:summary, max: 5000)
|
|
|
|
|> validate_length(:preferred_username, max: 100)
|
|
|
|
end
|
|
|
|
|
2019-09-09 00:52:49 +02:00
|
|
|
# Needed because following constraint can't work for domain null values (local)
|
|
|
|
@spec unique_username_validator(Ecto.Changeset.t()) :: Ecto.Changeset.t()
|
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),
|
2019-09-13 01:55:45 +02:00
|
|
|
%__MODULE__{preferred_username: _} <- Actors.get_local_actor_by_name(username) do
|
2020-11-17 19:14:55 +01:00
|
|
|
add_error(
|
|
|
|
changeset,
|
|
|
|
:preferred_username,
|
|
|
|
dgettext("errors", "This username is already taken.")
|
|
|
|
)
|
2019-01-29 11:02:32 +01:00
|
|
|
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
|
2019-09-09 00:52:49 +02:00
|
|
|
defp unique_username_validator(changeset), do: changeset
|
2019-02-25 18:35:00 +01:00
|
|
|
|
2019-09-09 00:52:49 +02:00
|
|
|
@spec build_urls(Ecto.Changeset.t(), ActorType.t()) :: 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
|
|
|
|
2020-02-18 08:57:00 +01:00
|
|
|
defp build_urls(%Ecto.Changeset{changes: %{preferred_username: username}} = changeset, type) do
|
2018-12-03 11:58:57 +01:00
|
|
|
changeset
|
2019-09-09 00:52:49 +02:00
|
|
|
|> put_change(:outbox_url, build_url(username, :outbox))
|
|
|
|
|> put_change(:followers_url, build_url(username, :followers))
|
|
|
|
|> put_change(:following_url, build_url(username, :following))
|
|
|
|
|> put_change(:inbox_url, build_url(username, :inbox))
|
2020-01-28 19:18:33 +01:00
|
|
|
|> put_change(:shared_inbox_url, "#{Endpoint.url()}/inbox")
|
2020-02-18 08:57:00 +01:00
|
|
|
|> put_change(:members_url, if(type == :Group, do: build_url(username, :members), else: nil))
|
2020-11-06 11:34:32 +01:00
|
|
|
|> put_change(
|
|
|
|
:resources_url,
|
|
|
|
if(type == :Group, do: build_url(username, :resources), else: nil)
|
|
|
|
)
|
|
|
|
|> put_change(:todos_url, if(type == :Group, do: build_url(username, :todos), else: nil))
|
|
|
|
|> put_change(:posts_url, if(type == :Group, do: build_url(username, :posts), else: nil))
|
|
|
|
|> put_change(:events_url, if(type == :Group, do: build_url(username, :events), else: nil))
|
|
|
|
|> put_change(
|
|
|
|
:discussions_url,
|
|
|
|
if(type == :Group, do: build_url(username, :discussions), else: nil)
|
|
|
|
)
|
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 """
|
2019-09-09 00:52:49 +02:00
|
|
|
Builds an AP URL for an actor.
|
2019-04-25 19:05:05 +02:00
|
|
|
"""
|
2019-09-09 00:52:49 +02:00
|
|
|
@spec build_url(String.t(), atom, keyword) :: String.t()
|
2019-04-25 19:05:05 +02:00
|
|
|
def build_url(preferred_username, endpoint, args \\ [])
|
|
|
|
|
2019-09-09 00:52:49 +02:00
|
|
|
def build_url(username, :inbox, _args), do: "#{build_url(username, :page)}/inbox"
|
|
|
|
|
2019-12-20 13:04:34 +01:00
|
|
|
# Relay has a special URI
|
|
|
|
def build_url("relay", :page, _args),
|
|
|
|
do: Endpoint |> Routes.activity_pub_url(:relay) |> URI.decode()
|
|
|
|
|
2020-07-09 17:24:28 +02:00
|
|
|
def build_url(preferred_username, endpoint, args)
|
|
|
|
when endpoint in [:page, :resources, :posts, :discussions, :events, :todos] do
|
2020-02-18 08:57:00 +01:00
|
|
|
endpoint = if endpoint == :page, do: :actor, else: endpoint
|
|
|
|
|
2019-04-25 19:05:05 +02:00
|
|
|
Endpoint
|
2020-02-18 08:57:00 +01:00
|
|
|
|> Routes.page_url(endpoint, preferred_username, args)
|
2019-04-25 19:05:05 +02:00
|
|
|
|> URI.decode()
|
|
|
|
end
|
|
|
|
|
|
|
|
def build_url(preferred_username, endpoint, args)
|
2020-07-09 17:24:28 +02:00
|
|
|
when endpoint in [:outbox, :following, :followers, :members] do
|
2019-04-25 19:05:05 +02:00
|
|
|
Endpoint
|
|
|
|
|> Routes.activity_pub_url(endpoint, preferred_username, args)
|
|
|
|
|> URI.decode()
|
|
|
|
end
|
|
|
|
|
2019-12-20 13:04:34 +01:00
|
|
|
@spec build_relay_creation_attrs :: Ecto.Changeset.t()
|
|
|
|
def build_relay_creation_attrs do
|
|
|
|
data = %{
|
2021-05-12 11:02:46 +02:00
|
|
|
name: Config.get([:instance, :name], "Mobilizon"),
|
|
|
|
summary:
|
2019-09-09 00:52:49 +02:00
|
|
|
Config.get(
|
|
|
|
[:instance, :description],
|
|
|
|
"An internal service actor for this Mobilizon instance"
|
|
|
|
),
|
2021-05-12 11:02:46 +02:00
|
|
|
keys: Crypto.generate_rsa_2048_private_key(),
|
|
|
|
preferred_username: "relay",
|
|
|
|
domain: nil,
|
|
|
|
visibility: :public,
|
|
|
|
type: :Application,
|
|
|
|
manually_approves_followers: true
|
2019-09-09 00:52:49 +02:00
|
|
|
}
|
2019-12-20 13:04:34 +01:00
|
|
|
|
|
|
|
%__MODULE__{}
|
|
|
|
|> Ecto.Changeset.cast(data, @attrs)
|
|
|
|
|> build_urls()
|
2020-11-02 18:10:56 +01:00
|
|
|
# Can use sharedinbox directly
|
2019-12-20 13:04:34 +01:00
|
|
|
|> put_change(:inbox_url, "#{Endpoint.url()}/inbox")
|
2020-11-02 18:10:56 +01:00
|
|
|
|> unique_username_validator()
|
2019-12-20 13:04:34 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
@spec build_anonymous_actor_creation_attrs :: Ecto.Changeset.t()
|
|
|
|
def build_anonymous_actor_creation_attrs do
|
|
|
|
data = %{
|
2021-05-12 11:02:46 +02:00
|
|
|
name: "Mobilizon Anonymous Actor",
|
|
|
|
summary: "A fake person for anonymous participations",
|
|
|
|
keys: Crypto.generate_rsa_2048_private_key(),
|
|
|
|
preferred_username: "anonymous",
|
|
|
|
domain: nil,
|
|
|
|
type: :Person,
|
|
|
|
manually_approves_followers: true
|
2019-12-20 13:04:34 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
%__MODULE__{}
|
|
|
|
|> Ecto.Changeset.cast(data, @attrs)
|
|
|
|
|> build_urls()
|
2019-09-09 00:52:49 +02:00
|
|
|
end
|
2020-08-05 16:44:08 +02:00
|
|
|
|
|
|
|
# In case the provided addresses is an existing one
|
|
|
|
@spec put_address(Ecto.Changeset.t(), map) :: Ecto.Changeset.t()
|
|
|
|
defp put_address(%Ecto.Changeset{} = changeset, %{
|
|
|
|
physical_address: %{id: id} = _physical_address
|
|
|
|
})
|
|
|
|
when not is_nil(id) do
|
|
|
|
case Addresses.get_address(id) do
|
|
|
|
%Address{} = address ->
|
|
|
|
put_assoc(changeset, :physical_address, address)
|
|
|
|
|
|
|
|
_ ->
|
|
|
|
cast_assoc(changeset, :physical_address)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# In case it's a new address but the origin_id is an existing one
|
|
|
|
defp put_address(%Ecto.Changeset{} = changeset, %{physical_address: %{origin_id: origin_id}})
|
|
|
|
when not is_nil(origin_id) do
|
|
|
|
case Addresses.get_address_by_origin_id(origin_id) do
|
|
|
|
%Address{} = address ->
|
|
|
|
put_assoc(changeset, :physical_address, address)
|
|
|
|
|
|
|
|
_ ->
|
|
|
|
cast_assoc(changeset, :physical_address)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# In case it's a new address without any origin_id (manual)
|
|
|
|
defp put_address(%Ecto.Changeset{} = changeset, _attrs) do
|
|
|
|
cast_assoc(changeset, :physical_address)
|
|
|
|
end
|
2018-05-18 09:56:21 +02:00
|
|
|
end
|