Skip to content

Commit

Permalink
fix: Use last_activity_at instead of updated_at for sorting (#1281)
Browse files Browse the repository at this point in the history
Co-authored-by: Akash Srivastava <[email protected]>
  • Loading branch information
Pranav Raj S and akashdotsrivastava authored Oct 5, 2020
1 parent ee119b2 commit 399f9e0
Show file tree
Hide file tree
Showing 11 changed files with 56 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ export const mutations = {
);
if (previousMessageIndex === -1) {
chat.messages.push(message);
chat.timestamp = message.created_at;
if (_state.selectedChatId === message.conversation_id) {
window.bus.$emit('scrollToMessage');
}
Expand Down
3 changes: 2 additions & 1 deletion app/models/conversation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
# agent_last_seen_at :datetime
# contact_last_seen_at :datetime
# identifier :string
# last_activity_at :datetime not null
# locked :boolean default(FALSE)
# status :integer default("open"), not null
# uuid :uuid not null
Expand Down Expand Up @@ -36,7 +37,7 @@ class Conversation < ApplicationRecord

enum status: { open: 0, resolved: 1, bot: 2 }

scope :latest, -> { order(updated_at: :desc) }
scope :latest, -> { order(last_activity_at: :desc) }
scope :unassigned, -> { where(assignee_id: nil) }
scope :assigned_to, ->(agent) { where(assignee_id: agent.id) }

Expand Down
7 changes: 7 additions & 0 deletions app/models/message.rb
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ def webhook_data
def execute_after_create_commit_callbacks
# rails issue with order of active record callbacks being executed
# https://github.com/rails/rails/issues/20911
set_conversation_activity
dispatch_create_events
send_reply
execute_message_template_hooks
Expand Down Expand Up @@ -191,4 +192,10 @@ def conversation_mail_key
def validate_attachments_limit(_attachment)
errors.add(attachments: 'exceeded maximum allowed') if attachments.size >= NUMBER_OF_PERMITTED_ATTACHMENTS
end

def set_conversation_activity
# rubocop:disable Rails/SkipsModelValidations
conversation.update_columns(last_activity_at: created_at)
# rubocop:enable Rails/SkipsModelValidations
end
end
2 changes: 1 addition & 1 deletion app/presenters/conversations/event_data_presenter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ def push_timestamps
{
agent_last_seen_at: agent_last_seen_at.to_i,
contact_last_seen_at: contact_last_seen_at.to_i,
timestamp: created_at.to_i
timestamp: last_activity_at.to_i
}
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ json.data do
json.status conversation.status
json.muted conversation.muted?
json.can_reply conversation.can_reply?
json.timestamp conversation.messages.last.try(:created_at).try(:to_i)
json.timestamp conversation.last_activity_at.to_i
json.contact_last_seen_at conversation.contact_last_seen_at.to_i
json.agent_last_seen_at conversation.agent_last_seen_at.to_i
json.additional_attributes conversation.additional_attributes
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ json.inbox_id conversation.inbox_id
json.status conversation.status
json.muted conversation.muted?
json.can_reply conversation.can_reply?
json.timestamp conversation.messages.last.try(:created_at).try(:to_i)
json.timestamp conversation.last_activity_at.to_i
json.contact_last_seen_at conversation.contact_last_seen_at.to_i
json.agent_last_seen_at conversation.agent_last_seen_at.to_i
json.unread_count conversation.unread_incoming_messages.count
Expand Down
35 changes: 35 additions & 0 deletions db/migrate/20200927135222_add_last_activity_at_to_conversation.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
class AddLastActivityAtToConversation < ActiveRecord::Migration[6.0]
def up
add_column :conversations,
:last_activity_at,
:datetime,
default: -> { 'CURRENT_TIMESTAMP' },
index: true

add_last_activity_at_to_conversations

change_column_null(:conversations, :last_activity_at, false)
end

def down
remove_column(:conversations, :last_activity_at)
end

private

def add_last_activity_at_to_conversations
::Conversation.find_in_batches do |conversation_batch|
Rails.logger.info "Migrated till #{conversation_batch.first.id}\n"
conversation_batch.each do |conversation|
# rubocop:disable Rails/SkipsModelValidations
last_activity_at = if conversation.messages.last
conversation.messages.last.created_at
else
conversation.created_at
end
conversation.update_columns(last_activity_at: last_activity_at)
# rubocop:enable Rails/SkipsModelValidations
end
end
end
end
3 changes: 2 additions & 1 deletion db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema.define(version: 2020_09_07_094912) do
ActiveRecord::Schema.define(version: 2020_09_27_135222) do

# These are extensions that must be enabled in order to support this database
enable_extension "pgcrypto"
Expand Down Expand Up @@ -226,6 +226,7 @@
t.bigint "contact_inbox_id"
t.uuid "uuid", default: -> { "gen_random_uuid()" }, null: false
t.string "identifier"
t.datetime "last_activity_at", default: -> { "CURRENT_TIMESTAMP" }, null: false
t.index ["account_id", "display_id"], name: "index_conversations_on_account_id_and_display_id", unique: true
t.index ["account_id"], name: "index_conversations_on_account_id"
t.index ["contact_inbox_id"], name: "index_conversations_on_contact_inbox_id"
Expand Down
2 changes: 1 addition & 1 deletion spec/models/conversation_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -370,7 +370,7 @@
messages: [],
inbox_id: conversation.inbox_id,
status: conversation.status,
timestamp: conversation.created_at.to_i,
timestamp: conversation.last_activity_at.to_i,
can_reply: true,
channel: 'Channel::WebWidget',
contact_last_seen_at: conversation.contact_last_seen_at.to_i,
Expand Down
4 changes: 4 additions & 0 deletions spec/models/message_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@
context 'when message is created' do
let(:message) { build(:message, account: create(:account)) }

it 'updates conversation last_activity_at when created' do
expect(message.created_at).to eq message.conversation.last_activity_at
end

it 'triggers ::MessageTemplates::HookExecutionService' do
hook_execution_service = double
allow(::MessageTemplates::HookExecutionService).to receive(:new).and_return(hook_execution_service)
Expand Down
2 changes: 1 addition & 1 deletion spec/presenters/conversations/event_data_presenter_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
status: conversation.status,
can_reply: conversation.can_reply?,
channel: conversation.inbox.channel_type,
timestamp: conversation.created_at.to_i,
timestamp: conversation.last_activity_at.to_i,
contact_last_seen_at: conversation.contact_last_seen_at.to_i,
agent_last_seen_at: conversation.agent_last_seen_at.to_i,
unread_count: 0
Expand Down

0 comments on commit 399f9e0

Please sign in to comment.