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 2 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
23 changes: 8 additions & 15 deletions lib/threema/receive/delivery_receipt.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
class Threema
module Receive
class DeliveryReceipt
STATUS_TYPE_BYTE = {
STATUS_BYTE_MAPPING = {
received: "\x01".b,
read: "\x02".b,
explicitly_acknowledged: "\x03".b,
Expand All @@ -17,30 +17,23 @@ class DeliveryReceipt

def initialize(content:, **)
@content = content
return unless type_by(content).present?

@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)
@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_TYPE_BYTE.key(content.slice(0))
STATUS_BYTE_MAPPING.key(content.slice(0))
end

def extract_message_ids(content)
type_removed = content.slice(1, content.length - 1)
num_message_ids = content.length / UNHEXIFIED_MESSAGE_ID_LENGTH

message_ids = []
num_message_ids.times do |index|
start_index = index.zero? ? index : (index * UNHEXIFIED_MESSAGE_ID_LENGTH)
end_index = (index + 1) * UNHEXIFIED_MESSAGE_ID_LENGTH - 1
message_ids << Threema::Util.hexify(type_removed[start_index..end_index])
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
message_ids
end
end
end
Expand Down
62 changes: 29 additions & 33 deletions spec/threema/receive/delivery_receipt_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,63 +7,59 @@
let(:attributes) { attributes_for(:delivery_receipt_receive) }
mattwr18 marked this conversation as resolved.
Show resolved Hide resolved

context '#content' do
it { is_expected.to respond_to(:content) }

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

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

it 'returns an array of message ids' do
expect(subject.message_ids).to be_a(Array)
end

it 'with each message id' do
expect(subject.message_ids.length).to eq(3)
end

it 'with correct length' do
expect(subject.message_ids.all? { |string| string.length.eql?(16) }).to be(true)
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
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 'received type' do
it 'returns status received' do
expect(subject.status).to eq(:received)
context '#status' do
context 'received type' do
it 'returns status received' do
expect(subject.status).to eq(:received)
end
end
end

context 'read type' do
let(:attributes) { { content: "\x02\xE0\xC8\x12n8]\xF3\x19".b } }
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)
it 'returns status read' do
expect(subject.status).to eq(:read)
end
end
end

context 'explicitly_acknowledged type' do
let(:attributes) { { content: "\x03\xE0\xC8\x12n8]\xF3\x19".b } }
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
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 'explicity_declined type' do
let(:attributes) { { content: "\x04\xE0\xC8\x12n8]\xF3\x19".b } }
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)
it 'returns status explicity_declined' do
expect(subject.status).to eq(:explicity_declined)
end
end
end
end
end