1
0
Fork 0
mirror of synced 2024-08-02 12:01:37 +12:00

Fix race condition

This commit is contained in:
Matej Bačo 2023-04-26 13:51:19 +02:00
parent a7f81bd628
commit 837cebcdda

View file

@ -76,6 +76,7 @@ $getUserGitHub = function (Document $user, Document $project, Database $dbForPro
$oauth2 = new $className($appId, $appSecret, '', [], []); $oauth2 = new $className($appId, $appSecret, '', [], []);
try {
$oauth2->refreshTokens($refreshToken); $oauth2->refreshTokens($refreshToken);
$accessToken = $oauth2->getAccessToken(''); $accessToken = $oauth2->getAccessToken('');
@ -86,9 +87,15 @@ $getUserGitHub = function (Document $user, Document $project, Database $dbForPro
->setAttribute('providerRefreshToken', $refreshToken) ->setAttribute('providerRefreshToken', $refreshToken)
->setAttribute('providerAccessTokenExpiry', DateTime::addSeconds(new \DateTime(), (int)$oauth2->getAccessTokenExpiry(''))); ->setAttribute('providerAccessTokenExpiry', DateTime::addSeconds(new \DateTime(), (int)$oauth2->getAccessTokenExpiry('')));
Authorization::skip(fn() => $dbForProject->updateDocument('sessions', $session->getId(), $session)); Authorization::skip(fn () => $dbForProject->updateDocument('sessions', $session->getId(), $session));
$dbForProject->deleteCachedDocument('users', $user->getId()); $dbForProject->deleteCachedDocument('users', $user->getId());
} catch (Throwable $err) {
// Race contition
$sessions = $user->getAttribute('sessions', []);
$session = $sessions[0] ?? new Document();
$accessToken = $session->getAttribute('providerAccessToken');
}
$githubUser = $oauth2->getUserSlug($accessToken); $githubUser = $oauth2->getUserSlug($accessToken);
$githubId = $oauth2->getUserID($accessToken); $githubId = $oauth2->getUserID($accessToken);
@ -474,9 +481,9 @@ App::get('/v1/cards/cloud')
->desc('Get Front Of Cloud Card') ->desc('Get Front Of Cloud Card')
->groups(['api', 'avatars']) ->groups(['api', 'avatars'])
->label('scope', 'avatars.read') ->label('scope', 'avatars.read')
->label('cache', true) // ->label('cache', true)
->label('cache.resourceType', 'cards/cloud') // ->label('cache.resourceType', 'cards/cloud')
->label('cache.resource', 'card/{request.userId}') // ->label('cache.resource', 'card/{request.userId}')
->label('docs', false) ->label('docs', false)
->label('origin', '*') ->label('origin', '*')
->param('userId', '', new UID(), 'User ID.', true) ->param('userId', '', new UID(), 'User ID.', true)
@ -498,7 +505,7 @@ App::get('/v1/cards/cloud')
throw new Exception(Exception::USER_NOT_FOUND); throw new Exception(Exception::USER_NOT_FOUND);
} }
if(!$mock) { if (!$mock) {
$name = $user->getAttribute('name', 'Anonymous'); $name = $user->getAttribute('name', 'Anonymous');
$email = $user->getAttribute('email', ''); $email = $user->getAttribute('email', '');
$createdAt = new \DateTime($user->getCreatedAt()); $createdAt = new \DateTime($user->getCreatedAt());
@ -538,7 +545,7 @@ App::get('/v1/cards/cloud')
$baseImage = new \Imagick("public/images/cards/cloud/" . $imagePath); $baseImage = new \Imagick("public/images/cards/cloud/" . $imagePath);
if($isEmployee) { if ($isEmployee) {
$image = new Imagick('public/images/cards/cloud/employee.png'); $image = new Imagick('public/images/cards/cloud/employee.png');
$image->setGravity(Imagick::GRAVITY_CENTER); $image->setGravity(Imagick::GRAVITY_CENTER);
$baseImage->compositeImage($image, Imagick::COMPOSITE_OVER, 795, 35); $baseImage->compositeImage($image, Imagick::COMPOSITE_OVER, 795, 35);
@ -562,11 +569,11 @@ App::get('/v1/cards/cloud')
$startX = 900; $startX = 900;
$totalWidth = $metricsHashtag['textWidth'] + 12 + $metricsText['textWidth']; $totalWidth = $metricsHashtag['textWidth'] + 12 + $metricsText['textWidth'];
$hashtagX = ($metricsHashtag['textWidth']/2); $hashtagX = ($metricsHashtag['textWidth'] / 2);
$textX = $hashtagX + 12 + ($metricsText['textWidth']/2); $textX = $hashtagX + 12 + ($metricsText['textWidth'] / 2);
$hashtagX -= $totalWidth/2; $hashtagX -= $totalWidth / 2;
$textX -= $totalWidth/2; $textX -= $totalWidth / 2;
$hashtagX += $startX; $hashtagX += $startX;
$textX += $startX; $textX += $startX;
@ -575,13 +582,13 @@ App::get('/v1/cards/cloud')
$baseImage->annotateImage($text, $textX, 150, 0, $employeeNumber); $baseImage->annotateImage($text, $textX, 150, 0, $employeeNumber);
} }
if($isContributor) { if ($isContributor) {
$image = new Imagick('public/images/cards/cloud/contributor.png'); $image = new Imagick('public/images/cards/cloud/contributor.png');
$image->setGravity(Imagick::GRAVITY_CENTER); $image->setGravity(Imagick::GRAVITY_CENTER);
$baseImage->compositeImage($image, Imagick::COMPOSITE_OVER, 795, 35); $baseImage->compositeImage($image, Imagick::COMPOSITE_OVER, 795, 35);
} }
if($isHero) { if ($isHero) {
$image = new Imagick('public/images/cards/cloud/hero.png'); $image = new Imagick('public/images/cards/cloud/hero.png');
$image->setGravity(Imagick::GRAVITY_CENTER); $image->setGravity(Imagick::GRAVITY_CENTER);
$baseImage->compositeImage($image, Imagick::COMPOSITE_OVER, 795, 35); $baseImage->compositeImage($image, Imagick::COMPOSITE_OVER, 795, 35);
@ -597,11 +604,11 @@ App::get('/v1/cards/cloud')
$text->setFont("public/fonts/Poppins-Bold.ttf"); $text->setFont("public/fonts/Poppins-Bold.ttf");
$text->setFillColor(new \ImagickPixel('#FFFFFF')); $text->setFillColor(new \ImagickPixel('#FFFFFF'));
if(\strlen($name) > 33) { if (\strlen($name) > 33) {
$name = \substr($name, 0, 33); $name = \substr($name, 0, 33);
} }
if(\strlen($name) <= 23) { if (\strlen($name) <= 23) {
$text->setFontSize(80); $text->setFontSize(80);
} else { } else {
$text->setFontSize(54); $text->setFontSize(54);
@ -649,9 +656,9 @@ App::get('/v1/cards/cloud-back')
->desc('Get Back Of Cloud Card') ->desc('Get Back Of Cloud Card')
->groups(['api', 'avatars']) ->groups(['api', 'avatars'])
->label('scope', 'avatars.read') ->label('scope', 'avatars.read')
->label('cache', true) // ->label('cache', true)
->label('cache.resourceType', 'cards/cloud-back') // ->label('cache.resourceType', 'cards/cloud-back')
->label('cache.resource', 'card/{request.userId}') // ->label('cache.resource', 'card/{request.userId}')
->label('docs', false) ->label('docs', false)
->label('origin', '*') ->label('origin', '*')
->param('userId', '', new UID(), 'User ID.', true) ->param('userId', '', new UID(), 'User ID.', true)
@ -673,7 +680,7 @@ App::get('/v1/cards/cloud-back')
throw new Exception(Exception::USER_NOT_FOUND); throw new Exception(Exception::USER_NOT_FOUND);
} }
if(!$mock) { if (!$mock) {
$userId = $user->getId(); $userId = $user->getId();
$email = $user->getAttribute('email', ''); $email = $user->getAttribute('email', '');
@ -750,7 +757,7 @@ App::get('/v1/cards/cloud-og')
throw new Exception(Exception::USER_NOT_FOUND); throw new Exception(Exception::USER_NOT_FOUND);
} }
if(!$mock) { if (!$mock) {
$internalId = $user->getInternalId(); $internalId = $user->getInternalId();
$bgVariation = $internalId % 3 === 0 ? '1' : ($internalId % 3 === 1 ? '2' : '3'); $bgVariation = $internalId % 3 === 0 ? '1' : ($internalId % 3 === 1 ? '2' : '3');
$cardVariation = $internalId % 2 === 0 ? '1' : '2'; $cardVariation = $internalId % 2 === 0 ? '1' : '2';
@ -770,7 +777,7 @@ App::get('/v1/cards/cloud-og')
$isPlatinum = $user->getInternalId() % 100 === 0; $isPlatinum = $user->getInternalId() % 100 === 0;
} else { } else {
$bgVariation = \str_ends_with($mock, '-bg2') ? '2' :( \str_ends_with($mock, '-bg3') ? '3' : '1'); $bgVariation = \str_ends_with($mock, '-bg2') ? '2' : (\str_ends_with($mock, '-bg3') ? '3' : '1');
$cardVariation = \str_ends_with($mock, '-right') ? '2' : '1'; $cardVariation = \str_ends_with($mock, '-right') ? '2' : '1';
$name = $mock === 'normal-long' ? 'Sir First Walter O\'Brian Junior' : 'Walter O\'Brian'; $name = $mock === 'normal-long' ? 'Sir First Walter O\'Brian Junior' : 'Walter O\'Brian';
$createdAt = new \DateTime('now'); $createdAt = new \DateTime('now');
@ -797,17 +804,17 @@ App::get('/v1/cards/cloud-og')
$cardType = $isGolden ? '-golden' : ($isPlatinum ? '-platinum' : ''); $cardType = $isGolden ? '-golden' : ($isPlatinum ? '-platinum' : '');
$image = new Imagick("public/images/cards/cloud/og-card{$cardType}{$cardVariation}.png"); $image = new Imagick("public/images/cards/cloud/og-card{$cardType}{$cardVariation}.png");
$baseImage->compositeImage($image, Imagick::COMPOSITE_OVER, 1008/2 - $image->getImageWidth() / 2, 1008/2 - $image->getImageHeight() / 2); $baseImage->compositeImage($image, Imagick::COMPOSITE_OVER, 1008 / 2 - $image->getImageWidth() / 2, 1008 / 2 - $image->getImageHeight() / 2);
$image = new Imagick("public/images/cards/cloud/og-background-logo.png"); $image = new Imagick("public/images/cards/cloud/og-background-logo.png");
if($cardVariation === '1') { if ($cardVariation === '1') {
$baseImage->compositeImage($image, Imagick::COMPOSITE_OVER, 32, 1008 - $image->getImageHeight() - 32); $baseImage->compositeImage($image, Imagick::COMPOSITE_OVER, 32, 1008 - $image->getImageHeight() - 32);
} else { } else {
$baseImage->compositeImage($image, Imagick::COMPOSITE_OVER, 1008 - $image->getImageWidth() - 32, 1008 - $image->getImageHeight() - 32); $baseImage->compositeImage($image, Imagick::COMPOSITE_OVER, 1008 - $image->getImageWidth() - 32, 1008 - $image->getImageHeight() - 32);
} }
$image = new Imagick("public/images/cards/cloud/og-shadow{$cardType}.png"); $image = new Imagick("public/images/cards/cloud/og-shadow{$cardType}.png");
if($cardVariation === '1') { if ($cardVariation === '1') {
$baseImage->compositeImage($image, Imagick::COMPOSITE_OVER, -450, 700); $baseImage->compositeImage($image, Imagick::COMPOSITE_OVER, -450, 700);
} else { } else {
$baseImage->compositeImage($image, Imagick::COMPOSITE_OVER, -20, 710); $baseImage->compositeImage($image, Imagick::COMPOSITE_OVER, -20, 710);