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 Pauli Järvinen <[email protected]> |
10
|
|
|
* @copyright Pauli Järvinen 2021 |
11
|
|
|
*/ |
12
|
|
|
|
13
|
|
|
namespace OCA\Music\Controller; |
14
|
|
|
|
15
|
|
|
use \OCP\AppFramework\Controller; |
16
|
|
|
use \OCP\AppFramework\Http; |
17
|
|
|
use \OCP\AppFramework\Http\JSONResponse; |
18
|
|
|
|
19
|
|
|
use \OCP\IRequest; |
20
|
|
|
|
21
|
|
|
use \OCA\Music\AppFramework\Core\Logger; |
22
|
|
|
use \OCA\Music\Http\ErrorResponse; |
23
|
|
|
use \OCA\Music\Utility\PodcastService; |
24
|
|
|
use \OCA\Music\Utility\Util; |
25
|
|
|
|
26
|
|
|
class PodcastApiController extends Controller { |
27
|
|
|
private $podcastService; |
28
|
|
|
private $userId; |
29
|
|
|
private $logger; |
30
|
|
|
|
31
|
|
|
public function __construct(string $appname, |
32
|
|
|
IRequest $request, |
33
|
|
|
PodcastService $podcastService, |
34
|
|
|
?string $userId, |
35
|
|
|
Logger $logger) { |
36
|
|
|
parent::__construct($appname, $request); |
37
|
|
|
$this->podcastService = $podcastService; |
38
|
|
|
$this->userId = $userId ?? ''; // ensure non-null to satisfy Scrutinizer; the null case should happen only when the user has already logged out |
39
|
|
|
$this->logger = $logger; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* lists all podcasts |
44
|
|
|
* |
45
|
|
|
* @NoAdminRequired |
46
|
|
|
* @NoCSRFRequired |
47
|
|
|
*/ |
48
|
|
|
public function getAll() { |
49
|
|
|
$channels = $this->podcastService->getAllChannels($this->userId, /*$includeEpisodes=*/ true); |
50
|
|
|
return Util::arrayMapMethod($channels, 'toApi'); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* add a followed podcast |
55
|
|
|
* |
56
|
|
|
* @NoAdminRequired |
57
|
|
|
* @NoCSRFRequired |
58
|
|
|
*/ |
59
|
|
|
public function subscribe(?string $url) { |
60
|
|
|
if ($url === null) { |
61
|
|
|
return new ErrorResponse(Http::STATUS_BAD_REQUEST, "Mandatory argument 'url' not given"); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
$result = $this->podcastService->subscribe($url, $this->userId); |
65
|
|
|
|
66
|
|
|
switch ($result['status']) { |
67
|
|
|
case PodcastService::STATUS_OK: |
68
|
|
|
return $result['channel']->toApi(); |
69
|
|
|
case PodcastService::STATUS_INVALID_URL: |
70
|
|
|
return new ErrorResponse(Http::STATUS_BAD_REQUEST, "Invalid URL $url"); |
71
|
|
|
case PodcastService::STATUS_INVALID_RSS: |
72
|
|
|
return new ErrorResponse(Http::STATUS_BAD_REQUEST, "The document at URL $url is not a valid podcast RSS feed"); |
73
|
|
|
case PodcastService::STATUS_ALREADY_EXISTS: |
74
|
|
|
return new ErrorResponse(Http::STATUS_CONFLICT, 'User already has this podcast channel subscribed'); |
75
|
|
|
default: |
76
|
|
|
return new ErrorResponse(Http::STATUS_INTERNAL_SERVER_ERROR, "Unexpected status code {$result['status']}"); |
77
|
|
|
} |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* deletes a station |
82
|
|
|
* |
83
|
|
|
* @NoAdminRequired |
84
|
|
|
* @NoCSRFRequired |
85
|
|
|
*/ |
86
|
|
|
public function unsubscribe(int $id) { |
87
|
|
|
$status = $this->podcastService->unsubscribe($id, $this->userId); |
88
|
|
|
|
89
|
|
|
switch ($status) { |
90
|
|
|
case PodcastService::STATUS_OK: |
91
|
|
|
return new JSONResponse(['success' => true]); |
92
|
|
|
case PodcastService::STATUS_NOT_FOUND: |
93
|
|
|
return new ErrorResponse(Http::STATUS_NOT_FOUND); |
94
|
|
|
default: |
95
|
|
|
return new ErrorResponse(Http::STATUS_INTERNAL_SERVER_ERROR, "Unexpected status code $status"); |
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* get a single podcast channel |
101
|
|
|
* |
102
|
|
|
* @NoAdminRequired |
103
|
|
|
* @NoCSRFRequired |
104
|
|
|
*/ |
105
|
|
|
public function get(int $id) { |
106
|
|
|
$channel = $this->podcastService->getChannel($id, $this->userId, /*includeEpisodes=*/ true); |
107
|
|
|
|
108
|
|
|
if ($channel !== null) { |
109
|
|
|
return $channel->toApi(); |
110
|
|
|
} else { |
111
|
|
|
return new ErrorResponse(Http::STATUS_NOT_FOUND); |
112
|
|
|
} |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* check a single channel for updates |
117
|
|
|
* @param int $id Channel ID |
118
|
|
|
* @param string|null $prevHash Previous content hash known by the client. If given, the result will tell |
119
|
|
|
* if the channel content has updated from this state. If omitted, the result |
120
|
|
|
* will tell if the channel changed from its previous server-known state. |
121
|
|
|
* |
122
|
|
|
* @NoAdminRequired |
123
|
|
|
* @NoCSRFRequired |
124
|
|
|
*/ |
125
|
|
|
public function updateChannel(int $id, ?string $prevHash) { |
126
|
|
|
$updateResult = $this->podcastService->updateChannel($id, $this->userId, $prevHash); |
127
|
|
|
|
128
|
|
|
$response = [ |
129
|
|
|
'success' => ($updateResult['status'] === PodcastService::STATUS_OK), |
130
|
|
|
'updated' => $updateResult['updated'] |
131
|
|
|
]; |
132
|
|
|
if ($updateResult['updated']) { |
133
|
|
|
$response['channel'] = $updateResult['channel']->toApi(); |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
return new JSONResponse($response); |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* reset all the subscribed podcasts of the user |
141
|
|
|
* |
142
|
|
|
* @NoAdminRequired |
143
|
|
|
* @NoCSRFRequired |
144
|
|
|
*/ |
145
|
|
|
public function resetAll() { |
146
|
|
|
$this->podcastService->resetAll($this->userId); |
147
|
|
|
return new JSONResponse(['success' => true]); |
148
|
|
|
} |
149
|
|
|
} |
150
|
|
|
|