2020-01-26 21:11:16 +01:00
|
|
|
defmodule Mobilizon.GraphQL.API.Groups do
|
2018-12-14 17:41:55 +01:00
|
|
|
@moduledoc """
|
2019-09-22 16:26:23 +02:00
|
|
|
API for Groups.
|
2018-12-14 17:41:55 +01:00
|
|
|
"""
|
2019-09-22 16:26:23 +02:00
|
|
|
|
2018-12-14 17:41:55 +01:00
|
|
|
alias Mobilizon.Actors
|
2019-09-07 19:54:11 +02:00
|
|
|
alias Mobilizon.Actors.Actor
|
2020-01-22 02:14:42 +01:00
|
|
|
|
2021-09-28 19:40:37 +02:00
|
|
|
alias Mobilizon.Federation.ActivityPub.{Actions, Activity}
|
2020-06-24 16:33:59 +02:00
|
|
|
alias Mobilizon.Service.Formatter.HTML
|
2018-12-14 17:41:55 +01:00
|
|
|
|
2019-03-19 11:16:03 +01:00
|
|
|
@doc """
|
|
|
|
Create a group
|
|
|
|
"""
|
2021-09-28 19:40:37 +02:00
|
|
|
@spec create_group(map) ::
|
|
|
|
{:ok, Activity.t(), Actor.t()}
|
|
|
|
| {:error, String.t() | Ecto.Changeset.t()}
|
2019-10-25 17:43:37 +02:00
|
|
|
def create_group(args) do
|
2021-09-28 19:40:37 +02:00
|
|
|
preferred_username =
|
|
|
|
args |> Map.get(:preferred_username) |> HTML.strip_tags() |> String.trim()
|
|
|
|
|
|
|
|
args = args |> Map.put(:type, :Group)
|
|
|
|
|
|
|
|
case Actors.get_local_actor_by_name(preferred_username) do
|
|
|
|
nil ->
|
|
|
|
Actions.Create.create(:actor, args, true, %{"actor" => args.creator_actor.url})
|
|
|
|
|
|
|
|
%Actor{} ->
|
|
|
|
{:error, "A profile or group with that name already exists"}
|
2020-07-09 17:24:28 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-09-28 19:40:37 +02:00
|
|
|
@spec update_group(map) ::
|
|
|
|
{:ok, Activity.t(), Actor.t()} | {:error, :group_not_found | Ecto.Changeset.t()}
|
2020-07-09 17:24:28 +02:00
|
|
|
def update_group(%{id: id} = args) do
|
2021-09-28 19:40:37 +02:00
|
|
|
with {:ok, %Actor{type: :Group} = group} <- Actors.get_group_by_actor_id(id) do
|
|
|
|
Actions.Update.update(group, args, true, %{"actor" => args.updater_actor.url})
|
2018-12-14 17:41:55 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|