mobilizon/lib/mobilizon_web/api/groups.ex

55 lines
1.5 KiB
Elixir
Raw Normal View History

defmodule MobilizonWeb.API.Groups do
@moduledoc """
API for Events
"""
alias Mobilizon.Actors
2019-09-02 10:50:00 +02:00
alias Mobilizon.Users.User
alias Mobilizon.Service.ActivityPub
alias Mobilizon.Service.ActivityPub.Utils, as: ActivityPubUtils
alias MobilizonWeb.API.Utils
@doc """
Create a group
"""
2019-09-02 10:50:00 +02:00
@spec create_group(User.t(), map()) :: {:ok, Activity.t(), Group.t()} | any()
def create_group(
2019-09-02 10:50:00 +02:00
user,
%{
preferred_username: title,
2019-09-02 10:50:00 +02:00
summary: summary,
creator_actor_id: creator_actor_id,
avatar: _avatar,
banner: _banner
} = args
) do
2019-09-02 10:50:00 +02:00
with {:is_owned, true, actor} <- User.owns_actor(user, creator_actor_id),
title <- String.trim(title),
{:existing_group, nil} <- {:existing_group, Actors.get_group_by_title(title)},
visibility <- Map.get(args, :visibility, :public),
{content_html, tags, to, cc} <-
2019-09-02 10:50:00 +02:00
Utils.prepare_content(actor, summary, visibility, [], nil),
group <-
ActivityPubUtils.make_group_data(
2019-09-02 10:50:00 +02:00
actor.url,
to,
title,
content_html,
tags,
cc
) do
ActivityPub.create(%{
to: ["https://www.w3.org/ns/activitystreams#Public"],
actor: actor,
object: group,
local: true
})
else
{:existing_group, _} ->
2019-09-02 10:50:00 +02:00
{:error, "A group with this name already exists"}
2019-09-02 10:50:00 +02:00
{:is_owned, _} ->
{:error, "Actor id is not owned by authenticated user"}
end
end
end