Perfectly synced all things should be.
This commit is contained in:
parent
8d685edec1
commit
998885b2a1
|
@ -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_update ( $self, $update ) {
|
||||
if ( !defined $self->_last_offset_update
|
||||
|| $self->_last_offset_update < $update->{update_id} )
|
||||
{
|
||||
$self->_last_offset_update( $update->{update_id} );
|
||||
sub _dispatch_updates_one_to_one($self, $promise, $updates) {
|
||||
my $update = shift @$updates;
|
||||
if (!defined $update) {
|
||||
$promise->resolve;
|
||||
return;
|
||||
}
|
||||
my $promise;
|
||||
if ( defined $update->{message} ) {
|
||||
$promise = $self->_handle_message( $update->{message} );
|
||||
$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} );
|
||||
}
|
||||
if ( !defined $update->{message} ) {
|
||||
$promise->resolve;
|
||||
next;
|
||||
}
|
||||
$self->_handle_message( $update->{message} )->then(sub {
|
||||
$promise->resolve;
|
||||
})->catch(sub {
|
||||
$promise->resolve;
|
||||
});
|
||||
}
|
||||
return $promise;
|
||||
}
|
||||
|
@ -80,56 +102,59 @@ 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;
|
||||
my $pdf_builder = TgMagicPdf::PdfBuilder->new;
|
||||
$pdf_builder->from_text($text)->then(
|
||||
sub ($pdf) {
|
||||
$self->sendDocument( $chat_id, 'mtgprint.pdf', $pdf )->wait;
|
||||
$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 );
|
||||
'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 )->then(sub {;
|
||||
$promise->resolve;
|
||||
});
|
||||
}
|
||||
if ($match) {
|
||||
my $error_to_user .= $err =~ s/ at.*$//r;
|
||||
if ( -1 != index $err,
|
||||
$TgMagicPdf::PdfBuilder::ERR_INVALID_CARD )
|
||||
)->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
|
||||
)
|
||||
{
|
||||
$error_to_user .= ' ' . $pdf_builder->last_invalid_card;
|
||||
$match = 1 if ( index( $err, $known_error ) != -1 );
|
||||
}
|
||||
if ( -1 != index $err,
|
||||
$TgMagicPdf::PdfBuilder::ERR_UNABLE_TO_FIND_IMAGE )
|
||||
{
|
||||
$error_to_user .= ' ' . $pdf_builder->last_invalid_card;
|
||||
if ($match) {
|
||||
my $error_to_user .= $err =~ s/ at.*$//r;
|
||||
if ( -1 != index $err,
|
||||
$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,
|
||||
'I could not process your deck take a look to this details: '
|
||||
. $error_to_user )->wait;
|
||||
return;
|
||||
'I could not process your deck because of a server error' )
|
||||
->wait;
|
||||
}
|
||||
warn $err;
|
||||
$self->sendMessage( $chat_id,
|
||||
'I could not process your deck because of a server error' )->wait;
|
||||
}
|
||||
);
|
||||
);
|
||||
});
|
||||
}
|
||||
return $promise;
|
||||
}
|
||||
|
@ -157,7 +182,7 @@ sub _pdf_builder {
|
|||
|
||||
sub _build__ua ($self) {
|
||||
my $ua = Mojo::UserAgent->new->with_roles('+Queued');
|
||||
$ua->max_active(5);
|
||||
$ua->max_active(5);
|
||||
$ua->inactivity_timeout(60);
|
||||
return $ua;
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue