Handle unknown categories from global search

Signed-off-by: Thomas Citharel <tcit@tcit.fr>
This commit is contained in:
Thomas Citharel 2022-10-06 09:32:47 +02:00
parent 0265e0220e
commit 0758995ac7
No known key found for this signature in database
GPG Key ID: A061B9DDE0CA0773
3 changed files with 19 additions and 14 deletions

View File

@ -74,7 +74,7 @@ defmodule Mobilizon.Federation.ActivityStream.Converter.Event do
medias: medias,
begins_on: object["startTime"],
ends_on: object["endTime"],
category: get_category(object["category"]),
category: Categories.get_category(object["category"]),
visibility: visibility,
join_options: Map.get(object, "joinMode", "free"),
local: is_local?(object["id"]),
@ -331,15 +331,4 @@ defmodule Mobilizon.Federation.ActivityStream.Converter.Event do
_participant_count
),
do: nil
@spec get_category(String.t() | nil) :: String.t()
defp get_category(nil), do: "MEETING"
defp get_category(category) when is_binary(category) do
if category in Enum.map(Categories.list(), &String.upcase(to_string(&1.id))) do
category
else
get_category(nil)
end
end
end

View File

@ -4,11 +4,27 @@ defmodule Mobilizon.Events.Categories do
"""
import Mobilizon.Web.Gettext
@default "MEETING"
@spec default :: String.t()
def default do
@default
end
@spec list :: [%{id: atom(), label: String.t()}]
def list do
build_in_categories() ++ extra_categories()
end
@spec get_category(String.t() | nil) :: String.t()
def get_category(category) do
if category in Enum.map(list(), &String.upcase(to_string(&1.id))) do
category
else
default()
end
end
defp build_in_categories do
[
%{

View File

@ -5,7 +5,7 @@ defmodule Mobilizon.Service.GlobalSearch.SearchMobilizon do
alias Mobilizon.Actors.Actor
alias Mobilizon.Addresses.Address
alias Mobilizon.Events.Tag
alias Mobilizon.Events.{Categories, Tag}
alias Mobilizon.Service.GlobalSearch.{EventResult, GroupResult, Provider}
alias Mobilizon.Service.HTTP.GenericJSONClient
alias Mobilizon.Storage.Page
@ -187,7 +187,7 @@ defmodule Mobilizon.Service.GlobalSearch.SearchMobilizon do
ends_on: parse_date(data["endTime"]),
url: data["url"],
picture: picture,
category: String.to_existing_atom(String.downcase(data["category"])),
category: String.to_existing_atom(Categories.get_category(data["category"])),
organizer_actor: %Actor{
id: data["creator"]["id"],
name: data["creator"]["displayName"],