You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Listener.php 3.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Arthur Schiwon <blizzz@arthur-schiwon.de>
  6. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  7. * @author Joas Schilling <coding@schilljs.com>
  8. *
  9. * @license AGPL-3.0
  10. *
  11. * This code is free software: you can redistribute it and/or modify
  12. * it under the terms of the GNU Affero General Public License, version 3,
  13. * as published by the Free Software Foundation.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU Affero General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Affero General Public License, version 3,
  21. * along with this program. If not, see <http://www.gnu.org/licenses/>
  22. *
  23. */
  24. namespace OCA\Comments\Notification;
  25. use OCP\Comments\CommentsEvent;
  26. use OCP\Comments\IComment;
  27. use OCP\IUserManager;
  28. use OCP\Notification\IManager;
  29. class Listener {
  30. /** @var IManager */
  31. protected $notificationManager;
  32. /** @var IUserManager */
  33. protected $userManager;
  34. /**
  35. * Listener constructor.
  36. *
  37. * @param IManager $notificationManager
  38. * @param IUserManager $userManager
  39. */
  40. public function __construct(
  41. IManager $notificationManager,
  42. IUserManager $userManager
  43. ) {
  44. $this->notificationManager = $notificationManager;
  45. $this->userManager = $userManager;
  46. }
  47. /**
  48. * @param CommentsEvent $event
  49. */
  50. public function evaluate(CommentsEvent $event) {
  51. $comment = $event->getComment();
  52. $mentions = $this->extractMentions($comment->getMentions());
  53. if (empty($mentions)) {
  54. // no one to notify
  55. return;
  56. }
  57. $notification = $this->instantiateNotification($comment);
  58. foreach ($mentions as $uid) {
  59. if (($comment->getActorType() === 'users' && $uid === $comment->getActorId())
  60. || !$this->userManager->userExists($uid)
  61. ) {
  62. // do not notify unknown users or yourself
  63. continue;
  64. }
  65. $notification->setUser($uid);
  66. if ($event->getEvent() === CommentsEvent::EVENT_DELETE
  67. || $event->getEvent() === CommentsEvent::EVENT_PRE_UPDATE) {
  68. $this->notificationManager->markProcessed($notification);
  69. } else {
  70. $this->notificationManager->notify($notification);
  71. }
  72. }
  73. }
  74. /**
  75. * creates a notification instance and fills it with comment data
  76. *
  77. * @param IComment $comment
  78. * @return \OCP\Notification\INotification
  79. */
  80. public function instantiateNotification(IComment $comment) {
  81. $notification = $this->notificationManager->createNotification();
  82. $notification
  83. ->setApp('comments')
  84. ->setObject('comment', $comment->getId())
  85. ->setSubject('mention', [ $comment->getObjectType(), $comment->getObjectId() ])
  86. ->setDateTime($comment->getCreationDateTime());
  87. return $notification;
  88. }
  89. /**
  90. * flattens the mention array returned from comments to a list of user ids.
  91. *
  92. * @param array $mentions
  93. * @return string[] containing the mentions, e.g. ['alice', 'bob']
  94. */
  95. public function extractMentions(array $mentions) {
  96. if (empty($mentions)) {
  97. return [];
  98. }
  99. $uids = [];
  100. foreach ($mentions as $mention) {
  101. if ($mention['type'] === 'user') {
  102. $uids[] = $mention['id'];
  103. }
  104. }
  105. return $uids;
  106. }
  107. }