diff --git a/lib/Activity/ActivityManager.php b/lib/Activity/ActivityManager.php index 5f3e1b03d..f8b622fe2 100644 --- a/lib/Activity/ActivityManager.php +++ b/lib/Activity/ActivityManager.php @@ -41,6 +41,7 @@ use OCP\Activity\IEvent; use OCP\Activity\IManager; use OCP\AppFramework\Db\DoesNotExistException; use OCP\AppFramework\Db\MultipleObjectsReturnedException; +use OCP\Comments\IComment; use OCP\IL10N; use OCP\IUser; @@ -98,6 +99,8 @@ class ActivityManager { const SUBJECT_LABEL_ASSIGN = 'label_assign'; const SUBJECT_LABEL_UNASSING = 'label_unassign'; + const SUBJECT_CARD_COMMENT_CREATE = 'card_comment_create'; + public function __construct( IManager $manager, PermissionService $permissionsService, @@ -227,6 +230,9 @@ class ActivityManager { case self::SUBJECT_ATTACHMENT_RESTORE: $subject = $ownActivity ? $this->l10n->t('You have restored the attachment {attachment} to {card}') : $this->l10n->t('{user} has restored the attachment {attachment} to {card}'); break; + case self::SUBJECT_CARD_COMMENT_CREATE: + $subject = $ownActivity ? $this->l10n->t('You have commented on {card}') : $this->l10n->t('{user} has commented on {card}'); + break; default: break; } @@ -315,6 +321,13 @@ class ActivityManager { // Not defined as there is no activity for // case self::SUBJECT_BOARD_UPDATE_COLOR break; + case self::SUBJECT_CARD_COMMENT_CREATE: + /** @var IComment $entity */ + $subjectParams = [ + 'comment' => $entity->getMessage() + ]; + $message = $entity->getMessage(); + break; case self::SUBJECT_STACK_CREATE: case self::SUBJECT_STACK_UPDATE: @@ -409,6 +422,9 @@ class ActivityManager { */ private function findObjectForEntity($objectType, $entity) { $className = \get_class($entity); + if ($entity instanceof IComment) { + $className = IComment::class; + } $objectId = null; if ($objectType === self::DECK_OBJECT_CARD) { switch ($className) { @@ -420,6 +436,9 @@ class ActivityManager { case AssignedUsers::class: $objectId = $entity->getCardId(); break; + case IComment::class: + $objectId = $entity->getObjectId(); + break; default: throw new InvalidArgumentException('No entity relation present for '. $className . ' to ' . $objectType); } diff --git a/lib/Activity/DeckProvider.php b/lib/Activity/DeckProvider.php index e8ff3e070..670f2a61c 100644 --- a/lib/Activity/DeckProvider.php +++ b/lib/Activity/DeckProvider.php @@ -28,6 +28,7 @@ use cogpowered\FineDiff\Diff; use OCA\Deck\Db\Acl; use OCP\Activity\IEvent; use OCP\Activity\IProvider; +use OCP\Comments\IComment; use OCP\IURLGenerator; use OCP\IUserManager; @@ -106,7 +107,7 @@ class DeckProvider implements IProvider { 'type' => 'user', 'id' => $author, 'name' => $user !== null ? $user->getDisplayName() : $author - ] + ], ]; $params = $this->parseParamForBoard('board', $subjectParams, $params); @@ -117,6 +118,7 @@ class DeckProvider implements IProvider { $params = $this->parseParamForAssignedUser($subjectParams, $params); $params = $this->parseParamForAcl($subjectParams, $params); $params = $this->parseParamForChanges($subjectParams, $params, $event); + $params = $this->parseParamForComment($subjectParams, $params, $event); try { $subject = $this->activityManager->getActivityFormat($subjectIdentifier, $subjectParams, $ownActivity); @@ -147,6 +149,9 @@ class DeckProvider implements IProvider { if (strpos($event->getSubject(), 'attachment_') !== false) { $event->setIcon($this->urlGenerator->imagePath('core', 'places/files.svg')); } + if (strpos($event->getSubject(), 'comment_') !== false) { + $event->setIcon($this->urlGenerator->imagePath('core', 'actions/comment.svg')); + } return $event; } @@ -227,6 +232,16 @@ class DeckProvider implements IProvider { return $params; } + private function parseParamForComment($subjectParams, $params, IEvent $event) { + if (array_key_exists('comment', $subjectParams)) { + /** @var IComment $comment */ + $comment = \OC::$server->getCommentsManager()->get((int) $subjectParams['comment']); + $event->setParsedMessage($comment->getMessage()); + } + $params['comment'] = $subjectParams['comment']; + return $params; + } + /** * Add diff to message if the subject parameter 'diff' is set, otherwise * the changed values are added to before/after diff --git a/lib/AppInfo/Application.php b/lib/AppInfo/Application.php index 528c1e416..10dedd319 100644 --- a/lib/AppInfo/Application.php +++ b/lib/AppInfo/Application.php @@ -23,12 +23,15 @@ namespace OCA\Deck\AppInfo; +use OCA\Deck\Activity\CommentEventHandler; use OCA\Deck\Db\Acl; use OCA\Deck\Db\AclMapper; use OCA\Deck\Db\AssignedUsersMapper; +use OCA\Deck\Db\CardMapper; use OCA\Deck\Notification\Notifier; use OCP\AppFramework\App; use OCA\Deck\Middleware\SharingMiddleware; +use OCP\Comments\CommentsEntityEvent; use OCP\IGroup; use OCP\IUser; use OCP\IUserManager; @@ -120,6 +123,27 @@ class Application extends App { }, function() { return ['id' => 'deck', 'name' => 'Deck']; }); + } + public function registerCommentsEntity() { + $this->getContainer()->getServer()->getEventDispatcher()->addListener(CommentsEntityEvent::EVENT_ENTITY, function(CommentsEntityEvent $event) { + $event->addEntityCollection('deckCard', function($name) { + /** @var CardMapper */ + $service = $this->getContainer()->query(CardMapper::class); + try { + $card = $service->find((int) $name); + } catch (\InvalidArgumentException $e) { + return false; + } + return true; + }); + }); + $this->registerCommentsEventHandler(); + } + + protected function registerCommentsEventHandler() { + $this->getContainer()->getServer()->getCommentsManager()->registerEventHandler(function () { + return $this->getContainer()->query(CommentEventHandler::class); + }); } } diff --git a/lib/Db/Card.php b/lib/Db/Card.php index eb5e93911..91b580ada 100644 --- a/lib/Db/Card.php +++ b/lib/Db/Card.php @@ -43,6 +43,7 @@ class Card extends RelationalEntity { protected $duedate; protected $notified = false; protected $deletedAt = 0; + protected $commentsUnread = 0; private $databaseType = 'sqlite'; @@ -65,6 +66,7 @@ class Card extends RelationalEntity { $this->addRelation('attachments'); $this->addRelation('attachmentCount'); $this->addRelation('participants'); + $this->addRelation('commentsUnread'); $this->addResolvable('owner'); } diff --git a/lib/Service/CardService.php b/lib/Service/CardService.php index 721ebe165..78e957383 100644 --- a/lib/Service/CardService.php +++ b/lib/Service/CardService.php @@ -83,6 +83,11 @@ class CardService { $card->setAssignedUsers($this->assignedUsersMapper->find($cardId)); $card->setLabels($this->labelMapper->findAssignedLabelsForCard($cardId)); $card->setAttachmentCount($this->attachmentService->count($cardId)); + /** @var ICommentsManager commentsManager */ + $this->commentsManager = \OC::$server->getCommentsManager(); + $lastRead = $this->commentsManager->getReadMark('deckCard', (string)$card->getId(), \OC::$server->getUserSession()->getUser()); + $count = $this->commentsManager->getNumberOfCommentsForObject('deckCard', (string)$card->getId(), $lastRead); + $card->setCommentsUnread($count); } public function fetchDeleted($boardId) { @@ -114,6 +119,7 @@ class CardService { $attachments = $this->attachmentService->findAll($cardId, true); $card->setAssignedUsers($assignedUsers); $card->setAttachments($attachments); + $this->enrich($card); return $card; } diff --git a/lib/Service/StackService.php b/lib/Service/StackService.php index 0947986c4..0ee599133 100644 --- a/lib/Service/StackService.php +++ b/lib/Service/StackService.php @@ -34,6 +34,7 @@ use OCA\Deck\Db\Stack; use OCA\Deck\Db\StackMapper; use OCA\Deck\StatusException; use OCA\Deck\BadRequestException; +use OCP\Comments\ICommentsManager; class StackService {