From 2065720088969460354cf7d87d4f6bd140a9a77e Mon Sep 17 00:00:00 2001 From: Joshua Date: Fri, 17 Mar 2023 01:27:05 +0700 Subject: [PATCH 1/6] add all_recipients method on chunk class --- lib/push/chunk.rb | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/lib/push/chunk.rb b/lib/push/chunk.rb index 8fa5f22..617e135 100644 --- a/lib/push/chunk.rb +++ b/lib/push/chunk.rb @@ -49,6 +49,10 @@ def as_json notifications.map(&:as_json) end + def all_recipients + notifications.flat_map(&:recipients) + end + private attr_accessor :notifications From cc9ba53444c9a6a6fbd82507e2b969d0075d7e83 Mon Sep 17 00:00:00 2001 From: Joshua Date: Fri, 17 Mar 2023 01:28:09 +0700 Subject: [PATCH 2/6] add get token on the ticket that not error --- lib/push/tickets.rb | 22 ++++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/lib/push/tickets.rb b/lib/push/tickets.rb index 02394d1..ee3eddf 100644 --- a/lib/push/tickets.rb +++ b/lib/push/tickets.rb @@ -12,10 +12,11 @@ module Push # valid. This is exposed via #original_push_token. # class Ticket - attr_reader :data + attr_reader :data, :token - def initialize(data) + def initialize(data, token) self.data = data + self.token = token end def id @@ -23,13 +24,7 @@ def id end def original_push_token - return nil if ok? - - if message.include?('PushToken[') - return /Expo(?:nent)?PushToken\[(?:[^\]]+?)\]/.match(message) { |match| match[0] } - end - - /\A[a-z\d]{8}-[a-z\d]{4}-[a-z\d]{4}-[a-z\d]{4}-[a-z\d]{12}\z/i.match(message) { |match| match[0] } + token end def message @@ -50,7 +45,7 @@ def error? private - attr_writer :data + attr_writer :data, :token end ## @@ -80,6 +75,13 @@ def ids end end + def tokens_by_receipt_id_hash + tokens_by_receipt_id = Hash.new { |hash, key| hash[key] = [] } + tokens_by_receipt_id.tap do |hash| + each { |ticket| hash[ticket.id] = ticket.original_push_token } + end + end + def batch_ids ids.each_slice(PUSH_NOTIFICATION_RECEIPT_CHUNK_LIMIT).to_a end From 1b11caa7f0dac700516e21639bf4692cb67e53e8 Mon Sep 17 00:00:00 2001 From: Joshua Date: Fri, 17 Mar 2023 01:28:21 +0700 Subject: [PATCH 3/6] Add token when creating a new ticket --- lib/push/client.rb | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/lib/push/client.rb b/lib/push/client.rb index 9eba722..ca70f10 100644 --- a/lib/push/client.rb +++ b/lib/push/client.rb @@ -183,6 +183,7 @@ def send(notifications) threads = Chunk.for(notifications).map do |chunk| expected_count = chunk.count + tokens = chunk.all_recipients Thread.new do pool.with do |http| @@ -197,7 +198,10 @@ def send(notifications) elsif !data.is_a?(Array) || data.length != expected_count TicketsExpectationFailed.new(expected_count: expected_count, data: data) else - data.map { |ticket| Ticket.new(ticket) } + data.map do |ticket| + currentTicketToken = tokens.shift(1)[0] + Ticket.new(ticket, currentTicketToken) + end end end end From f896f694f70bb375d00bb00e69b61baae462180c Mon Sep 17 00:00:00 2001 From: Joshua Date: Tue, 30 May 2023 01:00:34 +0700 Subject: [PATCH 4/6] Adjust function name --- lib/push/tickets.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/push/tickets.rb b/lib/push/tickets.rb index ee3eddf..ea8db5b 100644 --- a/lib/push/tickets.rb +++ b/lib/push/tickets.rb @@ -75,8 +75,8 @@ def ids end end - def tokens_by_receipt_id_hash - tokens_by_receipt_id = Hash.new { |hash, key| hash[key] = [] } + def token_by_receipt_id + tokens_by_receipt_id = {} tokens_by_receipt_id.tap do |hash| each { |ticket| hash[ticket.id] = ticket.original_push_token } end From 1363e40e59c188bba855bc9b6e32721f39e6e08a Mon Sep 17 00:00:00 2001 From: Joshua Date: Tue, 30 May 2023 01:00:45 +0700 Subject: [PATCH 5/6] Fix naming convention --- lib/push/client.rb | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/lib/push/client.rb b/lib/push/client.rb index ca70f10..cd4f03f 100644 --- a/lib/push/client.rb +++ b/lib/push/client.rb @@ -184,7 +184,6 @@ def send(notifications) threads = Chunk.for(notifications).map do |chunk| expected_count = chunk.count tokens = chunk.all_recipients - Thread.new do pool.with do |http| response = http.post(PUSH_API_URL, json: chunk.as_json) @@ -199,8 +198,8 @@ def send(notifications) TicketsExpectationFailed.new(expected_count: expected_count, data: data) else data.map do |ticket| - currentTicketToken = tokens.shift(1)[0] - Ticket.new(ticket, currentTicketToken) + current_ticket_token = tokens.shift(1)[0] + Ticket.new(ticket, current_ticket_token) end end end From 181a04d4c09a12e516d175c1cee468d57e692cd2 Mon Sep 17 00:00:00 2001 From: Joshua Date: Tue, 30 May 2023 01:00:59 +0700 Subject: [PATCH 6/6] Expose error_message from receipts --- lib/push/receipts.rb | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/lib/push/receipts.rb b/lib/push/receipts.rb index 63334a9..bd28db6 100644 --- a/lib/push/receipts.rb +++ b/lib/push/receipts.rb @@ -33,6 +33,10 @@ def message data.fetch('message') end + def error_message + data.fetch('details').fetch('error') + end + def explain Expo::Push::Error.explain((data['details'] || {})['error']) end