Skip to content

Commit

Permalink
Merge pull request #40 from threemarb/39_improve_delivery_receipt_class
Browse files Browse the repository at this point in the history
Improve Threema::Receive::DeliveryReceipt class
  • Loading branch information
mattwr18 authored May 9, 2024
2 parents 537dd1d + 66ac8a4 commit 40477ef
Show file tree
Hide file tree
Showing 3 changed files with 104 additions and 1 deletion.
29 changes: 28 additions & 1 deletion lib/threema/receive/delivery_receipt.rb
Original file line number Diff line number Diff line change
@@ -1,12 +1,39 @@
# frozen_string_literal: true

require 'threema/util'

class Threema
module Receive
class DeliveryReceipt
attr_reader :content
STATUS_BYTE_MAPPING = {
received: "\x01".b,
read: "\x02".b,
explicitly_acknowledged: "\x03".b,
explicity_declined: "\x04".b
}.freeze
UNHEXIFIED_MESSAGE_ID_LENGTH = 8

attr_reader :content, :status, :timestamp, :message_ids

def initialize(content:, **)
@content = content
@status = type_by(content)
return unless @status

@timestamp = Time.now.utc.to_i
@message_ids = extract_message_ids(content.slice(1, content.length - 1))
end

private

def type_by(content)
STATUS_BYTE_MAPPING.key(content.slice(0))
end

def extract_message_ids(message_ids_payload)
message_ids_payload.scan(/.{,#{UNHEXIFIED_MESSAGE_ID_LENGTH}}/).reject(&:empty?).map do |message_id|
Threema::Util.hexify(message_id)
end
end
end
end
Expand Down
11 changes: 11 additions & 0 deletions spec/factories/threema_delivery_receipt_receive.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# frozen_string_literal: true

FactoryBot.define do
factory :delivery_receipt_receive, class: Threema::Receive::DeliveryReceipt do
content { "\x01\xE0\xC8\x12n8]\xF3\x19".b }

initialize_with do
new(**attributes)
end
end
end
65 changes: 65 additions & 0 deletions spec/threema/receive/delivery_receipt_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe Threema::Receive::DeliveryReceipt do
subject { described_class.new(**attributes) }
let(:attributes) { attributes_for(:delivery_receipt_receive) }

context '#content' do
it 'returns the content' do
expect(subject.content).to eq(attributes[:content])
end
end

context '#message_ids' do
let(:attributes) { { content: "\x01\xE0\xC8\x12n8]\xF3\x19J\xB4+ \xC9\xAB|\xB6\xAAc#\v|Ro(".b } }

describe 'given a content string with three unhexified message ids' do
it 'returns the hexified message ids' do
expect(subject.message_ids).to eq(%w[e0c8126e385df319 4ab42b20c9ab7cb6 aa63230b7c526f28])
end
end
end

context '#timestamp' do
it 'returns a timestamp' do
expect(subject.timestamp).to be_an_instance_of(Integer)
expect(Time.at(subject.timestamp, in: 'UTC')).to be_an_instance_of(Time)
end
end

context '#status' do
context 'received type' do
it 'returns status received' do
expect(subject.status).to eq(:received)
end
end

context 'read type' do
let(:attributes) { { content: "\x02\xE0\xC8\x12n8]\xF3\x19".b } }

it 'returns status read' do
expect(subject.status).to eq(:read)
end
end

context 'the recipient explicitly reacts to the message by long pressing on it and' do
context 'acknowledges it by tapping on the thumbs up' do
let(:attributes) { { content: "\x03\xE0\xC8\x12n8]\xF3\x19".b } }

it 'returns status explicitly_acknowledged' do
expect(subject.status).to eq(:explicitly_acknowledged)
end
end

context 'declines it by tapping on the thumbs down' do
let(:attributes) { { content: "\x04\xE0\xC8\x12n8]\xF3\x19".b } }

it 'returns status explicity_declined' do
expect(subject.status).to eq(:explicity_declined)
end
end
end
end
end

0 comments on commit 40477ef

Please sign in to comment.