Add participant info in event search results

Signed-off-by: Thomas Citharel <tcit@tcit.fr>
This commit is contained in:
Thomas Citharel 2022-09-26 10:29:20 +02:00
parent a37bab3b84
commit 6f7d5f649b
No known key found for this signature in database
GPG Key ID: A061B9DDE0CA0773
9 changed files with 145 additions and 29 deletions

View File

@ -26,13 +26,13 @@
variant="info" variant="info"
v-if="event.status === EventStatus.TENTATIVE" v-if="event.status === EventStatus.TENTATIVE"
> >
{{ $t("Tentative") }} {{ t("Tentative") }}
</mobilizon-tag> </mobilizon-tag>
<mobilizon-tag <mobilizon-tag
variant="danger" variant="danger"
v-if="event.status === EventStatus.CANCELLED" v-if="event.status === EventStatus.CANCELLED"
> >
{{ $t("Cancelled") }} {{ t("Cancelled") }}
</mobilizon-tag> </mobilizon-tag>
<router-link <router-link
:to="{ name: RouteName.TAG, params: { tag: tag.title } }" :to="{ name: RouteName.TAG, params: { tag: tag.title } }"
@ -100,24 +100,40 @@
v-else-if="event.options && event.options.isOnline" v-else-if="event.options && event.options.isOnline"
> >
<Video /> <Video />
<span class="ltr:pl-2 rtl:pr-2">{{ $t("Online") }}</span> <span class="ltr:pl-2 rtl:pr-2">{{ t("Online") }}</span>
</div> </div>
<div <div
class="mt-1 no-underline gap-1 items-center hidden" class="mt-1 no-underline gap-1 items-center hidden"
:class="{ 'sm:flex': mode === 'row' }" :class="{ 'sm:flex': mode === 'row' }"
v-if="event.tags || event.status !== EventStatus.CONFIRMED" v-if="
event.tags ||
event.status !== EventStatus.CONFIRMED ||
event.participantStats?.participant > 0
"
> >
<mobilizon-tag
variant="info"
v-if="event.participantStats?.participant > 0"
>
{{
t(
"{count} participants",
event.participantStats?.participant,
{ count: event.participantStats?.participant }
)
}}
</mobilizon-tag>
<mobilizon-tag <mobilizon-tag
variant="info" variant="info"
v-if="event.status === EventStatus.TENTATIVE" v-if="event.status === EventStatus.TENTATIVE"
> >
{{ $t("Tentative") }} {{ t("Tentative") }}
</mobilizon-tag> </mobilizon-tag>
<mobilizon-tag <mobilizon-tag
variant="danger" variant="danger"
v-if="event.status === EventStatus.CANCELLED" v-if="event.status === EventStatus.CANCELLED"
> >
{{ $t("Cancelled") }} {{ t("Cancelled") }}
</mobilizon-tag> </mobilizon-tag>
<router-link <router-link
:to="{ name: RouteName.TAG, params: { tag: tag.title } }" :to="{ name: RouteName.TAG, params: { tag: tag.title } }"
@ -156,6 +172,9 @@ import Video from "vue-material-design-icons/Video.vue";
import { formatDateTimeForEvent } from "@/utils/datetime"; import { formatDateTimeForEvent } from "@/utils/datetime";
import type { Locale } from "date-fns"; import type { Locale } from "date-fns";
import LinkOrRouterLink from "../core/LinkOrRouterLink.vue"; import LinkOrRouterLink from "../core/LinkOrRouterLink.vue";
import { useI18n } from "vue-i18n";
const { t } = useI18n({ useScope: "global" });
const props = withDefaults( const props = withDefaults(
defineProps<{ defineProps<{

View File

@ -1,6 +1,6 @@
<template> <template>
<span <span
class="rounded-md my-1 truncate text-sm text-violet-title px-2 py-1" class="rounded-md truncate text-sm text-violet-title px-2 py-1"
:class="[ :class="[
typeClasses, typeClasses,
capitalize, capitalize,

View File

@ -38,6 +38,9 @@ export const SEARCH_EVENTS_AND_GROUPS = gql`
$eventPage: Int $eventPage: Int
$groupPage: Int $groupPage: Int
$limit: Int $limit: Int
$sortByEvents: SearchEventSortOptions
$sortByGroups: SearchGroupSortOptions
$boostLanguages: [String]
) { ) {
searchEvents( searchEvents(
location: $location location: $location
@ -55,6 +58,8 @@ export const SEARCH_EVENTS_AND_GROUPS = gql`
zoom: $zoom zoom: $zoom
page: $eventPage page: $eventPage
limit: $limit limit: $limit
sortBy: $sortByEvents
boostLanguages: $boostLanguages
) { ) {
total total
elements { elements {
@ -80,6 +85,9 @@ export const SEARCH_EVENTS_AND_GROUPS = gql`
attributedTo { attributedTo {
...ActorFragment ...ActorFragment
} }
participantStats {
participant
}
options { options {
isOnline isOnline
} }
@ -96,6 +104,8 @@ export const SEARCH_EVENTS_AND_GROUPS = gql`
zoom: $zoom zoom: $zoom
page: $groupPage page: $groupPage
limit: $limit limit: $limit
sortBy: $sortByGroups
boostLanguages: $boostLanguages
) { ) {
total total
elements { elements {

View File

@ -2,7 +2,7 @@
<div class="max-w-4xl mx-auto"> <div class="max-w-4xl mx-auto">
<SearchFields <SearchFields
class="md:ml-10 mr-2" class="md:ml-10 mr-2"
v-model:search="searchDebounced" v-model:search="search"
v-model:location="location" v-model:location="location"
:locationDefaultText="locationName" :locationDefaultText="locationName"
/> />
@ -736,13 +736,26 @@ enum ViewMode {
MAP = "map", MAP = "map",
} }
enum EventSortValues {
MATCH_DESC = "MATCH_DESC",
START_TIME_DESC = "START_TIME_DESC",
CREATED_AT_DESC = "CREATED_AT_DESC",
CREATED_AT_ASC = "CREATED_AT_ASC",
PARTICIPANT_COUNT_DESC = "PARTICIPANT_COUNT_DESC",
}
enum GroupSortValues {
MATCH_DESC = "MATCH_DESC",
MEMBER_COUNT_DESC = "MEMBER_COUNT_DESC",
}
enum SortValues { enum SortValues {
MATCH_DESC = "-match", MATCH_DESC = "MATCH_DESC",
START_TIME_DESC = "-startTime", START_TIME_DESC = "START_TIME_DESC",
CREATED_AT_DESC = "-createdAt", CREATED_AT_DESC = "CREATED_AT_DESC",
CREATED_AT_ASC = "createdAt", CREATED_AT_ASC = "CREATED_AT_ASC",
PARTICIPANT_COUNT_DESC = "-participantCount", PARTICIPANT_COUNT_DESC = "PARTICIPANT_COUNT_DESC",
MEMBER_COUNT_DESC = "-memberCount", MEMBER_COUNT_DESC = "MEMBER_COUNT_DESC",
} }
const arrayTransformer: RouteQueryTransformer<string[]> = { const arrayTransformer: RouteQueryTransformer<string[]> = {
@ -1121,6 +1134,27 @@ watch(isOnline, (newIsOnline) => {
} }
}); });
const sortByForType = (
value: SortValues,
allowed: typeof EventSortValues | typeof GroupSortValues
): SortValues | undefined => {
return Object.values(allowed).includes(value) ? value : undefined;
};
const boostLanguagesQuery = computed((): string[] => {
const languages = new Set<string>();
for (const completeLanguage of navigator.languages) {
const language = completeLanguage.split("-")[0];
if (Object.keys(langs).find((langKey) => langKey === language)) {
languages.add(language);
}
}
return Array.from(languages);
});
const { result: searchElementsResult, loading: searchLoading } = useQuery<{ const { result: searchElementsResult, loading: searchLoading } = useQuery<{
searchEvents: Paginate<TypeNamed<IEvent>>; searchEvents: Paginate<TypeNamed<IEvent>>;
searchGroups: Paginate<TypeNamed<IGroup>>; searchGroups: Paginate<TypeNamed<IGroup>>;
@ -1139,7 +1173,10 @@ const { result: searchElementsResult, loading: searchLoading } = useQuery<{
statusOneOf: statusOneOf.value, statusOneOf: statusOneOf.value,
languageOneOf: languageOneOf.value, languageOneOf: languageOneOf.value,
searchTarget: searchTarget.value, searchTarget: searchTarget.value,
bbox: bbox.value, bbox: mode.value === ViewMode.MAP ? bbox.value : undefined,
zoom: zoom.value, zoom: zoom.value,
sortByEvents: sortByForType(sortBy.value, EventSortValues),
sortByGroups: sortByForType(sortBy.value, GroupSortValues),
boostLanguages: boostLanguagesQuery.value,
})); }));
</script> </script>

View File

@ -183,7 +183,7 @@ defmodule Mobilizon.GraphQL.Resolvers.Event do
stats.participant + stats.moderator + stats.administrator + stats.creator stats.participant + stats.moderator + stats.administrator + stats.creator
)} )}
else else
{:ok, %{participant: stats.participant}} {:ok, %EventParticipantStats{participant: stats.participant}}
end end
end end

View File

@ -24,6 +24,7 @@ defmodule Mobilizon.GraphQL.Schema.SearchType do
field(:tags, list_of(:tag), description: "The event's tags") field(:tags, list_of(:tag), description: "The event's tags")
field(:category, :event_category, description: "The event's category") field(:category, :event_category, description: "The event's category")
field(:options, :event_options, description: "The event options") field(:options, :event_options, description: "The event options")
field(:participant_stats, :participant_stats, description: "Statistics on the event's participants")
resolve_type(fn resolve_type(fn
%Event{}, _ -> %Event{}, _ ->
@ -54,6 +55,7 @@ defmodule Mobilizon.GraphQL.Schema.SearchType do
field(:tags, list_of(:tag), description: "The event's tags") field(:tags, list_of(:tag), description: "The event's tags")
field(:category, :event_category, description: "The event's category") field(:category, :event_category, description: "The event's category")
field(:options, :event_options, description: "The event options") field(:options, :event_options, description: "The event options")
field(:participant_stats, :participant_stats, description: "Statistics on the event's participants")
end end
interface :group_search_result do interface :group_search_result do
@ -152,6 +154,19 @@ defmodule Mobilizon.GraphQL.Schema.SearchType do
value(:global, description: "Search using the global fediverse search") value(:global, description: "Search using the global fediverse search")
end end
enum :search_group_sort_options do
value(:match_desc, description: "The pertinence of the result")
value(:member_count_desc, description: "The members count of the group")
end
enum :search_event_sort_options do
value(:match_desc, description: "The pertinence of the result")
value(:start_time_desc, description: "The start date of the result")
value(:created_at_desc, description: "When the event was published")
value(:created_at_asc, description: "When the event was published")
value(:participant_count_desc, description: "With the most participants")
end
object :search_queries do object :search_queries do
@desc "Search persons" @desc "Search persons"
field :search_persons, :persons do field :search_persons, :persons do
@ -183,6 +198,7 @@ defmodule Mobilizon.GraphQL.Schema.SearchType do
arg(:language_one_of, list_of(:string), arg(:language_one_of, list_of(:string),
description: "The list of languages this event can be in" description: "The list of languages this event can be in"
) )
arg(:boost_languages, list_of(:string), description: "The user's languages that can benefit from a boost in search results")
arg(:search_target, :search_target, arg(:search_target, :search_target,
default_value: :internal, default_value: :internal,
@ -195,6 +211,11 @@ defmodule Mobilizon.GraphQL.Schema.SearchType do
arg(:page, :integer, default_value: 1, description: "Result page") arg(:page, :integer, default_value: 1, description: "Result page")
arg(:limit, :integer, default_value: 10, description: "Results limit per page") arg(:limit, :integer, default_value: 10, description: "Results limit per page")
arg(:sort_by, :search_group_sort_options,
default_value: :match_desc,
description: "How to sort search results"
)
resolve(&Search.search_groups/3) resolve(&Search.search_groups/3)
end end
@ -217,6 +238,7 @@ defmodule Mobilizon.GraphQL.Schema.SearchType do
arg(:language_one_of, list_of(:string), arg(:language_one_of, list_of(:string),
description: "The list of languages this event can be in" description: "The list of languages this event can be in"
) )
arg(:boost_languages, list_of(:string), description: "The user's languages that can benefit from a boost in search results")
arg(:search_target, :search_target, arg(:search_target, :search_target,
default_value: :internal, default_value: :internal,
@ -236,6 +258,11 @@ defmodule Mobilizon.GraphQL.Schema.SearchType do
arg(:begins_on, :datetime, description: "Filter events by their start date") arg(:begins_on, :datetime, description: "Filter events by their start date")
arg(:ends_on, :datetime, description: "Filter events by their end date") arg(:ends_on, :datetime, description: "Filter events by their end date")
arg(:sort_by, :search_event_sort_options,
default_value: :match_desc,
description: "How to sort search results"
)
resolve(&Search.search_events/3) resolve(&Search.search_events/3)
end end

View File

@ -541,7 +541,7 @@ defmodule Mobilizon.Events do
|> filter_draft() |> filter_draft()
|> filter_local_or_from_followed_instances_events() |> filter_local_or_from_followed_instances_events()
|> filter_public_visibility() |> filter_public_visibility()
|> event_order_begins_on_asc() |> event_order(args.sort_by)
|> Page.build_page(page, limit, :begins_on) |> Page.build_page(page, limit, :begins_on)
end end
@ -1272,15 +1272,14 @@ defmodule Mobilizon.Events do
end end
end end
@spec events_for_search_query(String.t()) :: Ecto.Query.t() # @spec events_for_search_query(String.t()) :: Ecto.Query.t()
defp events_for_search_query("") do # defp events_for_search_query("") do
Event # Event
|> distinct([e], asc: e.begins_on, asc: e.id) # |> join: rank in fragment("")
end # end
defp events_for_search_query(search_string) do defp events_for_search_query(search_string) do
from(event in Event, from(event in Event,
distinct: [asc: event.begins_on, asc: event.id],
join: id_and_rank in matching_event_ids_and_ranks(search_string), join: id_and_rank in matching_event_ids_and_ranks(search_string),
on: id_and_rank.id == event.id on: id_and_rank.id == event.id
) )
@ -1820,6 +1819,13 @@ defmodule Mobilizon.Events do
|> event_order_begins_on_asc() |> event_order_begins_on_asc()
end end
defp event_order(query, :match_desc), do: order_by(query, [e, f], desc: f.rank, asc: e.begins_on)
defp event_order(query, :start_time_desc), do: order_by(query, [e], asc: e.begins_on)
defp event_order(query, :created_at_desc), do: order_by(query, [e], desc: e.publish_at)
defp event_order(query, :created_at_asc), do: order_by(query, [e], asc: e.publish_at)
defp event_order(query, :participant_count_desc), do: order_by(query, [e], fragment("participant_stats->>'participant' DESC"))
defp event_order(query, _), do: query
defp event_order_begins_on_asc(query), defp event_order_begins_on_asc(query),
do: order_by(query, [e], asc: e.begins_on) do: order_by(query, [e], asc: e.begins_on)

View File

@ -13,7 +13,7 @@ defmodule Mobilizon.Service.GlobalSearch.EventResult do
:category, :category,
:tags, :tags,
:organizer_actor, :organizer_actor,
:participants, :participant_stats,
:physical_address :physical_address
] ]
end end

View File

@ -15,6 +15,15 @@ defmodule Mobilizon.Service.GlobalSearch.SearchMobilizon do
@search_events_api "/api/v1/search/events" @search_events_api "/api/v1/search/events"
@search_groups_api "/api/v1/search/groups" @search_groups_api "/api/v1/search/groups"
@sort_by_options %{
match_desc: "-match",
start_time_desc: "-startTime",
created_at_desc: "-createdAt",
created_at_asc: "createdAt",
participant_count_desc: "-participantCount",
member_count_desc: "-memberCount"
}
@behaviour Provider @behaviour Provider
@impl Provider @impl Provider
@ -39,9 +48,11 @@ defmodule Mobilizon.Service.GlobalSearch.SearchMobilizon do
end), end),
distance: if(options[:radius], do: "#{options[:radius]}_km", else: nil), distance: if(options[:radius], do: "#{options[:radius]}_km", else: nil),
count: options[:limit], count: options[:limit],
start: (options[:page] - 1) * options[:limit], start: (Keyword.get(options, :page, 1) - 1) * Keyword.get(options, :limit, 16),
latlon: to_lat_lon(options[:location]), latlon: to_lat_lon(options[:location]),
bbox: options[:bbox] bbox: options[:bbox],
sortBy: Map.get(@sort_by_options, options[:sort_by]),
boostLanguages: options[:boost_languages]
) )
|> Keyword.take([ |> Keyword.take([
:search, :search,
@ -56,7 +67,8 @@ defmodule Mobilizon.Service.GlobalSearch.SearchMobilizon do
:statusOneOf, :statusOneOf,
:bbox, :bbox,
:start, :start,
:count :count,
:sortBy
]) ])
|> Keyword.reject(fn {_key, val} -> is_nil(val) end) |> Keyword.reject(fn {_key, val} -> is_nil(val) end)
@ -85,21 +97,25 @@ defmodule Mobilizon.Service.GlobalSearch.SearchMobilizon do
|> Keyword.merge( |> Keyword.merge(
term: options[:search], term: options[:search],
languageOneOf: options[:language_one_of], languageOneOf: options[:language_one_of],
boostLanguages: options[:boost_languages],
distance: if(options[:radius], do: "#{options[:radius]}_km", else: nil), distance: if(options[:radius], do: "#{options[:radius]}_km", else: nil),
count: options[:limit], count: options[:limit],
start: (options[:page] - 1) * options[:limit], start: (options[:page] - 1) * options[:limit],
latlon: to_lat_lon(options[:location]), latlon: to_lat_lon(options[:location]),
bbox: options[:bbox] bbox: options[:bbox],
sortBy: Map.get(@sort_by_options, options[:sort_by])
) )
|> Keyword.take([ |> Keyword.take([
:search, :search,
:languageOneOf,
:boostLanguages, :boostLanguages,
:latlon, :latlon,
:distance, :distance,
:sort, :sort,
:start, :start,
:count, :count,
:bbox :bbox,
:sortBy
]) ])
|> Keyword.reject(fn {_key, val} -> is_nil(val) end) |> Keyword.reject(fn {_key, val} -> is_nil(val) end)
@ -179,6 +195,7 @@ defmodule Mobilizon.Service.GlobalSearch.SearchMobilizon do
avatar: organizer_actor_avatar avatar: organizer_actor_avatar
}, },
physical_address: address, physical_address: address,
participant_stats: %{participant: data["participantCount"]},
tags: tags:
Enum.map(data["tags"], fn tag -> Enum.map(data["tags"], fn tag ->
tag = String.trim_leading(tag, "#") tag = String.trim_leading(tag, "#")