2020-01-26 20:11:16 +00:00
|
|
|
defmodule Mobilizon.GraphQL.API.Groups do
|
2018-12-14 16:41:55 +00:00
|
|
|
@moduledoc """
|
2019-09-22 14:26:23 +00:00
|
|
|
API for Groups.
|
2018-12-14 16:41:55 +00:00
|
|
|
"""
|
2019-09-22 14:26:23 +00:00
|
|
|
|
2018-12-14 16:41:55 +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
|
2020-06-24 14:33:59 +00:00
|
|
|
alias Mobilizon.Service.Formatter.HTML
|
2018-12-14 16:41:55 +00:00
|
|
|
|
2019-03-19 10:16:03 +00:00
|
|
|
@doc """
|
|
|
|
Create a group
|
|
|
|
"""
|
2020-01-26 20:11:16 +00:00
|
|
|
@spec create_group(map) :: {:ok, Activity.t(), Actor.t()} | any
|
2019-10-25 15:43:37 +00:00
|
|
|
def create_group(args) do
|
|
|
|
with preferred_username <-
|
2020-06-24 14:33:59 +00:00
|
|
|
args |> Map.get(:preferred_username) |> HTML.strip_tags() |> String.trim(),
|
2019-10-25 15:43:37 +00:00
|
|
|
{:existing_group, nil} <-
|
2020-08-27 09:53:24 +00:00
|
|
|
{:existing_group, Actors.get_local_actor_by_name(preferred_username)},
|
2020-07-09 15:24:28 +00:00
|
|
|
args <- args |> Map.put(:type, :Group),
|
2019-12-03 10:29:51 +00:00
|
|
|
{:ok, %Activity{} = activity, %Actor{} = group} <-
|
2020-07-09 15:24:28 +00:00
|
|
|
ActivityPub.create(:actor, args, true, %{"actor" => args.creator_actor.url}) do
|
|
|
|
{:ok, activity, group}
|
|
|
|
else
|
|
|
|
{:existing_group, _} ->
|
|
|
|
{:error, "A group with this name already exists"}
|
|
|
|
|
|
|
|
{:is_owned, nil} ->
|
2020-09-29 07:53:48 +00:00
|
|
|
{:error, "Profile is not owned by authenticated user"}
|
2020-07-09 15:24:28 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
@spec create_group(map) :: {:ok, Activity.t(), Actor.t()} | any
|
|
|
|
def update_group(%{id: id} = args) do
|
|
|
|
with {:existing_group, {:ok, %Actor{type: :Group} = group}} <-
|
|
|
|
{:existing_group, Actors.get_group_by_actor_id(id)},
|
|
|
|
{:ok, %Activity{} = activity, %Actor{} = group} <-
|
|
|
|
ActivityPub.update(group, args, true, %{"actor" => args.updater_actor.url}) do
|
2019-10-25 15:43:37 +00:00
|
|
|
{:ok, activity, group}
|
2018-12-14 16:41:55 +00:00
|
|
|
else
|
|
|
|
{:existing_group, _} ->
|
2019-09-02 08:50:00 +00:00
|
|
|
{:error, "A group with this name already exists"}
|
2018-12-14 16:41:55 +00:00
|
|
|
|
2019-09-07 17:54:11 +00:00
|
|
|
{:is_owned, nil} ->
|
2020-09-29 07:53:48 +00:00
|
|
|
{:error, "Profile is not owned by authenticated user"}
|
2018-12-14 16:41:55 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|