Skip to content

Commit

Permalink
Cache GitHub calls
Browse files Browse the repository at this point in the history
  • Loading branch information
Pyker committed May 22, 2022
1 parent 2bdd43f commit a193948
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 52 deletions.
31 changes: 12 additions & 19 deletions app/Http/Controllers/AuthController.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,6 @@
namespace App\Http\Controllers;

use App\Libraries\UpdateUtils;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Request;

class AuthController extends Controller
{
Expand All @@ -16,22 +13,18 @@ public function showLogin()

public function postLogin()
{
$email = Request::input('email');
$password = Request::input('password');
$remember = Request::boolean('remember');

$credentials = [
'email' => $email,
'password' => $password,
];

if (Auth::attempt($credentials, $remember)) {
Auth::user()->last_ip = Request::ip();
Auth::user()->save();

//Check for update on login
if (UpdateUtils::getUpdateCheck()) {
Cache::put('update', true, now()->addMinutes(60));
$remember = request()->boolean('remember');

$credentials = request()->only(['email', 'password']);

if (auth()->attempt($credentials, $remember)) {
$user = auth()->user();
$user->last_ip = request()->ip();
$user->save();

// Check for update on login if the user is a superuser
if ($user->permission->solder_full && UpdateUtils::getUpdateCheck()) {
cache()->put('update', true, now()->addMinutes(60));
}

return redirect()->intended('dashboard');
Expand Down
8 changes: 4 additions & 4 deletions app/Http/Controllers/SolderController.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,20 +48,20 @@ public function getUpdateCheck()
abort(404);
}

if (UpdateUtils::getUpdateCheck()) {
if (UpdateUtils::getUpdateCheck(true)) {
Cache::put('update', true, now()->addMinutes(60));

return response()->json([
'status' => 'success',
'success' => true,
'update' => true,
]);
} else {
if (Cache::get('update')) {
if (Cache::has('update')) {
Cache::forget('update');
}

return response()->json([
'status' => 'success',
'success' => true,
'update' => false,
]);
}
Expand Down
42 changes: 19 additions & 23 deletions app/Libraries/UpdateUtils.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,19 @@

class UpdateUtils
{
public static $githubclient;
private static Client $githubClient;

public static function init()
public static function getGithubClient(): Client
{
$client = new Client();
// TODO: Re-add caching (it got removed upstream)
self::$githubclient = $client;
return self::$githubClient ??= new Client();
}

public static function getUpdateCheck()
public static function getUpdateCheck($forceRefresh = false): bool
{
if ($forceRefresh) {
cache()->forget('update:github:tags');
}

$allVersions = self::getAllVersions();

if (! array_key_exists('error', $allVersions)) {
Expand All @@ -41,33 +43,27 @@ public static function getLatestVersion()

public static function getAllVersions()
{
try {
return self::$githubclient->api('repo')->tags('technicpack', 'technicsolder');
} catch (RuntimeException $e) {
return ['error' => 'Unable to pull version from Github - '.$e->getMessage()];
}
}

public static function getCommitInfo($commit = null)
{
if (is_null($commit)) {
$commit = self::getLatestVersion()['commit']['sha'];
}
$client = self::getGithubClient();

try {
return self::$githubclient->api('repo')->commits()->show('technicpack', 'technicsolder', $commit);
return cache()->remember('update:github:tags', now()->addMinutes(60), function () use ($client) {
return $client->api('repo')->tags('technicpack', 'technicsolder');
});
} catch (RuntimeException $e) {
return ['error' => 'Unable to pull commit info from Github - '.$e->getMessage()];
return ['error' => 'Unable to fetch versions from Github: ' . $e->getMessage()];
}
}

public static function getLatestChangeLog($branch = 'master')
{
$client = self::getGithubClient();

try {
return self::$githubclient->api('repo')->commits()->all('technicpack', 'technicsolder',
['sha' => $branch]);
return cache()->remember('update:github:changelog:' . $branch, now()->addMinutes(60), function () use ($client, $branch) {
return $client->api('repo')->commits()->all('technicpack', 'technicsolder', ['sha' => $branch]);
});
} catch (RuntimeException $e) {
return ['error' => 'Unable to pull changelog from Github - '.$e->getMessage()];
return ['error' => 'Unable to fetch changelog from Github: ' . $e->getMessage()];
}
}
}
2 changes: 0 additions & 2 deletions app/Providers/AppServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,6 @@ public function boot()
define('SOLDER_VERSION', 'v0.7.10');
}

UpdateUtils::init();

View::composer('layouts.master', function ($view) {
$modpacks = Cache::remember('allmodpacks', now()->addMinute(), function () {
return Modpack::all()->sortBy('name', SORT_NATURAL | SORT_FLAG_CASE);
Expand Down
8 changes: 4 additions & 4 deletions resources/views/solder/update.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@
@else
@foreach ($changelog as $change)
<a href="{{ $change['html_url'] }}" target="blank_" class="list-group-item">
<span class="badge" style="margin-left:5px;">{{ date_format(date_create($change['commit']['author']['date']), 'M, d, Y | g:i a') }}</span>
<span class="badge" style="margin-left:5px;">{{ date_format(date_create($change['commit']['author']['date']), 'M, d Y - g:i a') }}</span>
<img src="{{ $change['author']['avatar_url'] ?? $change['committer']['avatar_url'] }}" alt="{{ $change['author']['login'] ?? $change['commit']['author']['name'] ?? $change['committer']['login'] }}" height="23" width="23"> {{ $change['commit']['message'] }}
</a>
@endforeach
Expand All @@ -85,14 +85,14 @@
type: "GET",
url: "{{ URL::to('solder/update-check/') }}/",
success: function (data) {
if (data.status == "success") {
if(data.update) {
if (data.success) {
if (data.update) {
$("#solder-update-ajax").removeClass("alert-warning alert-success alert-danger").addClass("alert-danger").html('Solder is out of date. Please refer to the wiki on how to update.').fadeIn();
} else {
$("#solder-update-ajax").removeClass("alert-warning alert-success alert-danger").addClass("alert-success").html('Solder is up to date.').fadeIn();
}
} else {
$("#solder-update-ajax").removeClass("alert-warning alert-success alert-danger").addClass("alert-danger").html('Error checking for update. ' + data.reason);
$("#solder-update-ajax").removeClass("alert-warning alert-success alert-danger").addClass("alert-danger").html('Error checking for update: ' + data.reason);
}
$("#solder-checking").addClass("hidden");
},
Expand Down

0 comments on commit a193948

Please sign in to comment.