From 1146e5c9b56e31f666857171f01dd13009a6ae8e Mon Sep 17 00:00:00 2001 From: Adam Coffman Date: Thu, 16 Jun 2022 09:57:14 -0500 Subject: [PATCH] in the filter sidebar, order users by their most recent contribution --- .../types/connections/events_connection.rb | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/server/app/graphql/types/connections/events_connection.rb b/server/app/graphql/types/connections/events_connection.rb index c94174331..fb7b93176 100644 --- a/server/app/graphql/types/connections/events_connection.rb +++ b/server/app/graphql/types/connections/events_connection.rb @@ -15,7 +15,20 @@ class EventsConnection < Types::BaseConnection description: 'When filtered on a subject, user, or organization, the total number of events for that subject/user/organization, irregardless of other filters. Returns null when there is no subject, user, or organization.' def unique_participants - User.where(id: unscoped_events_base_query.select(:originating_user_id)).distinct + #Users who's originating ids appear in the events query, + #joined to events table, limited to only events in the events query + #select the user id and the time of the newest relevant event + ranked_user_ids = User.where(id: unscoped_events_base_query.select(:originating_user_id)) + .joins(:events) + .where(events: { id: unscoped_events_base_query.select(:id) } ) + .select("users.id as user_id, max(events.created_at) newest_event_timestamp") + .group("users.id") + + + #users, ranked by most recent relevant event + User.joins("INNER JOIN (#{ranked_user_ids.to_sql}) ordered ON ordered.user_id = users.id") + .order('ordered.newest_event_timestamp desc') + .limit(15) end def event_types