Perfectly synced all things should be.

This commit is contained in:
Sergiotarxz 2024-05-23 17:12:11 +02:00
parent 8d685edec1
commit 998885b2a1
1 changed files with 77 additions and 52 deletions

View File

@ -30,7 +30,7 @@ sub run ($self) {
$self->_dispatch_updates;
my $promise_dispatch;
while (1) {
if (defined $promise_dispatch) {
if ( defined $promise_dispatch ) {
$promise_dispatch->wait;
}
$promise_dispatch = $self->_dispatch_updates;
@ -39,31 +39,53 @@ sub run ($self) {
sub _dispatch_updates ($self) {
my $updates_p = $self->_get_updates;
my @promises;
my $promise = Mojo::Promise->new;
$updates_p->then(
sub ($res) {
my $updates = $res->result->json->{result};
for my $update (@$updates) {
push @promises, $self->_dispatch_update($update);
}
$self->_dispatch_updates_one_to_one($promise, $updates);
}
)->catch(
sub ($err) {
$promise->resolve;
warn $err;
}
);
return Mojo::Promise->all($updates_p, @promises);;
return $promise;
}
sub _dispatch_updates_one_to_one($self, $promise, $updates) {
my $update = shift @$updates;
if (!defined $update) {
$promise->resolve;
return;
}
$self->_dispatch_update($update)->then(sub {
$self->_dispatch_updates_one_to_one($promise, $updates);
})->catch(sub($err){
$self->_dispatch_updates_one_to_one($promise, $updates);
warn $err;
});
}
sub _dispatch_update ( $self, $update ) {
my $promise = Mojo::Promise->new;;
{
if ( !defined $self->_last_offset_update
|| $self->_last_offset_update < $update->{update_id} )
{
$self->_last_offset_update( $update->{update_id} );
}
my $promise;
if ( defined $update->{message} ) {
$promise = $self->_handle_message( $update->{message} );
if ( !defined $update->{message} ) {
$promise->resolve;
next;
}
$self->_handle_message( $update->{message} )->then(sub {
$promise->resolve;
})->catch(sub {
$promise->resolve;
});
}
return $promise;
}
@ -80,21 +102,22 @@ sub _handle_message ( $self, $message ) {
my $chat_type = $message->{chat}{type};
if ( $chat_type ne 'private' ) {
$promise->resolve;
return;
next;
}
my $chat_id = $message->{chat}{id};
my $text = $message->{text};
if ( !defined $text ) {
$promise->resolve;
return;
next;
}
$self->sendMessage( $chat_id,
'Got your message, attempting to generate a pdf.' )->wait;
'Got your message, attempting to generate a pdf.' )->then(sub {
my $pdf_builder = TgMagicPdf::PdfBuilder->new;
$pdf_builder->from_text($text)->then(
sub ($pdf) {
$self->sendDocument( $chat_id, 'mtgprint.pdf', $pdf )->wait;
$self->sendDocument( $chat_id, 'mtgprint.pdf', $pdf )->then(sub {;
$promise->resolve;
});
}
)->catch(
sub ($err) {
@ -121,15 +144,17 @@ sub _handle_message ( $self, $message ) {
$error_to_user .= ' ' . $pdf_builder->last_invalid_card;
}
$self->sendMessage( $chat_id,
'I could not process your deck take a look to this details: '
'I could not process your deck take a look to this details: '
. $error_to_user )->wait;
return;
}
warn $err;
$self->sendMessage( $chat_id,
'I could not process your deck because of a server error' )->wait;
'I could not process your deck because of a server error' )
->wait;
}
);
});
}
return $promise;
}