Skip to content

Commit

Permalink
Merge branch 'caching-responses-serverside' into alpha
Browse files Browse the repository at this point in the history
  • Loading branch information
ildyria committed Jan 2, 2025
2 parents 36546c2 + d88fa69 commit ec4161b
Show file tree
Hide file tree
Showing 33 changed files with 1,052 additions and 21 deletions.
5 changes: 3 additions & 2 deletions app/Actions/Photo/Create.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,10 @@
use App\Image\Files\NativeLocalFile;
use App\Legacy\Actions\Photo\Create as LegacyPhotoCreate;
use App\Models\Photo;
use App\Models\User;
use Illuminate\Database\Eloquent\ModelNotFoundException;
use Illuminate\Pipeline\Pipeline;
use LycheeVerify\Verify;
use User;

class Create
{
Expand Down Expand Up @@ -212,6 +212,7 @@ private function executePipeOnDTO(array $pipes, VideoPartnerDTO|StandaloneDTO|Ph
// If source file could not be put into final destination, remove
// freshly created photo from DB to avoid having "zombie" entries.
try {
/** @disregard */
$dto->getPhoto()->delete();
} catch (\Throwable) {
// Sic! If anything goes wrong here, we still throw the original exception
Expand Down Expand Up @@ -311,7 +312,7 @@ private function checkQuota(NativeLocalFile $sourceFile): void
return;
}

$user = \User::find($this->strategyParameters->intendedOwnerId) ?? throw new ModelNotFoundException();
$user = User::find($this->strategyParameters->intendedOwnerId) ?? throw new ModelNotFoundException();

// User does not have quota
if ($user->quota_kb === null) {
Expand Down
18 changes: 18 additions & 0 deletions app/Enum/CacheTag.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

namespace App\Enum;

/**
* Enum ConfigType.
*
* The most important type possibilities.
*/
enum CacheTag: string
{
case GALLERY = 'gallery';
case AUTH = 'auth';
case USER = 'user';
case SETTINGS = 'settings';
case STATISTICS = 'statistics';
case USERS = 'users';
}
21 changes: 21 additions & 0 deletions app/Events/AlbumRouteCacheUpdated.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

namespace App\Events;

use Illuminate\Broadcasting\InteractsWithSockets;
use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Queue\SerializesModels;

class AlbumRouteCacheUpdated
{
use Dispatchable;
use InteractsWithSockets;
use SerializesModels;

/**
* Create a new event instance.
*/
public function __construct(public ?string $album_id = null)
{
}
}
22 changes: 22 additions & 0 deletions app/Events/TaggedRouteCacheUpdated.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

namespace App\Events;

use App\Enum\CacheTag;
use Illuminate\Broadcasting\InteractsWithSockets;
use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Queue\SerializesModels;

class TaggedRouteCacheUpdated
{
use Dispatchable;
use InteractsWithSockets;
use SerializesModels;

/**
* Create a new event instance.
*/
public function __construct(public CacheTag $tag)
{
}
}
29 changes: 29 additions & 0 deletions app/Http/Controllers/Admin/Maintenance/FlushCache.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

namespace App\Http\Controllers\Admin\Maintenance;

use App\Http\Requests\Maintenance\MaintenanceRequest;
use Illuminate\Routing\Controller;
use Illuminate\Support\Facades\Cache;

/**
* Sometimes the job history is a bit messed up,
* this happens when there are crashes or error in the logic.
*
* In theory this should not be needed but if this is not resolved
* the pulse feedback would always stay alive.
*/
class FlushCache extends Controller
{
/**
* Flush all the caches.
*
* @param MaintenanceRequest $_request
*
* @return void
*/
public function do(MaintenanceRequest $_request): void
{
Cache::flush();
}
}
3 changes: 3 additions & 0 deletions app/Http/Controllers/Admin/Maintenance/FullTree.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace App\Http\Controllers\Admin\Maintenance;

use App\Events\AlbumRouteCacheUpdated;
use App\Http\Controllers\Admin\Maintenance\Model\Album;
use App\Http\Requests\Maintenance\FullTreeUpdateRequest;
use App\Http\Requests\Maintenance\MaintenanceRequest;
Expand All @@ -26,6 +27,8 @@ public function do(FullTreeUpdateRequest $request): void
$keyName = 'id';
$albumInstance = new Album();
batch()->update($albumInstance, $request->albums(), $keyName);

AlbumRouteCacheUpdated::dispatch();
}

/**
Expand Down
3 changes: 3 additions & 0 deletions app/Http/Controllers/Admin/Maintenance/GenSizeVariants.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use App\Contracts\Models\SizeVariantFactory;
use App\Enum\SizeVariantType;
use App\Events\AlbumRouteCacheUpdated;
use App\Exceptions\MediaFileOperationException;
use App\Http\Requests\Maintenance\CreateThumbsRequest;
use App\Image\PlaceholderEncoder;
Expand Down Expand Up @@ -56,6 +57,8 @@ public function do(CreateThumbsRequest $request, SizeVariantFactory $sizeVariant
} catch (MediaFileOperationException $e) {
Log::error('Failed to create ' . $request->kind()->value . ' for photo id ' . $photo->id . '');
}

AlbumRouteCacheUpdated::dispatch();
// @codeCoverageIgnoreEnd
}
}
Expand Down
3 changes: 3 additions & 0 deletions app/Http/Controllers/Admin/Maintenance/MissingFileSizes.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace App\Http\Controllers\Admin\Maintenance;

use App\Enum\StorageDiskType;
use App\Events\AlbumRouteCacheUpdated;
use App\Http\Requests\Maintenance\MaintenanceRequest;
use App\Models\SizeVariant;
use Illuminate\Routing\Controller;
Expand Down Expand Up @@ -52,6 +53,8 @@ public function do(MaintenanceRequest $request): void
}
// @codeCoverageIgnoreEnd
}

AlbumRouteCacheUpdated::dispatch();
}

/**
Expand Down
4 changes: 4 additions & 0 deletions app/Http/Controllers/Admin/Maintenance/RegisterController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace App\Http\Controllers\Admin\Maintenance;

use App\Enum\CacheTag;
use App\Events\TaggedRouteCacheUpdated;
use App\Http\Requests\Maintenance\RegisterRequest;
use App\Http\Resources\GalleryConfigs\RegisterData;
use App\Models\Configs;
Expand Down Expand Up @@ -29,6 +31,8 @@ public function __invoke(RegisterRequest $request): RegisterData
// Not valid, reset the key.
Configs::set('license_key', '');

TaggedRouteCacheUpdated::dispatch(CacheTag::SETTINGS);

return new RegisterData(false);
}
}
3 changes: 3 additions & 0 deletions app/Http/Controllers/Admin/SettingsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace App\Http\Controllers\Admin;

use App\Enum\CacheTag;
use App\Events\TaggedRouteCacheUpdated;
use App\Exceptions\InsufficientFilesystemPermissions;
use App\Http\Requests\Settings\GetAllConfigsRequest;
use App\Http\Requests\Settings\SetConfigsRequest;
Expand Down Expand Up @@ -49,6 +51,7 @@ public function setConfigs(SetConfigsRequest $request): ConfigCollectionResource
});

Configs::invalidateCache();
TaggedRouteCacheUpdated::dispatch(CacheTag::SETTINGS);

return new ConfigCollectionResource(Configs::orderBy('cat', 'asc')->get());
}
Expand Down
8 changes: 8 additions & 0 deletions app/Http/Controllers/Admin/UserManagementController.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
use App\Actions\Statistics\Spaces;
use App\Actions\User\Create;
use App\Actions\User\Save;
use App\Enum\CacheTag;
use App\Events\TaggedRouteCacheUpdated;
use App\Exceptions\UnauthorizedException;
use App\Http\Requests\UserManagement\AddUserRequest;
use App\Http\Requests\UserManagement\DeleteUserRequest;
Expand Down Expand Up @@ -60,6 +62,8 @@ public function save(SetUserSettingsRequest $request, Save $save): void
quota_kb: $request->quota_kb(),
note: $request->note()
);

TaggedRouteCacheUpdated::dispatch(CacheTag::USERS);
}

/**
Expand All @@ -78,6 +82,8 @@ public function delete(DeleteUserRequest $request): void
throw new UnauthorizedException('You are not allowed to delete yourself');
}
$request->user2()->delete();

TaggedRouteCacheUpdated::dispatch(CacheTag::USERS);
}

/**
Expand All @@ -99,6 +105,8 @@ public function create(AddUserRequest $request, Create $create): UserManagementR
note: $request->note()
);

TaggedRouteCacheUpdated::dispatch(CacheTag::USERS);

return new UserManagementResource($user, ['id' => $user->id, 'size' => 0], $request->is_se());
}
}
54 changes: 41 additions & 13 deletions app/Http/Controllers/Gallery/AlbumController.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use App\Actions\Album\Transfer;
use App\Actions\Album\Unlock;
use App\Actions\Photo\Archive as PhotoArchive;
use App\Events\AlbumRouteCacheUpdated;
use App\Exceptions\Internal\LycheeLogicException;
use App\Exceptions\UnauthenticatedException;
use App\Http\Requests\Album\AddAlbumRequest;
Expand Down Expand Up @@ -108,6 +109,9 @@ public function createAlbum(AddAlbumRequest $request): string
*/
public function createTagAlbum(AddTagAlbumRequest $request, CreateTagAlbum $create): string
{
// Root
AlbumRouteCacheUpdated::dispatch('');

return $create->create($request->title(), $request->tags())->id;
}

Expand Down Expand Up @@ -140,7 +144,8 @@ public function updateAlbum(UpdateAlbumRequest $request, SetHeader $setHeader):
album: $album,
is_compact: $request->is_compact(),
photo: $request->photo(),
shall_override: true);
shall_override: true
);

return EditableBaseAlbumResource::fromModel($album);
}
Expand All @@ -167,6 +172,7 @@ public function updateTagAlbum(UpdateTagAlbumRequest $request): EditableBaseAlbu
$album->photo_timeline = $request->photo_timeline();
$album->save();

// Root
return EditableBaseAlbumResource::fromModel($album);
}

Expand All @@ -179,26 +185,46 @@ public function updateTagAlbum(UpdateTagAlbumRequest $request): EditableBaseAlbu
*
* @return AlbumProtectionPolicy
*/
public function updateProtectionPolicy(SetAlbumProtectionPolicyRequest $request,
public function updateProtectionPolicy(
SetAlbumProtectionPolicyRequest $request,
SetProtectionPolicy $setProtectionPolicy,
SetSmartProtectionPolicy $setSmartProtectionPolicy): AlbumProtectionPolicy
{
SetSmartProtectionPolicy $setSmartProtectionPolicy,
): AlbumProtectionPolicy {
if ($request->album() instanceof BaseSmartAlbum) {
$setSmartProtectionPolicy->do(
$request->album(),
$request->albumProtectionPolicy()->is_public
);

return AlbumProtectionPolicy::ofSmartAlbum($request->album());
return $this->updateProtectionPolicySmart($request->album(), $request->albumProtectionPolicy()->is_public, $setSmartProtectionPolicy);
}

/** @var BaseAlbum $album */
$album = $request->album();
$setProtectionPolicy->do(

return $this->updateProtectionPolicyBase(
$album,
$request->albumProtectionPolicy(),
$request->isPasswordProvided(),
$request->password()
$request->password(),
$setProtectionPolicy
);
}

private function updateProtectionPolicySmart(BaseSmartAlbum $album, bool $is_public, SetSmartProtectionPolicy $setSmartProtectionPolicy): AlbumProtectionPolicy
{
$setSmartProtectionPolicy->do($album, $is_public);

return AlbumProtectionPolicy::ofSmartAlbum($album);
}

private function updateProtectionPolicyBase(
BaseAlbum $album,
AlbumProtectionPolicy $protectionPolicy,
bool $shallSetPassword,
?string $password,
SetProtectionPolicy $setProtectionPolicy): AlbumProtectionPolicy
{
$setProtectionPolicy->do(
$album,
$protectionPolicy,
$shallSetPassword,
$password
);

return AlbumProtectionPolicy::ofBaseAlbum($album->refresh());
Expand Down Expand Up @@ -244,6 +270,7 @@ public function getTargetListAlbums(TargetListAlbumRequest $request, ListAlbums
*/
public function merge(MergeAlbumsRequest $request, Merge $merge): void
{
$request->albums()->each(fn (Album $album) => AlbumRouteCacheUpdated::dispatch($album->id));
$merge->do($request->album(), $request->albums());
}

Expand All @@ -257,6 +284,7 @@ public function merge(MergeAlbumsRequest $request, Merge $merge): void
*/
public function move(MoveAlbumsRequest $request, Move $move): void
{
$request->albums()->each(fn (Album $album) => AlbumRouteCacheUpdated::dispatch($album->id));
$move->do($request->album(), $request->albums());
}

Expand Down Expand Up @@ -367,4 +395,4 @@ public function deleteTrack(DeleteTrackRequest $request): void
{
$request->album()->deleteTrack();
}
}
}
6 changes: 6 additions & 0 deletions app/Http/Controllers/OauthController.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
namespace App\Http\Controllers;

use App\Actions\Oauth\Oauth as OauthAction;
use App\Enum\CacheTag;
use App\Enum\OauthProvidersType;
use App\Events\TaggedRouteCacheUpdated;
use App\Exceptions\UnauthenticatedException;
use App\Exceptions\UnauthorizedException;
use App\Http\Requests\Profile\ClearOauthRequest;
Expand Down Expand Up @@ -86,6 +88,8 @@ public function register(string $provider)
$providerEnum = $this->oauth->validateProviderOrDie($provider);
Session::put($providerEnum->value, OauthAction::OAUTH_REGISTER);

TaggedRouteCacheUpdated::dispatch(CacheTag::USER);

return Socialite::driver($providerEnum->value)->redirect();
}

Expand Down Expand Up @@ -115,6 +119,8 @@ public function clear(ClearOauthRequest $request): void
/** @var User $user */
$user = Auth::user() ?? throw new UnauthenticatedException();
$user->oauthCredentials()->where('provider', '=', $request->provider())->delete();

TaggedRouteCacheUpdated::dispatch(CacheTag::USER);
}

/**
Expand Down
Loading

0 comments on commit ec4161b

Please sign in to comment.