Elgg  Version master
MentionsEventHandler.php
Go to the documentation of this file.
1 <?php
2 
3 namespace Elgg\Notifications;
4 
6 
13 
17  public function getSubscriptions(): array {
18  /* @var $entity \ElggEntity */
19  $entity = $this->event->getObject();
20  if ($entity->access_id === ACCESS_PRIVATE) {
21  // no actions taken on private content
22  return [];
23  }
24 
25  $mentions = $this->getMentions();
26  if (empty($mentions)) {
27  return [];
28  }
29 
30  // store the usernames as notified
31  $notified = (array) $entity->_mentioned_usernames;
32  $entity->_mentioned_usernames = array_merge($notified, $mentions);
33 
34  // transform usernames to users to find notification preferences
35  $result = [];
37  'type' => 'user',
38  'limit' => false,
39  'batch' => true,
40  'metadata_name_value_pairs' => [
41  [
42  'name' => 'username',
43  'value' => $mentions,
44  ],
45  ],
46  ]);
47  /* @var $user \ElggUser */
48  foreach ($users as $user) {
49  $preference = $user->getNotificationSettings('mentions');
50  $preference = array_keys(array_filter($preference));
51  if (empty($preference)) {
52  continue;
53  }
54 
55  $result[$user->guid] = $preference;
56  }
57 
58  // remove the actor from the subscribers
59  unset($result[$this->event->getActorGUID()]);
60 
61  return $result;
62  }
63 
67  protected function getNotificationSubject(\ElggUser $recipient, string $method): string {
68  $language = $recipient->getLanguage();
69  $actor = $this->event->getActor();
70 
71  $lan_key = 'notification:mentions:subject';
72  if (elgg_language_key_exists("notification:{$this->event->getDescription()}:subject")) {
73  $lan_key = "notification:{$this->event->getDescription()}:subject";
74  }
75 
77  }
78 
82  protected function getNotificationBody(\ElggUser $recipient, string $method): string {
83  $language = $recipient->getLanguage();
84  $actor = $this->event->getActor();
85  $entity = $this->event->getObject();
86 
87  $lan_key = 'notification:mentions:body';
88  if (elgg_language_key_exists("notification:{$this->event->getDescription()}:body")) {
89  $lan_key = "notification:{$this->event->getDescription()}:body";
90  }
91 
92  return elgg_echo($lan_key, [
94  $entity->getDisplayName(),
95  $entity->getURL(),
96  ], $language);
97  }
98 
102  protected static function isConfigurableForUser(\ElggUser $user): bool {
103  return false;
104  }
105 
109  protected static function isConfigurableForGroup(\ElggGroup $group): bool {
110  return false;
111  }
112 
116  protected function addMuteLink(): bool {
117  return false;
118  }
119 
125  protected function getMetadataFields(): array {
126  return ['description'];
127  }
128 
134  protected function getMentions(): array {
135  $result = [];
136  /* @var $entity \ElggEntity */
137  $entity = $this->event->getObject();
138  $metadata_fields = $this->getMetadataFields();
139 
140  foreach ($metadata_fields as $field) {
141  $value = $entity->$field;
142  if (empty($value)) {
143  continue;
144  }
145 
146  if (!is_array($value)) {
147  $value = [$value];
148  }
149 
150  foreach ($value as $text) {
151  if (empty($text) || !is_string($text)) {
152  continue;
153  }
154 
155  $matches = [];
156  preg_match_all(HtmlFormatter::MENTION_REGEX, $text, $matches);
157 
158  $text_mentions = (array) elgg_extract(3, $matches);
159  // remove empty/duplicate values
160  $text_mentions = array_values(array_unique(array_filter($text_mentions)));
161  // remove trailing punctuation (.) from the username
162  $text_mentions = array_map(function($mention) {
163  return rtrim($mention, '.');
164  }, $text_mentions);
165 
166  // prepare hook so other plugins can extend the mentions found in the text
167  $params = [
168  'entity' => $entity,
169  'field' => $field,
170  'text' => $text,
171  ];
172  $text_mentions = (array) _elgg_services()->events->triggerResults('usernames', 'mentions', $params, $text_mentions);
173 
174  $result = array_merge($result, $text_mentions);
175  }
176  }
177 
178  $result = array_values(array_unique($result));
179 
180  return $this->filterMentions($result);
181  }
182 
191  protected function filterMentions(array $mentions): array {
192  /* @var $entity \ElggEntity */
193  $entity = $this->event->getObject();
194 
195  $already_notified = (array) $entity->_mentioned_usernames;
196 
197  return array_diff($mentions, $already_notified);
198  }
199 }
static isConfigurableForGroup(\ElggGroup $group)
{}
static isConfigurableForUser(\ElggUser $user)
{}
if(($owner instanceof\ElggGroup||$owner instanceof\ElggUser)&&!in_array($owner->guid, $mute_guids)) $actor
Definition: mute.php:86
if(empty($user_guids)) $users
Definition: ban.php:12
elgg_echo(string $message_key, array $args=[], string $language= '')
Elgg language module Functions to manage language and translations.
Definition: languages.php:17
getNotificationBody(\ElggUser $recipient, string $method)
{}
$value
Definition: generic.php:51
elgg_extract($key, $array, $default=null, bool $strict=true)
Checks for $array[$key] and returns its value if it exists, else returns $default.
Definition: elgglib.php:254
Notification Event Handler handles preparation of a notification.
elgg_language_key_exists(string $key, string $language= 'en')
Check if a given language key exists.
Definition: languages.php:44
const ACCESS_PRIVATE
Definition: constants.php:10
$entity
Definition: reset.php:8
$language
Definition: useradd.php:17
elgg_get_entities(array $options=[])
Fetches/counts entities or performs a calculation on their properties.
Definition: entities.php:505
getLanguage(string $fallback=null)
Get user language or default to site language.
Definition: ElggUser.php:71
const MENTION_REGEX
Mentions regex.
if(!$annotation instanceof ElggAnnotation) if(!$annotation->canEdit()) if(!$annotation->delete()) $lan_key
Definition: delete.php:33
getMetadataFields()
Get the metadata fields to check for mentions.
$user
Definition: ban.php:7
if(elgg_extract('required', $vars)) if(elgg_extract('disabled', $vars)) $field
Definition: field.php:38
getNotificationSubject(\ElggUser $recipient, string $method)
{}
getMentions()
Get usernames which are mentioned in the configured metadata fields.
_elgg_services()
Get the global service provider.
Definition: elgglib.php:346
$text
Definition: button.php:33
filterMentions(array $mentions)
Filter mentions.
$recipient
Definition: mute.php:8
getDisplayName()
Get the entity&#39;s display name.
Definition: ElggEntity.php:314