2018-10-11 17:37:39 +02:00
|
|
|
defmodule Mobilizon.Actors do
|
2018-05-18 09:56:21 +02:00
|
|
|
@moduledoc """
|
|
|
|
The Actors context.
|
|
|
|
"""
|
|
|
|
|
|
|
|
import Ecto.Query, warn: false
|
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-12 18:17:53 +01:00
|
|
|
alias Mobilizon.Actors.{Actor, Bot, Member, Follower, User}
|
2018-05-18 09:56:21 +02:00
|
|
|
|
2018-10-11 17:37:39 +02:00
|
|
|
alias Mobilizon.Service.ActivityPub
|
2019-01-25 13:59:58 +01:00
|
|
|
# import Exgravatar
|
2018-05-18 09:56:21 +02:00
|
|
|
|
2018-11-23 15:03:53 +01:00
|
|
|
@doc false
|
2018-11-06 10:30:27 +01:00
|
|
|
def data() do
|
|
|
|
Dataloader.Ecto.new(Repo, query: &query/2)
|
|
|
|
end
|
|
|
|
|
2018-11-23 15:03:53 +01:00
|
|
|
@doc false
|
2018-11-06 10:30:27 +01:00
|
|
|
def query(queryable, _params) do
|
|
|
|
queryable
|
|
|
|
end
|
|
|
|
|
2018-05-18 09:56:21 +02:00
|
|
|
@doc """
|
|
|
|
Returns the list of actors.
|
|
|
|
|
|
|
|
## Examples
|
|
|
|
|
2018-11-12 18:17:53 +01:00
|
|
|
iex> Mobilizon.Actors.list_actors()
|
|
|
|
[%Mobilizon.Actors.Actor{}]
|
2018-05-18 09:56:21 +02:00
|
|
|
|
|
|
|
"""
|
2018-11-12 18:17:53 +01:00
|
|
|
@spec list_actors() :: list()
|
2018-05-18 09:56:21 +02:00
|
|
|
def list_actors do
|
|
|
|
Repo.all(Actor)
|
|
|
|
end
|
|
|
|
|
|
|
|
@doc """
|
|
|
|
Gets a single actor.
|
|
|
|
|
|
|
|
Raises `Ecto.NoResultsError` if the Actor does not exist.
|
|
|
|
|
|
|
|
## Examples
|
|
|
|
|
|
|
|
iex> get_actor!(123)
|
2018-11-12 18:17:53 +01:00
|
|
|
%Mobilizon.Actors.Actor{}
|
2018-05-18 09:56:21 +02:00
|
|
|
|
|
|
|
iex> get_actor!(456)
|
|
|
|
** (Ecto.NoResultsError)
|
|
|
|
|
|
|
|
"""
|
2018-11-12 18:17:53 +01:00
|
|
|
@spec get_actor!(integer()) :: Actor.t()
|
2018-05-18 09:56:21 +02:00
|
|
|
def get_actor!(id) do
|
|
|
|
Repo.get!(Actor, id)
|
|
|
|
end
|
|
|
|
|
2018-11-06 10:30:27 +01:00
|
|
|
@doc """
|
|
|
|
Returns the associated actor for an user, either the default set one or the first found
|
|
|
|
"""
|
2018-11-12 18:17:53 +01:00
|
|
|
@spec get_actor_for_user(Mobilizon.Actors.User.t()) :: Mobilizon.Actors.Actor.t()
|
2018-11-06 10:30:27 +01:00
|
|
|
def get_actor_for_user(%Mobilizon.Actors.User{} = user) do
|
2018-12-28 16:13:33 +01:00
|
|
|
case Repo.one(
|
|
|
|
from(a in Actor,
|
|
|
|
join: u in User,
|
|
|
|
on: u.default_actor_id == a.id,
|
|
|
|
where: u.id == ^user.id
|
|
|
|
)
|
|
|
|
) do
|
2019-01-25 13:59:58 +01:00
|
|
|
nil ->
|
|
|
|
case user |> get_actors_for_user() do
|
|
|
|
[] -> nil
|
|
|
|
actors -> hd(actors)
|
|
|
|
end
|
|
|
|
|
|
|
|
actor ->
|
|
|
|
actor
|
2018-11-06 10:30:27 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-11-29 17:43:22 +01:00
|
|
|
def get_actors_for_user(%User{id: user_id}) do
|
|
|
|
Repo.all(from(a in Actor, where: a.user_id == ^user_id))
|
|
|
|
end
|
|
|
|
|
2019-01-21 15:08:22 +01:00
|
|
|
@spec get_actor_with_everything(integer()) :: Ecto.Query
|
|
|
|
defp do_get_actor_with_everything(id) do
|
|
|
|
from(a in Actor, where: a.id == ^id, preload: [:organized_events, :followers, :followings])
|
|
|
|
end
|
|
|
|
|
|
|
|
@doc """
|
|
|
|
Returns an actor with every relation
|
|
|
|
"""
|
|
|
|
@spec get_actor_with_everything(integer()) :: Mobilizon.Actors.Actor.t()
|
|
|
|
def get_actor_with_everything(id) do
|
|
|
|
id
|
|
|
|
|> do_get_actor_with_everything
|
|
|
|
|> Repo.one()
|
|
|
|
end
|
|
|
|
|
|
|
|
@doc """
|
|
|
|
Returns an actor with every relation
|
|
|
|
"""
|
|
|
|
@spec get_local_actor_with_everything(integer()) :: Mobilizon.Actors.Actor.t()
|
|
|
|
def get_local_actor_with_everything(id) do
|
|
|
|
id
|
|
|
|
|> do_get_actor_with_everything
|
|
|
|
|> where([a], is_nil(a.domain))
|
|
|
|
|> Repo.one()
|
2018-05-18 09:56:21 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
@doc """
|
|
|
|
Creates a actor.
|
|
|
|
|
|
|
|
## Examples
|
|
|
|
|
2018-11-12 18:17:53 +01:00
|
|
|
iex> create_actor(%{preferred_username: "test"})
|
|
|
|
{:ok, %Mobilizon.Actors.Actor{preferred_username: "test"}}
|
2018-05-18 09:56:21 +02:00
|
|
|
|
2018-11-12 18:17:53 +01:00
|
|
|
iex> create_actor(%{preferred_username: nil})
|
2018-05-18 09:56:21 +02:00
|
|
|
{:error, %Ecto.Changeset{}}
|
|
|
|
|
|
|
|
"""
|
|
|
|
def create_actor(attrs \\ %{}) do
|
|
|
|
%Actor{}
|
|
|
|
|> Actor.changeset(attrs)
|
|
|
|
|> Repo.insert()
|
|
|
|
end
|
|
|
|
|
|
|
|
@doc """
|
|
|
|
Updates a actor.
|
|
|
|
|
|
|
|
## Examples
|
|
|
|
|
2018-11-12 18:17:53 +01:00
|
|
|
iex> update_actor(%Actor{preferred_username: "toto"}, %{preferred_username: "tata"})
|
|
|
|
{:ok, %Mobilizon.Actors.Actor{preferred_username: "tata"}}
|
2018-05-18 09:56:21 +02:00
|
|
|
|
2018-11-12 18:17:53 +01:00
|
|
|
iex> update_actor(%Actor{preferred_username: "toto"}, %{preferred_username: nil})
|
2018-05-18 09:56:21 +02:00
|
|
|
{:error, %Ecto.Changeset{}}
|
|
|
|
|
|
|
|
"""
|
|
|
|
def update_actor(%Actor{} = actor, attrs) do
|
|
|
|
actor
|
|
|
|
|> Actor.changeset(attrs)
|
|
|
|
|> Repo.update()
|
|
|
|
end
|
|
|
|
|
2018-11-29 17:43:22 +01:00
|
|
|
def update_user_default_actor(user_id, actor_id) do
|
|
|
|
with from(u in User, where: u.id == ^user_id, update: [set: [default_actor_id: ^actor_id]])
|
|
|
|
|> Repo.update_all([]) do
|
|
|
|
Repo.get!(User, user_id) |> Repo.preload([:default_actor])
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-05-18 09:56:21 +02:00
|
|
|
@doc """
|
|
|
|
Deletes a Actor.
|
|
|
|
|
|
|
|
## Examples
|
|
|
|
|
2018-11-12 18:17:53 +01:00
|
|
|
iex> delete_actor(%Actor{})
|
|
|
|
{:ok, %Mobilizon.Actors.Actor{}}
|
2018-05-18 09:56:21 +02:00
|
|
|
|
2018-11-12 18:17:53 +01:00
|
|
|
iex> delete_actor(nil)
|
2018-05-18 09:56:21 +02:00
|
|
|
{:error, %Ecto.Changeset{}}
|
|
|
|
|
|
|
|
"""
|
2018-11-12 18:17:53 +01:00
|
|
|
@spec delete_actor(Actor.t()) :: {:ok, Actor.t()} | {:error, Ecto.Changeset.t()}
|
2018-05-18 09:56:21 +02:00
|
|
|
def delete_actor(%Actor{} = actor) do
|
|
|
|
Repo.delete(actor)
|
|
|
|
end
|
|
|
|
|
|
|
|
@doc """
|
|
|
|
Returns an `%Ecto.Changeset{}` for tracking actor changes.
|
|
|
|
|
|
|
|
## Examples
|
|
|
|
|
2018-11-12 18:17:53 +01:00
|
|
|
iex> change_actor(%Actor{})
|
|
|
|
%Ecto.Changeset{data: %Mobilizon.Actors.Actor{}}
|
2018-05-18 09:56:21 +02:00
|
|
|
|
|
|
|
"""
|
|
|
|
def change_actor(%Actor{} = actor) do
|
|
|
|
Actor.changeset(actor, %{})
|
|
|
|
end
|
|
|
|
|
2018-05-30 18:59:13 +02:00
|
|
|
@doc """
|
|
|
|
List the groups
|
|
|
|
"""
|
2018-12-14 11:23:36 +01:00
|
|
|
def list_groups(page \\ nil, limit \\ nil) do
|
|
|
|
Repo.all(
|
|
|
|
from(
|
|
|
|
a in Actor,
|
|
|
|
where: a.type == ^:Group
|
|
|
|
)
|
|
|
|
|> paginate(page, limit)
|
|
|
|
)
|
2018-05-30 18:59:13 +02:00
|
|
|
end
|
|
|
|
|
2018-12-14 17:41:55 +01:00
|
|
|
@doc """
|
|
|
|
Get a group by it's title
|
|
|
|
"""
|
|
|
|
@spec get_group_by_title(String.t()) :: Actor.t() | nil
|
|
|
|
def get_group_by_title(title) do
|
|
|
|
case String.split(title, "@") do
|
|
|
|
[title] ->
|
|
|
|
get_local_group_by_title(title)
|
|
|
|
|
|
|
|
[title, domain] ->
|
|
|
|
Repo.one(
|
|
|
|
from(a in Actor,
|
|
|
|
where: a.preferred_username == ^title and a.type == "Group" and a.domain == ^domain
|
|
|
|
)
|
|
|
|
)
|
2018-08-01 14:45:18 +02:00
|
|
|
end
|
2018-06-06 17:42:53 +02:00
|
|
|
end
|
|
|
|
|
2018-12-14 17:41:55 +01:00
|
|
|
@doc """
|
|
|
|
Get a local group by it's title
|
|
|
|
"""
|
|
|
|
@spec get_local_group_by_title(String.t()) :: Actor.t() | nil
|
|
|
|
def get_local_group_by_title(title) do
|
|
|
|
title
|
|
|
|
|> do_get_local_group_by_title
|
|
|
|
|> Repo.one()
|
|
|
|
end
|
|
|
|
|
|
|
|
@spec do_get_local_group_by_title(String.t()) :: Ecto.Query.t()
|
|
|
|
defp do_get_local_group_by_title(title) do
|
|
|
|
from(a in Actor,
|
|
|
|
where: a.preferred_username == ^title and a.type == "Group" and is_nil(a.domain)
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2018-05-30 18:59:13 +02:00
|
|
|
@doc """
|
|
|
|
Creates a group.
|
|
|
|
|
|
|
|
## Examples
|
|
|
|
|
2018-11-12 18:17:53 +01:00
|
|
|
iex> create_group(%{name: "group name"})
|
|
|
|
{:ok, %Mobilizon.Actors.Actor{}}
|
2018-05-30 18:59:13 +02:00
|
|
|
|
2018-11-12 18:17:53 +01:00
|
|
|
iex> create_group(%{name: nil})
|
2018-05-30 18:59:13 +02:00
|
|
|
{:error, %Ecto.Changeset{}}
|
|
|
|
|
|
|
|
"""
|
|
|
|
def create_group(attrs \\ %{}) do
|
|
|
|
%Actor{}
|
|
|
|
|> Actor.group_creation(attrs)
|
|
|
|
|> Repo.insert()
|
|
|
|
end
|
|
|
|
|
2019-01-25 09:23:44 +01:00
|
|
|
@doc """
|
|
|
|
Delete a group
|
|
|
|
"""
|
|
|
|
def delete_group!(%Actor{type: :Group} = group) do
|
|
|
|
Repo.delete!(group)
|
|
|
|
end
|
|
|
|
|
2018-10-11 17:37:39 +02:00
|
|
|
alias Mobilizon.Actors.User
|
2018-05-18 09:56:21 +02:00
|
|
|
|
|
|
|
@doc """
|
|
|
|
Returns the list of users.
|
|
|
|
|
|
|
|
## Examples
|
|
|
|
|
|
|
|
iex> list_users()
|
2018-11-12 18:17:53 +01:00
|
|
|
[%Mobilizon.Actors.User{}]
|
2018-05-18 09:56:21 +02:00
|
|
|
|
|
|
|
"""
|
|
|
|
def list_users do
|
|
|
|
Repo.all(User)
|
|
|
|
end
|
|
|
|
|
2018-11-12 09:05:31 +01:00
|
|
|
def insert_or_update_actor(data, preload \\ false) do
|
2018-05-18 09:56:21 +02:00
|
|
|
cs = Actor.remote_actor_creation(data)
|
2018-07-27 10:45:35 +02:00
|
|
|
|
2018-11-12 18:17:53 +01:00
|
|
|
{:ok, actor} =
|
2018-11-12 09:05:31 +01:00
|
|
|
Repo.insert(
|
|
|
|
cs,
|
|
|
|
on_conflict: [
|
|
|
|
set: [
|
|
|
|
keys: data.keys,
|
|
|
|
avatar_url: data.avatar_url,
|
|
|
|
banner_url: data.banner_url,
|
2018-12-14 17:41:55 +01:00
|
|
|
name: data.name,
|
|
|
|
summary: data.summary
|
2018-11-12 09:05:31 +01:00
|
|
|
]
|
|
|
|
],
|
2018-12-14 17:41:55 +01:00
|
|
|
conflict_target: [:url]
|
2018-11-12 09:05:31 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
if preload, do: {:ok, Repo.preload(actor, [:followers])}, else: {:ok, actor}
|
2018-05-18 09:56:21 +02:00
|
|
|
end
|
|
|
|
|
2018-07-27 10:45:35 +02:00
|
|
|
# def increase_event_count(%Actor{} = actor) do
|
|
|
|
# event_count = (actor.info["event_count"] || 0) + 1
|
|
|
|
# new_info = Map.put(actor.info, "note_count", note_count)
|
|
|
|
#
|
|
|
|
# cs = info_changeset(actor, %{info: new_info})
|
|
|
|
#
|
|
|
|
# update_and_set_cache(cs)
|
|
|
|
# end
|
2018-05-18 09:56:21 +02:00
|
|
|
|
|
|
|
def count_users() do
|
|
|
|
Repo.one(
|
2018-07-27 10:45:35 +02:00
|
|
|
from(
|
|
|
|
u in User,
|
|
|
|
select: count(u.id)
|
|
|
|
)
|
2018-05-18 09:56:21 +02:00
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
@doc """
|
|
|
|
Gets a single user.
|
|
|
|
|
|
|
|
Raises `Ecto.NoResultsError` if the User does not exist.
|
|
|
|
|
|
|
|
## Examples
|
|
|
|
|
|
|
|
iex> get_user!(123)
|
2018-11-12 18:17:53 +01:00
|
|
|
%Mobilizon.Actors.User{}
|
2018-05-18 09:56:21 +02:00
|
|
|
|
|
|
|
iex> get_user!(456)
|
|
|
|
** (Ecto.NoResultsError)
|
|
|
|
|
|
|
|
"""
|
|
|
|
def get_user!(id), do: Repo.get!(User, id)
|
|
|
|
|
2018-11-12 18:17:53 +01:00
|
|
|
@doc """
|
|
|
|
Get an user with it's actors
|
|
|
|
|
|
|
|
Raises `Ecto.NoResultsError` if the User does not exist.
|
|
|
|
"""
|
|
|
|
@spec get_user_with_actors!(integer()) :: User.t()
|
|
|
|
def get_user_with_actors!(id) do
|
2018-05-18 09:56:21 +02:00
|
|
|
user = Repo.get!(User, id)
|
2018-11-29 17:43:22 +01:00
|
|
|
Repo.preload(user, [:actors, :default_actor])
|
2018-05-18 09:56:21 +02:00
|
|
|
end
|
|
|
|
|
2018-11-29 17:43:22 +01:00
|
|
|
@doc """
|
|
|
|
Get user with it's actors by ID
|
|
|
|
"""
|
2018-11-12 18:17:53 +01:00
|
|
|
@spec get_user_with_actors(integer()) :: User.t()
|
|
|
|
def get_user_with_actors(id) do
|
2018-11-06 10:30:27 +01:00
|
|
|
case Repo.get(User, id) do
|
2018-11-29 17:43:22 +01:00
|
|
|
nil ->
|
|
|
|
{:error, "User with ID #{id} not found"}
|
|
|
|
|
|
|
|
user ->
|
|
|
|
user =
|
|
|
|
user
|
|
|
|
|> Repo.preload([:actors, :default_actor])
|
|
|
|
|> Map.put(:actors, get_actors_for_user(user))
|
|
|
|
|
|
|
|
{:ok, user}
|
2018-11-06 10:30:27 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-11-12 09:05:31 +01:00
|
|
|
@doc """
|
2018-11-12 18:17:53 +01:00
|
|
|
Get an actor by it's URL (ActivityPub ID). The `:preload` option allows preloading the Followers relation.
|
|
|
|
|
|
|
|
Raises `Ecto.NoResultsError` if the Actor does not exist.
|
|
|
|
|
|
|
|
## Examples
|
|
|
|
iex> get_actor_by_url("https://mastodon.server.tld/users/user")
|
|
|
|
{:ok, %Mobilizon.Actors.Actor{preferred_username: "user"}}
|
|
|
|
|
|
|
|
iex> get_actor_by_url("https://mastodon.server.tld/users/user", true)
|
|
|
|
{:ok, %Mobilizon.Actors.Actor{preferred_username: "user", followers: []}}
|
|
|
|
|
|
|
|
iex> get_actor_by_url("non existent")
|
|
|
|
{:error, :actor_not_found}
|
|
|
|
|
2018-11-12 09:05:31 +01:00
|
|
|
"""
|
2018-11-12 18:17:53 +01:00
|
|
|
@spec get_actor_by_url(String.t(), boolean()) :: {:ok, Actor.t()} | {:error, :actor_not_found}
|
2018-11-12 09:05:31 +01:00
|
|
|
def get_actor_by_url(url, preload \\ false) do
|
|
|
|
case Repo.get_by(Actor, url: url) do
|
|
|
|
nil ->
|
|
|
|
{:error, :actor_not_found}
|
|
|
|
|
|
|
|
actor ->
|
|
|
|
if preload, do: {:ok, Repo.preload(actor, [:followers])}, else: {:ok, actor}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-11-12 18:17:53 +01:00
|
|
|
@doc """
|
|
|
|
Get an actor by it's URL (ActivityPub ID). The `:preload` option allows preloading the Followers relation.
|
|
|
|
|
|
|
|
Raises `Ecto.NoResultsError` if the Actor does not exist.
|
|
|
|
|
|
|
|
## Examples
|
|
|
|
iex> get_actor_by_url!("https://mastodon.server.tld/users/user")
|
|
|
|
%Mobilizon.Actors.Actor{}
|
|
|
|
|
|
|
|
iex> get_actor_by_url!("https://mastodon.server.tld/users/user", true)
|
|
|
|
{:ok, %Mobilizon.Actors.Actor{preferred_username: "user", followers: []}}
|
|
|
|
|
|
|
|
iex> get_actor_by_url!("non existent")
|
|
|
|
** (Ecto.NoResultsError)
|
|
|
|
|
|
|
|
"""
|
2018-11-12 09:05:31 +01:00
|
|
|
@spec get_actor_by_url!(String.t(), boolean()) :: struct()
|
|
|
|
def get_actor_by_url!(url, preload \\ false) do
|
|
|
|
actor = Repo.get_by!(Actor, url: url)
|
|
|
|
if preload, do: Repo.preload(actor, [:followers]), else: actor
|
2018-05-18 09:56:21 +02:00
|
|
|
end
|
|
|
|
|
2018-11-23 15:03:53 +01:00
|
|
|
@doc """
|
|
|
|
Get an actor by name
|
2018-07-27 10:45:35 +02:00
|
|
|
|
2018-11-23 15:03:53 +01:00
|
|
|
## Examples
|
|
|
|
iex> get_actor_by_name("tcit")
|
|
|
|
%Mobilizon.Actors.Actor{preferred_username: "tcit", domain: nil}
|
|
|
|
|
|
|
|
iex> get_actor_by_name("tcit@social.tcit.fr")
|
|
|
|
%Mobilizon.Actors.Actor{preferred_username: "tcit", domain: "social.tcit.fr"}
|
|
|
|
|
|
|
|
iex> get_actor_by_name("tcit", :Group)
|
|
|
|
nil
|
|
|
|
|
|
|
|
"""
|
|
|
|
@spec get_actor_by_name(String.t(), atom() | nil) :: Actor.t()
|
|
|
|
def get_actor_by_name(name, type \\ nil) do
|
|
|
|
# Base query
|
|
|
|
query = from(a in Actor)
|
|
|
|
|
|
|
|
# If we have Person / Group information
|
|
|
|
query =
|
|
|
|
if type in [:Person, :Group] do
|
|
|
|
from(a in query, where: a.type == ^type)
|
|
|
|
else
|
|
|
|
query
|
|
|
|
end
|
|
|
|
|
|
|
|
# If the name is a remote actor
|
|
|
|
query =
|
|
|
|
case String.split(name, "@") do
|
|
|
|
[name] -> do_get_actor_by_name(query, name)
|
|
|
|
[name, domain] -> do_get_actor_by_name(query, name, domain)
|
|
|
|
end
|
|
|
|
|
|
|
|
Repo.one(query)
|
|
|
|
end
|
|
|
|
|
|
|
|
# Get actor by username and domain is nil
|
|
|
|
defp do_get_actor_by_name(query, name) do
|
|
|
|
from(a in query, where: a.preferred_username == ^name and is_nil(a.domain))
|
|
|
|
end
|
|
|
|
|
|
|
|
# Get actor by username and domain
|
|
|
|
defp do_get_actor_by_name(query, name, domain) do
|
|
|
|
from(a in query, where: a.preferred_username == ^name and a.domain == ^domain)
|
2018-05-18 09:56:21 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
def get_local_actor_by_name(name) do
|
2018-07-27 10:45:35 +02:00
|
|
|
Repo.one(from(a in Actor, where: a.preferred_username == ^name and is_nil(a.domain)))
|
2018-05-18 09:56:21 +02:00
|
|
|
end
|
|
|
|
|
2018-05-19 10:19:21 +02:00
|
|
|
def get_local_actor_by_name_with_everything(name) do
|
2018-07-27 10:45:35 +02:00
|
|
|
actor = Repo.one(from(a in Actor, where: a.preferred_username == ^name and is_nil(a.domain)))
|
2018-11-27 17:54:54 +01:00
|
|
|
Repo.preload(actor, [:organized_events, :followers, :followings])
|
2018-05-19 10:19:21 +02:00
|
|
|
end
|
|
|
|
|
2018-11-23 15:03:53 +01:00
|
|
|
@spec get_actor_by_name_with_everything(String.t(), atom() | nil) :: Actor.t()
|
|
|
|
def get_actor_by_name_with_everything(name, type \\ nil) do
|
|
|
|
name
|
|
|
|
|> get_actor_by_name(type)
|
|
|
|
|> Repo.preload(:organized_events)
|
2018-05-19 10:19:21 +02:00
|
|
|
end
|
|
|
|
|
2019-03-04 17:20:18 +01:00
|
|
|
@doc """
|
|
|
|
Returns a cached local actor by username
|
|
|
|
"""
|
|
|
|
@spec get_cached_local_actor_by_name(String.t()) ::
|
|
|
|
{:ok, Actor.t()} | {:commit, Actor.t()} | {:ignore, any()}
|
|
|
|
def get_cached_local_actor_by_name(name) do
|
|
|
|
Cachex.fetch(:activity_pub, "actor_" <> name, fn "actor_" <> name ->
|
|
|
|
case get_local_actor_by_name(name) do
|
|
|
|
nil -> {:ignore, nil}
|
|
|
|
%Actor{} = actor -> {:commit, actor}
|
|
|
|
end
|
|
|
|
end)
|
|
|
|
end
|
|
|
|
|
2019-02-21 18:11:49 +01:00
|
|
|
@doc """
|
|
|
|
Getting an actor from url, eventually creating it
|
|
|
|
"""
|
|
|
|
# TODO: Move this to Mobilizon.Service.ActivityPub
|
|
|
|
@spec get_or_fetch_by_url(String.t(), bool()) :: {:ok, Actor.t()} | {:error, String.t()}
|
2018-11-12 09:05:31 +01:00
|
|
|
def get_or_fetch_by_url(url, preload \\ false) do
|
2018-11-12 18:17:53 +01:00
|
|
|
with {:ok, actor} <- get_actor_by_url(url, preload) do
|
2018-08-24 11:34:00 +02:00
|
|
|
{:ok, actor}
|
2018-05-18 09:56:21 +02:00
|
|
|
else
|
2018-11-12 18:17:53 +01:00
|
|
|
_ ->
|
|
|
|
case ActivityPub.make_actor_from_url(url, preload) do
|
|
|
|
{:ok, actor} ->
|
|
|
|
{:ok, actor}
|
2018-05-18 09:56:21 +02:00
|
|
|
|
2018-11-12 18:17:53 +01:00
|
|
|
_ ->
|
|
|
|
{:error, "Could not fetch by AP id"}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def get_or_fetch_by_url!(url, preload \\ false) do
|
|
|
|
with {:ok, actor} <- get_actor_by_url(url, preload) do
|
|
|
|
actor
|
|
|
|
else
|
|
|
|
_ ->
|
|
|
|
case ActivityPub.make_actor_from_url(url, preload) do
|
|
|
|
{:ok, actor} ->
|
|
|
|
actor
|
|
|
|
|
|
|
|
_ ->
|
|
|
|
raise "Could not fetch by AP id"
|
|
|
|
end
|
2018-05-18 09:56:21 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-05-30 14:27:21 +02:00
|
|
|
@doc """
|
2019-02-21 18:11:49 +01:00
|
|
|
Find local users by their username
|
2018-05-30 14:27:21 +02:00
|
|
|
"""
|
2019-02-21 18:11:49 +01:00
|
|
|
# TODO: This doesn't seem to be used anyway
|
2018-05-30 14:27:21 +02:00
|
|
|
def find_local_by_username(username) do
|
2018-07-27 10:45:35 +02:00
|
|
|
actors =
|
|
|
|
Repo.all(
|
|
|
|
from(
|
|
|
|
a in Actor,
|
|
|
|
where:
|
2019-02-21 18:11:49 +01:00
|
|
|
fragment(
|
|
|
|
"f_unaccent(?) <% f_unaccent(?) or
|
|
|
|
f_unaccent(coalesce(?, '')) <% f_unaccent(?)",
|
|
|
|
a.preferred_username,
|
|
|
|
^username,
|
|
|
|
a.name,
|
|
|
|
^username
|
|
|
|
),
|
|
|
|
where: is_nil(a.domain),
|
|
|
|
order_by:
|
|
|
|
fragment(
|
|
|
|
"word_similarity(?, ?) + word_similarity(coalesce(?, ''), ?) desc",
|
|
|
|
a.preferred_username,
|
|
|
|
^username,
|
|
|
|
a.name,
|
|
|
|
^username
|
|
|
|
)
|
2018-07-27 10:45:35 +02:00
|
|
|
)
|
|
|
|
)
|
|
|
|
|
2018-05-30 14:27:21 +02:00
|
|
|
Repo.preload(actors, :organized_events)
|
|
|
|
end
|
|
|
|
|
|
|
|
@doc """
|
|
|
|
Find actors by their name or displayed name
|
|
|
|
"""
|
2018-11-23 15:03:53 +01:00
|
|
|
@spec find_actors_by_username_or_name(String.t(), integer(), integer()) :: list(Actor.t())
|
2018-12-14 11:23:36 +01:00
|
|
|
def find_actors_by_username_or_name(username, page \\ nil, limit \\ nil)
|
2018-11-12 23:30:47 +01:00
|
|
|
def find_actors_by_username_or_name("", _page, _limit), do: []
|
2018-11-06 10:30:27 +01:00
|
|
|
|
|
|
|
def find_actors_by_username_or_name(username, page, limit) do
|
2018-07-27 10:45:35 +02:00
|
|
|
Repo.all(
|
|
|
|
from(
|
|
|
|
a in Actor,
|
|
|
|
where:
|
2019-02-21 18:11:49 +01:00
|
|
|
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
|
|
|
|
)
|
2018-07-27 10:45:35 +02:00
|
|
|
)
|
2018-12-14 11:23:36 +01:00
|
|
|
|> paginate(page, limit)
|
2018-07-27 10:45:35 +02:00
|
|
|
)
|
2018-05-30 14:27:21 +02:00
|
|
|
end
|
|
|
|
|
2019-01-25 09:23:44 +01:00
|
|
|
@doc """
|
2019-02-21 18:11:49 +01:00
|
|
|
Get a group by its actor id
|
2019-01-25 09:23:44 +01:00
|
|
|
"""
|
2019-02-21 18:11:49 +01:00
|
|
|
def get_group_by_actor_id(actor_id) do
|
2019-01-25 09:23:44 +01:00
|
|
|
case Repo.get_by(Actor, id: actor_id, type: :Group) do
|
|
|
|
nil -> {:error, :group_not_found}
|
|
|
|
actor -> {:ok, actor}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-05-18 09:56:21 +02:00
|
|
|
@doc """
|
|
|
|
Authenticate user
|
|
|
|
"""
|
|
|
|
def authenticate(%{user: user, password: password}) do
|
|
|
|
# Does password match the one stored in the database?
|
2019-02-14 14:19:55 +01:00
|
|
|
case Argon2.verify_pass(password, user.password_hash) do
|
2018-05-18 09:56:21 +02:00
|
|
|
true ->
|
|
|
|
# Yes, create and return the token
|
2018-10-11 17:37:39 +02:00
|
|
|
MobilizonWeb.Guardian.encode_and_sign(user)
|
2018-07-27 10:45:35 +02:00
|
|
|
|
2018-05-18 09:56:21 +02:00
|
|
|
_ ->
|
|
|
|
# No, return an error
|
|
|
|
{:error, :unauthorized}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-12-14 17:41:55 +01:00
|
|
|
@doc """
|
|
|
|
Create a new RSA key
|
|
|
|
"""
|
2018-12-03 11:58:57 +01:00
|
|
|
@spec create_keys() :: String.t()
|
2018-12-14 17:41:55 +01:00
|
|
|
def create_keys() do
|
2018-12-03 11:58:57 +01:00
|
|
|
key = :public_key.generate_key({:rsa, 2048, 65_537})
|
|
|
|
entry = :public_key.pem_entry_encode(:RSAPrivateKey, key)
|
|
|
|
[entry] |> :public_key.pem_encode() |> String.trim_trailing()
|
|
|
|
end
|
|
|
|
|
2018-05-18 09:56:21 +02:00
|
|
|
@doc """
|
|
|
|
Register user
|
|
|
|
"""
|
2019-01-25 13:59:58 +01:00
|
|
|
@spec register(map()) :: {:ok, User.t()} | {:error, String.t()}
|
|
|
|
def register(%{email: _email, password: _password} = args) do
|
|
|
|
with {:ok, %User{} = user} <-
|
|
|
|
%User{} |> User.registration_changeset(args) |> Mobilizon.Repo.insert() do
|
|
|
|
{:ok, user}
|
2018-11-28 17:16:23 +01:00
|
|
|
end
|
|
|
|
end
|
2018-11-06 10:30:27 +01:00
|
|
|
|
2019-01-21 15:08:22 +01:00
|
|
|
@doc """
|
|
|
|
Create a new person actor
|
|
|
|
"""
|
|
|
|
def new_person(args) do
|
|
|
|
key = :public_key.generate_key({:rsa, 2048, 65_537})
|
|
|
|
entry = :public_key.pem_entry_encode(:RSAPrivateKey, key)
|
|
|
|
pem = [entry] |> :public_key.pem_encode() |> String.trim_trailing()
|
|
|
|
args = Map.put(args, :keys, pem)
|
|
|
|
|
|
|
|
actor = Mobilizon.Actors.Actor.registration_changeset(%Mobilizon.Actors.Actor{}, args)
|
|
|
|
Mobilizon.Repo.insert(actor)
|
|
|
|
end
|
|
|
|
|
2018-05-30 14:27:21 +02:00
|
|
|
def register_bot_account(%{name: name, summary: summary}) do
|
2018-06-14 18:15:27 +02:00
|
|
|
key = :public_key.generate_key({:rsa, 2048, 65_537})
|
2018-05-30 14:27:21 +02:00
|
|
|
entry = :public_key.pem_entry_encode(:RSAPrivateKey, key)
|
2018-06-14 18:15:27 +02:00
|
|
|
pem = [entry] |> :public_key.pem_encode() |> String.trim_trailing()
|
2018-05-30 14:27:21 +02:00
|
|
|
|
2018-07-27 10:45:35 +02:00
|
|
|
actor =
|
2018-10-11 17:37:39 +02:00
|
|
|
Mobilizon.Actors.Actor.registration_changeset(%Mobilizon.Actors.Actor{}, %{
|
2018-07-27 10:45:35 +02:00
|
|
|
preferred_username: name,
|
|
|
|
domain: nil,
|
|
|
|
keys: pem,
|
|
|
|
summary: summary,
|
|
|
|
type: :Service
|
|
|
|
})
|
2018-05-30 14:27:21 +02:00
|
|
|
|
|
|
|
try do
|
2018-10-11 17:37:39 +02:00
|
|
|
Mobilizon.Repo.insert!(actor)
|
2018-05-30 14:27:21 +02:00
|
|
|
rescue
|
|
|
|
e in Ecto.InvalidChangesetError ->
|
2018-06-14 17:25:55 +02:00
|
|
|
{:error, e.changeset}
|
2018-05-30 14:27:21 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-05-18 09:56:21 +02:00
|
|
|
@doc """
|
|
|
|
Creates a user.
|
|
|
|
|
|
|
|
## Examples
|
|
|
|
|
2018-11-12 18:17:53 +01:00
|
|
|
iex> create_user(%{email: "test@test.tld"})
|
|
|
|
{:ok, %Mobilizon.Actors.User{}}
|
2018-05-18 09:56:21 +02:00
|
|
|
|
2018-11-12 18:17:53 +01:00
|
|
|
iex> create_user(%{email: "not an email"})
|
2018-05-18 09:56:21 +02:00
|
|
|
{:error, %Ecto.Changeset{}}
|
|
|
|
|
|
|
|
"""
|
|
|
|
def create_user(attrs \\ %{}) do
|
|
|
|
%User{}
|
|
|
|
|> User.registration_changeset(attrs)
|
|
|
|
|> Repo.insert()
|
|
|
|
end
|
|
|
|
|
2018-08-24 11:34:00 +02:00
|
|
|
@doc """
|
|
|
|
Gets an user by it's email
|
|
|
|
|
|
|
|
## Examples
|
|
|
|
|
2018-11-12 18:17:53 +01:00
|
|
|
iex> get_user_by_email("test@test.tld", true)
|
|
|
|
{:ok, %Mobilizon.Actors.User{}}
|
2018-08-24 11:34:00 +02:00
|
|
|
|
2018-11-12 18:17:53 +01:00
|
|
|
iex> get_user_by_email("test@notfound.tld", false)
|
|
|
|
{:error, :user_not_found}
|
2018-08-24 11:34:00 +02:00
|
|
|
"""
|
2018-11-06 10:30:27 +01:00
|
|
|
def get_user_by_email(email, activated \\ nil) do
|
|
|
|
query =
|
|
|
|
case activated do
|
2018-11-29 17:43:22 +01:00
|
|
|
nil ->
|
|
|
|
from(u in User, where: u.email == ^email, preload: :default_actor)
|
|
|
|
|
|
|
|
true ->
|
|
|
|
from(u in User,
|
|
|
|
where: u.email == ^email and not is_nil(u.confirmed_at),
|
|
|
|
preload: :default_actor
|
|
|
|
)
|
|
|
|
|
|
|
|
false ->
|
|
|
|
from(u in User,
|
|
|
|
where: u.email == ^email and is_nil(u.confirmed_at),
|
|
|
|
preload: :default_actor
|
|
|
|
)
|
2018-11-06 10:30:27 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
case Repo.one(query) do
|
|
|
|
nil -> {:error, :user_not_found}
|
2018-08-24 11:34:00 +02:00
|
|
|
user -> {:ok, user}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-11-29 17:43:22 +01:00
|
|
|
@doc """
|
|
|
|
Get an user by it's activation token
|
|
|
|
"""
|
2018-11-28 17:16:23 +01:00
|
|
|
@spec get_user_by_activation_token(String.t()) :: Actor.t()
|
|
|
|
def get_user_by_activation_token(token) do
|
|
|
|
Repo.one(
|
|
|
|
from(u in User,
|
|
|
|
where: u.confirmation_token == ^token,
|
|
|
|
preload: [:default_actor]
|
|
|
|
)
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2018-11-29 17:43:22 +01:00
|
|
|
@doc """
|
|
|
|
Get an user by it's reset password token
|
|
|
|
"""
|
|
|
|
@spec get_user_by_reset_password_token(String.t()) :: Actor.t()
|
|
|
|
def get_user_by_reset_password_token(token) do
|
|
|
|
Repo.one(
|
|
|
|
from(u in User,
|
|
|
|
where: u.reset_password_token == ^token,
|
|
|
|
preload: [:default_actor]
|
|
|
|
)
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2018-05-18 09:56:21 +02:00
|
|
|
@doc """
|
|
|
|
Updates a user.
|
|
|
|
|
|
|
|
## Examples
|
|
|
|
|
2018-11-12 18:17:53 +01:00
|
|
|
iex> update_user(User{}, %{password: "coucou"})
|
|
|
|
{:ok, %Mobilizon.Actors.User{}}
|
2018-05-18 09:56:21 +02:00
|
|
|
|
2018-11-12 18:17:53 +01:00
|
|
|
iex> update_user(User{}, %{password: nil})
|
2018-05-18 09:56:21 +02:00
|
|
|
{:error, %Ecto.Changeset{}}
|
|
|
|
|
|
|
|
"""
|
|
|
|
def update_user(%User{} = user, attrs) do
|
2018-11-29 17:43:22 +01:00
|
|
|
with {:ok, %User{} = user} <-
|
|
|
|
user
|
|
|
|
|> User.changeset(attrs)
|
|
|
|
|> Repo.update() do
|
|
|
|
{:ok, Repo.preload(user, [:default_actor])}
|
|
|
|
end
|
2018-05-18 09:56:21 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
@doc """
|
|
|
|
Deletes a User.
|
|
|
|
|
|
|
|
## Examples
|
|
|
|
|
2018-11-12 18:17:53 +01:00
|
|
|
iex> delete_user(%User{email: "test@test.tld"})
|
|
|
|
{:ok, %Mobilizon.Actors.User{}}
|
2018-05-18 09:56:21 +02:00
|
|
|
|
2018-11-12 18:17:53 +01:00
|
|
|
iex> delete_user(%User{})
|
2018-05-18 09:56:21 +02:00
|
|
|
{:error, %Ecto.Changeset{}}
|
|
|
|
|
|
|
|
"""
|
|
|
|
def delete_user(%User{} = user) do
|
|
|
|
Repo.delete(user)
|
|
|
|
end
|
|
|
|
|
2018-11-28 17:16:23 +01:00
|
|
|
# @doc """
|
|
|
|
# Returns an `%Ecto.Changeset{}` for tracking user changes.
|
2018-05-18 09:56:21 +02:00
|
|
|
|
2018-11-28 17:16:23 +01:00
|
|
|
# ## Examples
|
2018-05-18 09:56:21 +02:00
|
|
|
|
2018-11-28 17:16:23 +01:00
|
|
|
# iex> change_user(%Mobilizon.Actors.User{})
|
|
|
|
# %Ecto.Changeset{data: %Mobilizon.Actors.User{}}
|
2018-05-18 09:56:21 +02:00
|
|
|
|
2018-11-28 17:16:23 +01:00
|
|
|
# """
|
|
|
|
# def change_user(%User{} = user) do
|
|
|
|
# User.changeset(user, %{})
|
|
|
|
# end
|
2018-05-18 09:56:21 +02:00
|
|
|
|
2018-10-11 17:37:39 +02:00
|
|
|
alias Mobilizon.Actors.Member
|
2018-05-18 09:56:21 +02:00
|
|
|
|
|
|
|
@doc """
|
|
|
|
Gets a single member.
|
|
|
|
|
|
|
|
Raises `Ecto.NoResultsError` if the Member does not exist.
|
|
|
|
|
|
|
|
## Examples
|
|
|
|
|
|
|
|
iex> get_member!(123)
|
2018-11-12 18:17:53 +01:00
|
|
|
%Mobilizon.Actors.Member{}
|
2018-05-18 09:56:21 +02:00
|
|
|
|
|
|
|
iex> get_member!(456)
|
|
|
|
** (Ecto.NoResultsError)
|
|
|
|
|
|
|
|
"""
|
|
|
|
def get_member!(id), do: Repo.get!(Member, id)
|
|
|
|
|
|
|
|
@doc """
|
|
|
|
Creates a member.
|
|
|
|
|
|
|
|
## Examples
|
|
|
|
|
2018-11-12 18:17:53 +01:00
|
|
|
iex> create_member(%{actor: %Actor{}})
|
|
|
|
{:ok, %Mobilizon.Actors.Member{}}
|
2018-05-18 09:56:21 +02:00
|
|
|
|
2018-11-12 18:17:53 +01:00
|
|
|
iex> create_member(%{actor: nil})
|
2018-05-18 09:56:21 +02:00
|
|
|
{:error, %Ecto.Changeset{}}
|
|
|
|
|
|
|
|
"""
|
|
|
|
def create_member(attrs \\ %{}) do
|
2018-08-24 11:34:00 +02:00
|
|
|
with {:ok, %Member{} = member} <-
|
|
|
|
%Member{}
|
|
|
|
|> Member.changeset(attrs)
|
|
|
|
|> Repo.insert() do
|
|
|
|
{:ok, Repo.preload(member, [:actor, :parent])}
|
|
|
|
end
|
2018-05-18 09:56:21 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
@doc """
|
|
|
|
Updates a member.
|
|
|
|
|
|
|
|
## Examples
|
|
|
|
|
2018-11-12 18:17:53 +01:00
|
|
|
iex> update_member(%Member{}, %{role: 3})
|
|
|
|
{:ok, %Mobilizon.Actors.Member{}}
|
2018-05-18 09:56:21 +02:00
|
|
|
|
2018-11-12 18:17:53 +01:00
|
|
|
iex> update_member(%Member{}, %{role: nil})
|
2018-05-18 09:56:21 +02:00
|
|
|
{:error, %Ecto.Changeset{}}
|
|
|
|
|
|
|
|
"""
|
|
|
|
def update_member(%Member{} = member, attrs) do
|
|
|
|
member
|
|
|
|
|> Member.changeset(attrs)
|
|
|
|
|> Repo.update()
|
|
|
|
end
|
|
|
|
|
|
|
|
@doc """
|
|
|
|
Deletes a Member.
|
|
|
|
|
|
|
|
## Examples
|
|
|
|
|
2018-11-12 18:17:53 +01:00
|
|
|
iex> delete_member(%Member{})
|
|
|
|
{:ok, %Mobilizon.Actors.Member{}}
|
2018-05-18 09:56:21 +02:00
|
|
|
|
2018-11-12 18:17:53 +01:00
|
|
|
iex> delete_member(%Member{})
|
2018-05-18 09:56:21 +02:00
|
|
|
{:error, %Ecto.Changeset{}}
|
|
|
|
|
|
|
|
"""
|
|
|
|
def delete_member(%Member{} = member) do
|
|
|
|
Repo.delete(member)
|
|
|
|
end
|
|
|
|
|
|
|
|
@doc """
|
|
|
|
Returns an `%Ecto.Changeset{}` for tracking member changes.
|
|
|
|
|
|
|
|
## Examples
|
|
|
|
|
2018-11-12 18:17:53 +01:00
|
|
|
iex> change_member(%Member{})
|
|
|
|
%Ecto.Changeset{data: %Mobilizon.Actors.Member{}}
|
2018-05-18 09:56:21 +02:00
|
|
|
|
|
|
|
"""
|
|
|
|
def change_member(%Member{} = member) do
|
|
|
|
Member.changeset(member, %{})
|
|
|
|
end
|
|
|
|
|
2018-06-06 17:42:53 +02:00
|
|
|
def groups_for_actor(%Actor{id: id} = _actor) do
|
|
|
|
Repo.all(
|
2018-07-27 10:45:35 +02:00
|
|
|
from(
|
|
|
|
m in Member,
|
|
|
|
where: m.actor_id == ^id,
|
|
|
|
preload: [:parent]
|
|
|
|
)
|
2018-06-06 17:42:53 +02:00
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
def members_for_group(%Actor{type: :Group, id: id} = _group) do
|
|
|
|
Repo.all(
|
2018-07-27 10:45:35 +02:00
|
|
|
from(
|
|
|
|
m in Member,
|
|
|
|
where: m.parent_id == ^id,
|
|
|
|
preload: [:parent, :actor]
|
|
|
|
)
|
2018-06-06 17:42:53 +02:00
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2018-10-11 17:37:39 +02:00
|
|
|
alias Mobilizon.Actors.Bot
|
2018-05-30 14:27:21 +02:00
|
|
|
|
|
|
|
@doc """
|
|
|
|
Returns the list of bots.
|
|
|
|
|
|
|
|
## Examples
|
|
|
|
|
|
|
|
iex> list_bots()
|
2018-11-12 18:17:53 +01:00
|
|
|
[%Mobilizon.Actors.Bot{}]
|
2018-05-30 14:27:21 +02:00
|
|
|
|
|
|
|
"""
|
|
|
|
def list_bots do
|
|
|
|
Repo.all(Bot)
|
|
|
|
end
|
|
|
|
|
|
|
|
@doc """
|
|
|
|
Gets a single bot.
|
|
|
|
|
|
|
|
Raises `Ecto.NoResultsError` if the Bot does not exist.
|
|
|
|
|
|
|
|
## Examples
|
|
|
|
|
|
|
|
iex> get_bot!(123)
|
2018-11-12 18:17:53 +01:00
|
|
|
%Mobilizon.Actors.Bot{}
|
2018-05-30 14:27:21 +02:00
|
|
|
|
|
|
|
iex> get_bot!(456)
|
|
|
|
** (Ecto.NoResultsError)
|
|
|
|
|
|
|
|
"""
|
|
|
|
def get_bot!(id), do: Repo.get!(Bot, id)
|
|
|
|
|
2018-07-27 10:45:35 +02:00
|
|
|
@spec get_bot_by_actor(Actor.t()) :: Bot.t()
|
2018-05-30 14:27:21 +02:00
|
|
|
def get_bot_by_actor(%Actor{} = actor) do
|
|
|
|
Repo.get_by!(Bot, actor_id: actor.id)
|
|
|
|
end
|
|
|
|
|
|
|
|
@doc """
|
|
|
|
Creates a bot.
|
|
|
|
|
|
|
|
## Examples
|
|
|
|
|
2018-11-12 18:17:53 +01:00
|
|
|
iex> create_bot(%{source: "toto"})
|
|
|
|
{:ok, %Mobilizon.Actors.Bot{}}
|
2018-05-30 14:27:21 +02:00
|
|
|
|
2018-11-12 18:17:53 +01:00
|
|
|
iex> create_bot(%{source: nil})
|
2018-05-30 14:27:21 +02:00
|
|
|
{:error, %Ecto.Changeset{}}
|
|
|
|
|
|
|
|
"""
|
|
|
|
def create_bot(attrs \\ %{}) do
|
|
|
|
%Bot{}
|
|
|
|
|> Bot.changeset(attrs)
|
|
|
|
|> Repo.insert()
|
|
|
|
end
|
|
|
|
|
|
|
|
@doc """
|
|
|
|
Updates a bot.
|
|
|
|
|
|
|
|
## Examples
|
|
|
|
|
2018-11-12 18:17:53 +01:00
|
|
|
iex> update_bot(%Bot{}, %{source: "new"})
|
|
|
|
{:ok, %Mobilizon.Actors.Bot{}}
|
2018-05-30 14:27:21 +02:00
|
|
|
|
2018-11-12 18:17:53 +01:00
|
|
|
iex> update_bot(%Bot{}, %{source: nil})
|
2018-05-30 14:27:21 +02:00
|
|
|
{:error, %Ecto.Changeset{}}
|
|
|
|
|
|
|
|
"""
|
|
|
|
def update_bot(%Bot{} = bot, attrs) do
|
|
|
|
bot
|
|
|
|
|> Bot.changeset(attrs)
|
|
|
|
|> Repo.update()
|
|
|
|
end
|
|
|
|
|
|
|
|
@doc """
|
|
|
|
Deletes a Bot.
|
|
|
|
|
|
|
|
## Examples
|
|
|
|
|
2018-11-12 18:17:53 +01:00
|
|
|
iex> delete_bot(%Bot{})
|
|
|
|
{:ok, %Mobilizon.Actors.Bot{}}
|
2018-05-30 14:27:21 +02:00
|
|
|
|
2018-11-12 18:17:53 +01:00
|
|
|
iex> delete_bot(%Bot{})
|
2018-05-30 14:27:21 +02:00
|
|
|
{:error, %Ecto.Changeset{}}
|
|
|
|
|
|
|
|
"""
|
|
|
|
def delete_bot(%Bot{} = bot) do
|
|
|
|
Repo.delete(bot)
|
|
|
|
end
|
|
|
|
|
|
|
|
@doc """
|
|
|
|
Returns an `%Ecto.Changeset{}` for tracking bot changes.
|
|
|
|
|
|
|
|
## Examples
|
|
|
|
|
2018-11-12 18:17:53 +01:00
|
|
|
iex> change_bot(%Bot{})
|
|
|
|
%Ecto.Changeset{data: %Mobilizon.Actors.Bot{}}
|
2018-05-30 14:27:21 +02:00
|
|
|
|
|
|
|
"""
|
|
|
|
def change_bot(%Bot{} = bot) do
|
|
|
|
Bot.changeset(bot, %{})
|
|
|
|
end
|
2018-08-01 14:45:18 +02:00
|
|
|
|
2018-10-11 17:37:39 +02:00
|
|
|
alias Mobilizon.Actors.Follower
|
2018-08-01 14:45:18 +02:00
|
|
|
|
|
|
|
@doc """
|
|
|
|
Gets a single follower.
|
|
|
|
|
|
|
|
Raises `Ecto.NoResultsError` if the Follower does not exist.
|
|
|
|
|
|
|
|
## Examples
|
|
|
|
|
|
|
|
iex> get_follower!(123)
|
2018-11-12 18:17:53 +01:00
|
|
|
%Mobilizon.Actors.Follower{}
|
2018-08-01 14:45:18 +02:00
|
|
|
|
|
|
|
iex> get_follower!(456)
|
|
|
|
** (Ecto.NoResultsError)
|
|
|
|
|
|
|
|
"""
|
|
|
|
def get_follower!(id) do
|
|
|
|
Repo.get!(Follower, id)
|
|
|
|
|> Repo.preload([:actor, :target_actor])
|
|
|
|
end
|
|
|
|
|
2018-12-14 17:41:55 +01:00
|
|
|
@spec get_follower(Actor.t(), Actor.t()) :: Follower.t()
|
|
|
|
def get_follower(%Actor{id: followed_id}, %Actor{id: follower_id}) do
|
|
|
|
Repo.one(
|
|
|
|
from(f in Follower, where: f.target_actor_id == ^followed_id and f.actor_id == ^follower_id)
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2018-08-01 14:45:18 +02:00
|
|
|
@doc """
|
|
|
|
Creates a follower.
|
|
|
|
|
|
|
|
## Examples
|
|
|
|
|
2018-11-12 18:17:53 +01:00
|
|
|
iex> create_follower(%{actor: %Actor{}})
|
|
|
|
{:ok, %Mobilizon.Actors.Follower{}}
|
2018-08-01 14:45:18 +02:00
|
|
|
|
2018-11-12 18:17:53 +01:00
|
|
|
iex> create_follower(%{actor: nil})
|
2018-08-01 14:45:18 +02:00
|
|
|
{:error, %Ecto.Changeset{}}
|
|
|
|
|
|
|
|
"""
|
|
|
|
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.
|
|
|
|
|
|
|
|
## Examples
|
|
|
|
|
2018-11-12 18:17:53 +01:00
|
|
|
iex> update_follower(Follower{}, %{approved: true})
|
|
|
|
{:ok, %Mobilizon.Actors.Follower{}}
|
2018-08-01 14:45:18 +02:00
|
|
|
|
2018-11-12 18:17:53 +01:00
|
|
|
iex> update_follower(Follower{}, %{approved: nil})
|
2018-08-01 14:45:18 +02:00
|
|
|
{:error, %Ecto.Changeset{}}
|
|
|
|
|
|
|
|
"""
|
|
|
|
def update_follower(%Follower{} = follower, attrs) do
|
|
|
|
follower
|
|
|
|
|> Follower.changeset(attrs)
|
|
|
|
|> Repo.update()
|
|
|
|
end
|
|
|
|
|
|
|
|
@doc """
|
|
|
|
Deletes a Follower.
|
|
|
|
|
|
|
|
## Examples
|
|
|
|
|
2018-11-12 18:17:53 +01:00
|
|
|
iex> delete_follower(Follower{})
|
|
|
|
{:ok, %Mobilizon.Actors.Follower{}}
|
2018-08-01 14:45:18 +02:00
|
|
|
|
2018-11-12 18:17:53 +01:00
|
|
|
iex> delete_follower(Follower{})
|
2018-08-01 14:45:18 +02:00
|
|
|
{:error, %Ecto.Changeset{}}
|
|
|
|
|
|
|
|
"""
|
|
|
|
def delete_follower(%Follower{} = follower) do
|
|
|
|
Repo.delete(follower)
|
|
|
|
end
|
|
|
|
|
2018-12-14 17:41:55 +01:00
|
|
|
@doc """
|
|
|
|
Delete a follower by followed and follower actors
|
|
|
|
|
|
|
|
## Examples
|
|
|
|
|
|
|
|
iex> delete_follower(%Actor{}, %Actor{})
|
|
|
|
{:ok, %Mobilizon.Actors.Follower{}}
|
|
|
|
|
|
|
|
iex> delete_follower(%Actor{}, %Actor{})
|
|
|
|
{:error, %Ecto.Changeset{}}
|
|
|
|
|
|
|
|
"""
|
|
|
|
@spec delete_follower(Actor.t(), Actor.t()) ::
|
|
|
|
{:ok, Follower.t()} | {:error, Ecto.Changeset.t()}
|
|
|
|
def delete_follower(%Actor{} = followed, %Actor{} = follower) do
|
|
|
|
get_follower(followed, follower) |> Repo.delete()
|
|
|
|
end
|
|
|
|
|
2018-08-01 14:45:18 +02:00
|
|
|
@doc """
|
|
|
|
Returns an `%Ecto.Changeset{}` for tracking follower changes.
|
|
|
|
|
|
|
|
## Examples
|
|
|
|
|
2018-11-12 18:17:53 +01:00
|
|
|
iex> change_follower(Follower{})
|
|
|
|
%Ecto.Changeset{data: %Mobilizon.Actors.Follower{}}
|
2018-08-01 14:45:18 +02:00
|
|
|
|
|
|
|
"""
|
|
|
|
def change_follower(%Follower{} = follower) do
|
|
|
|
Follower.changeset(follower, %{})
|
|
|
|
end
|
2018-05-18 09:56:21 +02:00
|
|
|
end
|