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 4 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
36 changes: 35 additions & 1 deletion lib/threema/receive/delivery_receipt.rb
Original file line number Diff line number Diff line change
@@ -1,12 +1,46 @@
# frozen_string_literal: true

require 'threema/util'

class Threema
module Receive
class DeliveryReceipt
attr_reader :content
STATUS_TYPE_BYTE = {
mattwr18 marked this conversation as resolved.
Show resolved Hide resolved
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
return unless type_by(content).present?

@status = type_by(content)
mattwr18 marked this conversation as resolved.
Show resolved Hide resolved
mattwr18 marked this conversation as resolved.
Show resolved Hide resolved
@timestamp = Time.now.utc.to_i
@message_ids = extract_message_ids(content)
end

private

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

def extract_message_ids(content)
mattwr18 marked this conversation as resolved.
Show resolved Hide resolved
type_removed = content.slice(1, content.length - 1)
mattwr18 marked this conversation as resolved.
Show resolved Hide resolved
num_message_ids = content.length / UNHEXIFIED_MESSAGE_ID_LENGTH

message_ids = []
num_message_ids.times do |index|
mattwr18 marked this conversation as resolved.
Show resolved Hide resolved
start_index = index.zero? ? index : (index * UNHEXIFIED_MESSAGE_ID_LENGTH)
mattwr18 marked this conversation as resolved.
Show resolved Hide resolved
end_index = (index + 1) * UNHEXIFIED_MESSAGE_ID_LENGTH - 1
message_ids << Threema::Util.hexify(type_removed[start_index..end_index])
end
message_ids
mattwr18 marked this conversation as resolved.
Show resolved Hide resolved
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
69 changes: 69 additions & 0 deletions spec/threema/receive/delivery_receipt_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
# 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 { is_expected.to respond_to(:content) }

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 } }

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)
end
mattwr18 marked this conversation as resolved.
Show resolved Hide resolved
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 '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 'explicitly_acknowledged type' 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 'explicity_declined type' do
mattwr18 marked this conversation as resolved.
Show resolved Hide resolved
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