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
|
|
|
|
|
|
|
alias Mobilizon.Federation.ActivityPub
|
|
|
|
alias Mobilizon.Federation.ActivityPub.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
|
|
|
|
"""
|
2020-01-26 21:11:16 +01:00
|
|
|
@spec create_group(map) :: {:ok, Activity.t(), Actor.t()} | any
|
2019-10-25 17:43:37 +02:00
|
|
|
def create_group(args) do
|
|
|
|
with preferred_username <-
|
2020-06-24 16:33:59 +02:00
|
|
|
args |> Map.get(:preferred_username) |> HTML.strip_tags() |> String.trim(),
|
2019-10-25 17:43:37 +02:00
|
|
|
{:existing_group, nil} <-
|
|
|
|
{:existing_group, Actors.get_local_group_by_title(preferred_username)},
|
2019-12-03 11:29:51 +01:00
|
|
|
{:ok, %Activity{} = activity, %Actor{} = group} <-
|
|
|
|
ActivityPub.create(:group, args, true, %{"actor" => args.creator_actor.url}) do
|
2019-10-25 17:43:37 +02:00
|
|
|
{:ok, activity, group}
|
2018-12-14 17:41:55 +01:00
|
|
|
else
|
|
|
|
{:existing_group, _} ->
|
2019-09-02 10:50:00 +02:00
|
|
|
{:error, "A group with this name already exists"}
|
2018-12-14 17:41:55 +01:00
|
|
|
|
2019-09-07 19:54:11 +02:00
|
|
|
{:is_owned, nil} ->
|
2019-09-02 10:50:00 +02:00
|
|
|
{:error, "Actor id is not owned by authenticated user"}
|
2018-12-14 17:41:55 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|