|
1
|
|
|
<?php declare(strict_types=1); |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* ownCloud - Music app |
|
5
|
|
|
* |
|
6
|
|
|
* This file is licensed under the Affero General Public License version 3 or |
|
7
|
|
|
* later. See the COPYING file. |
|
8
|
|
|
* |
|
9
|
|
|
* @author Morris Jobke <[email protected]> |
|
10
|
|
|
* @author Pauli Järvinen <[email protected]> |
|
11
|
|
|
* @copyright Morris Jobke 2013, 2014 |
|
12
|
|
|
* @copyright Pauli Järvinen 2019 - 2025 |
|
13
|
|
|
*/ |
|
14
|
|
|
|
|
15
|
|
|
namespace OCA\Music\Controller; |
|
16
|
|
|
|
|
17
|
|
|
use OCA\Music\Service\ScrobblerService; |
|
18
|
|
|
use OCP\AppFramework\Controller; |
|
19
|
|
|
use OCP\AppFramework\Http\JSONResponse; |
|
20
|
|
|
use OCP\AppFramework\Http\StandaloneTemplateResponse; |
|
21
|
|
|
use OCP\IL10N; |
|
22
|
|
|
use OCP\IRequest; |
|
23
|
|
|
|
|
24
|
|
|
class ScrobblerController extends Controller { |
|
25
|
|
|
private IL10N $l10n; |
|
26
|
|
|
|
|
27
|
|
|
private ?string $userId; |
|
28
|
|
|
|
|
29
|
|
|
private ScrobblerService $scrobblerService; |
|
30
|
|
|
|
|
31
|
|
|
public function __construct(string $appName, |
|
32
|
|
|
IRequest $request, |
|
33
|
|
|
IL10N $l10n, |
|
34
|
|
|
?string $userId, |
|
35
|
|
|
ScrobblerService $scrobblerService) { |
|
36
|
|
|
parent::__construct($appName, $request); |
|
37
|
|
|
$this->l10n = $l10n; |
|
38
|
|
|
$this->userId = $userId; |
|
39
|
|
|
$this->appName = $appName; |
|
40
|
|
|
$this->scrobblerService = $scrobblerService; |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* @PublicPage |
|
45
|
|
|
* @NoCSRFRequired |
|
46
|
|
|
* @NoSameSiteCookieRequired |
|
47
|
|
|
*/ |
|
48
|
|
|
public function handleToken(string $token) : StandaloneTemplateResponse { |
|
49
|
|
|
try { |
|
50
|
|
|
$this->scrobblerService->generateSession($token, $this->userId); |
|
|
|
|
|
|
51
|
|
|
$success = true; |
|
52
|
|
|
$headline = 'All Set!'; |
|
53
|
|
|
$getSessionResponse = ''; |
|
54
|
|
|
$instructions = 'You are now ready to scrobble.'; |
|
55
|
|
|
} catch (\Throwable $t) { |
|
56
|
|
|
$success = false; |
|
57
|
|
|
$headline = 'Failed to authenticate.'; |
|
58
|
|
|
$getSessionResponse = $t->getMessage(); |
|
59
|
|
|
$instructions = 'Authentication failure. Please review the error message and try again.'; |
|
60
|
|
|
} finally { |
|
61
|
|
|
return new StandaloneTemplateResponse($this->appName, 'scrobble-getsession-result', [ |
|
62
|
|
|
'lang' => $this->l10n->getLanguageCode(), |
|
63
|
|
|
'success' => $success, |
|
|
|
|
|
|
64
|
|
|
'headline' => $this->l10n->t($headline), |
|
|
|
|
|
|
65
|
|
|
'getsession_response' => $getSessionResponse, |
|
|
|
|
|
|
66
|
|
|
'instructions' => $this->l10n->t($instructions) |
|
|
|
|
|
|
67
|
|
|
], 'base'); |
|
68
|
|
|
} |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* @PublicPage |
|
73
|
|
|
* @NoCSRFRequired |
|
74
|
|
|
* @noSameSiteCookieRequired |
|
75
|
|
|
* @throws \TypeError when $userId is null |
|
76
|
|
|
*/ |
|
77
|
|
|
public function clearSession(): JSONResponse { |
|
78
|
|
|
try { |
|
79
|
|
|
$this->scrobblerService->clearSession($this->userId); |
|
80
|
|
|
} catch (\Throwable $t) { |
|
81
|
|
|
$exception = $t; |
|
82
|
|
|
} |
|
83
|
|
|
return new JSONResponse( |
|
84
|
|
|
empty($exception) ? true : [ |
|
|
|
|
|
|
85
|
|
|
'error' => $this->l10n->t($exception->getMessage()) |
|
86
|
|
|
] |
|
87
|
|
|
); |
|
88
|
|
|
} |
|
89
|
|
|
} |
|
90
|
|
|
|