1
0
Fork 0
mirror of https://codeberg.org/demostf/api.git synced 2026-06-03 18:04:08 +02:00

prepare for private demos

This commit is contained in:
Robin Appelman 2025-04-01 22:49:04 +02:00
commit 4f07dbbf34
13 changed files with 131 additions and 25 deletions

View file

@ -28,7 +28,8 @@ class UploadController extends BaseController {
return;
}
$demoFile = $demo['tmp_name'];
$private = $this->post('private', '0') === '1';
echo $this->uploadProvider->upload($key, $red, $blu, $name, $demoFile);
echo $this->uploadProvider->upload($key, $red, $blu, $name, $demoFile, $private);
}
}

View file

@ -10,13 +10,15 @@ class Upload {
private string $blue;
private int $uploaderId;
private string $hash;
private bool $private;
public function __construct(string $name, string $red, string $blue, int $uploaderId, string $hash) {
public function __construct(string $name, string $red, string $blue, int $uploaderId, string $hash, bool $private) {
$this->name = $name;
$this->red = $red;
$this->blue = $blue;
$this->uploaderId = $uploaderId;
$this->hash = $hash;
$this->private = $private;
}
public function getName(): string {
@ -38,4 +40,8 @@ class Upload {
public function getHash(): string {
return $this->hash;
}
public function isPrivate(): bool {
return $this->private;
}
}

View file

@ -30,6 +30,9 @@ class Demo implements JsonSerializable {
private string $hash;
private string $backend;
private string $path;
private bool $showPrivateData = false;
private ?\DateTimeImmutable $privateUntil;
public function __construct(
int $id,
@ -48,7 +51,8 @@ class Demo implements JsonSerializable {
int $uploader,
string $hash,
string $backend,
string $path
string $path,
?\DateTimeImmutable $privateUntil,
) {
$this->id = $id;
$this->url = $url;
@ -69,6 +73,7 @@ class Demo implements JsonSerializable {
$this->path = $path;
$this->players = null;
$this->uploaderUser = null;
$this->privateUntil = $privateUntil;
}
public function getId(): int {
@ -154,11 +159,13 @@ class Demo implements JsonSerializable {
* 'hash': string,
* 'backend': string,
* 'path': string,
* 'private_until': ?string,
* } $row
*
* @return Demo
*/
public static function fromRow(array $row): self {
$private = $row['private_until'];
return new self(
(int) $row['id'],
$row['url'],
@ -176,7 +183,8 @@ class Demo implements JsonSerializable {
(int) $row['uploader'],
$row['hash'],
$row['backend'],
$row['path']
$row['path'],
$private ? new \DateTimeImmutable($private) : null,
);
}
@ -206,6 +214,10 @@ class Demo implements JsonSerializable {
return $this->path;
}
public function getPrivateUntil(): ?\DateTimeImmutable {
return $this->privateUntil;
}
/**
* @return array{
* 'id': int,
@ -226,12 +238,15 @@ class Demo implements JsonSerializable {
* 'backend': string,
* 'path': string,
* 'players': ?DemoPlayer
* 'private_until': ?string,
* }
*/
public function jsonSerialize(): array {
$now = new \DateTimeImmutable();
$isPublic = $this->showPrivateData || ($this->getPrivateUntil() ? $this->getPrivateUntil() <= $now : true);
$data = [
'id' => $this->getId(),
'url' => $this->getUrl(),
'url' => $isPublic ? $this->getUrl() : '',
'name' => $this->getName(),
'server' => $this->getServer(),
'duration' => $this->getDuration(),
@ -245,8 +260,9 @@ class Demo implements JsonSerializable {
'playerCount' => $this->getPlayerCount(),
'uploader' => $this->uploaderUser ? $this->getUploaderUser()->jsonSerialize() : $this->getUploader(),
'hash' => $this->getHash(),
'backend' => $this->getBackend(),
'path' => $this->getPath(),
'backend' => $isPublic ? $this->getBackend() : '',
'path' => $isPublic ? $this->getPath() : '',
'private_until' => $this->getPrivateUntil()?->format(\DateTimeImmutable::ATOM),
];
if (\is_array($this->players)) {
$data['players'] = $this->getPlayers();
@ -254,4 +270,8 @@ class Demo implements JsonSerializable {
return $data;
}
function showPrivateData(bool $show): void {
$this->showPrivateData = $show;
}
}

View file

@ -38,6 +38,9 @@ class DemoSaver {
public function saveDemo(ParsedDemo $demo, Header $header, StoredDemo $storedDemo, Upload $upload): int {
$this->connection->beginTransaction();
$now = new \DateTimeImmutable();
$week = \DateInterval::createFromDateString('7 days');
$privateUntil = $upload->isPrivate() ? $now->add($week) : null;
$demoId = $this->demoProvider->storeDemo(new Demo(
0,
@ -56,7 +59,8 @@ class DemoSaver {
$upload->getUploaderId(),
$upload->getHash(),
$storedDemo->getBackend(),
$storedDemo->getPath()
$storedDemo->getPath(),
$privateUntil,
), $storedDemo->getBackend(), $storedDemo->getPath());
$kills = [];

View file

@ -182,6 +182,7 @@ class DemoListProvider extends BaseProvider {
* 'hash': string,
* 'backend': string,
* 'path': string,
* 'private_until': ?string,
* }[] $rows
*
* @return Demo[]

View file

@ -96,6 +96,7 @@ class DemoProvider extends BaseProvider {
'nick' => $query->createNamedParameter($demo->getNick()),
'"playerCount"' => $query->createNamedParameter($demo->getPlayerCount(), PDO::PARAM_INT),
'hash' => $query->createNamedParameter($demo->getHash()),
'private_until' => $query->createNamedParameter($demo->getPrivateUntil()?->format(DATE_ATOM)),
])
->executeStatement();

View file

@ -46,7 +46,7 @@ class UploadProvider extends BaseProvider {
$this->uploadKey = $uploadKey;
}
public function upload(string $key, string $red, string $blu, string $name, string $demoFile): string {
public function upload(string $key, string $red, string $blu, string $name, string $demoFile, bool $private): string {
$nameParts = explode('/', $name);
$name = array_pop($nameParts);
$name = str_replace('%', '_', $name);
@ -86,7 +86,7 @@ class UploadProvider extends BaseProvider {
try {
$storedDemo = $this->store->store($demoFile, $hash . '_' . $name);
$upload = new Upload($name, $red, $blu, $user->getId(), $hash);
$upload = new Upload($name, $red, $blu, $user->getId(), $hash, $private);
$id = $this->demoSaver->saveDemo($parsed, $header, $storedDemo, $upload);
} catch (\Exception $e) {