2018-12-14 17:41:55 +01:00
|
|
|
defmodule MobilizonWeb.API.Groups do
|
|
|
|
@moduledoc """
|
|
|
|
API for Events
|
|
|
|
"""
|
|
|
|
alias Mobilizon.Actors
|
2019-09-07 19:54:11 +02:00
|
|
|
alias Mobilizon.Actors.Actor
|
2019-09-02 10:50:00 +02:00
|
|
|
alias Mobilizon.Users.User
|
2018-12-14 17:41:55 +01:00
|
|
|
alias Mobilizon.Service.ActivityPub
|
|
|
|
alias Mobilizon.Service.ActivityPub.Utils, as: ActivityPubUtils
|
2019-07-26 11:30:28 +02:00
|
|
|
alias MobilizonWeb.API.Utils
|
2018-12-14 17:41:55 +01:00
|
|
|
|
2019-03-19 11:16:03 +01:00
|
|
|
@doc """
|
|
|
|
Create a group
|
|
|
|
"""
|
2019-09-02 10:50:00 +02:00
|
|
|
@spec create_group(User.t(), map()) :: {:ok, Activity.t(), Group.t()} | any()
|
2018-12-14 17:41:55 +01:00
|
|
|
def create_group(
|
2019-09-02 10:50:00 +02:00
|
|
|
user,
|
2018-12-14 17:41:55 +01:00
|
|
|
%{
|
|
|
|
preferred_username: title,
|
2019-09-02 10:50:00 +02:00
|
|
|
summary: summary,
|
|
|
|
creator_actor_id: creator_actor_id,
|
2019-09-04 18:24:31 +02:00
|
|
|
avatar: _avatar,
|
|
|
|
banner: _banner
|
2018-12-14 17:41:55 +01:00
|
|
|
} = args
|
|
|
|
) do
|
2019-09-07 19:54:11 +02:00
|
|
|
with {:is_owned, %Actor{} = actor} <- User.owns_actor(user, creator_actor_id),
|
2018-12-14 17:41:55 +01:00
|
|
|
title <- String.trim(title),
|
2019-09-04 18:24:31 +02:00
|
|
|
{:existing_group, nil} <- {:existing_group, Actors.get_group_by_title(title)},
|
2019-07-26 11:30:28 +02:00
|
|
|
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),
|
2019-09-07 19:54:11 +02:00
|
|
|
group <- ActivityPubUtils.make_group_data(actor.url, to, title, content_html, tags, cc) do
|
2018-12-14 17:41:55 +01:00
|
|
|
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"}
|
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
|