1
0
Fork 0
mirror of https://framagit.org/framasoft/mobilizon.git synced 2024-12-23 00:14:48 +00:00
mobilizon/lib/graphql/api/groups.ex

41 lines
1.2 KiB
Elixir
Raw Normal View History

2020-01-26 20:11:16 +00:00
defmodule Mobilizon.GraphQL.API.Groups do
@moduledoc """
2019-09-22 14:26:23 +00:00
API for Groups.
"""
2019-09-22 14:26:23 +00:00
alias Mobilizon.Actors
2019-09-07 17:54:11 +00:00
alias Mobilizon.Actors.Actor
2020-01-22 01:14:42 +00:00
alias Mobilizon.Federation.ActivityPub.{Actions, Activity}
alias Mobilizon.Service.Formatter.HTML
@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, "A profile or group with that name already exists"}
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