|
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 Matthew Wells |
|
10
|
|
|
* @copyright Matthew Wells 2025 |
|
11
|
|
|
*/ |
|
12
|
|
|
|
|
13
|
|
|
namespace OCA\Music\Controller; |
|
14
|
|
|
|
|
15
|
|
|
use OCA\Music\Service\ScrobbleServiceException; |
|
16
|
|
|
use OCA\Music\Service\ExternalScrobbler; |
|
17
|
|
|
use OCP\AppFramework\Controller; |
|
18
|
|
|
use OCP\AppFramework\Http\JSONResponse; |
|
19
|
|
|
use OCP\AppFramework\Http\StandaloneTemplateResponse; |
|
20
|
|
|
use OCP\IL10N; |
|
21
|
|
|
use OCP\IRequest; |
|
22
|
|
|
|
|
23
|
|
|
class ScrobblerController extends Controller { |
|
24
|
|
|
private IL10N $l10n; |
|
25
|
|
|
|
|
26
|
|
|
private ?string $userId; |
|
27
|
|
|
|
|
28
|
|
|
/** @var ExternalScrobbler[] $externalScrobblers */ |
|
29
|
|
|
private array $externalScrobblers; |
|
30
|
|
|
|
|
31
|
|
|
public function __construct(string $appName, |
|
32
|
|
|
IRequest $request, |
|
33
|
|
|
IL10N $l10n, |
|
34
|
|
|
?string $userId, |
|
35
|
|
|
array $externalScrobblers) { |
|
36
|
|
|
parent::__construct($appName, $request); |
|
37
|
|
|
$this->l10n = $l10n; |
|
38
|
|
|
$this->userId = $userId; |
|
39
|
|
|
$this->appName = $appName; |
|
40
|
|
|
$this->externalScrobblers = $externalScrobblers; |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* @PublicPage |
|
45
|
|
|
* @NoCSRFRequired |
|
46
|
|
|
* @NoSameSiteCookieRequired |
|
47
|
|
|
*/ |
|
48
|
|
|
public function handleToken(string $serviceIdentifier, ?string $token) : StandaloneTemplateResponse { |
|
49
|
|
|
$params = [ |
|
50
|
|
|
'lang' => $this->l10n->getLanguageCode(), |
|
51
|
|
|
'success' => false, |
|
52
|
|
|
'headline' => $this->l10n->t('Unexpected error'), |
|
53
|
|
|
'identifier' => $serviceIdentifier, |
|
54
|
|
|
'getsession_response' => '', |
|
55
|
|
|
'instructions' => $this->l10n->t('Please contact your server administrator for assistance.') |
|
56
|
|
|
]; |
|
57
|
|
|
$response = new StandaloneTemplateResponse($this->appName, 'scrobble-getsession-result', [], 'base'); |
|
58
|
|
|
|
|
59
|
|
|
$scrobbler = $this->getExternalScrobbler($serviceIdentifier); |
|
60
|
|
|
|
|
61
|
|
|
if (!$scrobbler) { |
|
62
|
|
|
$params['headline'] = $this->l10n->t('Unknown Service'); |
|
63
|
|
|
$params['getsession_response'] = $this->l10n->t('Unkonwn service %s', [$serviceIdentifier]); |
|
64
|
|
|
$response->setParams($params); |
|
65
|
|
|
return $response; |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
try { |
|
69
|
|
|
$scrobbler->generateSession($token, $this->userId); |
|
|
|
|
|
|
70
|
|
|
$params['success'] = true; |
|
71
|
|
|
$params['headline'] = $this->l10n->t('All Set!'); |
|
72
|
|
|
$params['instructions'] = $this->l10n->t('Your streams will be scrobbled to %s.', [$scrobbler->getName()]); |
|
73
|
|
|
$params['getsession_response'] = ''; |
|
74
|
|
|
} catch (ScrobbleServiceException $e) { |
|
75
|
|
|
$params['headline'] = $this->l10n->t('Authentication failure'); |
|
76
|
|
|
$params['instructions'] = $this->l10n->t('Please review the error message prior to trying again.'); |
|
77
|
|
|
$params['getsession_response'] = $e->getMessage(); |
|
78
|
|
|
} catch (\Exception $t) { |
|
79
|
|
|
$params['getsession_response'] = $t->getMessage(); |
|
80
|
|
|
} catch (\TypeError $t) { |
|
81
|
|
|
$params['getsession_response'] = $t->getMessage(); |
|
82
|
|
|
} finally { |
|
83
|
|
|
$response->setParams($params); |
|
84
|
|
|
return $response; |
|
85
|
|
|
} |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* @PublicPage |
|
90
|
|
|
* @NoCSRFRequired |
|
91
|
|
|
* @noSameSiteCookieRequired |
|
92
|
|
|
*/ |
|
93
|
|
|
public function clearSession(string $serviceIdentifier): JSONResponse { |
|
94
|
|
|
$response = new JSONResponse(['error' => [ |
|
95
|
|
|
'message' => 'Unknown error' |
|
96
|
|
|
]]); |
|
97
|
|
|
|
|
98
|
|
|
$scrobbler = $this->getExternalScrobbler($serviceIdentifier); |
|
99
|
|
|
if (!$scrobbler) { |
|
100
|
|
|
$response->setData(['error' => [ |
|
101
|
|
|
'message' => $this->l10n->t('Unknown service %s', [$serviceIdentifier]) |
|
102
|
|
|
]]); |
|
103
|
|
|
return $response; |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
try { |
|
107
|
|
|
$scrobbler->clearSession($this->userId); |
|
108
|
|
|
$response->setData(['success' => true]); |
|
109
|
|
|
} catch (\InvalidArgumentException $e) { |
|
110
|
|
|
$response->setData(['error' => [ |
|
111
|
|
|
'message' => $this->l10n->t('Check the error log for details.') |
|
112
|
|
|
]]); |
|
113
|
|
|
} finally { |
|
114
|
|
|
return $response; |
|
115
|
|
|
} |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
private function getExternalScrobbler(string $serviceIdentifier) : ?ExternalScrobbler { |
|
119
|
|
|
foreach ($this->externalScrobblers as $scrobbler) { |
|
120
|
|
|
if ($scrobbler->getIdentifier() === $serviceIdentifier) { |
|
121
|
|
|
return $scrobbler; |
|
122
|
|
|
} |
|
123
|
|
|
} |
|
124
|
|
|
return null; |
|
125
|
|
|
} |
|
126
|
|
|
} |
|
127
|
|
|
|