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

dont rely on the steam api for getting user names

This commit is contained in:
Robin Appelman 2020-11-29 20:06:49 +01:00
commit fd9a7e8527
13 changed files with 54 additions and 78 deletions

View file

@ -4,19 +4,19 @@ docker:
.PHONY: testdb
testdb:
docker run -d --name api-test-db -p 5433:5432 -e POSTGRES_PASSWORD=test demostf/db
docker run -d --name api-unit-test-db -p 5434:5432 -e POSTGRES_PASSWORD=test demostf/db
node_modules: package.json
npm install
.PHONY: mocha
mocha: node_modules
DEMO_ROOT=/tmp/demos DB_PORT=5433 DB_TYPE=pgsql DB_HOST=localhost DB_USERNAME=postgres DB_PASSWORD=test DB_DATABASE=postgres\
DEMO_ROOT=/tmp/demos DB_PORT=5434 DB_TYPE=pgsql DB_HOST=localhost DB_USERNAME=postgres DB_PASSWORD=test DB_DATABASE=postgres\
PARSER_PATH=/home/robin/Projects/demostf/tf-demo-parser/target/release/parse_demo node node_modules/.bin/mocha --recursive
.PHONY: phpunit
phpunit:
DB_PORT=5433 DB_TYPE=pgsql DB_HOST=localhost DB_USERNAME=postgres DB_USERNAME=postgres DB_PASSWORD=test DB_DATABASE=postgres ./vendor/bin/phpunit test
DB_PORT=5434 DB_TYPE=pgsql DB_HOST=localhost DB_USERNAME=postgres DB_USERNAME=postgres DB_PASSWORD=test DB_DATABASE=postgres ./vendor/bin/phpunit test
.PHONY: test
tests: phpunit mocha

View file

@ -71,7 +71,7 @@ class AuthController extends BaseController {
$steamId = $steam->validate();
if ($steamId) {
$steamIdObject = new SteamId($steamId);
$key = $this->userProvider->store($steamIdObject);
$key = $this->userProvider->store($steamIdObject, $steamIdObject->getNickname());
$this->authProvider->setUser($token, $steamIdObject, $key);
}
Flight::redirect($return);

View file

@ -13,19 +13,17 @@ class DemoPlayer implements JsonSerializable {
private string $team;
private string $class;
private string $steamId;
private string $avatar;
private int $kills;
private int $assists;
private int $deaths;
public function __construct(int $id, int $userId, string $name, string $team, string $class, string $steamId, string $avatar, int $kills, int $assists, int $deaths) {
public function __construct(int $id, int $userId, string $name, string $team, string $class, string $steamId, int $kills, int $assists, int $deaths) {
$this->id = $id;
$this->userId = $userId;
$this->name = $name;
$this->team = $team;
$this->class = $class;
$this->steamId = $steamId;
$this->avatar = $avatar;
$this->kills = $kills;
$this->assists = $assists;
$this->deaths = $deaths;
@ -55,10 +53,6 @@ class DemoPlayer implements JsonSerializable {
return $this->steamId;
}
public function getAvatar(): string {
return $this->avatar;
}
public function getKills(): int {
return $this->kills;
}
@ -84,7 +78,6 @@ class DemoPlayer implements JsonSerializable {
$row['team'],
$row['class'],
$row['steamid'],
$row['avatar'],
$row['kills'],
$row['assists'],
$row['deaths']
@ -99,7 +92,6 @@ class DemoPlayer implements JsonSerializable {
'team' => $this->getTeam(),
'class' => $this->getClass(),
'steamid' => $this->getSteamId(),
'avatar' => $this->getAvatar(),
'kills' => $this->getKills(),
'assists' => $this->getAssists(),
'deaths' => $this->getDeaths(),

View file

@ -10,14 +10,12 @@ class User implements JsonSerializable {
private int $id;
private string $steamId;
private string $name;
private string $avatar;
private string $token;
public function __construct(int $id, string $steamId, string $name, string $avatar, string $token) {
public function __construct(int $id, string $steamId, string $name, string $token) {
$this->id = $id;
$this->steamId = $steamId;
$this->name = $name;
$this->avatar = $avatar;
$this->token = $token;
}
@ -33,10 +31,6 @@ class User implements JsonSerializable {
return $this->name;
}
public function getAvatar(): string {
return $this->avatar;
}
public function getToken(): string {
return $this->token;
}
@ -46,7 +40,6 @@ class User implements JsonSerializable {
'id' => $this->getId(),
'steamid' => $this->getSteamId(),
'name' => $this->getName(),
'avatar' => $this->getAvatar(),
];
}
@ -60,7 +53,6 @@ class User implements JsonSerializable {
(int) $row['id'],
$row['steamid'],
$row['name'],
$row['avatar'],
$row['token'] ?? ''
);
}

View file

@ -68,7 +68,7 @@ class DemoSaver {
), $storedDemo->getBackend(), $storedDemo->getPath());
foreach ($demo->getPlayers() as $player) {
$userId = $this->userProvider->getUserId($player->getSteamId());
$userId = $this->userProvider->getUserId($player->getSteamId(), $player->getName());
$userMap[$player->getDemoUserId()] = $userId;
$this->playerProvider->store(new Player(

View file

@ -22,7 +22,7 @@ class UserProvider extends BaseProvider {
$this->generator = $generator;
}
public function store(SteamId $steamId): string {
public function store(SteamId $steamId, string $name): string {
$token = $this->generator->generateString(64, Generator::EASY_TO_READ);
$user = $this->get($steamId->getSteamId64());
@ -34,8 +34,8 @@ class UserProvider extends BaseProvider {
$query->insert('users')
->values([
'steamid' => $query->createNamedParameter($steamId->getSteamId64()),
'name' => $query->createNamedParameter($steamId->getNickname()),
'avatar' => $query->createNamedParameter($steamId->getMediumAvatarUrl()),
'name' => $query->createNamedParameter($name),
'avatar' => $query->createNamedParameter(''),
'token' => $query->createNamedParameter($token),
])->add('orderBy', 'ON CONFLICT DO NOTHING')// hack to append arbitrary string to sql
->execute();
@ -49,7 +49,7 @@ class UserProvider extends BaseProvider {
// first search in the view which contains the most used name for the users
$query = $this->getQueryBuilder();
$query->select(['id', 'steamid', 'name', 'avatar', 'token'])
$query->select(['id', 'steamid', 'name', 'token'])
->from('users_named')
->where($query->expr()->eq('steamid', $query->createNamedParameter($steamid)));
@ -59,7 +59,7 @@ class UserProvider extends BaseProvider {
// if the user is newly inserted it wont be in our view yet
$query = $this->getQueryBuilder();
$query->select(['id', 'steamid', 'name', 'avatar', 'token'])
$query->select(['id', 'steamid', 'name', 'token'])
->from('users')
->where($query->expr()->eq('steamid', $query->createNamedParameter($steamid)));
@ -76,7 +76,7 @@ class UserProvider extends BaseProvider {
// first search in the view which contains the most used name for the users
$query = $this->getQueryBuilder();
$query->select(['id', 'steamid', 'name', 'avatar', 'token'])
$query->select(['id', 'steamid', 'name', 'token'])
->from('users_named')
->where($query->expr()->eq('id', $query->createNamedParameter($userId, \PDO::PARAM_INT)));
@ -86,7 +86,7 @@ class UserProvider extends BaseProvider {
// if the user is newly inserted it wont be in our view yet
$query = $this->getQueryBuilder();
$query->select(['id', 'steamid', 'name', 'avatar', 'token'])
$query->select(['id', 'steamid', 'name', 'token'])
->from('users')
->where($query->expr()->eq('id', $query->createNamedParameter($userId, \PDO::PARAM_INT)));
@ -162,7 +162,7 @@ class UserProvider extends BaseProvider {
public function byKey(string $key): ?User {
$query = $this->getQueryBuilder();
$query->select(['id', 'steamid', 'name', 'avatar', 'token'])
$query->select(['id', 'steamid', 'name', 'token'])
->from('users')
->where($query->expr()->eq('token', $query->createNamedParameter($key)));
@ -171,13 +171,13 @@ class UserProvider extends BaseProvider {
return $row ? User::fromRow($row) : null;
}
public function getUserId(string $steamId): int {
public function getUserId(string $steamId, string $name): int {
$existing = $this->get($steamId);
if ($existing) {
return $existing->getId();
}
$this->store(new SteamId($steamId));
$this->store(new SteamId($steamId, false), $name);
return $this->get($steamId)->getId();
}

View file

@ -16,7 +16,6 @@ class DemoPlayerTest extends TestCase {
'team' => 'red',
'class' => 'sniper',
'steamid' => 'asd',
'avatar' => 'asd.png',
'kills' => 5,
'assists' => 3,
'deaths' => 7,

View file

@ -29,14 +29,14 @@ class DemoSaverTest extends TestCase {
$demoProvider = new DemoProvider($this->getDatabaseConnection(), $userProvider);
$chatProvider = new ChatProvider($this->getDatabaseConnection());
$userProvider->store($steamId1);
$userProvider->store($steamId2);
$userProvider->store($steamId1, 'user1');
$userProvider->store($steamId2, 'user2');
$upload = new Upload(
'foodemo',
'DER',
'ULB',
$userProvider->getUserId('2345678'),
$userProvider->getUserId('2345678', 'user2'),
'securehash'
);

View file

@ -112,7 +112,7 @@ class DemoListProviderTest extends TestCase {
public function testByUploader() {
$steamId = $this->getSteamId('12345', 'bar');
$this->userProvider->store($steamId);
$this->userProvider->store($steamId, 'bar');
$userId = $this->userProvider->get($steamId->getSteamId64())->getId();
$id1 = $this->demoProvider->storeDemo($this->getDemo($userId, 'map1', 17), 'foo', 'bar');
$id2 = $this->demoProvider->storeDemo($this->getDemo($userId, 'map2', 18), 'foo', 'bar');
@ -126,7 +126,7 @@ class DemoListProviderTest extends TestCase {
public function testByUploaderFilter() {
$steamId = $this->getSteamId('12345', 'bar');
$this->userProvider->store($steamId);
$this->userProvider->store($steamId, 'bar');
$userId = $this->userProvider->get($steamId->getSteamId64())->getId();
$id1 = $this->demoProvider->storeDemo($this->getDemo($userId, 'map1', 12), 'foo', 'bar');
$id2 = $this->demoProvider->storeDemo($this->getDemo($userId, 'map2', 18), 'foo', 'bar');
@ -146,9 +146,9 @@ class DemoListProviderTest extends TestCase {
$steamId1 = $this->getSteamId('12345', 'bar1');
$steamId2 = $this->getSteamId('22345', 'bar2');
$steamId3 = $this->getSteamId('32345', 'bar3');
$this->userProvider->store($steamId1);
$this->userProvider->store($steamId2);
$this->userProvider->store($steamId3);
$this->userProvider->store($steamId1, 'bar1');
$this->userProvider->store($steamId2, 'bar2');
$this->userProvider->store($steamId3, 'bar3');
$userId1 = $this->userProvider->get($steamId1->getSteamId64())->getId();
$userId2 = $this->userProvider->get($steamId2->getSteamId64())->getId();
$userId3 = $this->userProvider->get($steamId3->getSteamId64())->getId();
@ -187,7 +187,7 @@ class DemoListProviderTest extends TestCase {
public function testByUploaderFilterBackend() {
$steamId = $this->getSteamId('12345', 'bar');
$this->userProvider->store($steamId);
$this->userProvider->store($steamId, 'bar');
$userId = $this->userProvider->get($steamId->getSteamId64())->getId();
$id1 = $this->demoProvider->storeDemo($this->getDemo($userId, 'map1', 12), 'foo1', 'bar');
$id2 = $this->demoProvider->storeDemo($this->getDemo($userId, 'map2', 18), 'foo2', 'bar');

View file

@ -42,7 +42,7 @@ class DemoProviderTest extends TestCase {
public function testStoreRetrieve() {
$uploaderSteamId = $this->getSteamId('12345', 'test');
$this->userProvider->store($uploaderSteamId);
$this->userProvider->store($uploaderSteamId, 'test');
$uploader = $this->userProvider->get($uploaderSteamId->getSteamId64());
@ -82,17 +82,17 @@ class DemoProviderTest extends TestCase {
public function testRetrieveWithPlayers() {
$uploaderSteamId = $this->getSteamId('12345', 'test');
$this->userProvider->store($uploaderSteamId);
$this->userProvider->store($uploaderSteamId, 'test');
$steamId1 = $this->getSteamId('1', 'u1');
$steamId2 = $this->getSteamId('2', 'u2');
$steamId3 = $this->getSteamId('3', 'u3');
$steamId4 = $this->getSteamId('4', 'u4');
$this->userProvider->store($steamId1);
$this->userProvider->store($steamId2);
$this->userProvider->store($steamId3);
$this->userProvider->store($steamId4);
$this->userProvider->store($steamId1, 'u1');
$this->userProvider->store($steamId2, 'u2');
$this->userProvider->store($steamId3, 'u3');
$this->userProvider->store($steamId4, 'u4');
$user1 = $this->userProvider->get($steamId1->getSteamId64());
$user2 = $this->userProvider->get($steamId2->getSteamId64());
@ -142,10 +142,10 @@ class DemoProviderTest extends TestCase {
return $a->getUserId() - $b->getUserId();
});
$this->assertEquals([
new DemoPlayer($player1, $user1->getId(), 'user_' . $user1->getId(), 'red', 'scout', '1', 'foo_medium.jpg', 2, 0, 1),
new DemoPlayer($player2, $user2->getId(), 'user_' . $user2->getId(), 'red', 'soldier', '2', 'foo_medium.jpg', 0, 1, 0),
new DemoPlayer($player3, $user3->getId(), 'user_' . $user3->getId(), 'blue', 'engineer', '3', 'foo_medium.jpg', 0, 0, 2),
new DemoPlayer($player4, $user4->getId(), 'user_' . $user4->getId(), 'blue', 'spy', '4', 'foo_medium.jpg', 1, 0, 0),
new DemoPlayer($player1, $user1->getId(), 'user_' . $user1->getId(), 'red', 'scout', '1', 2, 0, 1),
new DemoPlayer($player2, $user2->getId(), 'user_' . $user2->getId(), 'red', 'soldier', '2', 0, 1, 0),
new DemoPlayer($player3, $user3->getId(), 'user_' . $user3->getId(), 'blue', 'engineer', '3', 0, 0, 2),
new DemoPlayer($player4, $user4->getId(), 'user_' . $user4->getId(), 'blue', 'spy', '4', 1, 0, 0),
], $players);
}
@ -163,7 +163,7 @@ class DemoProviderTest extends TestCase {
public function testSetDemoUrl() {
$uploaderSteamId = $this->getSteamId('12345', 'test');
$this->userProvider->store($uploaderSteamId);
$this->userProvider->store($uploaderSteamId, 'test');
$uploader = $this->userProvider->get($uploaderSteamId->getSteamId64());

View file

@ -226,7 +226,7 @@ class UploadProviderTest extends TestCase {
file_put_contents($this->tmpDir . '/foo.dem', 'asd');
$steamId = $this->getSteamId('123', 'a');
$token = $this->userProvider->store($steamId);
$token = $this->userProvider->store($steamId, 'a');
$this->uploadProvider->upload($token, 'RED', 'BLU', 'dummy', $this->tmpDir . '/foo.dem');
}
@ -262,7 +262,7 @@ class UploadProviderTest extends TestCase {
);
$steamId = $this->getSteamId('123', 'a');
$token = $this->userProvider->store($steamId);
$token = $this->userProvider->store($steamId, 'a');
$this->assertEquals(
'STV available at: http://example.com/' . $id,
@ -272,7 +272,7 @@ class UploadProviderTest extends TestCase {
private function saveSteamId($steamId, $name) {
$steamId = $this->getSteamId(Parser::convertSteamIdToCommunityId($steamId), $name);
$this->userProvider->store($steamId);
$this->userProvider->store($steamId, $name);
}
public function testUpload() {
@ -280,7 +280,7 @@ class UploadProviderTest extends TestCase {
copy(__DIR__ . '/../data/product-raw.json', $this->tmpDir . '/foo-raw.json');
$steamId = $this->getSteamId('123', 'a');
$token = $this->userProvider->store($steamId);
$token = $this->userProvider->store($steamId, 'a');
// pre-save the names so we dont have to get them from steam
$this->saveSteamId('[U:1:64229260]', 'Icewind');

View file

@ -31,30 +31,30 @@ class UserProviderTest extends TestCase {
}
public function testStoreRetrieve() {
$this->provider->store($this->steamId);
$this->provider->store($this->steamId, 'Icewind');
$user = $this->provider->get('76561198024494988');
$this->assertEquals($this->steamId->getNickname(), $user->getName());
$this->assertEquals('Icewind', $user->getName());
$this->assertEquals($this->steamId->getSteamId64(), $user->getSteamId());
}
public function returnTokenExisting() {
$token1 = $this->provider->store($this->steamId);
$token2 = $this->provider->store($this->steamId);
$token1 = $this->provider->store($this->steamId, 'Icewind');
$token2 = $this->provider->store($this->steamId, 'Icewind');
$this->assertEquals($token1, $token2);
}
public function testDoubleInsert() {
$this->provider->store($this->steamId);
$this->provider->store($this->steamId);
$this->provider->store($this->steamId, 'Icewind');
$this->provider->store($this->steamId, 'Icewind');
$this->assertTrue(true);
}
public function testByKey() {
$token = $this->provider->store($this->steamId);
$token = $this->provider->store($this->steamId, 'Icewind');
$byKey = $this->provider->byKey($token);
$this->assertEquals('76561198024494988', $byKey->getSteamId());
@ -65,7 +65,7 @@ class UserProviderTest extends TestCase {
$this->assertCount(0, $result);
$this->provider->store($this->steamId);
$this->provider->store($this->steamId, 'Icewind');
$user = $this->provider->get($this->steamId->getSteamId64());
$this->playerProvider->store(new Player(
0,
@ -85,15 +85,15 @@ class UserProviderTest extends TestCase {
}
public function testGetIdExisting() {
$this->provider->store($this->steamId);
$this->provider->store($this->steamId, 'Icewind');
$user = $this->provider->get($this->steamId->getSteamId64());
$this->assertEquals($user->getId(), $this->provider->getUserId($this->steamId->getSteamId64()));
$this->assertEquals($user->getId(), $this->provider->getUserId($this->steamId->getSteamId64(), 'Icewind'));
}
public function testGetIdNew() {
$id = $this->provider->getUserId($this->steamId->getSteamId64());
$id = $this->provider->getUserId($this->steamId->getSteamId64(), 'Icewind');
$user = $this->provider->get($this->steamId->getSteamId64());

View file

@ -59,14 +59,7 @@ abstract class TestCase extends \PHPUnit\Framework\TestCase {
return $factory->getMediumStrengthGenerator();
}
protected function getSteamId($steamId, $name) {
$steamId = new \SteamId($steamId, false);
$closure = \Closure::bind(function ($steamId) use ($name) {
$steamId->nickname = $name;
$steamId->imageUrl = 'foo';
}, null, $steamId);
$closure($steamId);
return $steamId;
protected function getSteamId($steamId, $_name) {
return new \SteamId($steamId, false);
}
}