summaryrefslogtreecommitdiff
path: root/lib/Travelynx/Controller/Traewelling.pm
blob: 3cdeff81328be0c24d1d3abd30bb6245b2f4a811 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
package Travelynx::Controller::Traewelling;

# Copyright (C) 2020-2023 Birte Kristina Friesel
#
# SPDX-License-Identifier: AGPL-3.0-or-later
use Mojo::Base 'Mojolicious::Controller';
use Mojo::Promise;

sub oauth {
	my ($self) = @_;

	if (    $self->param('action')
		and $self->validation->csrf_protect->has_error('csrf_token') )
	{
		$self->render(
			'bad_request',
			csrf   => 1,
			status => 400
		);
		return;
	}

	$self->render_later;

	my $oa = $self->config->{traewelling}{oauth};

	return $self->oauth2->get_token_p(
		traewelling => {
			redirect_uri =>
			  $self->base_url_for('/oauth/traewelling')->to_abs->scheme(
				$self->app->mode eq 'development' ? 'http' : 'https'
			)->to_string,
			scope => 'read-statuses write-statuses'
		}
	)->then(
		sub {
			my ($provider) = @_;
			if ( not defined $provider ) {

				# OAuth2 plugin performed a redirect, no need to render
				return;
			}
			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;
			}
			my $uid   = $self->current_user->{id};
			my $token = $provider->{access_token};
			$self->traewelling->link(
				uid           => $self->current_user->{id},
				token         => $provider->{access_token},
				refresh_token => $provider->{refresh_token},
				expires_in    => $provider->{expires_in},
			);
			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
		);
		return;
	}

	if ( $self->param('action') and $self->param('action') eq 'logout' ) {
		$self->render_later;
		my $traewelling = $self->traewelling->get( uid => $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,
			toot      => $self->param('toot')                         ? 1 : 0,
			tweet     => $self->param('tweet')                        ? 1 : 0,
		);
		$self->flash( success => 'traewelling' );
		$self->redirect_to('account');
		return;
	}

	my $traewelling = $self->traewelling->get( uid => $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' );
	}
	if ( $traewelling->{data}{toot} ) {
		$self->param( toot => 1 );
	}
	if ( $traewelling->{data}{tweet} ) {
		$self->param( tweet => 1 );
	}

	$self->stash( title => 'travelynx × träwelling' );
	$self->render(
		'traewelling',
		traewelling => $traewelling,
	);
}

1;