Allow to register custom categories

Signed-off-by: Thomas Citharel <tcit@tcit.fr>
This commit is contained in:
Thomas Citharel
2022-03-28 20:02:43 +02:00
parent f5bdedf789
commit 368911b58e
40 changed files with 4587 additions and 725 deletions

View File

@@ -4,6 +4,7 @@ defmodule Mobilizon.GraphQL.Resolvers.Config do
"""
alias Mobilizon.Config
alias Mobilizon.Events.Categories
@doc """
Gets config.
@@ -57,6 +58,17 @@ defmodule Mobilizon.GraphQL.Resolvers.Config do
{:ok, %{body_html: body_html, type: type, url: url}}
end
@spec event_categories(any(), map(), Absinthe.Resolution.t()) :: {:ok, [map()]}
def event_categories(_parent, _args, _resolution) do
categories =
Categories.list()
|> Enum.map(fn %{id: id, label: label} ->
%{id: id |> to_string |> String.upcase(), label: label}
end)
{:ok, categories}
end
@spec config_cache :: map()
defp config_cache do
case Cachex.fetch(:config, "full_config", fn _key ->

View File

@@ -16,6 +16,13 @@ defmodule Mobilizon.GraphQL.Schema.ConfigType do
field(:contact, :string, description: "The instance's contact details")
field(:languages, list_of(:string), description: "The instance's admins languages")
field(:event_categories, list_of(:event_category_option),
description: "The instance list of event categories possibilities"
) do
resolve(&Config.event_categories/3)
end
field(:registrations_open, :boolean, description: "Whether the registrations are opened")
field(:registrations_allowlist, :boolean,
@@ -332,6 +339,14 @@ defmodule Mobilizon.GraphQL.Schema.ConfigType do
)
end
@desc """
Event categories list configuration
"""
object :event_category_option do
field(:id, :string, description: "The ID of the event category")
field(:label, :string, description: "The translated name of the event category")
end
object :config_queries do
@desc "Get the instance config"
field :config, :config do

View File

@@ -1,81 +1,18 @@
defmodule Mobilizon.GraphQL.Schema.Custom.EnumTypes do
@moduledoc """
Register extra enum types dynamically
"""
alias Absinthe.Blueprint.Schema
alias Absinthe.Schema.Notation
alias Absinthe.{Blueprint, Pipeline, Phase}
@categories [
%{
id: :arts,
label: "ARTS"
},
%{
id: :book_clubs,
label: "BOOK_CLUBS"
},
%{
id: :business,
label: "BUSINESS"
},
%{
id: :causes,
label: "CAUSES"
},
%{
id: :comedy,
label: "COMEDY"
},
%{
id: :crafts,
label: "CRAFTS"
},
%{
id: :food_drink,
label: "FOOD_DRINK"
},
%{
id: :health,
label: "HEALTH"
},
%{
id: :music,
label: "MUSIC"
},
%{
id: :auto_boat_air,
label: "AUTO_BOAT_AIR"
},
%{
id: :community,
label: "COMMUNITY"
},
%{
id: :family_education,
label: "FAMILY_EDUCATION"
},
%{
id: :fashion_beauty,
label: "FASHION_BEAUTY"
},
%{
id: :film_media,
label: "FILM_MEDIA"
},
%{
id: :games,
label: "GAMES"
},
# Legacy default value
%{
id: :meeting,
label: "MEETING"
}
]
alias Absinthe.{Blueprint, Phase, Pipeline}
alias Mobilizon.Events.Categories
def pipeline(pipeline) do
Pipeline.insert_after(pipeline, Phase.Schema.TypeImports, __MODULE__)
end
def run(blueprint = %Blueprint{}, _) do
@spec run(Absinthe.Blueprint.t(), any()) :: {:ok, Absinthe.Blueprint.t()}
def run(%Blueprint{} = blueprint, _) do
%{schema_definitions: [schema]} = blueprint
new_enum = build_dynamic_enum()
@@ -88,18 +25,19 @@ defmodule Mobilizon.GraphQL.Schema.Custom.EnumTypes do
{:ok, %{blueprint | schema_definitions: [schema]}}
end
def build_dynamic_enum() do
@spec build_dynamic_enum :: Absinthe.Blueprint.Schema.EnumTypeDefinition.t()
defp build_dynamic_enum do
%Schema.EnumTypeDefinition{
name: "EventCategory",
identifier: :event_category,
module: __MODULE__,
__reference__: Notation.build_reference(__ENV__),
values:
Enum.map(@categories, fn %{id: id, label: label} ->
Enum.map(Categories.list(), fn %{id: id} ->
%Schema.EnumValueDefinition{
identifier: id,
value: label,
name: label,
value: String.upcase(to_string(id)),
name: String.upcase(to_string(id)),
module: __MODULE__,
__reference__: Notation.build_reference(__ENV__)
}