Skip to content

Commit

Permalink
Add read_at to messages, fix datetime logic
Browse files Browse the repository at this point in the history
- Signal sends 13 digit timestamps(milliseconds), which need to be
  converted to seconds before converting to a datetime in ruby otherwise
the datetime is not accurate
  • Loading branch information
mattwr18 committed Oct 24, 2023
1 parent 59b9d99 commit 9d91c1f
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 4 deletions.
11 changes: 8 additions & 3 deletions app/jobs/signal_adapter/receive_polling_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,7 @@ def perform(*_args)
end

adapter.on(SignalAdapter::HANDLE_DELIVERY_RECEIPT) do |delivery_receipt, contributor|
if delivery_receipt[:isDelivery]
contributor.received_messages.first.update(received_at: Time.zone.at(delivery_receipt[:when]).to_datetime)
end
handle_delivery_receipt(delivery_receipt, contributor)
end

signal_messages.each do |raw_message|
Expand Down Expand Up @@ -70,5 +68,12 @@ def handle_connect(contributor)
SignalAdapter::Outbound.send_welcome_message!(contributor)
SignalAdapter::AttachContributorsAvatarJob.perform_later(contributor)
end

def handle_delivery_receipt(delivery_receipt, contributor)
datetime = Time.zone.at(delivery_receipt[:when] / 1000).to_datetime
latest_received_message = contributor.received_messages.first
latest_received_message.update(received_at: datetime) if delivery_receipt[:isDelivery]
latest_received_message.update(read_at: datetime) if delivery_receipt[:isRead]
end
end
end
7 changes: 7 additions & 0 deletions db/migrate/20231024144124_add_read_at_to_messages.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# frozen_string_literal: true

class AddReadAtToMessages < ActiveRecord::Migration[6.1]
def change
add_column :messages, :read_at, :datetime, default: nil
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: 2023_09_15_070544) do
ActiveRecord::Schema.define(version: 2023_10_24_144124) do

# These are extensions that must be enabled in order to support this database
enable_extension "pg_trgm"
Expand Down Expand Up @@ -170,6 +170,7 @@
t.bigint "creator_id"
t.string "sender_type"
t.datetime "received_at"
t.datetime "read_at"
t.index ["creator_id"], name: "index_messages_on_creator_id"
t.index ["recipient_id"], name: "index_messages_on_recipient_id"
t.index ["request_id"], name: "index_messages_on_request_id"
Expand Down

0 comments on commit 9d91c1f

Please sign in to comment.