Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions lib/composer/composer/autoload_classmap.php
Original file line number Diff line number Diff line change
Expand Up @@ -848,8 +848,10 @@
'OCP\\SystemTag\\MapperEvent' => $baseDir . '/lib/public/SystemTag/MapperEvent.php',
'OCP\\SystemTag\\SystemTagsEntityEvent' => $baseDir . '/lib/public/SystemTag/SystemTagsEntityEvent.php',
'OCP\\SystemTag\\TagAlreadyExistsException' => $baseDir . '/lib/public/SystemTag/TagAlreadyExistsException.php',
'OCP\\SystemTag\\TagAssignedEvent' => $baseDir . '/lib/public/SystemTag/TagAssignedEvent.php',
'OCP\\SystemTag\\TagCreationForbiddenException' => $baseDir . '/lib/public/SystemTag/TagCreationForbiddenException.php',
'OCP\\SystemTag\\TagNotFoundException' => $baseDir . '/lib/public/SystemTag/TagNotFoundException.php',
'OCP\\SystemTag\\TagUnassignedEvent' => $baseDir . '/lib/public/SystemTag/TagUnassignedEvent.php',
'OCP\\SystemTag\\TagUpdateForbiddenException' => $baseDir . '/lib/public/SystemTag/TagUpdateForbiddenException.php',
'OCP\\Talk\\Exceptions\\NoBackendException' => $baseDir . '/lib/public/Talk/Exceptions/NoBackendException.php',
'OCP\\Talk\\IBroker' => $baseDir . '/lib/public/Talk/IBroker.php',
Expand Down
2 changes: 2 additions & 0 deletions lib/composer/composer/autoload_static.php
Original file line number Diff line number Diff line change
Expand Up @@ -889,8 +889,10 @@ class ComposerStaticInit749170dad3f5e7f9ca158f5a9f04f6a2
'OCP\\SystemTag\\MapperEvent' => __DIR__ . '/../../..' . '/lib/public/SystemTag/MapperEvent.php',
'OCP\\SystemTag\\SystemTagsEntityEvent' => __DIR__ . '/../../..' . '/lib/public/SystemTag/SystemTagsEntityEvent.php',
'OCP\\SystemTag\\TagAlreadyExistsException' => __DIR__ . '/../../..' . '/lib/public/SystemTag/TagAlreadyExistsException.php',
'OCP\\SystemTag\\TagAssignedEvent' => __DIR__ . '/../../..' . '/lib/public/SystemTag/TagAssignedEvent.php',
'OCP\\SystemTag\\TagCreationForbiddenException' => __DIR__ . '/../../..' . '/lib/public/SystemTag/TagCreationForbiddenException.php',
'OCP\\SystemTag\\TagNotFoundException' => __DIR__ . '/../../..' . '/lib/public/SystemTag/TagNotFoundException.php',
'OCP\\SystemTag\\TagUnassignedEvent' => __DIR__ . '/../../..' . '/lib/public/SystemTag/TagUnassignedEvent.php',
'OCP\\SystemTag\\TagUpdateForbiddenException' => __DIR__ . '/../../..' . '/lib/public/SystemTag/TagUpdateForbiddenException.php',
'OCP\\Talk\\Exceptions\\NoBackendException' => __DIR__ . '/../../..' . '/lib/public/Talk/Exceptions/NoBackendException.php',
'OCP\\Talk\\IBroker' => __DIR__ . '/../../..' . '/lib/public/Talk/IBroker.php',
Expand Down
10 changes: 10 additions & 0 deletions lib/private/SystemTag/SystemTagObjectMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@
use OCP\SystemTag\ISystemTagManager;
use OCP\SystemTag\ISystemTagObjectMapper;
use OCP\SystemTag\MapperEvent;
use OCP\SystemTag\TagAssignedEvent;
use OCP\SystemTag\TagNotFoundException;
use OCP\SystemTag\TagUnassignedEvent;

class SystemTagObjectMapper implements ISystemTagObjectMapper {
public const RELATION_TABLE = 'systemtag_object_mapping';
Expand Down Expand Up @@ -172,6 +174,7 @@ public function assignTags(string $objId, string $objectType, $tagIds): void {
$objId,
$tagsAssigned
));
$this->dispatcher->dispatchTyped(new TagAssignedEvent($objectType, [$objId], $tagsAssigned));
}

/**
Expand Down Expand Up @@ -202,6 +205,7 @@ public function unassignTags(string $objId, string $objectType, $tagIds): void {
$objId,
$tagIds
));
$this->dispatcher->dispatchTyped(new TagUnassignedEvent($objectType, [$objId], $tagIds));
}

/**
Expand Down Expand Up @@ -307,6 +311,9 @@ public function setObjectIdsForTag(string $tagId, string $objectType, array $obj
[(int)$tagId]
));
}
if (!empty($removedObjectIds)) {
$this->dispatcher->dispatchTyped(new TagUnassignedEvent($objectType, array_map(fn ($objectId) => (string)$objectId, $removedObjectIds), [(int)$tagId]));
}

if (empty($objectIds)) {
return;
Expand Down Expand Up @@ -338,6 +345,9 @@ public function setObjectIdsForTag(string $tagId, string $objectType, array $obj
[(int)$tagId]
));
}
if (!empty($addedObjectIds)) {
$this->dispatcher->dispatchTyped(new TagAssignedEvent($objectType, array_map(fn ($objectId) => (string)$objectId, $addedObjectIds), [(int)$tagId]));
}

// Dispatch unassign events for removed object ids
foreach ($removedObjectIds as $objectId) {
Expand Down
73 changes: 73 additions & 0 deletions lib/public/SystemTag/TagAssignedEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
<?php

declare(strict_types=1);
/**
* SPDX-FileCopyrightText: 2025 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
namespace OCP\SystemTag;

use OCP\EventDispatcher\Event;
use OCP\EventDispatcher\IWebhookCompatibleEvent;

/**
* Event class for when a system tag is assigned to an object
*
* @since 32.0.0
*/
class TagAssignedEvent extends Event implements IWebhookCompatibleEvent {
protected string $objectType;
/** @var list<string> */
protected array $objectIds;
/** @var list<int> */
protected array $tags;

/**
* constructor
*
* @param list<string> $objectIds
* @param list<int> $tags
* @since 32.0.0
*/
public function __construct(string $objectType, array $objectIds, array $tags) {
parent::__construct();
$this->objectType = $objectType;
$this->objectIds = $objectIds;
$this->tags = $tags;
}

/**
* @since 32.0.0
*/
public function getObjectType(): string {
return $this->objectType;
}

/**
* @return list<string>
* @since 32.0.0
*/
public function getObjectIds(): array {
return $this->objectIds;
}

/**
* @return list<int>
* @since 32.0.0
*/
public function getTags(): array {
return $this->tags;
}

/**
* @return array{objectType: string, objectIds: list<string>, tagIds: list<int>}
* @since 32.0.0
*/
public function getWebhookSerializable(): array {
return [
'objectType' => $this->getObjectType(),
'objectIds' => $this->getObjectIds(),
'tagIds' => $this->getTags(),
];
}
}
73 changes: 73 additions & 0 deletions lib/public/SystemTag/TagUnassignedEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
<?php

declare(strict_types=1);
/**
* SPDX-FileCopyrightText: 2025 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
namespace OCP\SystemTag;

use OCP\EventDispatcher\Event;
use OCP\EventDispatcher\IWebhookCompatibleEvent;

/**
* Event class for when a system tag is unassigned from an object
*
* @since 32.0.0
*/
class TagUnassignedEvent extends Event implements IWebhookCompatibleEvent {
protected string $objectType;
/** @var list<string> */
protected array $objectIds;
/** @var list<int> */
protected array $tags;

/**
* constructor
*
* @param list<string> $objectIds
* @param list<int> $tags
* @since 32.0.0
*/
public function __construct(string $objectType, array $objectIds, array $tags) {
parent::__construct();
$this->objectType = $objectType;
$this->objectIds = $objectIds;
$this->tags = $tags;
}

/**
* @since 32.0.0
*/
public function getObjectType(): string {
return $this->objectType;
}

/**
* @return list<string>
* @since 32.0.0
*/
public function getObjectIds(): array {
return $this->objectIds;
}

/**
* @return list<int>
* @since 32.0.0
*/
public function getTags(): array {
return $this->tags;
}

/**
* @return array{objectType: string, objectIds: list<string>, tagIds: list<int>}
* @since 32.0.0
*/
public function getWebhookSerializable(): array {
return [
'objectType' => $this->getObjectType(),
'objectIds' => $this->getObjectIds(),
'tagIds' => $this->getTags(),
];
}
}
33 changes: 33 additions & 0 deletions tests/lib/SystemTag/SystemTagObjectMapperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,16 @@
use OC\SystemTag\SystemTag;
use OC\SystemTag\SystemTagManager;
use OC\SystemTag\SystemTagObjectMapper;
use OCP\EventDispatcher\Event;
use OCP\EventDispatcher\IEventDispatcher;
use OCP\IDBConnection;
use OCP\Server;
use OCP\SystemTag\ISystemTag;
use OCP\SystemTag\ISystemTagManager;
use OCP\SystemTag\ISystemTagObjectMapper;
use OCP\SystemTag\TagAssignedEvent;
use OCP\SystemTag\TagNotFoundException;
use OCP\SystemTag\TagUnassignedEvent;
use Test\TestCase;

/**
Expand Down Expand Up @@ -208,16 +211,46 @@ public function testGetObjectsForNonExistingTag(): void {
}

public function testAssignUnassignTags(): void {
$event = null;
$this->dispatcher->expects($this->any())->method('dispatchTyped')->willReturnCallback(function (Event $e) use (&$event) {
$event = $e;
});

$this->tagMapper->unassignTags('1', 'testtype', [$this->tag1->getId()]);

$this->assertNotNull($event);
$this->assertEquals(TagUnassignedEvent::class, $event::class);
$this->assertEquals('testtype', $event->getObjectType());
$this->assertCount(1, $event->getObjectIds());
$this->assertEquals('1', current($event->getObjectIds()));
$this->assertCount(1, $event->getTags());
$this->assertEquals($this->tag1->getId(), current($event->getTags()));

$tagIdMapping = $this->tagMapper->getTagIdsForObjects('1', 'testtype');
$this->assertEquals([
1 => [$this->tag2->getId()],
], $tagIdMapping);

$this->tagMapper->assignTags('1', 'testtype', [$this->tag1->getId()]);

$this->assertNotNull($event);
$this->assertEquals(TagAssignedEvent::class, $event::class);
$this->assertEquals('testtype', $event->getObjectType());
$this->assertCount(1, $event->getObjectIds());
$this->assertEquals('1', current($event->getObjectIds()));
$this->assertCount(1, $event->getTags());
$this->assertEquals($this->tag1->getId(), current($event->getTags()));

$this->tagMapper->assignTags('1', 'testtype', $this->tag3->getId());

$this->assertNotNull($event);
$this->assertEquals(TagAssignedEvent::class, $event::class);
$this->assertEquals('testtype', $event->getObjectType());
$this->assertCount(1, $event->getObjectIds());
$this->assertEquals('1', current($event->getObjectIds()));
$this->assertCount(1, $event->getTags());
$this->assertEquals($this->tag3->getId(), current($event->getTags()));

$tagIdMapping = $this->tagMapper->getTagIdsForObjects('1', 'testtype');

$this->assertEquals([
Expand Down
Loading