From 4a8cd0b5857a49cd6c915c663532376829f4b7ca Mon Sep 17 00:00:00 2001 From: mgabdev <> Date: Sat, 8 Aug 2020 13:08:48 -0500 Subject: [PATCH] Updates to groups sorting MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit • Updates to groups sorting --- app/controllers/api/v1/groups_controller.rb | 3 ++- .../timelines/group_collection_controller.rb | 2 +- .../features/group_collection_timeline.js | 4 ++-- app/javascript/gabsocial/pages/groups_page.js | 2 +- app/services/fetch_groups_service.rb | 22 +++++++++++++++++++ ...08170254_add_index_to_status_created_at.rb | 8 +++++++ ...08_add_index_to_user_current_sign_in_at.rb | 7 ++++++ db/schema.rb | 5 ++++- 8 files changed, 47 insertions(+), 6 deletions(-) create mode 100644 app/services/fetch_groups_service.rb create mode 100644 db/migrate/20200808170254_add_index_to_status_created_at.rb create mode 100644 db/migrate/20200808170708_add_index_to_user_current_sign_in_at.rb diff --git a/app/controllers/api/v1/groups_controller.rb b/app/controllers/api/v1/groups_controller.rb index 885876d1..bffb8918 100644 --- a/app/controllers/api/v1/groups_controller.rb +++ b/app/controllers/api/v1/groups_controller.rb @@ -12,7 +12,8 @@ class Api::V1::GroupsController < Api::BaseController def index case current_tab when 'featured' - @groups = Group.where(is_featured: true, is_archived: false).limit(100).all + @groupIds = FetchGroupsService.new.call("featured") + @groups = Group.where(id: @groupIds).limit(150).all when 'new' if !current_user render json: { error: 'This method requires an authenticated user' }, status: 422 diff --git a/app/controllers/api/v1/timelines/group_collection_controller.rb b/app/controllers/api/v1/timelines/group_collection_controller.rb index 2c334275..a2bcbbff 100644 --- a/app/controllers/api/v1/timelines/group_collection_controller.rb +++ b/app/controllers/api/v1/timelines/group_collection_controller.rb @@ -62,7 +62,7 @@ class Api::V1::Timelines::GroupCollectionController < Api::BaseController @groupIds = [] if @collection_type == 'featured' - @groupIds = Group.where(is_featured: true, is_archived: false).limit(100).all.pluck(:id) + @groupIds = FetchGroupsService.new.call("featured") elsif @collection_type == 'member' && !current_user.nil? @groupIds = current_user.account.groups.pluck(:id) else diff --git a/app/javascript/gabsocial/features/group_collection_timeline.js b/app/javascript/gabsocial/features/group_collection_timeline.js index 2c99a549..a29e5b1e 100644 --- a/app/javascript/gabsocial/features/group_collection_timeline.js +++ b/app/javascript/gabsocial/features/group_collection_timeline.js @@ -13,7 +13,7 @@ import { } from '../actions/groups' import { GROUP_TIMELINE_SORTING_TYPE_TOP, - GROUP_TIMELINE_SORTING_TYPE_TOP_OPTION_WEEKLY, + GROUP_TIMELINE_SORTING_TYPE_TOP_OPTION_TODAY, GROUP_TIMELINE_SORTING_TYPE_NEWEST, } from '../constants' import getSortBy from '../utils/group_sort_by' @@ -54,7 +54,7 @@ const mapDispatchToProps = (dispatch) => ({ }, setFeaturedTop() { dispatch(setGroupTimelineSort(GROUP_TIMELINE_SORTING_TYPE_TOP)) - dispatch(setGroupTimelineTopSort(GROUP_TIMELINE_SORTING_TYPE_TOP_OPTION_WEEKLY)) + dispatch(setGroupTimelineTopSort(GROUP_TIMELINE_SORTING_TYPE_TOP_OPTION_TODAY)) }, setMemberNewest() { dispatch(setGroupTimelineSort(GROUP_TIMELINE_SORTING_TYPE_NEWEST)) diff --git a/app/javascript/gabsocial/pages/groups_page.js b/app/javascript/gabsocial/pages/groups_page.js index 2776dc3d..6531c5c8 100644 --- a/app/javascript/gabsocial/pages/groups_page.js +++ b/app/javascript/gabsocial/pages/groups_page.js @@ -52,7 +52,7 @@ class GroupsPage extends PureComponent { const tabs = !!me ? [ { - title: intl.formatMessage(dontShowChildren ? messages.myGroupsTimeline : messages.groups), + title: intl.formatMessage(messages.myGroupsTimeline), to: '/groups', }, { diff --git a/app/services/fetch_groups_service.rb b/app/services/fetch_groups_service.rb new file mode 100644 index 00000000..0e259aa6 --- /dev/null +++ b/app/services/fetch_groups_service.rb @@ -0,0 +1,22 @@ +# frozen_string_literal: true + +class FetchGroupsService < BaseService + def call(type) + + if type == "featured" + body = Redis.current.get("groups:featuredgroups") + + if body.nil? || !body || body.empty? + @groupIds = Group.where(is_featured: true, is_archived: false).limit(150).all.pluck(:id) + + Redis.current.set("groups:featuredgroups", @groupIds.join(",")) + Redis.current.expire("groups:featuredgroups", 6.hours.seconds) + + @groupIds + else + body.split(",") + end + end + + end +end diff --git a/db/migrate/20200808170254_add_index_to_status_created_at.rb b/db/migrate/20200808170254_add_index_to_status_created_at.rb new file mode 100644 index 00000000..524a9d62 --- /dev/null +++ b/db/migrate/20200808170254_add_index_to_status_created_at.rb @@ -0,0 +1,8 @@ +class AddIndexToStatusCreatedAt < ActiveRecord::Migration[5.2] + disable_ddl_transaction! + + def change + add_index :statuses, :created_at, algorithm: :concurrently + add_index :statuses, :updated_at, algorithm: :concurrently + end +end diff --git a/db/migrate/20200808170708_add_index_to_user_current_sign_in_at.rb b/db/migrate/20200808170708_add_index_to_user_current_sign_in_at.rb new file mode 100644 index 00000000..51789c0c --- /dev/null +++ b/db/migrate/20200808170708_add_index_to_user_current_sign_in_at.rb @@ -0,0 +1,7 @@ +class AddIndexToUserCurrentSignInAt < ActiveRecord::Migration[5.2] + disable_ddl_transaction! + + def change + add_index :users, :current_sign_in_at, algorithm: :concurrently + end +end diff --git a/db/schema.rb b/db/schema.rb index 6bcf0d50..46df0dea 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 2020_08_06_231714) do +ActiveRecord::Schema.define(version: 2020_08_08_170708) do # These are extensions that must be enabled in order to support this database enable_extension "pg_stat_statements" @@ -775,12 +775,14 @@ ActiveRecord::Schema.define(version: 2020_08_06_231714) do t.datetime "expires_at" t.boolean "has_quote" t.index ["account_id", "id", "visibility", "updated_at"], name: "index_statuses_20180106", order: { id: :desc } + t.index ["created_at"], name: "index_statuses_on_created_at" t.index ["group_id"], name: "index_statuses_on_group_id" t.index ["in_reply_to_account_id"], name: "index_statuses_on_in_reply_to_account_id" t.index ["in_reply_to_id"], name: "index_statuses_on_in_reply_to_id" t.index ["quote_of_id"], name: "index_statuses_on_quote_of_id" t.index ["reblog_of_id", "account_id"], name: "index_statuses_on_reblog_of_id_and_account_id" t.index ["reply"], name: "index_statuses_on_reply" + t.index ["updated_at"], name: "index_statuses_on_updated_at" t.index ["uri"], name: "index_statuses_on_uri", unique: true end @@ -893,6 +895,7 @@ ActiveRecord::Schema.define(version: 2020_08_06_231714) do t.index ["confirmation_token"], name: "index_users_on_confirmation_token", unique: true t.index ["created_at"], name: "index_users_on_created_at" t.index ["created_by_application_id"], name: "index_users_on_created_by_application_id" + t.index ["current_sign_in_at"], name: "index_users_on_current_sign_in_at" t.index ["email"], name: "index_users_on_email", unique: true t.index ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true t.index ["unique_email"], name: "index_users_on_unique_email"