Make Categories a predefined list

Signed-off-by: Thomas Citharel <tcit@tcit.fr>

Allow null values for categories for now

Signed-off-by: Thomas Citharel <tcit@tcit.fr>
This commit is contained in:
Thomas Citharel
2019-02-22 16:54:01 +01:00
parent 75554cd3f5
commit 7086fe8389
27 changed files with 89 additions and 655 deletions

View File

@@ -1,48 +0,0 @@
defmodule MobilizonWeb.Resolvers.Category do
@moduledoc """
Handles the category-related GraphQL calls
"""
require Logger
alias Mobilizon.Actors.User
###
# TODO : Refactor this into MobilizonWeb.API.Categories when a standard AS category is defined
###
def list_categories(_parent, %{page: page, limit: limit}, _resolution) do
categories =
Mobilizon.Events.list_categories(page, limit)
|> Enum.map(fn category ->
urls = MobilizonWeb.Uploaders.Category.urls({category.picture, category})
Map.put(category, :picture, %{url: urls.original, url_thumbnail: urls.thumb})
end)
{:ok, categories}
end
def create_category(_parent, %{title: title, picture: picture, description: description}, %{
context: %{current_user: %User{} = _user}
}) do
with {:ok, category} <-
Mobilizon.Events.create_category(%{
title: title,
description: description,
picture: picture
}),
urls <- MobilizonWeb.Uploaders.Category.urls({category.picture, category}) do
Logger.info("Created category " <> title)
{:ok, Map.put(category, :picture, %{url: urls.original, url_thumbnail: urls.thumb})}
else
{:error, %Ecto.Changeset{errors: errors} = _changeset} ->
# This is pretty ridiculous for changeset to error
errors =
Enum.into(errors, %{})
|> Enum.map(fn {key, {value, _}} -> Atom.to_string(key) <> ": " <> value end)
{:error, errors}
end
end
def create_category(_parent, _args, %{}) do
{:error, "You are not allowed to create a category if not connected"}
end
end

View File

@@ -130,7 +130,6 @@ defmodule MobilizonWeb.Schema do
import_fields(:group_queries)
import_fields(:event_queries)
import_fields(:participant_queries)
import_fields(:category_queries)
import_fields(:tag_queries)
end
@@ -142,7 +141,6 @@ defmodule MobilizonWeb.Schema do
import_fields(:person_mutations)
import_fields(:group_mutations)
import_fields(:event_mutations)
import_fields(:category_mutations)
import_fields(:comment_mutations)
import_fields(:participant_mutations)

View File

@@ -7,7 +7,6 @@ defmodule MobilizonWeb.Schema.EventType do
import Absinthe.Resolution.Helpers, only: [dataloader: 1]
import_types(MobilizonWeb.Schema.AddressType)
import_types(MobilizonWeb.Schema.Events.ParticipantType)
import_types(MobilizonWeb.Schema.Events.CategoryType)
import_types(MobilizonWeb.Schema.TagType)
alias MobilizonWeb.Resolvers
@@ -44,7 +43,7 @@ defmodule MobilizonWeb.Schema.EventType do
description: "The event's tags"
)
field(:category, :category, description: "The event's category")
field(:category, :string, description: "The event's category")
field(:participants, list_of(:participant),
resolve: &MobilizonWeb.Resolvers.Event.list_participants_for_event/3,

View File

@@ -1,34 +0,0 @@
defmodule MobilizonWeb.Schema.Events.CategoryType do
@moduledoc """
Schema representation for Category
"""
use Absinthe.Schema.Notation
alias MobilizonWeb.Resolvers
@desc "A category"
object :category do
field(:id, :id, description: "The category's ID")
field(:description, :string, description: "The category's description")
field(:picture, :picture, description: "The category's picture")
field(:title, :string, description: "The category's title")
end
object :category_queries do
@desc "Get the list of categories"
field :categories, non_null(list_of(:category)) do
arg(:page, :integer, default_value: 1)
arg(:limit, :integer, default_value: 10)
resolve(&Resolvers.Category.list_categories/3)
end
end
object :category_mutations do
@desc "Create a category with a title, description and picture"
field :create_category, type: :category do
arg(:title, non_null(:string))
arg(:description, non_null(:string))
arg(:picture, non_null(:upload))
resolve(&Resolvers.Category.create_category/3)
end
end
end

View File

@@ -30,10 +30,9 @@ defmodule MobilizonWeb.Uploaders.Category do
"#{title}_#{version}"
end
# TODO : When we're sure creating a category is secured and made possible only for admins, use category name
# Override the storage directory:
def storage_dir(_, _) do
"uploads/categories/"
"uploads/event/"
end
# Provide a default URL if there hasn't been a file uploaded

View File

@@ -1,6 +1,5 @@
defmodule MobilizonWeb.ActivityPub.ObjectView do
use MobilizonWeb, :view
alias MobilizonWeb.ActivityPub.ObjectView
alias Mobilizon.Service.ActivityPub.Utils
def render("event.json", %{event: event}) do
@@ -9,7 +8,7 @@ defmodule MobilizonWeb.ActivityPub.ObjectView do
"actor" => event["actor"],
"id" => event["id"],
"name" => event["title"],
"category" => render_one(event["category"], ObjectView, "category.json", as: :category),
"category" => event["category"],
"content" => event["summary"],
"mediaType" => "text/html"
# "published" => Timex.format!(event.inserted_at, "{ISO:Extended}"),
@@ -35,15 +34,4 @@ defmodule MobilizonWeb.ActivityPub.ObjectView do
Map.merge(comment, Utils.make_json_ld_header())
end
def render("category.json", %{category: category}) when not is_nil(category) do
%{
"identifier" => category.id,
"name" => category.title
}
end
def render("category.json", %{category: _category}) do
nil
end
end