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__)
}

View File

@@ -0,0 +1,144 @@
defmodule Mobilizon.Events.Categories do
@moduledoc """
Module that handles event categories
"""
import Mobilizon.Web.Gettext
@spec list :: [%{id: atom(), label: String.t()}]
def list do
build_in_categories() ++ extra_categories()
end
defp build_in_categories do
[
%{
id: :arts,
label: gettext("Arts")
},
%{
id: :book_clubs,
label: gettext("Book clubs")
},
%{
id: :business,
label: gettext("Business")
},
%{
id: :causes,
label: gettext("Causes")
},
%{
id: :comedy,
label: gettext("Comedy")
},
%{
id: :crafts,
label: gettext("Crafts")
},
%{
id: :food_drink,
label: gettext("Food & Drink")
},
%{
id: :health,
label: gettext("Health")
},
%{
id: :music,
label: gettext("Music")
},
%{
id: :auto_boat_air,
label: gettext("Auto, boat and air")
},
%{
id: :community,
label: gettext("Community")
},
%{
id: :family_education,
label: gettext("Family & Education")
},
%{
id: :fashion_beauty,
label: gettext("Fashion & Beauty")
},
%{
id: :film_media,
label: gettext("Film & Media")
},
%{
id: :games,
label: gettext("Games")
},
%{
id: :language_culture,
label: gettext("Language & Culture")
},
%{
id: :learning,
label: gettext("Learning")
},
%{
id: :lgbtq,
label: gettext("LGBTQ")
},
%{
id: :movements_politics,
label: gettext("Movements and politics")
},
%{
id: :networking,
label: gettext("Networking")
},
%{
id: :party,
label: gettext("Party")
},
%{
id: :performing_visual_arts,
label: gettext("Performing & Visual Arts")
},
%{
id: :pets,
label: gettext("Pets")
},
%{
id: :photography,
label: gettext("Photography")
},
%{
id: :outdoors_adventure,
label: gettext("Outdoors & Adventure")
},
%{
id: :spirituality_religion_beliefs,
label: gettext("Spirituality, Religion & Beliefs")
},
%{
id: :science_tech,
label: gettext("Science & Tech")
},
%{
id: :sports,
label: gettext("Sports")
},
%{
id: :theatre,
label: gettext("Theatre")
},
# Legacy default value
%{
id: :meeting,
label: gettext("Meeting")
}
]
end
@spec extra_categories :: [%{id: atom(), label: String.t()}]
defp extra_categories do
:mobilizon
|> Application.get_env(:instance)
|> Keyword.get(:extra_categories, [])
end
end