debianize-mobilizon/lib/mobilizon_web/api/groups.ex
Thomas Citharel cc820d6b63
Refactor Core things, including Ecto handling, ActivityPub & Transmogrifier modules
* Data doesn't need anymore to be converted to ActivityStream format to
be saved (this was taken from Pleroma and not at all a good idea here)
* Everything saved when creating an event is inserted into PostgreSQL in
a single transaction
2019-10-31 10:06:11 +01:00

31 lines
936 B
Elixir

defmodule MobilizonWeb.API.Groups do
@moduledoc """
API for Groups.
"""
alias Mobilizon.Actors
alias Mobilizon.Actors.Actor
alias Mobilizon.Service.ActivityPub
alias Mobilizon.Service.ActivityPub.Activity
@doc """
Create a group
"""
@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) do
{:ok, activity, group}
else
{:existing_group, _} ->
{:error, "A group with this name already exists"}
{:is_owned, nil} ->
{:error, "Actor id is not owned by authenticated user"}
end
end
end