Repository navigation
feat: favourite team resources per user #3079
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
stediefan
wants to merge
1
commit into
master
Choose a base branch
from
feature/3057-team-resource-favourite
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,86 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| /** | ||
| * SPDX-FileCopyrightText: 2026 Nextcloud GmbH and Nextcloud contributors | ||
| * SPDX-License-Identifier: AGPL-3.0-or-later | ||
| */ | ||
|
|
||
| namespace OCA\Circles\Controller; | ||
|
|
||
| use OCA\Circles\ConfigLexicon; | ||
| use OCA\Circles\Exceptions\InsufficientPermissionException; | ||
| use OCA\Circles\Service\PermissionService; | ||
| use OCP\AppFramework\Http; | ||
| use OCP\AppFramework\Http\Attribute\NoAdminRequired; | ||
| use OCP\AppFramework\Http\DataResponse; | ||
| use OCP\AppFramework\OCS\OCSException; | ||
| use OCP\AppFramework\OCSController; | ||
| use OCP\Config\IUserConfig; | ||
| use OCP\IRequest; | ||
| use OCP\IUserSession; | ||
|
|
||
| /** | ||
| * Per-user, ordered list of favorite navigation entries of a team, stored as | ||
| * the same entry ids the team-level tab order uses (e.g. "page-42"). | ||
| */ | ||
| class TeamResourceFavoritesController extends OCSController { | ||
| private const MAX_ID_LENGTH = 64; | ||
| private const MAX_ENTRIES = 100; | ||
|
|
||
| public function __construct( | ||
| string $appName, | ||
| IRequest $request, | ||
| private readonly IUserConfig $userConfig, | ||
| private readonly PermissionService $permissionService, | ||
| private readonly IUserSession $userSession, | ||
| ) { | ||
| parent::__construct($appName, $request); | ||
| } | ||
|
|
||
| #[NoAdminRequired] | ||
| public function getFavorites(string $circleId): DataResponse { | ||
| $userId = $this->assertAuthenticatedUserIsMember($circleId); | ||
| $favorites = $this->userConfig->getValueArray($userId, $this->appName, ConfigLexicon::USER_RESOURCE_FAVORITES_PREFIX . $circleId, [], true); | ||
|
|
||
| return new DataResponse(['favorites' => $this->sanitize($favorites)]); | ||
| } | ||
|
|
||
| /** | ||
| * @param list<string> $favorites Entry ids, first to last | ||
| */ | ||
| #[NoAdminRequired] | ||
| public function setFavorites(string $circleId, array $favorites): DataResponse { | ||
| $userId = $this->assertAuthenticatedUserIsMember($circleId); | ||
| $favorites = $this->sanitize($favorites); | ||
| $this->userConfig->setValueArray($userId, $this->appName, ConfigLexicon::USER_RESOURCE_FAVORITES_PREFIX . $circleId, $favorites, true); | ||
|
|
||
| return new DataResponse(['favorites' => $favorites]); | ||
| } | ||
|
|
||
| /** | ||
| * @return list<string> | ||
| */ | ||
| private function sanitize(array $favorites): array { | ||
| $favorites = array_filter($favorites, static fn ($id): bool => is_string($id) | ||
| && $id !== '' | ||
| && strlen($id) <= self::MAX_ID_LENGTH); | ||
|
|
||
| return array_slice(array_values(array_unique($favorites)), 0, self::MAX_ENTRIES); | ||
| } | ||
|
|
||
| private function assertAuthenticatedUserIsMember(string $circleId): string { | ||
| $user = $this->userSession->getUser(); | ||
| if ($user === null) { | ||
| throw new OCSException('Authentication required', Http::STATUS_UNAUTHORIZED); | ||
| } | ||
| try { | ||
| $this->permissionService->userMustBeMember($user->getUID(), $circleId); | ||
| } catch (InsufficientPermissionException $e) { | ||
| throw new OCSException($e->getMessage(), Http::STATUS_FORBIDDEN); | ||
| } | ||
|
|
||
| return $user->getUID(); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,65 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| /** | ||
| * SPDX-FileCopyrightText: 2026 Nextcloud GmbH and Nextcloud contributors | ||
| * SPDX-License-Identifier: AGPL-3.0-or-later | ||
| */ | ||
|
|
||
| namespace OCA\Circles\Listeners; | ||
|
|
||
| use OCA\Circles\AppInfo\Application; | ||
| use OCA\Circles\ConfigLexicon; | ||
| use OCA\Circles\Events\DestroyingCircleEvent; | ||
| use OCA\Circles\Events\MembershipsRemovedEvent; | ||
| use OCA\Circles\Model\Member; | ||
| use OCA\Circles\Service\FederatedUserService; | ||
| use OCP\Config\IUserConfig; | ||
| use OCP\EventDispatcher\Event; | ||
| use OCP\EventDispatcher\IEventListener; | ||
| use Psr\Log\LoggerInterface; | ||
|
|
||
| /** | ||
| * Drops the favorite resources a user keeps for a team once they can no | ||
| * longer see the team, or the team is gone. | ||
| * | ||
| * @template-implements IEventListener<DestroyingCircleEvent|MembershipsRemovedEvent> | ||
| */ | ||
| class TeamResourceFavoritesCleanup implements IEventListener { | ||
| public function __construct( | ||
| private readonly IUserConfig $userConfig, | ||
| private readonly FederatedUserService $federatedUserService, | ||
| private readonly LoggerInterface $logger, | ||
| ) { | ||
| } | ||
|
|
||
| #[\Override] | ||
| public function handle(Event $event): void { | ||
| if ($event instanceof DestroyingCircleEvent) { | ||
| $this->userConfig->deleteKey(Application::APP_ID, ConfigLexicon::USER_RESOURCE_FAVORITES_PREFIX . $event->getCircle()->getSingleId()); | ||
| return; | ||
| } | ||
|
|
||
| if (!$event instanceof MembershipsRemovedEvent) { | ||
| return; | ||
| } | ||
|
|
||
| foreach ($event->getMemberships() as $membership) { | ||
| try { | ||
| $federatedUser = $this->federatedUserService->getFederatedUser($membership->getSingleId()); | ||
| } catch (\Exception $e) { | ||
| $this->logger->debug('Could not resolve the member to clean up its favorite resources', ['exception' => $e]); | ||
| continue; | ||
| } | ||
| if ($federatedUser->getUserType() !== Member::TYPE_USER || !$federatedUser->isLocal()) { | ||
| continue; | ||
| } | ||
| $this->userConfig->deleteUserConfig( | ||
| $federatedUser->getUserId(), | ||
| Application::APP_ID, | ||
| ConfigLexicon::USER_RESOURCE_FAVORITES_PREFIX . $membership->getCircleId(), | ||
| ); | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Shouldn't we return the favorite state when fetching the ressources? This would save one request
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Currently there is the issue that not all resources are fetched from the resources request: the team-folder itself and the pages are fetched differently (e.g. pages by wevdav). That would complicate things imho.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do you mean that the team folder is fetched with a webdav request that might or might not fail? And if it fails, we do not display it?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No not quite. The folders are fetched by a separate API endpoint in Circles. The pages within are fetched via WebDAV. And the other Resources are actual Resources. Therefore a favourite state on the resources would not suffice (at least yet).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
with #3089 the pages will become resources