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; $self->_dispatch_updates;
my $promise_dispatch; my $promise_dispatch;
while (1) { while (1) {
if (defined $promise_dispatch) { if ( defined $promise_dispatch ) {
$promise_dispatch->wait; $promise_dispatch->wait;
} }
$promise_dispatch = $self->_dispatch_updates; $promise_dispatch = $self->_dispatch_updates;
@ -39,31 +39,53 @@ sub run ($self) {
sub _dispatch_updates ($self) { sub _dispatch_updates ($self) {
my $updates_p = $self->_get_updates; my $updates_p = $self->_get_updates;
my @promises; my $promise = Mojo::Promise->new;
$updates_p->then( $updates_p->then(
sub ($res) { sub ($res) {
my $updates = $res->result->json->{result}; my $updates = $res->result->json->{result};
for my $update (@$updates) { $self->_dispatch_updates_one_to_one($promise, $updates);
push @promises, $self->_dispatch_update($update);
}
} }
)->catch( )->catch(
sub ($err) { sub ($err) {
$promise->resolve;
warn $err; warn $err;
} }
); );
return Mojo::Promise->all($updates_p, @promises);; return $promise;
} }
sub _dispatch_update ( $self, $update ) { sub _dispatch_updates_one_to_one($self, $promise, $updates) {
if ( !defined $self->_last_offset_update my $update = shift @$updates;
|| $self->_last_offset_update < $update->{update_id} ) if (!defined $update) {
{ $promise->resolve;
$self->_last_offset_update( $update->{update_id} ); return;
} }
my $promise; $self->_dispatch_update($update)->then(sub {
if ( defined $update->{message} ) { $self->_dispatch_updates_one_to_one($promise, $updates);
$promise = $self->_handle_message( $update->{message} ); })->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} );
}
if ( !defined $update->{message} ) {
$promise->resolve;
next;
}
$self->_handle_message( $update->{message} )->then(sub {
$promise->resolve;
})->catch(sub {
$promise->resolve;
});
} }
return $promise; return $promise;
} }
@ -80,56 +102,59 @@ sub _handle_message ( $self, $message ) {
my $chat_type = $message->{chat}{type}; my $chat_type = $message->{chat}{type};
if ( $chat_type ne 'private' ) { if ( $chat_type ne 'private' ) {
$promise->resolve; $promise->resolve;
return; next;
} }
my $chat_id = $message->{chat}{id}; my $chat_id = $message->{chat}{id};
my $text = $message->{text}; my $text = $message->{text};
if ( !defined $text ) { if ( !defined $text ) {
$promise->resolve; $promise->resolve;
return; next;
} }
$self->sendMessage( $chat_id, $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; my $pdf_builder = TgMagicPdf::PdfBuilder->new;
$pdf_builder->from_text($text)->then( $pdf_builder->from_text($text)->then(
sub ($pdf) { sub ($pdf) {
$self->sendDocument( $chat_id, 'mtgprint.pdf', $pdf )->wait; $self->sendDocument( $chat_id, 'mtgprint.pdf', $pdf )->then(sub {;
$promise->resolve; $promise->resolve;
} });
)->catch(
sub ($err) {
$promise->resolve;
my $match = 0;
for my $known_error (
$TgMagicPdf::PdfBuilder::ERR_INVALID_CARD,
$TgMagicPdf::PdfBuilder::ERR_UNABLE_TO_FIND_IMAGE,
$TgMagicPdf::PdfBuilder::ERR_TOO_MANY_CARDS
)
{
$match = 1 if ( index( $err, $known_error ) != -1 );
} }
if ($match) { )->catch(
my $error_to_user .= $err =~ s/ at.*$//r; sub ($err) {
if ( -1 != index $err, $promise->resolve;
$TgMagicPdf::PdfBuilder::ERR_INVALID_CARD ) my $match = 0;
for my $known_error (
$TgMagicPdf::PdfBuilder::ERR_INVALID_CARD,
$TgMagicPdf::PdfBuilder::ERR_UNABLE_TO_FIND_IMAGE,
$TgMagicPdf::PdfBuilder::ERR_TOO_MANY_CARDS
)
{ {
$error_to_user .= ' ' . $pdf_builder->last_invalid_card; $match = 1 if ( index( $err, $known_error ) != -1 );
} }
if ( -1 != index $err, if ($match) {
$TgMagicPdf::PdfBuilder::ERR_UNABLE_TO_FIND_IMAGE ) my $error_to_user .= $err =~ s/ at.*$//r;
{ if ( -1 != index $err,
$error_to_user .= ' ' . $pdf_builder->last_invalid_card; $TgMagicPdf::PdfBuilder::ERR_INVALID_CARD )
{
$error_to_user .= ' ' . $pdf_builder->last_invalid_card;
}
if ( -1 != index $err,
$TgMagicPdf::PdfBuilder::ERR_UNABLE_TO_FIND_IMAGE )
{
$error_to_user .= ' ' . $pdf_builder->last_invalid_card;
}
$self->sendMessage( $chat_id,
'I could not process your deck take a look to this details: '
. $error_to_user )->wait;
return;
} }
warn $err;
$self->sendMessage( $chat_id, $self->sendMessage( $chat_id,
'I could not process your deck take a look to this details: ' 'I could not process your deck because of a server error' )
. $error_to_user )->wait; ->wait;
return;
} }
warn $err; );
$self->sendMessage( $chat_id, });
'I could not process your deck because of a server error' )->wait;
}
);
} }
return $promise; return $promise;
} }
@ -157,7 +182,7 @@ sub _pdf_builder {
sub _build__ua ($self) { sub _build__ua ($self) {
my $ua = Mojo::UserAgent->new->with_roles('+Queued'); my $ua = Mojo::UserAgent->new->with_roles('+Queued');
$ua->max_active(5); $ua->max_active(5);
$ua->inactivity_timeout(60); $ua->inactivity_timeout(60);
return $ua; return $ua;
} }