[GraphQL] Move events endpoint to paginated event list

Signed-off-by: Thomas Citharel <tcit@tcit.fr>
This commit is contained in:
Thomas Citharel 2020-12-09 17:55:38 +01:00
parent 79b52c1f10
commit 8e722032fb
No known key found for this signature in database
GPG Key ID: A061B9DDE0CA0773
10 changed files with 116 additions and 142 deletions

View File

@ -1949,7 +1949,7 @@ type RootQueryType {
"The limit of events per page" "The limit of events per page"
limit: Int limit: Int
): [Event] ): PaginatedEventList
"Get an event by uuid" "Get an event by uuid"
event("The event's UUID" uuid: UUID!): Event event("The event's UUID" uuid: UUID!): Event

View File

@ -196,54 +196,54 @@ export const FETCH_EVENT_BASIC = gql`
export const FETCH_EVENTS = gql` export const FETCH_EVENTS = gql`
query { query {
events { events {
id, total
uuid, elements {
url,
local,
title,
description,
beginsOn,
endsOn,
status,
visibility,
picture {
id id
uuid
url url
}, local
publishAt, title
# online_address, description
# phone_address, beginsOn
physicalAddress { endsOn
id, status
description, visibility
locality picture {
},
organizerActor {
id,
avatar {
id id
url url
}, }
preferredUsername, publishAt
domain, # online_address,
name, # phone_address,
}, physicalAddress {
# attributedTo { id
# avatar { description
# id locality
# url }
# }, organizerActor {
# preferredUsername, id
# name, avatar {
# }, id
category, url
participants { }
${participantsQuery} preferredUsername
}, domain
tags { name
slug, }
title # attributedTo {
}, # avatar {
# id
# url
# },
# preferredUsername,
# name,
# },
category
tags {
slug
title
}
}
} }
} }
`; `;

View File

@ -220,6 +220,7 @@
<script lang="ts"> <script lang="ts">
import { Component, Vue, Watch } from "vue-property-decorator"; import { Component, Vue, Watch } from "vue-property-decorator";
import { ParticipantRole } from "@/types/enums"; import { ParticipantRole } from "@/types/enums";
import { Paginate } from "@/types/paginate";
import { IParticipant, Participant } from "../types/participant.model"; import { IParticipant, Participant } from "../types/participant.model";
import { FETCH_EVENTS } from "../graphql/event"; import { FETCH_EVENTS } from "../graphql/event";
import EventListCard from "../components/Event/EventListCard.vue"; import EventListCard from "../components/Event/EventListCard.vue";
@ -295,7 +296,7 @@ import Subtitle from "../components/Utils/Subtitle.vue";
}, },
}) })
export default class Home extends Vue { export default class Home extends Vue {
events: IEvent[] = []; events!: Paginate<IEvent>;
locations = []; locations = [];
@ -437,7 +438,7 @@ export default class Home extends Vue {
* Return all events from server excluding the ones shown as participating * Return all events from server excluding the ones shown as participating
*/ */
get filteredFeaturedEvents(): IEvent[] { get filteredFeaturedEvents(): IEvent[] {
return this.events.filter( return this.events.elements.filter(
({ id }) => ({ id }) =>
!this.currentUserParticipations !this.currentUserParticipations
.filter( .filter(

View File

@ -59,17 +59,17 @@
<section class="events-featured" v-if="!tag && searchEvents.initial"> <section class="events-featured" v-if="!tag && searchEvents.initial">
<b-loading :active.sync="$apollo.loading"></b-loading> <b-loading :active.sync="$apollo.loading"></b-loading>
<h2 class="title">{{ $t("Featured events") }}</h2> <h2 class="title">{{ $t("Featured events") }}</h2>
<div v-if="events.length > 0" class="columns is-multiline"> <div v-if="events.elements.length > 0" class="columns is-multiline">
<div <div
class="column is-one-third-desktop" class="column is-one-third-desktop"
v-for="event in events" v-for="event in events.elements"
:key="event.uuid" :key="event.uuid"
> >
<EventCard :event="event" /> <EventCard :event="event" />
</div> </div>
</div> </div>
<b-message <b-message
v-else-if="events.length === 0 && $apollo.loading === false" v-else-if="events.elements.length === 0 && $apollo.loading === false"
type="is-danger" type="is-danger"
>{{ $t("No events found") }}</b-message >{{ $t("No events found") }}</b-message
> >
@ -250,7 +250,10 @@ const GROUP_PAGE_LIMIT = 10;
export default class Search extends Vue { export default class Search extends Vue {
@Prop({ type: String, required: false }) tag!: string; @Prop({ type: String, required: false }) tag!: string;
events: IEvent[] = []; events: Paginate<IEvent> = {
total: 0,
elements: [],
};
searchEvents: Paginate<IEvent> & { initial: boolean } = { searchEvents: Paginate<IEvent> & { initial: boolean } = {
total: 0, total: 0,

View File

@ -190,7 +190,7 @@ defmodule Mobilizon.GraphQL.Resolvers.Admin do
when is_admin(role) do when is_admin(role) do
last_public_event_published = last_public_event_published =
case Events.list_events(1, 1, :inserted_at, :desc) do case Events.list_events(1, 1, :inserted_at, :desc) do
[event | _] -> event %Page{elements: [event | _]} -> event
_ -> nil _ -> nil
end end

View File

@ -161,7 +161,9 @@ defmodule Mobilizon.GraphQL.Resolvers.Event do
events = events =
if @number_of_related_events - length(events) > 0 do if @number_of_related_events - length(events) > 0 do
events events
|> Enum.concat(Events.list_events(1, @number_of_related_events, :begins_on, :asc, true)) |> Enum.concat(
Events.list_events(1, @number_of_related_events, :begins_on, :asc, true).elements
)
|> uniq_events() |> uniq_events()
else else
events events

View File

@ -299,7 +299,7 @@ defmodule Mobilizon.GraphQL.Schema.EventType do
object :event_queries do object :event_queries do
@desc "Get all events" @desc "Get all events"
field :events, list_of(:event) do field :events, :paginated_event_list do
arg(:page, :integer, default_value: 1, description: "The page in the paginated event list") arg(:page, :integer, default_value: 1, description: "The page in the paginated event list")
arg(:limit, :integer, default_value: 10, description: "The limit of events per page") arg(:limit, :integer, default_value: 10, description: "The limit of events per page")
resolve(&Event.list_events/3) resolve(&Event.list_events/3)

View File

@ -357,7 +357,7 @@ defmodule Mobilizon.Events do
direction \\ :asc, direction \\ :asc,
is_future \\ true is_future \\ true
) do ) do
query = from(e in Event, distinct: true, preload: [:organizer_actor, :participants]) query = from(e in Event, preload: [:organizer_actor, :participants])
query query
|> sort(sort, direction) |> sort(sort, direction)
@ -365,8 +365,7 @@ defmodule Mobilizon.Events do
|> filter_public_visibility() |> filter_public_visibility()
|> filter_draft() |> filter_draft()
|> filter_local_or_from_followed_instances_events() |> filter_local_or_from_followed_instances_events()
|> Page.paginate(page, limit) |> Page.build_page(page, limit)
|> Repo.all()
end end
@spec stream_events_for_sitemap :: Enum.t() @spec stream_events_for_sitemap :: Enum.t()

View File

@ -1144,120 +1144,89 @@ defmodule Mobilizon.Web.Resolvers.EventTest do
] ]
end end
test "list_events/3 returns events", context do @fetch_events_query """
event = insert(:event) query Events($page: Int, $limit: Int) {
events(page: $page, limit: $limit) {
query = """ total
{ elements {
events { uuid
uuid,
} }
} }
""" }
"""
test "list_events/3 returns events", %{conn: conn} do
event = insert(:event)
res = res =
context.conn conn
|> get("/api", AbsintheHelpers.query_skeleton(query, "event")) |> AbsintheHelpers.graphql_query(query: @fetch_events_query)
assert json_response(res, 200)["data"]["events"] |> Enum.map(& &1["uuid"]) == [event.uuid] assert res["data"]["events"]["elements"] |> Enum.map(& &1["uuid"]) == [
event.uuid
]
Enum.each(0..15, fn _ -> Enum.each(0..15, fn _ ->
insert(:event) insert(:event)
end) end)
query = """ res =
{ conn
events { |> AbsintheHelpers.graphql_query(query: @fetch_events_query)
uuid,
} assert res["data"]["events"]["total"] == 17
} assert res["data"]["events"]["elements"] |> length == 10
"""
res = res =
context.conn conn
|> get("/api", AbsintheHelpers.query_skeleton(query, "event")) |> AbsintheHelpers.graphql_query(query: @fetch_events_query, variables: %{page: 2})
assert json_response(res, 200)["data"]["events"] |> length == 10 assert res["data"]["events"]["total"] == 17
assert res["data"]["events"]["elements"] |> length == 7
query = """
{
events(page: 2) {
uuid,
}
}
"""
res = res =
context.conn conn
|> get("/api", AbsintheHelpers.query_skeleton(query, "event")) |> AbsintheHelpers.graphql_query(
query: @fetch_events_query,
variables: %{page: 2, limit: 15}
)
assert json_response(res, 200)["data"]["events"] |> length == 7 assert res["data"]["events"]["total"] == 17
assert res["data"]["events"]["elements"] |> length == 2
query = """
{
events(page: 2, limit: 15) {
uuid,
}
}
"""
res = res =
context.conn conn
|> get("/api", AbsintheHelpers.query_skeleton(query, "event")) |> AbsintheHelpers.graphql_query(
query: @fetch_events_query,
variables: %{page: 3, limit: 15}
)
assert json_response(res, 200)["data"]["events"] |> length == 2 assert res["data"]["events"]["total"] == 17
assert res["data"]["events"]["elements"] |> length == 0
query = """
{
events(page: 3, limit: 15) {
uuid,
}
}
"""
res =
context.conn
|> get("/api", AbsintheHelpers.query_skeleton(query, "event"))
assert json_response(res, 200)["data"]["events"] |> length == 0
end end
test "list_events/3 doesn't list private events", context do test "list_events/3 doesn't list private events", %{conn: conn} do
insert(:event, visibility: :private) insert(:event, visibility: :private)
insert(:event, visibility: :unlisted) insert(:event, visibility: :unlisted)
insert(:event, visibility: :restricted) insert(:event, visibility: :restricted)
query = """
{
events {
uuid,
}
}
"""
res = res =
context.conn conn
|> get("/api", AbsintheHelpers.query_skeleton(query, "event")) |> AbsintheHelpers.graphql_query(query: @fetch_events_query)
assert json_response(res, 200)["data"]["events"] |> Enum.map(& &1["uuid"]) == [] assert res["data"]["events"]["total"] == 0
assert res["data"]["events"]["elements"] |> Enum.map(& &1["uuid"]) == []
end end
test "list_events/3 doesn't list draft events", context do test "list_events/3 doesn't list draft events", %{conn: conn} do
insert(:event, visibility: :public, draft: true) insert(:event, visibility: :public, draft: true)
query = """
{
events {
uuid,
}
}
"""
res = res =
context.conn conn
|> get("/api", AbsintheHelpers.query_skeleton(query, "event")) |> AbsintheHelpers.graphql_query(query: @fetch_events_query)
assert json_response(res, 200)["data"]["events"] |> Enum.map(& &1["uuid"]) == [] assert res["data"]["events"]["total"] == 0
assert res["data"]["events"]["elements"] |> Enum.map(& &1["uuid"]) == []
end end
test "find_event/3 returns an unlisted event", context do test "find_event/3 returns an unlisted event", context do

View File

@ -29,12 +29,12 @@ defmodule Mobilizon.EventsTest do
end end
test "list_events/0 returns all events", %{event: event} do test "list_events/0 returns all events", %{event: event} do
assert event.title == hd(Events.list_events()).title assert event.title == hd(Events.list_events().elements).title
end end
test "list_events/5 returns events from other instances if we follow them", test "list_events/5 returns events from other instances if we follow them",
%{event: _event} do %{event: _event} do
events = Events.list_events() events = Events.list_events().elements
assert length(events) == 1 assert length(events) == 1
%Actor{id: remote_instance_actor_id} = remote_instance_actor = insert(:instance_actor) %Actor{id: remote_instance_actor_id} = remote_instance_actor = insert(:instance_actor)
@ -46,7 +46,7 @@ defmodule Mobilizon.EventsTest do
insert(:follower, target_actor: remote_instance_actor, actor: own_instance_actor) insert(:follower, target_actor: remote_instance_actor, actor: own_instance_actor)
events = Events.list_events() events = Events.list_events().elements
assert length(events) == 2 assert length(events) == 2
assert events |> Enum.any?(fn event -> event.title == "My Remote event" end) assert events |> Enum.any?(fn event -> event.title == "My Remote event" end)
end end
@ -58,7 +58,7 @@ defmodule Mobilizon.EventsTest do
%Event{url: remote_event_url} = insert(:event, local: false, title: "My Remote event") %Event{url: remote_event_url} = insert(:event, local: false, title: "My Remote event")
Mobilizon.Share.create(remote_event_url, remote_instance_actor_id, remote_actor_id) Mobilizon.Share.create(remote_event_url, remote_instance_actor_id, remote_actor_id)
events = Events.list_events() events = Events.list_events().elements
assert length(events) == 1 assert length(events) == 1
assert events |> Enum.all?(fn event -> event.title != "My Remote event" end) assert events |> Enum.all?(fn event -> event.title != "My Remote event" end)
end end