|
14 | 14 | use OCP\EventDispatcher\IEventDispatcher; |
15 | 15 | use OCP\Files\Events\NodeAddedToFavorite; |
16 | 16 | use OCP\Files\Events\NodeRemovedFromFavorite; |
| 17 | +use OCP\Files\Folder; |
17 | 18 | use OCP\IDBConnection; |
18 | 19 | use OCP\ITags; |
19 | 20 | use OCP\IUserSession; |
@@ -65,6 +66,7 @@ public function __construct( |
65 | 66 | private IDBConnection $db, |
66 | 67 | private IEventDispatcher $dispatcher, |
67 | 68 | private IUserSession $userSession, |
| 69 | + private Folder $userFolder, |
68 | 70 | array $defaultTags = [], |
69 | 71 | ) { |
70 | 72 | $this->owners = [$this->user]; |
@@ -495,12 +497,8 @@ public function removeFromFavorites($objid) { |
495 | 497 |
|
496 | 498 | /** |
497 | 499 | * Creates a tag/object relation. |
498 | | - * |
499 | | - * @param int $objid The id of the object |
500 | | - * @param string $tag The id or name of the tag |
501 | | - * @return boolean Returns false on error. |
502 | 500 | */ |
503 | | - public function tagAs($objid, $tag, string $path = '') { |
| 501 | + public function tagAs($objid, $tag, ?string $path = null) { |
504 | 502 | if (is_string($tag) && !is_numeric($tag)) { |
505 | 503 | $tag = trim($tag); |
506 | 504 | if ($tag === '') { |
@@ -531,19 +529,24 @@ public function tagAs($objid, $tag, string $path = '') { |
531 | 529 | return false; |
532 | 530 | } |
533 | 531 | if ($tag === ITags::TAG_FAVORITE) { |
| 532 | + if ($path === null) { |
| 533 | + $node = $this->userFolder->getFirstNodeById($objid); |
| 534 | + if ($node !== null) { |
| 535 | + $path = $node->getPath(); |
| 536 | + } else { |
| 537 | + throw new Exception('Failed to favorite: node with id ' . $objid . ' not found'); |
| 538 | + } |
| 539 | + } |
| 540 | + |
534 | 541 | $this->dispatcher->dispatchTyped(new NodeAddedToFavorite($this->userSession->getUser(), $objid, $path)); |
535 | 542 | } |
536 | 543 | return true; |
537 | 544 | } |
538 | 545 |
|
539 | 546 | /** |
540 | 547 | * Delete single tag/object relation from the db |
541 | | - * |
542 | | - * @param int $objid The id of the object |
543 | | - * @param string $tag The id or name of the tag |
544 | | - * @return boolean |
545 | 548 | */ |
546 | | - public function unTag($objid, $tag, string $path = '') { |
| 549 | + public function unTag($objid, $tag, ?string $path = null) { |
547 | 550 | if (is_string($tag) && !is_numeric($tag)) { |
548 | 551 | $tag = trim($tag); |
549 | 552 | if ($tag === '') { |
@@ -571,6 +574,15 @@ public function unTag($objid, $tag, string $path = '') { |
571 | 574 | return false; |
572 | 575 | } |
573 | 576 | if ($tag === ITags::TAG_FAVORITE) { |
| 577 | + if ($path === null) { |
| 578 | + $node = $this->userFolder->getFirstNodeById($objid); |
| 579 | + if ($node !== null) { |
| 580 | + $path = $node->getPath(); |
| 581 | + } else { |
| 582 | + throw new Exception('Failed to unfavorite: node with id ' . $objid . ' not found'); |
| 583 | + } |
| 584 | + } |
| 585 | + |
574 | 586 | $this->dispatcher->dispatchTyped(new NodeRemovedFromFavorite($this->userSession->getUser(), $objid, $path)); |
575 | 587 | } |
576 | 588 | return true; |
|
0 commit comments