From a6f09dc0c78bd9f159616ceef5d8fe14f6aceab1 Mon Sep 17 00:00:00 2001 From: 2458773093 <2458773093@protonmail.com> Date: Sat, 20 Jul 2019 01:06:32 +0300 Subject: [PATCH] group unread count --- app/controllers/api/v1/timelines/group_controller.rb | 6 ++++++ app/models/group_account.rb | 1 + app/models/status.rb | 5 +++++ .../20190721214831_add_unread_count_to_group_accounts.rb | 5 +++++ db/schema.rb | 3 ++- 5 files changed, 19 insertions(+), 1 deletion(-) create mode 100644 db/migrate/20190721214831_add_unread_count_to_group_accounts.rb diff --git a/app/controllers/api/v1/timelines/group_controller.rb b/app/controllers/api/v1/timelines/group_controller.rb index 411418cb..b19f3961 100644 --- a/app/controllers/api/v1/timelines/group_controller.rb +++ b/app/controllers/api/v1/timelines/group_controller.rb @@ -9,6 +9,8 @@ class Api::V1::Timelines::GroupController < Api::BaseController after_action :insert_pagination_headers, unless: -> { @statuses.empty? } def show + mark_as_unread + render json: @statuses, each_serializer: REST::StatusSerializer, relationships: StatusRelationshipsPresenter.new(@statuses, current_user.account_id) @@ -16,6 +18,10 @@ class Api::V1::Timelines::GroupController < Api::BaseController private + def mark_as_unread + GroupAccount.where(group: @group, account: current_account).update_all("unread_count = 0") + end + def set_group @group = Group.find(params[:id]) end diff --git a/app/models/group_account.rb b/app/models/group_account.rb index ea4ec62d..12485d98 100644 --- a/app/models/group_account.rb +++ b/app/models/group_account.rb @@ -9,6 +9,7 @@ # role :string # created_at :datetime not null # updated_at :datetime not null +# unread_count :integer default(0) # class GroupAccount < ApplicationRecord diff --git a/app/models/status.rb b/app/models/status.rb index ae86194b..ce467daf 100644 --- a/app/models/status.rb +++ b/app/models/status.rb @@ -256,6 +256,7 @@ class Status < ApplicationRecord after_create_commit :store_uri, 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 @@ -535,4 +536,8 @@ class Status < ApplicationRecord AccountConversation.remove_status(inbox_owner, self) 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 diff --git a/db/migrate/20190721214831_add_unread_count_to_group_accounts.rb b/db/migrate/20190721214831_add_unread_count_to_group_accounts.rb new file mode 100644 index 00000000..8a9f19df --- /dev/null +++ b/db/migrate/20190721214831_add_unread_count_to_group_accounts.rb @@ -0,0 +1,5 @@ +class AddUnreadCountToGroupAccounts < ActiveRecord::Migration[5.2] + def change + add_column :group_accounts, :unread_count, :integer, default: 0 + end +end diff --git a/db/schema.rb b/db/schema.rb index a27da0bf..b3aaf19c 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: 2019_07_16_173227) do +ActiveRecord::Schema.define(version: 2019_07_21_214831) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" @@ -327,6 +327,7 @@ ActiveRecord::Schema.define(version: 2019_07_16_173227) do t.string "role" t.datetime "created_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"], name: "index_group_accounts_on_account_id" t.index ["group_id", "account_id"], name: "index_group_accounts_on_group_id_and_account_id"