Skip to content
This repository was archived by the owner on May 21, 2025. It is now read-only.

Commit 5624d1c

Browse files
committed
prepare for private demos
1 parent 1fef026 commit 5624d1c

11 files changed

Lines changed: 129 additions & 23 deletions

File tree

src/Controllers/UploadController.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,8 @@ public function upload(): void {
2828
return;
2929
}
3030
$demoFile = $demo['tmp_name'];
31+
$private = $this->post('private', '0') === '1';
3132

32-
echo $this->uploadProvider->upload($key, $red, $blu, $name, $demoFile);
33+
echo $this->uploadProvider->upload($key, $red, $blu, $name, $demoFile, $private);
3334
}
3435
}

src/Data/Upload.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,15 @@ class Upload {
1010
private string $blue;
1111
private int $uploaderId;
1212
private string $hash;
13+
private bool $private;
1314

14-
public function __construct(string $name, string $red, string $blue, int $uploaderId, string $hash) {
15+
public function __construct(string $name, string $red, string $blue, int $uploaderId, string $hash, bool $private) {
1516
$this->name = $name;
1617
$this->red = $red;
1718
$this->blue = $blue;
1819
$this->uploaderId = $uploaderId;
1920
$this->hash = $hash;
21+
$this->private = $private;
2022
}
2123

2224
public function getName(): string {
@@ -38,4 +40,8 @@ public function getUploaderId(): int {
3840
public function getHash(): string {
3941
return $this->hash;
4042
}
43+
44+
public function isPrivate(): bool {
45+
return $this->private;
46+
}
4147
}

src/Demo/Demo.php

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,9 @@ class Demo implements JsonSerializable {
3030
private string $hash;
3131
private string $backend;
3232
private string $path;
33+
private bool $showPrivateData = false;
34+
35+
private ?\DateTimeImmutable $privateUntil;
3336

3437
public function __construct(
3538
int $id,
@@ -48,7 +51,8 @@ public function __construct(
4851
int $uploader,
4952
string $hash,
5053
string $backend,
51-
string $path
54+
string $path,
55+
?\DateTimeImmutable $privateUntil,
5256
) {
5357
$this->id = $id;
5458
$this->url = $url;
@@ -69,6 +73,7 @@ public function __construct(
6973
$this->path = $path;
7074
$this->players = null;
7175
$this->uploaderUser = null;
76+
$this->privateUntil = $privateUntil;
7277
}
7378

7479
public function getId(): int {
@@ -154,11 +159,13 @@ public function setUploaderUser(User $uploaderUser): void {
154159
* 'hash': string,
155160
* 'backend': string,
156161
* 'path': string,
162+
* 'private_until': ?string,
157163
* } $row
158164
*
159165
* @return Demo
160166
*/
161167
public static function fromRow(array $row): self {
168+
$private = $row['private_until'];
162169
return new self(
163170
(int) $row['id'],
164171
$row['url'],
@@ -176,7 +183,8 @@ public static function fromRow(array $row): self {
176183
(int) $row['uploader'],
177184
$row['hash'],
178185
$row['backend'],
179-
$row['path']
186+
$row['path'],
187+
$private ? new \DateTimeImmutable($private) : null,
180188
);
181189
}
182190

@@ -206,6 +214,10 @@ public function getPath(): string {
206214
return $this->path;
207215
}
208216

217+
public function getPrivateUntil(): ?\DateTimeImmutable {
218+
return $this->privateUntil;
219+
}
220+
209221
/**
210222
* @return array{
211223
* 'id': int,
@@ -226,12 +238,15 @@ public function getPath(): string {
226238
* 'backend': string,
227239
* 'path': string,
228240
* 'players': ?DemoPlayer
241+
* 'private_until': ?string,
229242
* }
230243
*/
231244
public function jsonSerialize(): array {
245+
$now = new \DateTimeImmutable();
246+
$isPublic = $this->showPrivateData || ($this->getPrivateUntil() ? $this->getPrivateUntil() <= $now : true);
232247
$data = [
233248
'id' => $this->getId(),
234-
'url' => $this->getUrl(),
249+
'url' => $isPublic ? $this->getUrl() : '',
235250
'name' => $this->getName(),
236251
'server' => $this->getServer(),
237252
'duration' => $this->getDuration(),
@@ -245,13 +260,18 @@ public function jsonSerialize(): array {
245260
'playerCount' => $this->getPlayerCount(),
246261
'uploader' => $this->uploaderUser ? $this->getUploaderUser()->jsonSerialize() : $this->getUploader(),
247262
'hash' => $this->getHash(),
248-
'backend' => $this->getBackend(),
249-
'path' => $this->getPath(),
263+
'backend' => $isPublic ? $this->getBackend() : '',
264+
'path' => $isPublic ? $this->getPath() : '',
265+
'private_until' => $this->getPrivateUntil()?->format(\DateTimeImmutable::ATOM),
250266
];
251267
if (\is_array($this->players)) {
252268
$data['players'] = $this->getPlayers();
253269
}
254270

255271
return $data;
256272
}
273+
274+
function showPrivateData(bool $show): void {
275+
$this->showPrivateData = $show;
276+
}
257277
}

src/Demo/DemoSaver.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,9 @@ public function __construct(
3838

3939
public function saveDemo(ParsedDemo $demo, Header $header, StoredDemo $storedDemo, Upload $upload): int {
4040
$this->connection->beginTransaction();
41+
$now = new \DateTimeImmutable();
42+
$week = \DateInterval::createFromDateString('7 days');
43+
$privateUntil = $upload->isPrivate() ? $now->add($week) : null;
4144

4245
$demoId = $this->demoProvider->storeDemo(new Demo(
4346
0,
@@ -56,7 +59,8 @@ public function saveDemo(ParsedDemo $demo, Header $header, StoredDemo $storedDem
5659
$upload->getUploaderId(),
5760
$upload->getHash(),
5861
$storedDemo->getBackend(),
59-
$storedDemo->getPath()
62+
$storedDemo->getPath(),
63+
$privateUntil,
6064
), $storedDemo->getBackend(), $storedDemo->getPath());
6165

6266
$kills = [];

src/Providers/DemoListProvider.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,7 @@ public function listDemos(int $page, array $where = [], string $order = 'DESC'):
182182
* 'hash': string,
183183
* 'backend': string,
184184
* 'path': string,
185+
* 'private_until': ?string,
185186
* }[] $rows
186187
*
187188
* @return Demo[]

src/Providers/DemoProvider.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ public function storeDemo(Demo $demo, string $backend, string $path): int {
9696
'nick' => $query->createNamedParameter($demo->getNick()),
9797
'"playerCount"' => $query->createNamedParameter($demo->getPlayerCount(), PDO::PARAM_INT),
9898
'hash' => $query->createNamedParameter($demo->getHash()),
99+
'private_until' => $query->createNamedParameter($demo->getPrivateUntil()?->format(DATE_ATOM)),
99100
])
100101
->executeStatement();
101102

src/Providers/UploadProvider.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ public function __construct(
4646
$this->uploadKey = $uploadKey;
4747
}
4848

49-
public function upload(string $key, string $red, string $blu, string $name, string $demoFile): string {
49+
public function upload(string $key, string $red, string $blu, string $name, string $demoFile, bool $private): string {
5050
$nameParts = explode('/', $name);
5151
$name = array_pop($nameParts);
5252
$name = str_replace('%', '_', $name);
@@ -86,7 +86,7 @@ public function upload(string $key, string $red, string $blu, string $name, stri
8686

8787
try {
8888
$storedDemo = $this->store->store($demoFile, $hash . '_' . $name);
89-
$upload = new Upload($name, $red, $blu, $user->getId(), $hash);
89+
$upload = new Upload($name, $red, $blu, $user->getId(), $hash, $private);
9090

9191
$id = $this->demoSaver->saveDemo($parsed, $header, $storedDemo, $upload);
9292
} catch (\Exception $e) {

test/Demo/DemoSaverTest.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,8 @@ public function testSave() {
3737
'DER',
3838
'ULB',
3939
$userProvider->getUserId('2345678', 'user2'),
40-
'securehash'
40+
'securehash',
41+
false,
4142
);
4243

4344
$header = new Header(

test/Providers/DemoListProviderTest.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,8 @@ private function getDemo(int $uploaderId, $map = 'map', $playerCount = 18, int $
5151
$uploaderId,
5252
'hash',
5353
'backend',
54-
'path'
54+
'path',
55+
null,
5556
);
5657
}
5758

test/Providers/DemoProviderTest.php

Lines changed: 62 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,8 @@ public function testStoreRetrieve() {
5757
$uploader->getId(),
5858
'hash',
5959
'dummy',
60-
'path'
60+
'path',
61+
null,
6162
);
6263
$demo->setUploaderUser($uploader);
6364

@@ -114,8 +115,7 @@ public function testRetrieveWithPlayers() {
114115
'hash',
115116
'backend',
116117
'path',
117-
'dummy',
118-
'path'
118+
null,
119119
);
120120

121121
$id = $this->provider->storeDemo($demo, 'dummy', 'path');
@@ -179,7 +179,8 @@ public function testSetDemoUrl() {
179179
$uploader->getId(),
180180
'hash',
181181
'dummy',
182-
'path'
182+
'path',
183+
null,
183184
);
184185

185186
$id = $this->provider->storeDemo($demo, 'dummy', 'path');
@@ -195,4 +196,61 @@ public function testSetDemoUrl() {
195196
$storedDemo2 = $this->provider->get($id2);
196197
$this->assertEquals('http://example.com', $storedDemo2->getUrl());
197198
}
199+
200+
public function privateDateProvider() {
201+
return [
202+
['2 days', false],
203+
['-2 days', true],
204+
];
205+
}
206+
207+
/**
208+
* @dataProvider privateDateProvider
209+
*/
210+
public function testPrivateDemo(string $until, bool $visible) {
211+
$now = new \DateTimeImmutable();
212+
$until = \DateInterval::createFromDateString($until);
213+
214+
$uploaderSteamId = $this->getSteamId('12345', 'test');
215+
$this->userProvider->store($uploaderSteamId, 'test');
216+
217+
$uploader = $this->userProvider->get($uploaderSteamId->getSteamId64());
218+
219+
$demo = new Demo(
220+
0,
221+
'http://example.com',
222+
'name',
223+
'server',
224+
12,
225+
'nick',
226+
'map',
227+
new \DateTime(),
228+
'RED',
229+
'BLUE',
230+
1,
231+
2,
232+
18,
233+
$uploader->getId(),
234+
'hash',
235+
'dummy',
236+
'path',
237+
$now->add($until),
238+
);
239+
240+
$id = $this->provider->storeDemo($demo, 'dummy', 'path');
241+
242+
$this->provider->setDemoUrl($id, 'foobackend', 'http://foo.example.com', 'bar');
243+
244+
$storedDemo = $this->provider->get($id);
245+
$json = $storedDemo->jsonSerialize();
246+
if ($visible) {
247+
$this->assertEquals('http://foo.example.com', $json['url']);
248+
} else {
249+
$this->assertEquals('', $json['url']);
250+
}
251+
252+
$storedDemo->showPrivateData(true);
253+
$json = $storedDemo->jsonSerialize();
254+
$this->assertEquals('http://foo.example.com', $json['url']);
255+
}
198256
}

0 commit comments

Comments
 (0)