2016-09-21 13:43:43 +05:30
|
|
|
|
<?php
|
|
|
|
|
namespace api\modules\mojang\controllers;
|
|
|
|
|
|
|
|
|
|
use api\controllers\Controller;
|
|
|
|
|
use common\models\Account;
|
|
|
|
|
use common\models\UsernameHistory;
|
|
|
|
|
use Ramsey\Uuid\Uuid;
|
|
|
|
|
use Yii;
|
|
|
|
|
use yii\web\Response;
|
|
|
|
|
|
|
|
|
|
class ApiController extends Controller {
|
|
|
|
|
|
2017-09-19 22:36:16 +05:30
|
|
|
|
public function behaviors(): array {
|
2016-09-21 13:43:43 +05:30
|
|
|
|
$behaviors = parent::behaviors();
|
|
|
|
|
unset($behaviors['authenticator']);
|
|
|
|
|
|
|
|
|
|
return $behaviors;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function actionUuidByUsername($username, $at = null) {
|
|
|
|
|
if ($at !== null) {
|
|
|
|
|
/** @var UsernameHistory|null $record */
|
|
|
|
|
$record = UsernameHistory::find()
|
|
|
|
|
->andWhere(['username' => $username])
|
|
|
|
|
->orderBy(['applied_in' => SORT_DESC])
|
|
|
|
|
->andWhere(['<=', 'applied_in', $at])
|
|
|
|
|
->one();
|
|
|
|
|
|
|
|
|
|
// Запрос выше находит просто последний случай использования, не учитывая то, что ник
|
|
|
|
|
// мог быть сменён с тех пор. Поэтому дополнительно проводим проверку, чтобы ник находился
|
|
|
|
|
// в каком-либо периоде (т.е. существовала последующая запись) или последний использовавший
|
|
|
|
|
// ник пользователь не сменил его на нечто иное
|
|
|
|
|
$account = null;
|
|
|
|
|
if ($record !== null) {
|
2016-11-01 22:06:39 +05:30
|
|
|
|
if ($record->account->username === $record->username || $record->findNext($at) !== null) {
|
2016-09-21 13:43:43 +05:30
|
|
|
|
$account = $record->account;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
/** @var Account|null $record */
|
|
|
|
|
$account = Account::findOne(['username' => $username]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ($account === null) {
|
|
|
|
|
return $this->noContentResponse();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return [
|
|
|
|
|
'id' => str_replace('-', '', $account->uuid),
|
|
|
|
|
'name' => $account->username,
|
|
|
|
|
];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function actionUsernamesByUuid($uuid) {
|
|
|
|
|
try {
|
|
|
|
|
$uuid = Uuid::fromString($uuid)->toString();
|
2018-04-18 02:17:25 +05:30
|
|
|
|
} catch (\InvalidArgumentException $e) {
|
2016-09-21 13:43:43 +05:30
|
|
|
|
return $this->illegalArgumentResponse('Invalid uuid format.');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$account = Account::findOne(['uuid' => $uuid]);
|
|
|
|
|
if ($account === null) {
|
|
|
|
|
return $this->noContentResponse();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** @var UsernameHistory[] $usernameHistory */
|
|
|
|
|
$usernameHistory = $account->getUsernameHistory()
|
|
|
|
|
->orderBy(['applied_in' => SORT_ASC])
|
|
|
|
|
->all();
|
|
|
|
|
|
|
|
|
|
$data = [];
|
2018-04-18 02:17:25 +05:30
|
|
|
|
foreach ($usernameHistory as $record) {
|
2016-09-21 13:43:43 +05:30
|
|
|
|
$data[] = [
|
|
|
|
|
'name' => $record->username,
|
|
|
|
|
'changedToAt' => $record->applied_in * 1000,
|
|
|
|
|
];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// У первого элемента не должно быть времени, когда он был применён
|
|
|
|
|
// Хотя мы в принципе эту инфу знаем. А вот Mojang, вероятно, нет
|
|
|
|
|
unset($data[0]['changedToAt']);
|
|
|
|
|
|
|
|
|
|
return $data;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function actionUuidsByUsernames() {
|
|
|
|
|
$usernames = Yii::$app->request->post();
|
|
|
|
|
if (empty($usernames)) {
|
2017-05-31 05:40:22 +05:30
|
|
|
|
return $this->illegalArgumentResponse('Passed array of profile names is an invalid JSON string.');
|
2016-09-21 13:43:43 +05:30
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$usernames = array_unique($usernames);
|
|
|
|
|
if (count($usernames) > 100) {
|
|
|
|
|
return $this->illegalArgumentResponse('Not more that 100 profile name per call is allowed.');
|
|
|
|
|
}
|
|
|
|
|
|
2018-04-18 02:17:25 +05:30
|
|
|
|
foreach ($usernames as $username) {
|
2016-09-21 13:43:43 +05:30
|
|
|
|
if (empty($username) || is_array($username)) {
|
|
|
|
|
return $this->illegalArgumentResponse('profileName can not be null, empty or array key.');
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** @var Account[] $accounts */
|
|
|
|
|
$accounts = Account::find()
|
|
|
|
|
->andWhere(['username' => $usernames])
|
|
|
|
|
->orderBy(['username' => $usernames])
|
|
|
|
|
->limit(count($usernames))
|
|
|
|
|
->all();
|
|
|
|
|
|
|
|
|
|
$responseData = [];
|
2018-04-18 02:17:25 +05:30
|
|
|
|
foreach ($accounts as $account) {
|
2016-09-21 13:43:43 +05:30
|
|
|
|
$responseData[] = [
|
|
|
|
|
'id' => str_replace('-', '', $account->uuid),
|
|
|
|
|
'name' => $account->username,
|
|
|
|
|
];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $responseData;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private function noContentResponse() {
|
|
|
|
|
$response = Yii::$app->getResponse();
|
|
|
|
|
$response->setStatusCode(204);
|
|
|
|
|
$response->format = Response::FORMAT_RAW;
|
|
|
|
|
$response->content = '';
|
|
|
|
|
|
|
|
|
|
return $response;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private function illegalArgumentResponse(string $errorMessage) {
|
|
|
|
|
$response = Yii::$app->getResponse();
|
|
|
|
|
$response->setStatusCode(400);
|
|
|
|
|
$response->format = Response::FORMAT_JSON;
|
|
|
|
|
$response->data = [
|
|
|
|
|
'error' => 'IllegalArgumentException',
|
|
|
|
|
'errorMessage' => $errorMessage,
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
return $response;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|