Skip to content

Commit

Permalink
feat(systemtags): add color support backend
Browse files Browse the repository at this point in the history
Signed-off-by: skjnldsv <skjnldsv@protonmail.com>
  • Loading branch information
skjnldsv committed Nov 15, 2024
1 parent b565a40 commit e433c88
Show file tree
Hide file tree
Showing 10 changed files with 86 additions and 62 deletions.
5 changes: 3 additions & 2 deletions apps/dav/lib/SystemTag/SystemTagNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,12 +86,13 @@ public function setName($name) {
* @param string $name new tag name
* @param bool $userVisible user visible
* @param bool $userAssignable user assignable
* @param string $color color
*
* @throws NotFound whenever the given tag id does not exist
* @throws Forbidden whenever there is no permission to update said tag
* @throws Conflict whenever a tag already exists with the given attributes
*/
public function update($name, $userVisible, $userAssignable): void {
public function update($name, $userVisible, $userAssignable, $color): void {
try {
if (!$this->tagManager->canUserSeeTag($this->tag, $this->user)) {
throw new NotFound('Tag with id ' . $this->tag->getId() . ' does not exist');
Expand All @@ -110,7 +111,7 @@ public function update($name, $userVisible, $userAssignable): void {
}
}

$this->tagManager->updateTag($this->tag->getId(), $name, $userVisible, $userAssignable);
$this->tagManager->updateTag($this->tag->getId(), $name, $userVisible, $userAssignable, $color);
} catch (TagNotFoundException $e) {
throw new NotFound('Tag with id ' . $this->tag->getId() . ' does not exist');
} catch (TagAlreadyExistsException $e) {
Expand Down
14 changes: 13 additions & 1 deletion apps/dav/lib/SystemTag/SystemTagPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ class SystemTagPlugin extends \Sabre\DAV\ServerPlugin {
public const NUM_FILES_PROPERTYNAME = '{http://nextcloud.org/ns}files-assigned';
public const REFERENCE_FILEID_PROPERTYNAME = '{http://nextcloud.org/ns}reference-fileid';
public const OBJECTIDS_PROPERTYNAME = '{http://nextcloud.org/ns}object-ids';
public const COLOR_PROPERTYNAME = '{http://nextcloud.org/ns}color';

/**
* @var \Sabre\DAV\Server $server
Expand Down Expand Up @@ -243,6 +244,10 @@ public function handleGetProperties(
return $this->tagManager->canUserAssignTag($node->getSystemTag(), $this->userSession->getUser()) ? 'true' : 'false';
});

$propFind->handle(self::COLOR_PROPERTYNAME, function () use ($node) {
return $node->getSystemTag()->getColor() ?? '';
});

$propFind->handle(self::GROUPS_PROPERTYNAME, function () use ($node) {
if (!$this->groupManager->isAdmin($this->userSession->getUser()->getUID())) {
// property only available for admins
Expand Down Expand Up @@ -406,6 +411,7 @@ public function handleUpdateProperties($path, PropPatch $propPatch) {
self::GROUPS_PROPERTYNAME,
self::NUM_FILES_PROPERTYNAME,
self::REFERENCE_FILEID_PROPERTYNAME,
self::COLOR_PROPERTYNAME,
], function ($props) use ($node) {
if (!($node instanceof SystemTagNode)) {
return false;
Expand All @@ -415,6 +421,7 @@ public function handleUpdateProperties($path, PropPatch $propPatch) {
$name = $tag->getName();
$userVisible = $tag->isUserVisible();
$userAssignable = $tag->isUserAssignable();
$color = $tag->getColor();

$updateTag = false;

Expand All @@ -435,6 +442,11 @@ public function handleUpdateProperties($path, PropPatch $propPatch) {
$updateTag = true;
}

if (isset($props[self::COLOR_PROPERTYNAME])) {
$color = $props[self::COLOR_PROPERTYNAME];
$updateTag = true;
}

if (isset($props[self::GROUPS_PROPERTYNAME])) {
if (!$this->groupManager->isAdmin($this->userSession->getUser()->getUID())) {
// property only available for admins
Expand All @@ -452,7 +464,7 @@ public function handleUpdateProperties($path, PropPatch $propPatch) {
}

if ($updateTag) {
$node->update($name, $userVisible, $userAssignable);
$node->update($name, $userVisible, $userAssignable, $color);
}

return true;
Expand Down
2 changes: 1 addition & 1 deletion apps/dav/lib/SystemTag/SystemTagsInUseCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ public function getChildren(): array {
$result = $this->systemTagsInFilesDetector->detectAssignedSystemTagsIn($userFolder, $this->mediaType);
$children = [];
foreach ($result as $tagData) {
$tag = new SystemTag((string)$tagData['id'], $tagData['name'], (bool)$tagData['visibility'], (bool)$tagData['editable'], $tagData['etag']);
$tag = new SystemTag((string)$tagData['id'], $tagData['name'], (bool)$tagData['visibility'], (bool)$tagData['editable'], $tagData['etag'], $tagData['color']);
// read only, so we can submit the isAdmin parameter as false generally
$node = new SystemTagNode($tag, $user, false, $this->systemTagManager, $this->tagMapper);
$node->setNumberOfFiles((int)$tagData['number_files']);
Expand Down
2 changes: 2 additions & 0 deletions apps/systemtags/composer/composer/autoload_classmap.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
'OCA\\SystemTags\\Listeners\\BeforeSabrePubliclyLoadedListener' => $baseDir . '/../lib/Listeners/BeforeSabrePubliclyLoadedListener.php',
'OCA\\SystemTags\\Listeners\\BeforeTemplateRenderedListener' => $baseDir . '/../lib/Listeners/BeforeTemplateRenderedListener.php',
'OCA\\SystemTags\\Listeners\\LoadAdditionalScriptsListener' => $baseDir . '/../lib/Listeners/LoadAdditionalScriptsListener.php',
'OCA\\SystemTags\\Migration\\Version31000Date20241018063111' => $baseDir . '/../lib/Migration/Version31000Date20241018063111.php',
'OCA\\SystemTags\\Migration\\Version31000Date20241114171300' => $baseDir . '/../lib/Migration/Version31000Date20241114171300.php',
'OCA\\SystemTags\\Search\\TagSearchProvider' => $baseDir . '/../lib/Search/TagSearchProvider.php',
'OCA\\SystemTags\\Settings\\Admin' => $baseDir . '/../lib/Settings/Admin.php',
);
2 changes: 2 additions & 0 deletions apps/systemtags/composer/composer/autoload_static.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ class ComposerStaticInitSystemTags
'OCA\\SystemTags\\Listeners\\BeforeSabrePubliclyLoadedListener' => __DIR__ . '/..' . '/../lib/Listeners/BeforeSabrePubliclyLoadedListener.php',
'OCA\\SystemTags\\Listeners\\BeforeTemplateRenderedListener' => __DIR__ . '/..' . '/../lib/Listeners/BeforeTemplateRenderedListener.php',
'OCA\\SystemTags\\Listeners\\LoadAdditionalScriptsListener' => __DIR__ . '/..' . '/../lib/Listeners/LoadAdditionalScriptsListener.php',
'OCA\\SystemTags\\Migration\\Version31000Date20241018063111' => __DIR__ . '/..' . '/../lib/Migration/Version31000Date20241018063111.php',
'OCA\\SystemTags\\Migration\\Version31000Date20241114171300' => __DIR__ . '/..' . '/../lib/Migration/Version31000Date20241114171300.php',
'OCA\\SystemTags\\Search\\TagSearchProvider' => __DIR__ . '/..' . '/../lib/Search/TagSearchProvider.php',
'OCA\\SystemTags\\Settings\\Admin' => __DIR__ . '/..' . '/../lib/Settings/Admin.php',
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
* SPDX-License-Identifier: AGPL-3.0-or-later
*/

namespace OC\Core\Migrations;
namespace OCA\SystemTags\Migration;

use Closure;
use Doctrine\DBAL\Types\Types;
Expand All @@ -26,12 +26,6 @@
#[AddIndex(table: 'systemtag_object_mapping', type: IndexType::INDEX, description: 'Adding objecttype index to systemtag_object_mapping')]
class Version31000Date20241018063111 extends SimpleMigrationStep {

/**
* @param IOutput $output
* @param Closure(): ISchemaWrapper $schemaClosure
* @param array $options
* @return null|ISchemaWrapper
*/
public function changeSchema(IOutput $output, Closure $schemaClosure, array $options): ?ISchemaWrapper {
/** @var ISchemaWrapper $schema */
$schema = $schemaClosure();
Expand Down
43 changes: 43 additions & 0 deletions apps/systemtags/lib/Migration/Version31000Date20241114171300.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

declare(strict_types=1);

/**
* SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/

namespace OCA\SystemTags\Migration;

use Closure;
use Doctrine\DBAL\Types\Types;
use OCP\DB\ISchemaWrapper;
use OCP\Migration\Attributes\AddColumn;
use OCP\Migration\Attributes\ColumnType;
use OCP\Migration\IOutput;
use OCP\Migration\SimpleMigrationStep;

/**
* Add objecttype index to systemtag_object_mapping
*/
#[AddColumn(table: 'systemtag', name: 'color', type: ColumnType::STRING, description: 'Adding color for systemtag table')]
class Version31000Date20241114171300 extends SimpleMigrationStep {

public function changeSchema(IOutput $output, Closure $schemaClosure, array $options): ?ISchemaWrapper {
/** @var ISchemaWrapper $schema */
$schema = $schemaClosure();

if ($schema->hasTable('systemtag')) {
$table = $schema->getTable('systemtag');

if (!$table->hasColumn('color')) {
$table->addColumn('color', Types::STRING, [
'notnull' => false,
'length' => 6,
]);
}
}

return $schema;
}
}
23 changes: 5 additions & 18 deletions lib/private/SystemTag/SystemTag.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,40 +17,26 @@ public function __construct(
private bool $userVisible,
private bool $userAssignable,
private ?string $etag = null,
private ?string $color = null
) {
}

/**
* {@inheritdoc}
*/
public function getId(): string {
return $this->id;
}

/**
* {@inheritdoc}
*/
public function getName(): string {
return $this->name;
}

/**
* {@inheritdoc}
*/
public function isUserVisible(): bool {
return $this->userVisible;
}

/**
* {@inheritdoc}
*/
public function isUserAssignable(): bool {
return $this->userAssignable;
}

/**
* {@inheritdoc}
*/
public function getAccessLevel(): int {
if (!$this->userVisible) {
return self::ACCESS_LEVEL_INVISIBLE;
Expand All @@ -63,10 +49,11 @@ public function getAccessLevel(): int {
return self::ACCESS_LEVEL_PUBLIC;
}

/**
* {@inheritdoc}
*/
public function getETag(): ?string {
return $this->etag;
}

public function getColor(): ?string {
return $this->color;
}
}
42 changes: 9 additions & 33 deletions lib/private/SystemTag/SystemTagManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,6 @@ public function __construct(
->andWhere($query->expr()->eq('editable', $query->createParameter('editable')));
}

/**
* {@inheritdoc}
*/
public function getTagsByIds($tagIds, ?IUser $user = null): array {
if (!\is_array($tagIds)) {
$tagIds = [$tagIds];
Expand Down Expand Up @@ -92,9 +89,6 @@ public function getTagsByIds($tagIds, ?IUser $user = null): array {
return $tags;
}

/**
* {@inheritdoc}
*/
public function getAllTags($visibilityFilter = null, $nameSearchPattern = null): array {
$tags = [];

Expand Down Expand Up @@ -130,9 +124,6 @@ public function getAllTags($visibilityFilter = null, $nameSearchPattern = null):
return $tags;
}

/**
* {@inheritdoc}
*/
public function getTag(string $tagName, bool $userVisible, bool $userAssignable): ISystemTag {
// Length of name column is 64
$truncatedTagName = substr($tagName, 0, 64);
Expand All @@ -153,9 +144,6 @@ public function getTag(string $tagName, bool $userVisible, bool $userAssignable)
return $this->createSystemTagFromRow($row);
}

/**
* {@inheritdoc}
*/
public function createTag(string $tagName, bool $userVisible, bool $userAssignable): ISystemTag {
// Length of name column is 64
$truncatedTagName = substr($tagName, 0, 64);
Expand Down Expand Up @@ -194,14 +182,12 @@ public function createTag(string $tagName, bool $userVisible, bool $userAssignab
return $tag;
}

/**
* {@inheritdoc}
*/
public function updateTag(
string $tagId,
string $newName,
bool $userVisible,
bool $userAssignable,
string $color = ''
): void {
try {
$tags = $this->getTagsByIds($tagId);
Expand All @@ -218,19 +204,23 @@ public function updateTag(
$tagId,
$truncatedNewName,
$userVisible,
$userAssignable
$userAssignable,
$beforeUpdate->getETag(),
$color
);

$query = $this->connection->getQueryBuilder();
$query->update(self::TAG_TABLE)
->set('name', $query->createParameter('name'))
->set('visibility', $query->createParameter('visibility'))
->set('editable', $query->createParameter('editable'))
->set('color', $query->createParameter('color'))
->where($query->expr()->eq('id', $query->createParameter('tagid')))
->setParameter('name', $truncatedNewName)
->setParameter('visibility', $userVisible ? 1 : 0)
->setParameter('editable', $userAssignable ? 1 : 0)
->setParameter('tagid', $tagId);
->setParameter('tagid', $tagId)
->setParameter('color', $color);

try {
if ($query->execute() === 0) {
Expand All @@ -251,9 +241,6 @@ public function updateTag(
));
}

/**
* {@inheritdoc}
*/
public function deleteTags($tagIds): void {
if (!\is_array($tagIds)) {
$tagIds = [$tagIds];
Expand Down Expand Up @@ -303,9 +290,6 @@ public function deleteTags($tagIds): void {
}
}

/**
* {@inheritdoc}
*/
public function canUserAssignTag(ISystemTag $tag, ?IUser $user): bool {
if ($user === null) {
return false;
Expand Down Expand Up @@ -335,9 +319,6 @@ public function canUserAssignTag(ISystemTag $tag, ?IUser $user): bool {
return false;
}

/**
* {@inheritdoc}
*/
public function canUserSeeTag(ISystemTag $tag, ?IUser $user): bool {
// If no user, then we only show public tags
if (!$user && $tag->getAccessLevel() === ISystemTag::ACCESS_LEVEL_PUBLIC) {
Expand All @@ -361,12 +342,9 @@ public function canUserSeeTag(ISystemTag $tag, ?IUser $user): bool {
}

private function createSystemTagFromRow($row): SystemTag {
return new SystemTag((string)$row['id'], $row['name'], (bool)$row['visibility'], (bool)$row['editable'], $row['etag']);
return new SystemTag((string)$row['id'], $row['name'], (bool)$row['visibility'], (bool)$row['editable'], $row['etag'], $row['color']);
}

/**
* {@inheritdoc}
*/
public function setTagGroups(ISystemTag $tag, array $groupIds): void {
// delete relationships first
$this->connection->beginTransaction();
Expand Down Expand Up @@ -398,9 +376,6 @@ public function setTagGroups(ISystemTag $tag, array $groupIds): void {
}
}

/**
* {@inheritdoc}
*/
public function getTagGroups(ISystemTag $tag): array {
$groupIds = [];
$query = $this->connection->getQueryBuilder();
Expand All @@ -418,4 +393,5 @@ public function getTagGroups(ISystemTag $tag): array {

return $groupIds;
}

}
7 changes: 7 additions & 0 deletions lib/public/SystemTag/ISystemTag.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,4 +89,11 @@ public function getAccessLevel(): int;
* @since 31.0.0
*/
public function getETag(): ?string;

/**
* Returns the color of the tag
*
* @since 31.0.0
*/
public function getColor(): ?string;
}

0 comments on commit e433c88

Please sign in to comment.