Skip to content

Commit

Permalink
Merge branch 'EN-7826-denorm-messageable-count'
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolas-entourage committed Nov 25, 2024
2 parents 3454227 + 6d4ae08 commit d61f726
Show file tree
Hide file tree
Showing 11 changed files with 92 additions and 21 deletions.
23 changes: 9 additions & 14 deletions app/controllers/admin/conversations_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,7 @@ def index
.search_by_member(params[:search])
.with_chat_messages
.merge(current_admin.join_requests.accepted)
.select(%(
entourages.*,
last_message_read < feed_updated_at or last_message_read is null as unread
))
.select("entourages.*, (unread_messages_count > 0) as unread")
.order("feed_updated_at desc")
.page(params[:page])
.per(50)
Expand All @@ -31,7 +28,7 @@ def index
@conversations.where("archived_at < feed_updated_at or archived_at is null")
end

@conversations = @conversations.where("last_message_read < feed_updated_at or last_message_read is null") if params[:filter] == 'unread'
@conversations = @conversations.where("unread_messages_count > 0") if params[:filter] == 'unread'

respond_to do |format|
format.js
Expand Down Expand Up @@ -117,7 +114,7 @@ def message

chat_builder.create do |on|
on.success do |message|
join_request.update_column(:last_message_read, message.created_at)
join_request.set_chat_messages_as_read_from(message.created_at)

respond_to do |format|
@chat_messages = [message]
Expand Down Expand Up @@ -201,15 +198,13 @@ def read_status
status = params[:status]&.to_sym
raise unless status.in?([:read, :unread])

timestamp =
case status
when :read
Time.now
when :unread
nil
end
join_request = JoinRequest.where(joinable: @conversation, user: current_admin).first

JoinRequest.where(joinable: @conversation).update_all(last_message_read: timestamp)
if status == :read
join_request.set_chat_messages_as_read
else
join_request.set_chat_messages_as_unread
end

redirect_to admin_conversations_path(filter: Rack::Utils.parse_query(URI(request.referer).query)['filter'])
end
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/admin/entourages_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -372,7 +372,7 @@ def message
end

on.success do |message|
@join_request.update_column(:last_message_read, message.created_at)
@join_request.set_chat_messages_as_read_from(message.created_at)
ModeratorReadsService
.new(instance: @entourage, moderator: current_user)
.mark_as_read
Expand Down
3 changes: 1 addition & 2 deletions app/helpers/conversations_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,7 @@ def conversation_recipients_display_names recipients, max: 3
def read_for_user? conversation, user
conversation.join_requests.any? do |join_request|
join_request.user_id == user.id &&
join_request.last_message_read.present? &&
join_request.last_message_read >= (conversation.feed_updated_at || conversation.updated_at)
join_request.unread_messages_count == 0
end
end

Expand Down
26 changes: 26 additions & 0 deletions app/jobs/count_chat_message_job.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
class CountChatMessageJob
include Sidekiq::Worker

sidekiq_options :retry => true, queue: :default

def perform messageable_type, messageable_id
return unless klass = messageable_type.constantize
return unless instance = klass.find(messageable_id)
return unless instance.respond_to?(:number_of_root_chat_messages)

instance.update_attribute(:number_of_root_chat_messages,
number_of_root_chat_messages(messageable_type, messageable_id)
)
end

def number_of_root_chat_messages messageable_type, messageable_id
ChatMessage
.where(messageable_type: messageable_type, messageable_id: messageable_id, ancestry: nil)
.where.not(status: :deleted)
.count
end

def self.perform_later messageable_type, messageable_id
CountChatMessageJob.perform_async(messageable_type, messageable_id)
end
end
2 changes: 1 addition & 1 deletion app/models/entourage.rb
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ class Entourage < ApplicationRecord
joins(:moderation).where(entourage_moderations: { action_outcome: EntourageModeration::SUCCESSFUL_VALUES })
}

scope :with_chat_messages, -> { joins(:chat_messages).distinct }
scope :with_chat_messages, -> { where("number_of_root_chat_messages > 0").distinct }

attribute :preload_performed, :boolean, default: false
attribute :preload_landscape_url, :string, default: nil
Expand Down
9 changes: 9 additions & 0 deletions app/models/join_request.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,15 @@ def set_chat_messages_as_read
update_column(:unread_messages_count, 0)
end

def set_chat_messages_as_unread
update_column(:last_message_read, nil)
update_column(:unread_messages_count, joinable.chat_messages
.where(status: [:active, :updated])
.where(ancestry: nil)
.count
)
end

def set_chat_messages_as_read_from datetime
update_column(:last_message_read, datetime)
update_column(:unread_messages_count, joinable.chat_messages
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
class UnreadChatMessageObserver < ActiveRecord::Observer
class DenormChatMessageObserver < ActiveRecord::Observer
observe :chat_message

def after_commit record
return unless record.is_a?(ChatMessage)
return unless record.messageable
return unless commit_is?(record, [:create])
return unless commit_is?(record, [:create]) || record.saved_change_to_status?

UnreadChatMessageJob.perform_later(record.messageable_type, record.messageable_id)
CountChatMessageJob.perform_later(record.messageable_type, record.messageable_id)
end

private
Expand Down
2 changes: 1 addition & 1 deletion config/application.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ class Application < Rails::Application

config.active_job.queue_adapter = :sidekiq

config.active_record.observers = [:entourage_denorm_observer, :user_block_observer, :join_request_observer, :push_notification_trigger_observer, :translation_observer, :unread_chat_message_observer]
config.active_record.observers = [:entourage_denorm_observer, :user_block_observer, :join_request_observer, :push_notification_trigger_observer, :translation_observer, :denorm_chat_message_observer]

Rails.application.routes.default_url_options[:host] = ENV["HOST"]
config.action_mailer.default_url_options = { :host => ENV["HOST"] }
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
class AddNumberOfRootChatMessagesToMessageables < ActiveRecord::Migration[6.1]
def change
add_column :entourages, :number_of_root_chat_messages, :integer, default: 0
add_column :neighborhoods, :number_of_root_chat_messages, :integer, default: 0
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
class InitializeNumberOfRootChatMessages < ActiveRecord::Migration[6.1]
def up
# entourages
execute <<-SQL.squish
UPDATE entourages
SET number_of_root_chat_messages = (
SELECT COUNT(*)
FROM chat_messages
WHERE chat_messages.messageable_id = entourages.id
AND chat_messages.messageable_type = 'Entourage'
AND chat_messages.ancestry IS NULL
AND chat_messages.status != 'deleted'
)
SQL

# neighborhoods
execute <<-SQL.squish
UPDATE neighborhoods
SET number_of_root_chat_messages = (
SELECT COUNT(*)
FROM chat_messages
WHERE chat_messages.messageable_id = neighborhoods.id
AND chat_messages.messageable_type = 'Neighborhood'
AND chat_messages.ancestry IS NULL
AND chat_messages.status != 'deleted'
)
SQL
end

def down
Entourage.update_all(number_of_root_chat_messages: nil)
Neighborhood.update_all(number_of_root_chat_messages: nil)
end
end
1 change: 1 addition & 0 deletions db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -348,6 +348,7 @@
t.datetime "working_hours_sent_at"
t.integer "number_of_confirmed_people", default: 0
t.boolean "auto_post_at_create", default: false
t.integer "number_of_root_chat_messages", default: 0
t.index "((metadata ->> 'ends_at'::text)), ((metadata ->> 'starts_at'::text))", name: "entourages_metadata_idx"
t.index "((metadata ->> 'ends_at'::text)), ((metadata ->> 'starts_at'::text))", name: "index_entourages_metadata_dates"
t.index "st_setsrid(st_makepoint(longitude, latitude), 4326)", name: "index_entourages_on_coordinates", using: :gist
Expand Down

0 comments on commit d61f726

Please sign in to comment.