1
0
Fork 0
mirror of https://framagit.org/framasoft/mobilizon.git synced 2024-12-21 23:44:30 +00:00

[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"
limit: Int
): [Event]
): PaginatedEventList
"Get an event by uuid"
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`
query {
events {
id,
uuid,
url,
local,
title,
description,
beginsOn,
endsOn,
status,
visibility,
picture {
total
elements {
id
uuid
url
},
publishAt,
# online_address,
# phone_address,
physicalAddress {
id,
description,
locality
},
organizerActor {
id,
avatar {
local
title
description
beginsOn
endsOn
status
visibility
picture {
id
url
},
preferredUsername,
domain,
name,
},
# attributedTo {
# avatar {
# id
# url
# },
# preferredUsername,
# name,
# },
category,
participants {
${participantsQuery}
},
tags {
slug,
title
},
}
publishAt
# online_address,
# phone_address,
physicalAddress {
id
description
locality
}
organizerActor {
id
avatar {
id
url
}
preferredUsername
domain
name
}
# attributedTo {
# avatar {
# id
# url
# },
# preferredUsername,
# name,
# },
category
tags {
slug
title
}
}
}
}
`;

View file

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

View file

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

View file

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

View file

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

View file

@ -299,7 +299,7 @@ defmodule Mobilizon.GraphQL.Schema.EventType do
object :event_queries do
@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(:limit, :integer, default_value: 10, description: "The limit of events per page")
resolve(&Event.list_events/3)

View file

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

View file

@ -1144,120 +1144,89 @@ defmodule Mobilizon.Web.Resolvers.EventTest do
]
end
test "list_events/3 returns events", context do
event = insert(:event)
query = """
{
events {
uuid,
@fetch_events_query """
query Events($page: Int, $limit: Int) {
events(page: $page, limit: $limit) {
total
elements {
uuid
}
}
"""
}
"""
test "list_events/3 returns events", %{conn: conn} do
event = insert(:event)
res =
context.conn
|> get("/api", AbsintheHelpers.query_skeleton(query, "event"))
conn
|> 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 _ ->
insert(:event)
end)
query = """
{
events {
uuid,
}
}
"""
res =
conn
|> AbsintheHelpers.graphql_query(query: @fetch_events_query)
assert res["data"]["events"]["total"] == 17
assert res["data"]["events"]["elements"] |> length == 10
res =
context.conn
|> get("/api", AbsintheHelpers.query_skeleton(query, "event"))
conn
|> AbsintheHelpers.graphql_query(query: @fetch_events_query, variables: %{page: 2})
assert json_response(res, 200)["data"]["events"] |> length == 10
query = """
{
events(page: 2) {
uuid,
}
}
"""
assert res["data"]["events"]["total"] == 17
assert res["data"]["events"]["elements"] |> length == 7
res =
context.conn
|> get("/api", AbsintheHelpers.query_skeleton(query, "event"))
conn
|> AbsintheHelpers.graphql_query(
query: @fetch_events_query,
variables: %{page: 2, limit: 15}
)
assert json_response(res, 200)["data"]["events"] |> length == 7
query = """
{
events(page: 2, limit: 15) {
uuid,
}
}
"""
assert res["data"]["events"]["total"] == 17
assert res["data"]["events"]["elements"] |> length == 2
res =
context.conn
|> get("/api", AbsintheHelpers.query_skeleton(query, "event"))
conn
|> AbsintheHelpers.graphql_query(
query: @fetch_events_query,
variables: %{page: 3, limit: 15}
)
assert json_response(res, 200)["data"]["events"] |> length == 2
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
assert res["data"]["events"]["total"] == 17
assert res["data"]["events"]["elements"] |> length == 0
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: :unlisted)
insert(:event, visibility: :restricted)
query = """
{
events {
uuid,
}
}
"""
res =
context.conn
|> get("/api", AbsintheHelpers.query_skeleton(query, "event"))
conn
|> 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
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)
query = """
{
events {
uuid,
}
}
"""
res =
context.conn
|> get("/api", AbsintheHelpers.query_skeleton(query, "event"))
conn
|> 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
test "find_event/3 returns an unlisted event", context do

View file

@ -29,12 +29,12 @@ defmodule Mobilizon.EventsTest do
end
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
test "list_events/5 returns events from other instances if we follow them",
%{event: _event} do
events = Events.list_events()
events = Events.list_events().elements
assert length(events) == 1
%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)
events = Events.list_events()
events = Events.list_events().elements
assert length(events) == 2
assert events |> Enum.any?(fn event -> event.title == "My Remote event" end)
end
@ -58,7 +58,7 @@ defmodule Mobilizon.EventsTest do
%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)
events = Events.list_events()
events = Events.list_events().elements
assert length(events) == 1
assert events |> Enum.all?(fn event -> event.title != "My Remote event" end)
end