travelynx/lib/Travelynx/Controller/Traewelling.pm
Daniel Friesel 89e709d8d5 Allow linking a Träwelling account, auto-sync Träwelling→travelynx
travelynx→Träwelling is still work-in-progress

Squashed commit of the following:

commit 97faa6e2e6c8d20fba30f2d0f6e78187ceeb72e6
Author: Daniel Friesel <derf@finalrewind.org>
Date:   Wed Sep 30 18:50:05 2020 +0200

    improve traewelling log and tx handling

commit 487d7dd728b9d45b731bdc7098cf3358ea2e206e
Author: Daniel Friesel <derf@finalrewind.org>
Date:   Wed Sep 30 18:02:41 2020 +0200

    add missing traewelling template

commit 0148da2f48d9a52dcddc0ab81f83d8f8ac3062ab
Author: Daniel Friesel <derf@finalrewind.org>
Date:   Wed Sep 30 18:02:35 2020 +0200

    improve traewelling pull sync

commit 4861a9750f9f2d7621043361d0af6b0a8869a0df
Author: Daniel Friesel <derf@finalrewind.org>
Date:   Tue Sep 29 22:14:24 2020 +0200

    wip checkin from traewelling

commit f6aeb6f06998a2a7a80f63a7b1b688b1a26b66bd
Author: Daniel Friesel <derf@finalrewind.org>
Date:   Tue Sep 29 18:37:53 2020 +0200

    refactor traewelling integration. login and logout are less of a hack now.

    checkin and checkout are not supported at the moment.
2020-09-30 19:12:29 +02:00

104 lines
2.3 KiB
Perl

package Travelynx::Controller::Traewelling;
use Mojo::Base 'Mojolicious::Controller';
use Mojo::Promise;
sub settings {
my ($self) = @_;
my $uid = $self->current_user->{id};
if ( $self->param('action')
and $self->validation->csrf_protect->has_error('csrf_token') )
{
$self->render(
'traewelling',
invalid => 'csrf',
);
return;
}
if ( $self->param('action') and $self->param('action') eq 'login' ) {
my $email = $self->param('email');
my $password = $self->param('password');
$self->render_later;
$self->traewelling_api->login_p(
uid => $uid,
email => $email,
password => $password
)->then(
sub {
my $traewelling = $self->traewelling->get($uid);
$self->param( sync_source => 'none' );
$self->render(
'traewelling',
traewelling => $traewelling,
new_traewelling => 1,
);
}
)->catch(
sub {
my ($err) = @_;
$self->render(
'traewelling',
traewelling => {},
new_traewelling => 1,
login_error => $err,
);
}
)->wait;
return;
}
elsif ( $self->param('action') and $self->param('action') eq 'logout' ) {
$self->render_later;
my $traewelling = $self->traewelling->get($uid);
$self->traewelling_api->logout_p(
uid => $uid,
token => $traewelling->{token}
)->then(
sub {
$self->flash( success => 'traewelling' );
$self->redirect_to('account');
}
)->catch(
sub {
my ($err) = @_;
$self->render(
'traewelling',
traewelling => {},
new_traewelling => 1,
logout_error => $err,
);
}
)->wait;
return;
}
elsif ( $self->param('action') and $self->param('action') eq 'config' ) {
$self->traewelling->set_sync(
uid => $uid,
push_sync => $self->param('sync_source') eq 'travelynx' ? 1 : 0,
pull_sync => $self->param('sync_source') eq 'traewelling' ? 1 : 0
);
$self->flash( success => 'traewelling' );
$self->redirect_to('account');
return;
}
my $traewelling = $self->traewelling->get($uid);
if ( $traewelling->{push_sync} ) {
$self->param( sync_source => 'travelynx' );
}
elsif ( $traewelling->{pull_sync} ) {
$self->param( sync_source => 'traewelling' );
}
else {
$self->param( sync_source => 'none' );
}
$self->render(
'traewelling',
traewelling => $traewelling,
);
}
1;