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

Commit 4f07dbb

Browse files
committed
prepare for private demos
1 parent 1fef026 commit 4f07dbb

13 files changed

Lines changed: 131 additions & 25 deletions

nix/integration-tests.nix

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@
109109
testScript = let
110110
initSql = pkgs.fetchurl {
111111
url = "https://github.com/demostf/db/raw/refs/heads/master/schema.sql";
112-
hash = "sha256-AwXN9mh9CRk6HWdvyUR+YdBkpmExNIDOIeDMz6XqjEQ=";
112+
hash = "sha256-tdMYDxlvpuQRxHglX46sCldxzsh1cDxkch2lGWnFH8E=";
113113
};
114114
in ''
115115
machine.succeed("mkdir /demos && chmod 0777 /demos");

nix/unit-tests.nix

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
api = pkgs.demostf-api-dev;
3434
initSql = pkgs.fetchurl {
3535
url = "https://github.com/demostf/db/raw/refs/heads/master/schema.sql";
36-
hash = "sha256-AwXN9mh9CRk6HWdvyUR+YdBkpmExNIDOIeDMz6XqjEQ=";
36+
hash = "sha256-tdMYDxlvpuQRxHglX46sCldxzsh1cDxkch2lGWnFH8E=";
3737
};
3838
in ''
3939
machine.succeed("mkdir /demos && chmod 0777 /demos");

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(

0 commit comments

Comments
 (0)