mobilizon/lib/graphql/api/groups.ex

48 lines
1.4 KiB
Elixir
Raw Permalink Normal View History

2020-01-26 21:11:16 +01:00
defmodule Mobilizon.GraphQL.API.Groups do
@moduledoc """
2019-09-22 16:26:23 +02:00
API for Groups.
"""
2019-09-22 16:26:23 +02:00
alias Mobilizon.Actors
2019-09-07 19:54:11 +02:00
alias Mobilizon.Actors.Actor
alias Mobilizon.GraphQL.Error
alias Mobilizon.Federation.ActivityPub.{Actions, Activity}
alias Mobilizon.Service.Formatter.HTML
import Mobilizon.Web.Gettext
@doc """
Create a group
"""
@spec create_group(map) ::
{:ok, Activity.t(), Actor.t()}
| {:error, String.t() | Ecto.Changeset.t()}
def create_group(args) do
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,
%Error{
code: :validation,
message: dgettext("errors", "A profile or group with that name already exists"),
status_code: 409,
field: "preferred_username"
}}
end
end
@spec update_group(map) ::
{:ok, Activity.t(), Actor.t()} | {:error, :group_not_found | Ecto.Changeset.t()}
def update_group(%{id: id} = args) do
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})
end
end
end