Removed unread_count from group_accounts

This commit is contained in:
mgabdev 2020-05-09 23:57:38 -04:00
parent d16ef95681
commit f3c3a66e6b
11 changed files with 14 additions and 35 deletions

View File

@ -14,7 +14,7 @@ class Api::V1::GroupsController < Api::BaseController
when 'featured' when 'featured'
@groups = Group.where(is_featured: true).limit(50).all @groups = Group.where(is_featured: true).limit(50).all
when 'member' when 'member'
@groups = Group.joins(:group_accounts).where(is_archived: false, group_accounts: { account: current_account }).order('group_accounts.unread_count DESC, group_accounts.id DESC').all @groups = Group.joins(:group_accounts).where(is_archived: false, group_accounts: { account: current_account }).order('group_accounts.id DESC').all
when 'admin' when 'admin'
@groups = Group.joins(:group_accounts).where(is_archived: false, group_accounts: { account: current_account, role: :admin }).all @groups = Group.joins(:group_accounts).where(is_archived: false, group_accounts: { account: current_account, role: :admin }).all
end end

View File

@ -9,8 +9,6 @@ class Api::V1::Timelines::GroupController < Api::BaseController
after_action :insert_pagination_headers, unless: -> { @statuses.empty? } after_action :insert_pagination_headers, unless: -> { @statuses.empty? }
def show def show
mark_as_unread
render json: @statuses, render json: @statuses,
each_serializer: REST::StatusSerializer, each_serializer: REST::StatusSerializer,
relationships: StatusRelationshipsPresenter.new(@statuses, current_user.account_id) relationships: StatusRelationshipsPresenter.new(@statuses, current_user.account_id)
@ -18,10 +16,6 @@ class Api::V1::Timelines::GroupController < Api::BaseController
private private
def mark_as_unread
GroupAccount.where(group: @group, account: current_account).update_all("unread_count = 0")
end
def set_group def set_group
@group = Group.find(params[:id]) @group = Group.find(params[:id])
end end

View File

@ -30,15 +30,9 @@ class Item extends ImmutablePureComponent {
// Wait for relationships // Wait for relationships
if (!relationships) return null; if (!relationships) return null;
const unreadCount = relationships.get('unread_count');
return ( return (
<Link to={`/groups/${group.get('id')}`} className="group-sidebar-panel__item"> <Link to={`/groups/${group.get('id')}`} className="group-sidebar-panel__item">
<div className="group-sidebar-panel__item__title">{group.get('title')}</div> <div className="group-sidebar-panel__item__title">{group.get('title')}</div>
<div className="group-sidebar-panel__item__meta">
{unreadCount > 0 && <span className="group-sidebar-panel__item__meta__unread">{shortNumberFormat(unreadCount)} {intl.formatMessage(messages.new_statuses)}</span>}
{unreadCount === 0 && <span>{intl.formatMessage(messages.no_recent_activity)}</span>}
</div>
</Link> </Link>
); );
} }

View File

@ -13,19 +13,12 @@ module GroupInteractions
follow_mapping(GroupAccount.where(group_id: target_group_ids, account_id: account_id, role: :admin), :group_id) follow_mapping(GroupAccount.where(group_id: target_group_ids, account_id: account_id, role: :admin), :group_id)
end end
def unread_count_map(target_group_ids, account_id)
unread_count_mapping(GroupAccount.where(group_id: target_group_ids, account_id: account_id), :unread_count)
end
private private
def follow_mapping(query, field) def follow_mapping(query, field)
query.pluck(field).each_with_object({}) { |id, mapping| mapping[id] = true } query.pluck(field).each_with_object({}) { |id, mapping| mapping[id] = true }
end end
def unread_count_mapping(query, field)
query.pluck(:group_id, :unread_count).each_with_object({}) { |e, mapping| mapping[e[0]] = e[1] }
end
end end
def accounts_for_local_distribution def accounts_for_local_distribution

View File

@ -9,10 +9,10 @@
# role :string # role :string
# created_at :datetime not null # created_at :datetime not null
# updated_at :datetime not null # updated_at :datetime not null
# unread_count :integer default(0)
# #
class GroupAccount < ApplicationRecord class GroupAccount < ApplicationRecord
self.ignored_columns = ["unread_count"]
enum role: { admin: "admin" } enum role: { admin: "admin" }
belongs_to :group belongs_to :group

View File

@ -6,7 +6,7 @@
# id :bigint(8) not null, primary key # id :bigint(8) not null, primary key
# list_id :bigint(8) not null # list_id :bigint(8) not null
# account_id :bigint(8) not null # account_id :bigint(8) not null
# follow_id :bigint(8) not null # follow_id :bigint(8) default(1)
# #
class ListAccount < ApplicationRecord class ListAccount < ApplicationRecord

View File

@ -25,6 +25,7 @@
# group_id :integer # group_id :integer
# quote_of_id :bigint(8) # quote_of_id :bigint(8)
# revised_at :datetime # revised_at :datetime
# markdown :text
# #
class Status < ApplicationRecord class Status < ApplicationRecord
@ -266,7 +267,6 @@ class Status < ApplicationRecord
after_create_commit :store_uri, if: :local? after_create_commit :store_uri, if: :local?
after_create_commit :update_statistics, if: :local? after_create_commit :update_statistics, if: :local?
after_create_commit :increase_group_unread_counts, if: Proc.new { |status| !status.group_id.nil? }
around_create GabSocial::Snowflake::Callbacks around_create GabSocial::Snowflake::Callbacks
@ -553,7 +553,4 @@ class Status < ApplicationRecord
end end
end end
def increase_group_unread_counts
GroupAccount.where(group_id: group_id).where.not(account_id: account_id).update_all("unread_count = unread_count + 1")
end
end end

View File

@ -1,7 +1,7 @@
# frozen_string_literal: true # frozen_string_literal: true
class GroupRelationshipsPresenter class GroupRelationshipsPresenter
attr_reader :member, :admin, :unread_count attr_reader :member, :admin
def initialize(group_ids, current_account_id, **options) def initialize(group_ids, current_account_id, **options)
@group_ids = group_ids.map { |a| a.is_a?(Group) ? a.id : a } @group_ids = group_ids.map { |a| a.is_a?(Group) ? a.id : a }
@ -9,7 +9,6 @@ class GroupRelationshipsPresenter
@member = Group.member_map(@group_ids, @current_account_id) @member = Group.member_map(@group_ids, @current_account_id)
@admin = Group.admin_map(@group_ids, @current_account_id) @admin = Group.admin_map(@group_ids, @current_account_id)
@unread_count = Group.unread_count_map(@group_ids, @current_account_id)
end end
end end

View File

@ -1,7 +1,7 @@
# frozen_string_literal: true # frozen_string_literal: true
class REST::GroupRelationshipSerializer < ActiveModel::Serializer class REST::GroupRelationshipSerializer < ActiveModel::Serializer
attributes :id, :member, :admin, :unread_count attributes :id, :member, :admin
def id def id
object.id.to_s object.id.to_s
@ -15,7 +15,4 @@ class REST::GroupRelationshipSerializer < ActiveModel::Serializer
instance_options[:relationships].admin[object.id] ? true : false instance_options[:relationships].admin[object.id] ? true : false
end end
def unread_count
instance_options[:relationships].unread_count[object.id] || 0
end
end end

View File

@ -0,0 +1,5 @@
class RemoveUnreadCountFromGroupAccounts < ActiveRecord::Migration[5.2]
def change
safety_assured { remove_column :group_accounts, :unread_count, :integer }
end
end

View File

@ -10,7 +10,7 @@
# #
# It's strongly recommended that you check this file into your version control system. # It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema.define(version: 2020_03_10_224203) do ActiveRecord::Schema.define(version: 2020_05_10_034822) do
# These are extensions that must be enabled in order to support this database # These are extensions that must be enabled in order to support this database
enable_extension "plpgsql" enable_extension "plpgsql"
@ -327,7 +327,6 @@ ActiveRecord::Schema.define(version: 2020_03_10_224203) do
t.string "role" t.string "role"
t.datetime "created_at", null: false t.datetime "created_at", null: false
t.datetime "updated_at", null: false t.datetime "updated_at", null: false
t.integer "unread_count", default: 0
t.index ["account_id", "group_id"], name: "index_group_accounts_on_account_id_and_group_id", unique: true t.index ["account_id", "group_id"], name: "index_group_accounts_on_account_id_and_group_id", unique: true
t.index ["account_id"], name: "index_group_accounts_on_account_id" t.index ["account_id"], name: "index_group_accounts_on_account_id"
t.index ["group_id", "account_id"], name: "index_group_accounts_on_group_id_and_account_id" t.index ["group_id", "account_id"], name: "index_group_accounts_on_group_id_and_account_id"
@ -400,7 +399,7 @@ ActiveRecord::Schema.define(version: 2020_03_10_224203) do
create_table "list_accounts", force: :cascade do |t| create_table "list_accounts", force: :cascade do |t|
t.bigint "list_id", null: false t.bigint "list_id", null: false
t.bigint "account_id", null: false t.bigint "account_id", null: false
t.bigint "follow_id", null: false t.bigint "follow_id", default: 1
t.index ["account_id", "list_id"], name: "index_list_accounts_on_account_id_and_list_id", unique: true t.index ["account_id", "list_id"], name: "index_list_accounts_on_account_id_and_list_id", unique: true
t.index ["follow_id"], name: "index_list_accounts_on_follow_id" t.index ["follow_id"], name: "index_list_accounts_on_follow_id"
t.index ["list_id", "account_id"], name: "index_list_accounts_on_list_id_and_account_id" t.index ["list_id", "account_id"], name: "index_list_accounts_on_list_id_and_account_id"
@ -711,6 +710,7 @@ ActiveRecord::Schema.define(version: 2020_03_10_224203) do
t.integer "group_id" t.integer "group_id"
t.bigint "quote_of_id" t.bigint "quote_of_id"
t.datetime "revised_at" t.datetime "revised_at"
t.text "markdown"
t.index ["account_id", "id", "visibility", "updated_at"], name: "index_statuses_20180106", order: { id: :desc } t.index ["account_id", "id", "visibility", "updated_at"], name: "index_statuses_20180106", order: { id: :desc }
t.index ["group_id"], name: "index_statuses_on_group_id" 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_account_id"], name: "index_statuses_on_in_reply_to_account_id"