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
|
|
|
|
2018-11-23 15:03:53 +01:00
|
|
|
defenum(Mobilizon.Actors.ActorOpennesssEnum, :openness, [
|
|
|
|
:invite_only,
|
|
|
|
:moderated,
|
|
|
|
:open
|
|
|
|
])
|
|
|
|
|
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-10-11 17:37:39 +02:00
|
|
|
alias Mobilizon.Actors
|
|
|
|
alias Mobilizon.Actors.{Actor, User, Follower, Member}
|
|
|
|
alias Mobilizon.Events.Event
|
2018-05-18 09:56:21 +02:00
|
|
|
|
|
|
|
import Ecto.Query
|
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)
|
|
|
|
field(:domain, :string)
|
|
|
|
field(:summary, :string)
|
|
|
|
field(:preferred_username, :string)
|
|
|
|
field(:keys, :string)
|
|
|
|
field(:manually_approves_followers, :boolean, default: false)
|
|
|
|
field(:suspended, :boolean, default: false)
|
|
|
|
field(:avatar_url, :string)
|
|
|
|
field(:banner_url, :string)
|
2018-11-23 15:03:53 +01:00
|
|
|
# field(:openness, Mobilizon.Actors.ActorOpennesssEnum, 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)
|
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,
|
|
|
|
:avatar_url,
|
|
|
|
:banner_url,
|
|
|
|
:user_id
|
|
|
|
])
|
2018-10-11 17:37:39 +02:00
|
|
|
|> put_change(:url, "#{MobilizonWeb.Endpoint.url()}/@#{attrs["preferred_username"]}")
|
2018-06-14 17:25:55 +02:00
|
|
|
|> validate_required([:preferred_username, :keys, :suspended, :url])
|
2018-10-10 14:57:35 +02:00
|
|
|
|> unique_constraint(:preferred_username, name: :actors_preferred_username_domain_index)
|
2018-05-18 09:56:21 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
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,
|
|
|
|
:keys,
|
|
|
|
:suspended,
|
|
|
|
:url,
|
|
|
|
:type,
|
|
|
|
:avatar_url,
|
|
|
|
:user_id
|
|
|
|
])
|
2018-06-14 17:25:55 +02:00
|
|
|
|> unique_constraint(:preferred_username, name: :actors_preferred_username_domain_index)
|
2018-10-11 17:37:39 +02:00
|
|
|
|> put_change(:url, "#{MobilizonWeb.Endpoint.url()}/@#{attrs.preferred_username}")
|
|
|
|
|> put_change(:inbox_url, "#{MobilizonWeb.Endpoint.url()}/@#{attrs.preferred_username}/inbox")
|
2018-10-11 17:47:02 +02:00
|
|
|
|> put_change(
|
|
|
|
:outbox_url,
|
|
|
|
"#{MobilizonWeb.Endpoint.url()}/@#{attrs.preferred_username}/outbox"
|
|
|
|
)
|
2018-10-11 17:37:39 +02:00
|
|
|
|> put_change(:shared_inbox_url, "#{MobilizonWeb.Endpoint.url()}/inbox")
|
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])?)*$/
|
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,
|
|
|
|
:manually_approves_followers,
|
|
|
|
:avatar_url,
|
|
|
|
:banner_url
|
|
|
|
])
|
|
|
|
|> validate_required([
|
|
|
|
:url,
|
|
|
|
:outbox_url,
|
|
|
|
:inbox_url,
|
|
|
|
:type,
|
|
|
|
:domain,
|
|
|
|
:preferred_username,
|
|
|
|
:keys
|
|
|
|
])
|
2018-05-18 09:56:21 +02:00
|
|
|
|> unique_constraint(:preferred_username, name: :actors_preferred_username_domain_index)
|
|
|
|
|> 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
|
|
|
|
|
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,
|
|
|
|
:preferred_username,
|
|
|
|
:avatar_url,
|
|
|
|
:banner_url
|
|
|
|
])
|
|
|
|
|> put_change(
|
|
|
|
:outbox_url,
|
2018-10-11 17:37:39 +02:00
|
|
|
"#{MobilizonWeb.Endpoint.url()}/@#{params["preferred_username"]}/outbox"
|
2018-07-27 10:45:35 +02:00
|
|
|
)
|
|
|
|
|> put_change(
|
|
|
|
:inbox_url,
|
2018-10-11 17:37:39 +02:00
|
|
|
"#{MobilizonWeb.Endpoint.url()}/@#{params["preferred_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")
|
|
|
|
|> put_change(:url, "#{MobilizonWeb.Endpoint.url()}/@#{params["preferred_username"]}")
|
2018-05-30 18:59:13 +02:00
|
|
|
|> put_change(:domain, nil)
|
2018-07-27 10:45:35 +02:00
|
|
|
|> put_change(:type, :Group)
|
2018-05-30 18:59:13 +02:00
|
|
|
|> validate_required([:url, :outbox_url, :inbox_url, :type, :name, :preferred_username])
|
|
|
|
|> validate_length(:summary, max: 5000)
|
|
|
|
|> validate_length(:preferred_username, max: 100)
|
|
|
|
|> put_change(:local, true)
|
|
|
|
end
|
|
|
|
|
2018-11-12 18:17:53 +01:00
|
|
|
@doc """
|
|
|
|
Get a public key for a given ActivityPub actor ID (url)
|
|
|
|
"""
|
2018-08-01 14:45:18 +02:00
|
|
|
@spec get_public_key_for_url(String.t()) :: {:ok, String.t()}
|
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
|
|
|
|
"""
|
2018-08-01 14:45:18 +02:00
|
|
|
def get_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,
|
|
|
|
where: f.target_actor_id == ^actor_id
|
|
|
|
)
|
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
|
|
|
|
"""
|
2018-08-01 14:45:18 +02:00
|
|
|
def get_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,
|
|
|
|
where: f.actor_id == ^actor_id
|
|
|
|
)
|
2018-05-18 09:56:21 +02:00
|
|
|
)
|
|
|
|
end
|
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
|
|
|
|
|
|
|
|
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
|
|
|
|
|
2018-11-27 14:02:51 +01:00
|
|
|
|
|
|
|
@spec follow(struct(), struct(), boolean()) :: Follower.t() | {:error, String.t()}
|
2018-11-12 09:05:31 +01:00
|
|
|
def follow(%Actor{} = follower, %Actor{} = followed, approved \\ true) do
|
2018-11-27 14:02:51 +01:00
|
|
|
|
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
|
|
|
|
{:already_following, _} ->
|
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
|
|
|
|
|
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
|
|
|
|
|
2018-11-27 14:02:51 +01:00
|
|
|
@spec following?(struct(), struct()) :: boolean()
|
|
|
|
def following?(%Actor{id: follower_actor_id} = _follower_actor, %Actor{followers: followers} = _followed) do
|
|
|
|
followers
|
|
|
|
|> Enum.map(&(&1.actor_id))
|
|
|
|
|> Enum.member?(follower_actor_id)
|
2018-08-24 11:34:00 +02:00
|
|
|
end
|
2018-05-18 09:56:21 +02:00
|
|
|
end
|