Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve Threema::Receive::DeliveryReceipt class #40

Merged
merged 6 commits into from
May 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
mattwr18 marked this conversation as resolved.
Show resolved Hide resolved
return unless @status

@timestamp = Time.now.utc.to_i
@message_ids = extract_message_ids(content.slice(1, content.length - 1))
mattwr18 marked this conversation as resolved.
Show resolved Hide resolved
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) }
mattwr18 marked this conversation as resolved.
Show resolved Hide resolved

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)
mattwr18 marked this conversation as resolved.
Show resolved Hide resolved
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
mattwr18 marked this conversation as resolved.
Show resolved Hide resolved
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