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.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Arthur Schiwon <blizzz@arthur-schiwon.de>
  6. * @author Joas Schilling <coding@schilljs.com>
  7. *
  8. * @license AGPL-3.0
  9. *
  10. * This code is free software: you can redistribute it and/or modify
  11. * it under the terms of the GNU Affero General Public License, version 3,
  12. * as published by the Free Software Foundation.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU Affero General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Affero General Public License, version 3,
  20. * along with this program. If not, see <http://www.gnu.org/licenses/>
  21. *
  22. */
  23. namespace OCA\Comments\Notification;
  24. use OCP\Comments\CommentsEvent;
  25. use OCP\Comments\IComment;
  26. use OCP\IUserManager;
  27. use OCP\Notification\IManager;
  28. class Listener {
  29. /** @var IManager */
  30. protected $notificationManager;
  31. /** @var IUserManager */
  32. protected $userManager;
  33. /**
  34. * Listener constructor.
  35. *
  36. * @param IManager $notificationManager
  37. * @param IUserManager $userManager
  38. */
  39. public function __construct(
  40. IManager $notificationManager,
  41. IUserManager $userManager
  42. ) {
  43. $this->notificationManager = $notificationManager;
  44. $this->userManager = $userManager;
  45. }
  46. /**
  47. * @param CommentsEvent $event
  48. */
  49. public function evaluate(CommentsEvent $event) {
  50. $comment = $event->getComment();
  51. $mentions = $this->extractMentions($comment->getMentions());
  52. if(empty($mentions)) {
  53. // no one to notify
  54. return;
  55. }
  56. $notification = $this->instantiateNotification($comment);
  57. foreach($mentions as $uid) {
  58. if( ($comment->getActorType() === 'users' && $uid === $comment->getActorId())
  59. || !$this->userManager->userExists($uid)
  60. ) {
  61. // do not notify unknown users or yourself
  62. continue;
  63. }
  64. $notification->setUser($uid);
  65. if( $event->getEvent() === CommentsEvent::EVENT_DELETE
  66. || $event->getEvent() === CommentsEvent::EVENT_PRE_UPDATE)
  67. {
  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. }