Skip to content

Commit

Permalink
Improve performance of content listing
Browse files Browse the repository at this point in the history
* Content policy: Adjust check in to not fetch from database.
* Search result: Adjust data for policy and route
* Remove duplicate check from 'use' Content policy
* Improve performance of My content listing for non-admin users
  • Loading branch information
chrieinv authored Jan 30, 2025
1 parent 5bbcf62 commit c3e8db0
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 8 deletions.
7 changes: 4 additions & 3 deletions sourcecode/hub/app/Http/Requests/ContentFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -314,7 +314,8 @@ private function attachModel(array $hits, bool $forUser, bool $showDrafts): Coll
$eagerLoad = ['users'];
if ($showDrafts) {
$eagerLoad[] = 'latestVersion';
} else {
}
if (!$showDrafts || $forUser) {
$eagerLoad[] = 'latestPublishedVersion';
}

Expand All @@ -331,7 +332,7 @@ private function attachModel(array $hits, bool $forUser, bool $showDrafts): Coll
?? throw new NotFoundHttpException();

$canUse = Gate::allows('use', [$model, $version]);
$canEdit = Gate::allows('edit', $model);
$canEdit = Gate::allows('edit', [$model, $version]);
$canView = Gate::allows('view', $model);
$canDelete = $forUser && Gate::allows('delete', $model);
$canCopy = Gate::allows('copy', $model);
Expand All @@ -349,7 +350,7 @@ private function attachModel(array $hits, bool $forUser, bool $showDrafts): Coll
useUrl: $canUse ? route('content.use', [$model, $version]) : null,
editUrl: $canEdit ? route('content.edit', [$model, $version]) : null,
shareUrl: $canView ? route('content.share', [$model, SessionScope::TOKEN_PARAM => null]) : null,
copyUrl: $canCopy ? route('content.copy', [$model, $version]) : null,
copyUrl: $canCopy ? route('content.copy', [$model]) : null,
deleteUrl: $canDelete ? route('content.delete', [$model]) : null,
);
});
Expand Down
12 changes: 7 additions & 5 deletions sourcecode/hub/app/Policies/ContentPolicy.php
Original file line number Diff line number Diff line change
Expand Up @@ -129,10 +129,6 @@ public function use(User|null $user, Content $content, ContentVersion $version):
return false;
}

if (!$version->content?->is($content)) {
return false;
}

if (!$version->published) {
return false;
}
Expand All @@ -151,7 +147,13 @@ public function manageRoles(User $user, Content $content): bool

private function ensureVersionBelongsToContent(Content $content, ContentVersion|null $version): void
{
if ($version && !$version->content?->is($content)) {
if ($version && (
$version->content_id !== $content->id ||
$version->exists === false ||
$content->exists === false ||
$version->getConnectionName() !== $content->getConnectionName()
)
) {
throw new LogicException('Version does not belong to content');
}
}
Expand Down

0 comments on commit c3e8db0

Please sign in to comment.