2018-12-14 16:41:55 +00:00
|
|
|
defmodule MobilizonWeb.API.Groups do
|
|
|
|
@moduledoc """
|
|
|
|
API for Events
|
|
|
|
"""
|
|
|
|
alias Mobilizon.Actors
|
2019-09-07 17:54:11 +00:00
|
|
|
alias Mobilizon.Actors.Actor
|
2019-09-02 08:50:00 +00:00
|
|
|
alias Mobilizon.Users.User
|
2018-12-14 16:41:55 +00:00
|
|
|
alias Mobilizon.Service.ActivityPub
|
|
|
|
alias Mobilizon.Service.ActivityPub.Utils, as: ActivityPubUtils
|
2019-07-26 09:30:28 +00:00
|
|
|
alias MobilizonWeb.API.Utils
|
2018-12-14 16:41:55 +00:00
|
|
|
|
2019-03-19 10:16:03 +00:00
|
|
|
@doc """
|
|
|
|
Create a group
|
|
|
|
"""
|
2019-09-02 08:50:00 +00:00
|
|
|
@spec create_group(User.t(), map()) :: {:ok, Activity.t(), Group.t()} | any()
|
2018-12-14 16:41:55 +00:00
|
|
|
def create_group(
|
2019-09-02 08:50:00 +00:00
|
|
|
user,
|
2018-12-14 16:41:55 +00:00
|
|
|
%{
|
|
|
|
preferred_username: title,
|
2019-09-02 08:50:00 +00:00
|
|
|
summary: summary,
|
|
|
|
creator_actor_id: creator_actor_id,
|
2019-09-04 16:24:31 +00:00
|
|
|
avatar: _avatar,
|
|
|
|
banner: _banner
|
2018-12-14 16:41:55 +00:00
|
|
|
} = args
|
|
|
|
) do
|
2019-09-07 17:54:11 +00:00
|
|
|
with {:is_owned, %Actor{} = actor} <- User.owns_actor(user, creator_actor_id),
|
2018-12-14 16:41:55 +00:00
|
|
|
title <- String.trim(title),
|
2019-09-04 16:24:31 +00:00
|
|
|
{:existing_group, nil} <- {:existing_group, Actors.get_group_by_title(title)},
|
2019-07-26 09:30:28 +00:00
|
|
|
visibility <- Map.get(args, :visibility, :public),
|
|
|
|
{content_html, tags, to, cc} <-
|
2019-09-02 08:50:00 +00:00
|
|
|
Utils.prepare_content(actor, summary, visibility, [], nil),
|
2019-09-07 17:54:11 +00:00
|
|
|
group <- ActivityPubUtils.make_group_data(actor.url, to, title, content_html, tags, cc) do
|
2018-12-14 16:41:55 +00:00
|
|
|
ActivityPub.create(%{
|
|
|
|
to: ["https://www.w3.org/ns/activitystreams#Public"],
|
|
|
|
actor: actor,
|
|
|
|
object: group,
|
|
|
|
local: true
|
|
|
|
})
|
|
|
|
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} ->
|
2019-09-02 08:50:00 +00:00
|
|
|
{:error, "Actor id is not owned by authenticated user"}
|
2018-12-14 16:41:55 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|