/
StationRequestRepository.php
229 lines (192 loc) · 7.41 KB
/
StationRequestRepository.php
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
<?php
namespace App\Entity\Repository;
use App\Doctrine\ReloadableEntityManagerInterface;
use App\Doctrine\Repository;
use App\Entity;
use App\Environment;
use App\Exception;
use App\Radio\AutoDJ;
use App\Service\DeviceDetector;
use Carbon\CarbonImmutable;
use Carbon\CarbonInterface;
use Psr\Log\LoggerInterface;
use Symfony\Component\Serializer\Serializer;
class StationRequestRepository extends Repository
{
protected StationMediaRepository $mediaRepo;
protected DeviceDetector $deviceDetector;
public function __construct(
ReloadableEntityManagerInterface $em,
Serializer $serializer,
Environment $environment,
LoggerInterface $logger,
StationMediaRepository $mediaRepo,
DeviceDetector $deviceDetector
) {
parent::__construct($em, $serializer, $environment, $logger);
$this->mediaRepo = $mediaRepo;
$this->deviceDetector = $deviceDetector;
}
public function submit(
Entity\Station $station,
string $trackId,
bool $isAuthenticated,
string $ip,
string $userAgent
): int {
// Forbid web crawlers from using this feature.
$dd = $this->deviceDetector->parse($userAgent);
if ($dd->isBot()) {
throw new Exception(__('Search engine crawlers are not permitted to use this feature.'));
}
// Verify that the station supports requests.
if (!$station->getEnableRequests()) {
throw new Exception(__('This station does not accept requests currently.'));
}
// Verify that Track ID exists with station.
$media_item = $this->mediaRepo->findByUniqueId($trackId, $station);
if (!($media_item instanceof Entity\StationMedia)) {
throw new Exception(__('The song ID you specified could not be found in the station.'));
}
if (!$media_item->isRequestable()) {
throw new Exception(__('The song ID you specified cannot be requested for this station.'));
}
// Check if the song is already enqueued as a request.
$this->checkPendingRequest($media_item, $station);
// Check the most recent song history.
$this->checkRecentPlay($media_item, $station);
if (!$isAuthenticated) {
// Check for any request (on any station) within the last $threshold_seconds.
$thresholdMins = $station->getRequestThreshold() ?? 5;
$thresholdSeconds = $thresholdMins * 60;
// Always have a minimum threshold to avoid flooding.
if ($thresholdSeconds < 60) {
$thresholdSeconds = 15;
}
$recent_requests = $this->em->createQuery(
<<<'DQL'
SELECT sr FROM App\Entity\StationRequest sr
WHERE sr.ip = :user_ip
AND sr.timestamp >= :threshold
DQL
)->setParameter('user_ip', $ip)
->setParameter('threshold', time() - $thresholdSeconds)
->getArrayResult();
if (count($recent_requests) > 0) {
throw new Exception(
__(
'You have submitted a request too recently! Please wait before submitting another one.'
)
);
}
}
// Save request locally.
$record = new Entity\StationRequest($station, $media_item, $ip);
$this->em->persist($record);
$this->em->flush();
return $record->getId();
}
/**
* Check if the song is already enqueued as a request.
*
* @param Entity\StationMedia $media
* @param Entity\Station $station
*
* @throws Exception
*/
public function checkPendingRequest(Entity\StationMedia $media, Entity\Station $station): bool
{
$pending_request_threshold = time() - (60 * 10);
try {
$pending_request = $this->em->createQuery(
<<<'DQL'
SELECT sr.timestamp
FROM App\Entity\StationRequest sr
WHERE sr.track_id = :track_id
AND sr.station_id = :station_id
AND (sr.timestamp >= :threshold OR sr.played_at = 0)
ORDER BY sr.timestamp DESC
DQL
)->setParameter('track_id', $media->getId())
->setParameter('station_id', $station->getId())
->setParameter('threshold', $pending_request_threshold)
->setMaxResults(1)
->getSingleScalarResult();
} catch (\Exception $e) {
return true;
}
if ($pending_request > 0) {
throw new Exception(__('Duplicate request: this song was already requested and will play soon.'));
}
return true;
}
public function getNextPlayableRequest(
Entity\Station $station,
?CarbonInterface $now = null
): ?Entity\StationRequest {
$now ??= CarbonImmutable::now($station->getTimezoneObject());
// Look up all requests that have at least waited as long as the threshold.
$requests = $this->em->createQuery(
<<<'DQL'
SELECT sr, sm
FROM App\Entity\StationRequest sr JOIN sr.track sm
WHERE sr.played_at = 0
AND sr.station = :station
ORDER BY sr.skip_delay DESC, sr.id ASC
DQL
)->setParameter('station', $station)
->execute();
foreach ($requests as $request) {
/** @var Entity\StationRequest $request */
if ($request->shouldPlayNow($now)) {
try {
$this->checkRecentPlay($request->getTrack(), $station);
} catch (\Exception $e) {
continue;
}
return $request;
}
}
return null;
}
/**
* Check the most recent song history.
*
* @param Entity\StationMedia $media
* @param Entity\Station $station
*
* @throws Exception
*/
public function checkRecentPlay(Entity\StationMedia $media, Entity\Station $station): bool
{
$lastPlayThresholdMins = ($station->getRequestThreshold() ?? 15);
if (0 === $lastPlayThresholdMins) {
return true;
}
$lastPlayThreshold = time() - ($lastPlayThresholdMins * 60);
$recentTracks = $this->em->createQuery(
<<<'DQL'
SELECT sh FROM App\Entity\SongHistory sh
WHERE sh.station = :station
AND sh.timestamp_start >= :threshold
ORDER BY sh.timestamp_start DESC
DQL
)->setParameter('station', $station)
->setParameter('threshold', $lastPlayThreshold)
->getArrayResult();
$eligibleTrack = new Entity\Api\StationPlaylistQueue();
$eligibleTrack->media_id = $media->getId();
$eligibleTrack->song_id = $media->getSongId();
$eligibleTrack->title = $media->getTitle() ?? '';
$eligibleTrack->artist = $media->getArtist() ?? '';
$isDuplicate = (null === AutoDJ\Queue::getDistinctTrack([$eligibleTrack], $recentTracks));
if ($isDuplicate) {
throw new Exception(
__(
'This song or artist has been played too recently. Wait a while before requesting it again.'
)
);
}
return true;
}
}