From b12cea5ead60562350d35177467ba293c85dfe71 Mon Sep 17 00:00:00 2001 From: Thomas Citharel Date: Tue, 2 Nov 2021 19:48:38 +0100 Subject: [PATCH] Allow to get group followed events after a certain date Signed-off-by: Thomas Citharel --- lib/graphql/resolvers/user.ex | 10 ++++++++-- lib/graphql/schema/user.ex | 4 ++++ lib/mobilizon/followed_group_activity.ex | 21 +++++++++++++++++---- 3 files changed, 29 insertions(+), 6 deletions(-) diff --git a/lib/graphql/resolvers/user.ex b/lib/graphql/resolvers/user.ex index 0330cf18e..430bc6b48 100644 --- a/lib/graphql/resolvers/user.ex +++ b/lib/graphql/resolvers/user.ex @@ -633,11 +633,17 @@ defmodule Mobilizon.GraphQL.Resolvers.User do }} end - def user_followed_group_events(%User{id: user_id}, %{page: page, limit: limit}, %{ + def user_followed_group_events(%User{id: user_id}, %{page: page, limit: limit} = args, %{ context: %{current_user: %User{id: logged_in_user_id}} }) when user_id == logged_in_user_id do - activities = FollowedGroupActivity.user_followed_group_events(user_id, page, limit) + activities = + FollowedGroupActivity.user_followed_group_events( + user_id, + Map.get(args, :after_datetime), + page, + limit + ) activities = %Page{ activities diff --git a/lib/graphql/schema/user.ex b/lib/graphql/schema/user.ex index e954f05d6..cb567e289 100644 --- a/lib/graphql/schema/user.ex +++ b/lib/graphql/schema/user.ex @@ -117,6 +117,10 @@ defmodule Mobilizon.GraphQL.Schema.UserType do description: "The limit of follow group events per page" ) + arg(:after_datetime, :datetime, + description: "Filter follow group events by event start datetime" + ) + resolve(&User.user_followed_group_events/3) end diff --git a/lib/mobilizon/followed_group_activity.ex b/lib/mobilizon/followed_group_activity.ex index 0c8d5a6c6..7ef7e7774 100644 --- a/lib/mobilizon/followed_group_activity.ex +++ b/lib/mobilizon/followed_group_activity.ex @@ -8,7 +8,13 @@ defmodule Mobilizon.FollowedGroupActivity do alias Mobilizon.Events.{Event, Participant} alias Mobilizon.Storage.Page - def user_followed_group_events(user_id, page \\ nil, limit \\ nil) do + @spec user_followed_group_events( + integer() | String.t(), + DateTime.t() | nil, + integer() | nil, + integer() | nil + ) :: Page.t(Event.t()) + def user_followed_group_events(user_id, after_datetime, page \\ nil, limit \\ nil) do Event |> distinct([e], e.id) |> join(:left, [e], p in Participant, on: e.id == p.event_id) @@ -19,10 +25,10 @@ defmodule Mobilizon.FollowedGroupActivity do |> join(:inner, [_e, _p, pa, _g, f, m], a in Actor, on: a.id == f.actor_id or a.id == m.actor_id ) + |> add_after_datetime_filter(after_datetime) |> where( - [e, p, pa, _g, f, m, a], - e.begins_on > ^DateTime.utc_now() and - (f.approved or m.role in ^[:member, :moderator, :administrator, :creator]) and + [_e, p, pa, _g, f, m, a], + (f.approved or m.role in ^[:member, :moderator, :administrator, :creator]) and a.user_id == ^user_id and pa.user_id != ^user_id ) @@ -40,4 +46,11 @@ defmodule Mobilizon.FollowedGroupActivity do ]) |> Page.build_page(page, limit) end + + @spec add_after_datetime_filter(Ecto.Query.t(), DateTime.t() | nil) :: Ecto.Query.t() + defp add_after_datetime_filter(query, nil), + do: where(query, [e], e.begins_on > ^DateTime.utc_now()) + + defp add_after_datetime_filter(query, %DateTime{} = datetime), + do: where(query, [e], e.begins_on > ^datetime) end