2020-09-30 17:12:29 +00:00
|
|
|
|
package Travelynx::Controller::Traewelling;
|
2022-02-16 19:56:28 +00:00
|
|
|
|
|
2023-07-03 15:59:25 +00:00
|
|
|
|
# Copyright (C) 2020-2023 Birte Kristina Friesel
|
2020-11-27 21:12:56 +00:00
|
|
|
|
#
|
2021-01-29 17:32:13 +00:00
|
|
|
|
# SPDX-License-Identifier: AGPL-3.0-or-later
|
2020-09-30 17:12:29 +00:00
|
|
|
|
use Mojo::Base 'Mojolicious::Controller';
|
|
|
|
|
use Mojo::Promise;
|
|
|
|
|
|
2023-08-07 19:17:10 +00:00
|
|
|
|
sub oauth {
|
2020-09-30 17:12:29 +00:00
|
|
|
|
my ($self) = @_;
|
|
|
|
|
|
|
|
|
|
if ( $self->param('action')
|
|
|
|
|
and $self->validation->csrf_protect->has_error('csrf_token') )
|
|
|
|
|
{
|
|
|
|
|
$self->render(
|
2023-06-04 17:25:24 +00:00
|
|
|
|
'bad_request',
|
|
|
|
|
csrf => 1,
|
|
|
|
|
status => 400
|
2020-09-30 17:12:29 +00:00
|
|
|
|
);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2023-08-07 19:17:10 +00:00
|
|
|
|
$self->render_later;
|
|
|
|
|
|
|
|
|
|
my $oa = $self->config->{traewelling}{oauth};
|
|
|
|
|
|
|
|
|
|
return $self->oauth2->get_token_p(
|
2023-08-08 16:15:31 +00:00
|
|
|
|
traewelling => {
|
2023-08-09 17:26:25 +00:00
|
|
|
|
redirect_uri =>
|
|
|
|
|
$self->base_url_for('/oauth/traewelling')->to_abs->scheme(
|
|
|
|
|
$self->app->mode eq 'development' ? 'http' : 'https'
|
|
|
|
|
)->to_string,
|
2023-08-08 16:15:31 +00:00
|
|
|
|
scope => 'read-statuses write-statuses'
|
|
|
|
|
}
|
|
|
|
|
)->then(
|
2023-08-07 19:17:10 +00:00
|
|
|
|
sub {
|
|
|
|
|
my ($provider) = @_;
|
|
|
|
|
if ( not defined $provider ) {
|
|
|
|
|
|
|
|
|
|
# OAuth2 plugin performed a redirect, no need to render
|
|
|
|
|
return;
|
2020-09-30 17:12:29 +00:00
|
|
|
|
}
|
2023-08-07 19:17:10 +00:00
|
|
|
|
if ( not $provider or not $provider->{access_token} ) {
|
|
|
|
|
$self->flash( new_traewelling => 1 );
|
|
|
|
|
$self->flash( login_error => 'no token received' );
|
|
|
|
|
$self->redirect_to('/account/traewelling');
|
|
|
|
|
return;
|
2020-09-30 17:12:29 +00:00
|
|
|
|
}
|
2023-08-07 19:17:10 +00:00
|
|
|
|
my $uid = $self->current_user->{id};
|
|
|
|
|
my $token = $provider->{access_token};
|
|
|
|
|
$self->traewelling->link(
|
2023-08-09 17:28:36 +00:00
|
|
|
|
uid => $self->current_user->{id},
|
|
|
|
|
token => $provider->{access_token},
|
|
|
|
|
refresh_token => $provider->{refresh_token},
|
|
|
|
|
expires_in => $provider->{expires_in},
|
2023-08-07 19:17:10 +00:00
|
|
|
|
);
|
|
|
|
|
return $self->traewelling_api->get_user_p( $uid, $token )->then(
|
|
|
|
|
sub {
|
|
|
|
|
$self->flash( new_traewelling => 1 );
|
|
|
|
|
$self->redirect_to('/account/traewelling');
|
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
)->catch(
|
|
|
|
|
sub {
|
|
|
|
|
my ($err) = @_;
|
|
|
|
|
say "error $err";
|
|
|
|
|
$self->flash( new_traewelling => 1 );
|
|
|
|
|
$self->flash( login_error => $err );
|
|
|
|
|
$self->redirect_to('/account/traewelling');
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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(
|
|
|
|
|
'bad_request',
|
|
|
|
|
csrf => 1,
|
|
|
|
|
status => 400
|
|
|
|
|
);
|
2020-09-30 17:12:29 +00:00
|
|
|
|
return;
|
|
|
|
|
}
|
2023-08-07 19:17:10 +00:00
|
|
|
|
|
|
|
|
|
if ( $self->param('action') and $self->param('action') eq 'logout' ) {
|
2020-09-30 17:12:29 +00:00
|
|
|
|
$self->render_later;
|
2022-02-16 19:39:49 +00:00
|
|
|
|
my $traewelling = $self->traewelling->get( uid => $uid );
|
2020-09-30 17:12:29 +00:00
|
|
|
|
$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,
|
2022-02-16 19:56:28 +00:00
|
|
|
|
push_sync => $self->param('sync_source') eq 'travelynx' ? 1 : 0,
|
2020-10-04 10:27:20 +00:00
|
|
|
|
pull_sync => $self->param('sync_source') eq 'traewelling' ? 1 : 0,
|
2022-02-16 19:56:28 +00:00
|
|
|
|
toot => $self->param('toot') ? 1 : 0,
|
|
|
|
|
tweet => $self->param('tweet') ? 1 : 0,
|
2020-09-30 17:12:29 +00:00
|
|
|
|
);
|
|
|
|
|
$self->flash( success => 'traewelling' );
|
|
|
|
|
$self->redirect_to('account');
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2022-02-16 19:39:49 +00:00
|
|
|
|
my $traewelling = $self->traewelling->get( uid => $uid );
|
2020-09-30 17:12:29 +00:00
|
|
|
|
|
|
|
|
|
if ( $traewelling->{push_sync} ) {
|
|
|
|
|
$self->param( sync_source => 'travelynx' );
|
|
|
|
|
}
|
|
|
|
|
elsif ( $traewelling->{pull_sync} ) {
|
|
|
|
|
$self->param( sync_source => 'traewelling' );
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
$self->param( sync_source => 'none' );
|
|
|
|
|
}
|
2020-10-04 10:27:20 +00:00
|
|
|
|
if ( $traewelling->{data}{toot} ) {
|
|
|
|
|
$self->param( toot => 1 );
|
|
|
|
|
}
|
|
|
|
|
if ( $traewelling->{data}{tweet} ) {
|
|
|
|
|
$self->param( tweet => 1 );
|
|
|
|
|
}
|
2020-09-30 17:12:29 +00:00
|
|
|
|
|
2024-01-30 20:07:57 +00:00
|
|
|
|
$self->stash( title => 'travelynx × träwelling' );
|
2020-09-30 17:12:29 +00:00
|
|
|
|
$self->render(
|
|
|
|
|
'traewelling',
|
|
|
|
|
traewelling => $traewelling,
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
1;
|