2016-09-21 13:43:43 +05:30
|
|
|
<?php
|
2021-06-14 00:51:25 +05:30
|
|
|
declare(strict_types=1);
|
|
|
|
|
2016-09-21 13:43:43 +05:30
|
|
|
namespace api\modules\mojang\controllers;
|
|
|
|
|
|
|
|
use api\controllers\Controller;
|
|
|
|
use common\models\Account;
|
|
|
|
use common\models\UsernameHistory;
|
|
|
|
use Ramsey\Uuid\Uuid;
|
|
|
|
use Yii;
|
2021-06-14 00:51:25 +05:30
|
|
|
use yii\filters\VerbFilter;
|
|
|
|
use yii\helpers\ArrayHelper;
|
|
|
|
use yii\helpers\UnsetArrayValue;
|
2016-09-21 13:43:43 +05:30
|
|
|
use yii\web\Response;
|
|
|
|
|
|
|
|
class ApiController extends Controller {
|
|
|
|
|
2017-09-19 22:36:16 +05:30
|
|
|
public function behaviors(): array {
|
2021-06-14 00:51:25 +05:30
|
|
|
return ArrayHelper::merge(parent::behaviors(), [
|
|
|
|
'authenticator' => new UnsetArrayValue(),
|
|
|
|
'verbs' => [
|
|
|
|
'class' => VerbFilter::class,
|
|
|
|
'actions' => [
|
|
|
|
'actionUuidsByUsernames' => ['POST'],
|
|
|
|
],
|
|
|
|
],
|
|
|
|
]);
|
2016-09-21 13:43:43 +05:30
|
|
|
}
|
|
|
|
|
2021-06-14 00:58:13 +05:30
|
|
|
public function actionUuidByUsername(string $username, int $at = null) {
|
2016-09-21 13:43:43 +05:30
|
|
|
if ($at !== null) {
|
|
|
|
/** @var UsernameHistory|null $record */
|
|
|
|
$record = UsernameHistory::find()
|
|
|
|
->andWhere(['username' => $username])
|
|
|
|
->orderBy(['applied_in' => SORT_DESC])
|
|
|
|
->andWhere(['<=', 'applied_in', $at])
|
|
|
|
->one();
|
|
|
|
|
2019-07-15 04:29:56 +05:30
|
|
|
// The query above simply finds the latest case of usage, without taking into account the fact
|
|
|
|
// that the nickname may have been changed since then. Therefore, we additionally check
|
|
|
|
// that the nickname is in some period (i.e. there is a subsequent entry) or that the last user
|
|
|
|
// who used the nickname has not changed it to something else
|
2016-09-21 13:43:43 +05:30
|
|
|
$account = null;
|
|
|
|
if ($record !== null) {
|
2020-06-12 02:57:02 +05:30
|
|
|
if ($record->account->username === $record->username || $record->findNextOwnerUsername($at) !== null) {
|
2016-09-21 13:43:43 +05:30
|
|
|
$account = $record->account;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
2024-12-02 15:40:55 +05:30
|
|
|
/** @var Account|null $account */
|
2016-09-21 13:43:43 +05:30
|
|
|
$account = Account::findOne(['username' => $username]);
|
|
|
|
}
|
|
|
|
|
2020-06-12 02:57:02 +05:30
|
|
|
if ($account === null || $account->status === Account::STATUS_DELETED) {
|
2016-09-21 13:43:43 +05:30
|
|
|
return $this->noContentResponse();
|
|
|
|
}
|
|
|
|
|
|
|
|
return [
|
|
|
|
'id' => str_replace('-', '', $account->uuid),
|
|
|
|
'name' => $account->username,
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2021-06-14 00:58:13 +05:30
|
|
|
public function actionUsernamesByUuid(string $uuid) {
|
2016-09-21 13:43:43 +05:30
|
|
|
try {
|
|
|
|
$uuid = Uuid::fromString($uuid)->toString();
|
2024-12-02 15:40:55 +05:30
|
|
|
} catch (\InvalidArgumentException) {
|
2016-09-21 13:43:43 +05:30
|
|
|
return $this->illegalArgumentResponse('Invalid uuid format.');
|
|
|
|
}
|
|
|
|
|
2020-06-12 02:57:02 +05:30
|
|
|
$account = Account::find()->excludeDeleted()->andWhere(['uuid' => $uuid])->one();
|
2016-09-21 13:43:43 +05:30
|
|
|
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,
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2019-07-15 04:29:56 +05:30
|
|
|
// The first element shouldn't have time when it was applied.
|
|
|
|
// Although we know this information in fact. But Mojang probably doesn't.
|
2016-09-21 13:43:43 +05:30
|
|
|
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])
|
2020-06-12 02:57:02 +05:30
|
|
|
->excludeDeleted()
|
2016-09-21 13:43:43 +05:30
|
|
|
->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;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|