Set up all GetLink endpoints
This commit is contained in:
parent
25c2d13ca3
commit
10d19d35ef
@ -2,58 +2,118 @@
|
||||
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use App\LinkCode;
|
||||
use App\Services\CodeGeneratorService;
|
||||
use App\Services\LodestoneApiService;
|
||||
use App\Services\LodestoneCacheService;
|
||||
use App\UserLink;
|
||||
use Carbon\Carbon;
|
||||
|
||||
class LodestoneLinkController extends Controller
|
||||
{
|
||||
private $codeGeneratorService;
|
||||
private $lodestoneCacheService;
|
||||
private $lodestoneApiService;
|
||||
|
||||
public function __construct(
|
||||
CodeGeneratorService $codeGeneratorService,
|
||||
LodestoneCacheService $lodestoneCacheService,
|
||||
LodestoneApiService $lodestoneApiService
|
||||
)
|
||||
{
|
||||
$this->codeGeneratorService = $codeGeneratorService;
|
||||
$this->lodestoneCacheService = $lodestoneCacheService;
|
||||
$this->lodestoneApiService = $lodestoneApiService;
|
||||
}
|
||||
|
||||
public function checkDiscordName(
|
||||
string $server,
|
||||
string $name,
|
||||
int $discordGuildId,
|
||||
int $discordUserId,
|
||||
CodeGeneratorService $codeGeneratorService,
|
||||
LodestoneCacheService $lodestoneCacheService,
|
||||
LodestoneApiService $lodestoneApiService
|
||||
int $discordUserId
|
||||
) {
|
||||
$character = $lodestoneCacheService->getCharacterByName($server, $name, $lodestoneApiService)->content;
|
||||
$character = $this->lodestoneCacheService->getCharacterByName($server, $name, $this->lodestoneApiService)->content;
|
||||
|
||||
if (UserLink::where('lodestoneId', $character['Character']['ID'])->count() > 0)
|
||||
// Check already linked
|
||||
if (UserLink::where('lodestoneId', $character['Character']['ID'])->count() > 0) {
|
||||
return [
|
||||
'linked' => true
|
||||
'linked' => true,
|
||||
'linkCode' => null,
|
||||
'expires' => null
|
||||
];
|
||||
}
|
||||
|
||||
// TODO: Store this code in the LinkCode table
|
||||
$code = $codeGeneratorService->generateCode(
|
||||
// Check code exists
|
||||
$linkCode = LinkCode::where('lodestoneId', $character['Character']['ID'])->first();
|
||||
$expirationTime = Carbon::now()->addMinutes(5);
|
||||
if ($linkCode !== null && $linkCode->expires > Carbon::now()) {
|
||||
$linkCode->expires = $expirationTime;
|
||||
$linkCode->save();
|
||||
} else {
|
||||
if ($linkCode !== null)
|
||||
$linkCode->delete();
|
||||
$identifier = $this->codeGeneratorService->generateIdentifierDiscordName($server, $name, $discordGuildId, $discordUserId);
|
||||
$code = $this->codeGeneratorService->generateCode(
|
||||
'discord',
|
||||
$codeGeneratorService->generateIdentifierDiscordName($server, $name, $discordGuildId, $discordUserId)
|
||||
$identifier
|
||||
);
|
||||
$linkCode = LinkCode::create([
|
||||
'lodestoneId' => $character['Character']['ID'],
|
||||
'discordUserId' => $discordUserId,
|
||||
'expires' => $expirationTime,
|
||||
'code' => $code
|
||||
]);
|
||||
}
|
||||
|
||||
return [
|
||||
'linked' => false,
|
||||
'linkCode' => $code
|
||||
'linkCode' => $linkCode->code,
|
||||
'expires' => $linkCode->expires
|
||||
];
|
||||
}
|
||||
|
||||
public function checkDiscordId(
|
||||
int $lodestoneId,
|
||||
int $discordGuildId,
|
||||
int $discordUserId,
|
||||
CodeGeneratorService $codeGeneratorService
|
||||
int $discordUserId
|
||||
) {
|
||||
if (UserLink::where('lodestoneId', $lodestoneId)->count() > 0)
|
||||
$character = $this->lodestoneCacheService->getCharacterById($lodestoneId, $this->lodestoneApiService)->content;
|
||||
|
||||
// Check already linked
|
||||
if (UserLink::where('lodestoneId', $character['Character']['ID'])->count() > 0) {
|
||||
return [
|
||||
'linked' => true
|
||||
'linked' => true,
|
||||
'linkCode' => null,
|
||||
'expires' => null
|
||||
];
|
||||
}
|
||||
|
||||
// Check code exists
|
||||
$linkCode = LinkCode::where('lodestoneId', $character['Character']['ID'])->first();
|
||||
$expirationTime = Carbon::now()->addMinutes(5);
|
||||
if ($linkCode !== null && $linkCode->expires > Carbon::now()) {
|
||||
$linkCode->expires = $expirationTime;
|
||||
$linkCode->save();
|
||||
} else {
|
||||
if ($linkCode !== null)
|
||||
$linkCode->delete();
|
||||
$identifier = $this->codeGeneratorService->generateIdentifierDiscordId($lodestoneId, $discordGuildId, $discordUserId);
|
||||
$code = $this->codeGeneratorService->generateCode(
|
||||
'discord',
|
||||
$identifier
|
||||
);
|
||||
$linkCode = LinkCode::create([
|
||||
'lodestoneId' => $character['Character']['ID'],
|
||||
'discordUserId' => $discordUserId,
|
||||
'expires' => $expirationTime,
|
||||
'code' => $code
|
||||
]);
|
||||
}
|
||||
|
||||
return [
|
||||
'linked' => false,
|
||||
'linkCode' => $codeGeneratorService->generateCode(
|
||||
'discord',
|
||||
$codeGeneratorService->generateIdentifierDiscordId($lodestoneId, $discordGuildId, $discordUserId)
|
||||
)
|
||||
'linkCode' => $linkCode->code,
|
||||
'expires' => $linkCode->expires
|
||||
];
|
||||
}
|
||||
|
||||
@ -61,36 +121,90 @@ class LodestoneLinkController extends Controller
|
||||
string $server,
|
||||
string $name,
|
||||
string $hostname,
|
||||
int $websiteUserId,
|
||||
CodeGeneratorService $codeGeneratorService,
|
||||
LodestoneCacheService $lodestoneCacheService,
|
||||
LodestoneApiService $lodestoneApiService
|
||||
int $websiteUserId
|
||||
) {
|
||||
$character = $lodestoneCacheService->getCharacterByName($server, $name, $lodestoneApiService)->content;
|
||||
$character = $this->lodestoneCacheService->getCharacterByName($server, $name, $this->lodestoneApiService)->content;
|
||||
|
||||
// Check already linked
|
||||
if (UserLink::where('lodestoneId', $character['Character']['ID'])->count() > 0) {
|
||||
return [
|
||||
'linked' => true,
|
||||
'linkCode' => null,
|
||||
'expires' => null
|
||||
];
|
||||
}
|
||||
|
||||
// Check code exists
|
||||
$linkCode = LinkCode::where('lodestoneId', $character['Character']['ID'])->first();
|
||||
$expirationTime = Carbon::now()->addMinutes(5);
|
||||
if ($linkCode !== null && $linkCode->expires > Carbon::now()) {
|
||||
$linkCode->expires = $expirationTime;
|
||||
$linkCode->save();
|
||||
} else {
|
||||
if ($linkCode !== null)
|
||||
$linkCode->delete();
|
||||
$identifier = $this->codeGeneratorService->generateIdentifierWebsiteName($server, $name, $hostname, $websiteUserId);
|
||||
$code = $this->codeGeneratorService->generateCode(
|
||||
'discord',
|
||||
$identifier
|
||||
);
|
||||
$linkCode = LinkCode::create([
|
||||
'lodestoneId' => $character['Character']['ID'],
|
||||
'websiteUserId' => $websiteUserId,
|
||||
'expires' => $expirationTime,
|
||||
'code' => $code
|
||||
]);
|
||||
}
|
||||
|
||||
return [
|
||||
'linked' => false,
|
||||
'linkCode' => $codeGeneratorService->generateCode(
|
||||
'discord',
|
||||
$codeGeneratorService->generateIdentifierWebsiteName($server, $name, $hostname, $websiteUserId)
|
||||
)
|
||||
'linkCode' => $linkCode->code,
|
||||
'expires' => $linkCode->expires
|
||||
];
|
||||
}
|
||||
|
||||
public function checkWebsiteId(
|
||||
int $lodestoneId,
|
||||
string $hostname,
|
||||
int $websiteUserId,
|
||||
CodeGeneratorService $codeGeneratorService,
|
||||
LodestoneCacheService $lodestoneCacheService,
|
||||
LodestoneApiService $lodestoneApiService
|
||||
int $websiteUserId
|
||||
) {
|
||||
$character = $lodestoneCacheService->getCharacterById($lodestoneId, $lodestoneApiService)->content;
|
||||
$character = $this->lodestoneCacheService->getCharacterById($lodestoneId, $this->lodestoneApiService)->content;
|
||||
|
||||
// Check already linked
|
||||
if (UserLink::where('lodestoneId', $character['Character']['ID'])->count() > 0) {
|
||||
return [
|
||||
'linked' => true,
|
||||
'linkCode' => null,
|
||||
'expires' => null
|
||||
];
|
||||
}
|
||||
|
||||
// Check code exists
|
||||
$linkCode = LinkCode::where('lodestoneId', $character['Character']['ID'])->first();
|
||||
$expirationTime = Carbon::now()->addMinutes(5);
|
||||
if ($linkCode !== null && $linkCode->expires > Carbon::now()) {
|
||||
$linkCode->expires = $expirationTime;
|
||||
$linkCode->save();
|
||||
} else {
|
||||
if ($linkCode !== null)
|
||||
$linkCode->delete();
|
||||
$identifier = $this->codeGeneratorService->generateIdentifierWebsiteId($lodestoneId, $hostname, $websiteUserId);
|
||||
$code = $this->codeGeneratorService->generateCode(
|
||||
'discord',
|
||||
$identifier
|
||||
);
|
||||
$linkCode = LinkCode::create([
|
||||
'lodestoneId' => $character['Character']['ID'],
|
||||
'websiteUserId' => $websiteUserId,
|
||||
'expires' => $expirationTime,
|
||||
'code' => $code
|
||||
]);
|
||||
}
|
||||
|
||||
return [
|
||||
'linked' => false,
|
||||
'linkCode' => $codeGeneratorService->generateCode(
|
||||
'discord',
|
||||
$codeGeneratorService->generateIdentifierWebsiteId($lodestoneId, $hostname, $websiteUserId)
|
||||
)
|
||||
'linkCode' => $linkCode->code,
|
||||
'expires' => $linkCode->expires
|
||||
];
|
||||
}
|
||||
|
||||
|
||||
Loading…
Reference in New Issue
Block a user