Increase the number of related events to 4 and refactor function

Signed-off-by: Thomas Citharel <tcit@tcit.fr>
This commit is contained in:
Thomas Citharel 2021-10-29 10:38:03 +02:00
parent 2a9098975d
commit 88b078ebce
No known key found for this signature in database
GPG Key ID: A061B9DDE0CA0773
1 changed files with 42 additions and 28 deletions

View File

@ -21,7 +21,7 @@ defmodule Mobilizon.GraphQL.Resolvers.Event do
# We limit the max number of events that can be retrieved # We limit the max number of events that can be retrieved
@event_max_limit 100 @event_max_limit 100
@number_of_related_events 3 @number_of_related_events 4
@spec organizer_for_event(Event.t(), map(), Absinthe.Resolution.t()) :: @spec organizer_for_event(Event.t(), map(), Absinthe.Resolution.t()) ::
{:ok, Actor.t() | nil} | {:error, String.t()} {:ok, Actor.t() | nil} | {:error, String.t()}
@ -213,38 +213,19 @@ defmodule Mobilizon.GraphQL.Resolvers.Event do
""" """
@spec list_related_events(Event.t(), map(), Absinthe.Resolution.t()) :: {:ok, list(Event.t())} @spec list_related_events(Event.t(), map(), Absinthe.Resolution.t()) :: {:ok, list(Event.t())}
def list_related_events( def list_related_events(
%Event{tags: tags, organizer_actor: organizer_actor, uuid: uuid}, %Event{uuid: uuid} = event,
_args, _args,
_resolution _resolution
) do ) do
# We get the organizer's next public event # We get the organizer's next public event
events = events =
[Events.get_upcoming_public_event_for_actor(organizer_actor, uuid)] event
|> Enum.filter(&is_map/1) |> organizer_next_public_event()
# We find similar events with the same tags
# We find similar events with the same tags |> similar_events_common_tags(event)
# uniq_by : It's possible event_from_same_actor is inside events_from_tags # TODO: We should use tag_relations to find more appropriate events
events = # We've considered all recommended events, so we fetch the latest events
events |> add_latest_events()
|> Enum.concat(Events.list_events_by_tags(tags, @number_of_related_events))
|> uniq_events()
# TODO: We should use tag_relations to find more appropriate events
# We've considered all recommended events, so we fetch the latest events
events =
if @number_of_related_events - length(events) > 0 do
events
|> Enum.concat(
Events.list_events(1, @number_of_related_events, :begins_on, :asc, true).elements
)
|> uniq_events()
else
events
end
events =
events
# We remove the same event from the results # We remove the same event from the results
|> Enum.filter(fn event -> event.uuid != uuid end) |> Enum.filter(fn event -> event.uuid != uuid end)
# We return only @number_of_related_events right now # We return only @number_of_related_events right now
@ -253,6 +234,39 @@ defmodule Mobilizon.GraphQL.Resolvers.Event do
{:ok, events} {:ok, events}
end end
@spec organizer_next_public_event(Event.t()) :: list(Event.t())
defp organizer_next_public_event(%Event{attributed_to: %Actor{} = group, uuid: uuid}) do
[Events.get_upcoming_public_event_for_actor(group, uuid)]
|> Enum.filter(&is_map/1)
end
defp organizer_next_public_event(%Event{organizer_actor: %Actor{} = profile, uuid: uuid}) do
[Events.get_upcoming_public_event_for_actor(profile, uuid)]
|> Enum.filter(&is_map/1)
end
@spec similar_events_common_tags(list(Event.t()), Event.t()) :: list(Event.t())
defp similar_events_common_tags(events, %Event{tags: tags, uuid: uuid}) do
events
|> Enum.concat(Events.list_events_by_tags(tags, @number_of_related_events))
|> Enum.filter(fn event -> event.uuid != uuid end)
# uniq_by : It's possible event_from_same_actor is inside events_from_tags
|> uniq_events()
end
@spec add_latest_events(list(Event.t())) :: list(Event.t())
defp add_latest_events(events) do
if @number_of_related_events - length(events) > 0 do
events
|> Enum.concat(
Events.list_events(1, @number_of_related_events + 1, :begins_on, :asc, true).elements
)
|> uniq_events()
else
events
end
end
@spec uniq_events(list(Event.t())) :: list(Event.t()) @spec uniq_events(list(Event.t())) :: list(Event.t())
defp uniq_events(events), do: Enum.uniq_by(events, fn event -> event.uuid end) defp uniq_events(events), do: Enum.uniq_by(events, fn event -> event.uuid end)