Introduce event categories

Closes #1056

Signed-off-by: Thomas Citharel <tcit@tcit.fr>
This commit is contained in:
Thomas Citharel
2022-03-28 17:42:59 +02:00
parent 91ed13d5c2
commit f5bdedf789
21 changed files with 371 additions and 77 deletions

View File

@@ -22,12 +22,15 @@ defmodule Mobilizon.GraphQL.Schema do
alias Mobilizon.Events.{Event, Participant}
alias Mobilizon.GraphQL.Middleware.{CurrentActorProvider, ErrorHandler}
alias Mobilizon.GraphQL.Schema
alias Mobilizon.GraphQL.Schema.Custom
alias Mobilizon.Storage.Repo
@pipeline_modifier Custom.EnumTypes
import_types(Absinthe.Type.Custom)
import_types(Absinthe.Plug.Types)
import_types(Schema.Custom.UUID)
import_types(Schema.Custom.Point)
import_types(Custom.UUID)
import_types(Custom.Point)
import_types(Schema.ActivityType)
import_types(Schema.UserType)

View File

@@ -0,0 +1,109 @@
defmodule Mobilizon.GraphQL.Schema.Custom.EnumTypes do
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"
}
]
def pipeline(pipeline) do
Pipeline.insert_after(pipeline, Phase.Schema.TypeImports, __MODULE__)
end
def run(blueprint = %Blueprint{}, _) do
%{schema_definitions: [schema]} = blueprint
new_enum = build_dynamic_enum()
schema =
Map.update!(schema, :type_definitions, fn type_definitions ->
[new_enum | type_definitions]
end)
{:ok, %{blueprint | schema_definitions: [schema]}}
end
def 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} ->
%Schema.EnumValueDefinition{
identifier: id,
value: label,
name: label,
module: __MODULE__,
__reference__: Notation.build_reference(__ENV__)
}
end)
}
end
end

View File

@@ -66,7 +66,7 @@ defmodule Mobilizon.GraphQL.Schema.EventType do
description: "The event's tags"
)
field(:category, :string, description: "The event's category")
field(:category, :event_category, description: "The event's category")
field(:draft, :boolean, description: "Whether or not the event is a draft")
@@ -399,7 +399,11 @@ defmodule Mobilizon.GraphQL.Schema.EventType do
arg(:attributed_to_id, :id, description: "Who the event is attributed to ID (often a group)")
arg(:category, :string, default_value: "meeting", description: "The event's category")
arg(:category, :event_category,
default_value: "MEETING",
description: "The event's category"
)
arg(:physical_address, :address_input, description: "The event's physical address")
arg(:options, :event_options_input, default_value: %{}, description: "The event options")
arg(:metadata, list_of(:event_metadata_input), description: "The event metadata")
@@ -448,7 +452,7 @@ defmodule Mobilizon.GraphQL.Schema.EventType do
arg(:attributed_to_id, :id, description: "Who the event is attributed to ID (often a group)")
arg(:category, :string, description: "The event's category")
arg(:category, :event_category, description: "The event's category")
arg(:physical_address, :address_input, description: "The event's physical address")
arg(:options, :event_options_input, description: "The event options")
arg(:metadata, list_of(:event_metadata_input), description: "The event metadata")

View File

@@ -93,6 +93,7 @@ defmodule Mobilizon.GraphQL.Schema.SearchType do
arg(:tags, :string, description: "A comma-separated string listing the tags")
arg(:location, :string, description: "A geohash for coordinates")
arg(:type, :event_type, description: "Whether the event is online or in person")
arg(:category, :string, description: "The category for the event")
arg(:radius, :float,
default_value: 50,

View File

@@ -54,14 +54,6 @@ defmodule Mobilizon.Events do
:cancelled
])
defenum(EventCategory, :event_category, [
:business,
:conference,
:birthday,
:demonstration,
:meeting
])
defenum(ParticipantRole, :participant_role, [
:not_approved,
:not_confirmed,
@@ -536,6 +528,7 @@ defmodule Mobilizon.Events do
|> events_for_search_query()
|> events_for_begins_on(args)
|> events_for_ends_on(args)
|> events_for_category(args)
|> events_for_tags(args)
|> events_for_location(args)
|> filter_online(args)
@@ -1313,6 +1306,13 @@ defmodule Mobilizon.Events do
end
end
@spec events_for_category(Ecto.Queryable.t(), map()) :: Ecto.Query.t()
defp events_for_category(query, %{category: category}) when is_valid_string(category) do
where(query, [q], q.category == ^category)
end
defp events_for_category(query, _args), do: query
@spec events_for_tags(Ecto.Queryable.t(), map()) :: Ecto.Query.t()
defp events_for_tags(query, %{tags: tags}) when is_valid_string(tags) do
query