mobilizon/lib/graphql/api/groups.ex

33 lines
993 B
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
alias Mobilizon.Federation.ActivityPub.Activity
@doc """
Create a group
"""
2020-01-26 20:11:16 +00:00
@spec create_group(map) :: {:ok, Activity.t(), Actor.t()} | any
def create_group(args) do
with preferred_username <-
args |> Map.get(:preferred_username) |> HtmlSanitizeEx.strip_tags() |> String.trim(),
{:existing_group, nil} <-
{:existing_group, Actors.get_local_group_by_title(preferred_username)},
{:ok, %Activity{} = activity, %Actor{} = group} <-
ActivityPub.create(:group, args, true, %{"actor" => args.creator_actor.url}) do
{:ok, activity, group}
else
{:existing_group, _} ->
2019-09-02 08:50:00 +00:00
{:error, "A group with this name already exists"}
2019-09-07 17:54:11 +00:00
{:is_owned, nil} ->
2019-09-02 08:50:00 +00:00
{:error, "Actor id is not owned by authenticated user"}
end
end
end