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  $entity = $this->getEventEntity();
19  if (!$entity instanceof \ElggEntity || $entity->access_id === ACCESS_PRIVATE) {
20  // no actions taken on private content
21  return [];
22  }
23 
24  $mentions = $this->getMentions();
25  if (empty($mentions)) {
26  return [];
27  }
28 
29  // store the usernames as notified
30  $notified = (array) $entity->_mentioned_usernames;
31  $entity->_mentioned_usernames = array_merge($notified, $mentions);
32 
33  // transform usernames to users to find notification preferences
34  $result = [];
36  'type' => 'user',
37  'limit' => false,
38  'batch' => true,
39  'metadata_name_value_pairs' => [
40  [
41  'name' => 'username',
42  'value' => $mentions,
43  ],
44  ],
45  ]);
46  /* @var $user \ElggUser */
47  foreach ($users as $user) {
48  $preference = $user->getNotificationSettings('mentions');
49  $preference = array_keys(array_filter($preference));
50  if (empty($preference)) {
51  continue;
52  }
53 
54  $result[$user->guid] = $preference;
55  }
56 
57  // remove the actor from the subscribers
58  unset($result[$this->event->getActorGUID()]);
59 
60  return $result;
61  }
62 
66  protected function getNotificationSubject(\ElggUser $recipient, string $method): string {
67  $lan_key = 'notification:mentions:subject';
68  if (elgg_language_key_exists("notification:{$this->event->getDescription()}:subject")) {
69  $lan_key = "notification:{$this->event->getDescription()}:subject";
70  }
71 
72  return elgg_echo($lan_key, [$this->getEventActor()?->getDisplayName()]);
73  }
74 
78  protected function getNotificationBody(\ElggUser $recipient, string $method): string {
79  $entity = $this->getEventEntity();
80 
81  $lan_key = 'notification:mentions:body';
82  if (elgg_language_key_exists("notification:{$this->event->getDescription()}:body")) {
83  $lan_key = "notification:{$this->event->getDescription()}:body";
84  }
85 
86  return elgg_echo($lan_key, [
87  $this->getEventActor()?->getDisplayName(),
88  $entity?->getDisplayName(),
89  $entity?->getURL(),
90  ]);
91  }
92 
96  protected static function isConfigurableForUser(\ElggUser $user): bool {
97  return false;
98  }
99 
103  protected static function isConfigurableForGroup(\ElggGroup $group): bool {
104  return false;
105  }
106 
110  protected function addMuteLink(): bool {
111  return false;
112  }
113 
119  protected function getMetadataFields(): array {
120  return ['description'];
121  }
122 
128  protected function getMentions(): array {
129  $entity = $this->getEventEntity();
130  if (!$entity instanceof \ElggEntity) {
131  return [];
132  }
133 
134  $result = [];
135  $metadata_fields = $this->getMetadataFields();
136 
137  foreach ($metadata_fields as $field) {
138  $value = $entity->$field;
139  if (empty($value)) {
140  continue;
141  }
142 
143  if (!is_array($value)) {
144  $value = [$value];
145  }
146 
147  foreach ($value as $text) {
148  if (empty($text) || !is_string($text)) {
149  continue;
150  }
151 
152  $matches = [];
153  preg_match_all(HtmlFormatter::MENTION_REGEX, $text, $matches);
154 
155  $text_mentions = (array) elgg_extract(3, $matches);
156  // remove empty/duplicate values
157  $text_mentions = array_values(array_unique(array_filter($text_mentions)));
158  // remove trailing punctuation (.) from the username
159  $text_mentions = array_map(function($mention) {
160  return rtrim($mention, '.');
161  }, $text_mentions);
162 
163  // prepare hook so other plugins can extend the mentions found in the text
164  $params = [
165  'entity' => $entity,
166  'field' => $field,
167  'text' => $text,
168  ];
169  $text_mentions = (array) _elgg_services()->events->triggerResults('usernames', 'mentions', $params, $text_mentions);
170 
171  $result = array_merge($result, $text_mentions);
172  }
173  }
174 
175  $result = array_values(array_unique($result));
176 
177  return $this->filterMentions($result);
178  }
179 
188  protected function filterMentions(array $mentions): array {
189  $entity = $this->getEventEntity();
190  if (!$entity instanceof \ElggEntity) {
191  return $mentions;
192  }
193 
194  $already_notified = (array) $entity->_mentioned_usernames;
195 
196  return array_diff($mentions, $already_notified);
197  }
198 }
static isConfigurableForGroup(\ElggGroup $group)
{}
if(!elgg_get_config('trash_enabled')) $group
Definition: group.php:13
static isConfigurableForUser(\ElggUser $user)
{}
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
if(elgg_extract('input_type', $vars)) if(elgg_extract('required', $vars)) if(elgg_extract('disabled', $vars)) $field
Definition: field.php:42
getEventEntity()
Get the entity from the notification event.
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
elgg_get_entities(array $options=[])
Fetches/counts entities or performs a calculation on their properties.
Definition: entities.php:507
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.
getEventActor()
Get the acting user from the notification event.
$user
Definition: ban.php:7
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:351
$text
Definition: button.php:33
filterMentions(array $mentions)
Filter mentions.
$recipient
Definition: mute.php:8