]> git.mxchange.org Git - friendica.git/blob - src/Navigation/Notifications/Factory/Notification.php
Merge pull request #12532 from annando/warning
[friendica.git] / src / Navigation / Notifications / Factory / Notification.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2010-2022, the Friendica project
4  *
5  * @license GNU AGPL version 3 or any later version
6  *
7  * This program is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU Affero General Public License as
9  * published by the Free Software Foundation, either version 3 of the
10  * License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU Affero General Public License for more details.
16  *
17  * You should have received a copy of the GNU Affero General Public License
18  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
19  *
20  */
21
22 namespace Friendica\Navigation\Notifications\Factory;
23
24 use Friendica\App\BaseURL;
25 use Friendica\BaseFactory;
26 use Friendica\Capabilities\ICanCreateFromTableRow;
27 use Friendica\Contact\LocalRelationship\Repository\LocalRelationship;
28 use Friendica\Content\Text\BBCode;
29 use Friendica\Content\Text\Plaintext;
30 use Friendica\Core\Cache\Enum\Duration;
31 use Friendica\Core\Cache\Capability\ICanCache;
32 use Friendica\Core\L10n;
33 use Friendica\Model\Contact;
34 use Friendica\Model\Post;
35 use Friendica\Model\User;
36 use Friendica\Model\Verb;
37 use Friendica\Navigation\Notifications\Entity;
38 use Friendica\Network\HTTPException;
39 use Friendica\Protocol\Activity;
40 use Psr\Log\LoggerInterface;
41
42 class Notification extends BaseFactory implements ICanCreateFromTableRow
43 {
44         /** @var BaseURL */
45         private $baseUrl;
46         /** @var L10n */
47         private $l10n;
48         /** @var LocalRelationship */
49         private $localRelationshipRepo;
50         /** @var ICanCache */
51         private $cache;
52
53         public function __construct(\Friendica\App\BaseURL $baseUrl, \Friendica\Core\L10n $l10n, \Friendica\Contact\LocalRelationship\Repository\LocalRelationship $localRelationshipRepo, LoggerInterface $logger, ICanCache $cache)
54         {
55                 parent::__construct($logger);
56
57                 $this->baseUrl = $baseUrl;
58                 $this->l10n = $l10n;
59                 $this->localRelationshipRepo = $localRelationshipRepo;
60                 $this->cache = $cache;
61         }
62
63         public function createFromTableRow(array $row): Entity\Notification
64         {
65                 return new Entity\Notification(
66                         $row['uid'] ?? 0,
67                         Verb::getByID($row['vid']),
68                         $row['type'],
69                         $row['actor-id'],
70                         $row['target-uri-id'],
71                         $row['parent-uri-id'],
72                         new \DateTime($row['created'], new \DateTimeZone('UTC')),
73                         $row['seen'],
74                         $row['dismissed'],
75                         $row['id'],
76                 );
77         }
78
79         public function createForUser(int $uid, int $vid, int $type, int $actorId, int $targetUriId, int $parentUriId): Entity\Notification
80         {
81                 return new Entity\Notification(
82                         $uid,
83                         Verb::getByID($vid),
84                         $type,
85                         $actorId,
86                         $targetUriId,
87                         $parentUriId
88                 );
89         }
90
91         /**
92          * @param int    $uid
93          * @param int    $contactId Public contact id
94          * @param string $verb
95          * @return Entity\Notification
96          */
97         public function createForRelationship(int $uid, int $contactId, string $verb): Entity\Notification
98         {
99                 return new Entity\Notification(
100                         $uid,
101                         $verb,
102                         Post\UserNotification::TYPE_NONE,
103                         $contactId
104                 );
105         }
106
107         /**
108          * @param Entity\Notification $Notification
109          * @return array
110          * @throws HTTPException\InternalServerErrorException
111          * @throws HTTPException\NotFoundException
112          */
113         public function getMessageFromNotification(Entity\Notification $Notification): array
114         {
115                 $message = [];
116
117                 $cachekey = 'Notification:' . $Notification->id;
118                 $result = $this->cache->get($cachekey);
119                 if (!is_null($result)) {
120                         return $result;
121                 }
122
123                 $user = User::getById($Notification->uid, ['language']);
124                 $l10n = $this->l10n->withLang($user['language']);
125         
126                 $causer = $author = Contact::getById($Notification->actorId, ['id', 'name', 'url', 'contact-type', 'pending']);
127                 if (empty($causer)) {
128                         $this->logger->info('Causer not found', ['contact' => $Notification->actorId]);
129                         return $message;
130                 }
131
132                 if ($Notification->type === Post\UserNotification::TYPE_NONE) {
133                         if ($causer['pending']) {
134                                 $msg = $l10n->t('%1$s wants to follow you');
135                         } else {
136                                 $msg = $l10n->t('%1$s has started following you');
137                         }
138
139                         $title = $causer['name'];
140                         $link  = $this->baseUrl . '/contact/' . $causer['id'];
141                 } else {
142                         if (!$Notification->targetUriId) {
143                                 return $message;
144                         }
145
146                         if (Post\ThreadUser::getIgnored($Notification->parentUriId, $Notification->uid)) {
147                                 $this->logger->info('Thread is ignored', ['parent-uri-id' => $Notification->parentUriId, 'type' => $Notification->type]);
148                                 return $message;
149                         }
150
151                         if (in_array($Notification->type, [Post\UserNotification::TYPE_THREAD_COMMENT, Post\UserNotification::TYPE_COMMENT_PARTICIPATION, Post\UserNotification::TYPE_ACTIVITY_PARTICIPATION, Post\UserNotification::TYPE_FOLLOW, Post\UserNotification::TYPE_EXPLICIT_TAGGED])) {
152                                 $item = Post::selectFirst([], ['uri-id' => $Notification->parentUriId, 'uid' => [0, $Notification->uid]], ['order' => ['uid' => true]]);
153                                 if (empty($item)) {
154                                         $this->logger->info('Parent post not found', ['uri-id' => $Notification->parentUriId]);
155                                         return $message;
156                                 }
157                                 $link_item = Post::selectFirstPost(['guid'], ['uri-id' => $Notification->targetUriId]);
158                         } else {
159                                 $link_item = $item = Post::selectFirst([], ['uri-id' => $Notification->targetUriId, 'uid' => [0, $Notification->uid]], ['order' => ['uid' => true]]);
160                                 if (empty($item)) {
161                                         $this->logger->info('Post not found', ['uri-id' => $Notification->targetUriId]);
162                                         return $message;
163                                 }
164
165                                 if (($Notification->verb == Activity::POST) || ($Notification->type === Post\UserNotification::TYPE_SHARED)) {
166                                         $thrparentid = $item['thr-parent-id'];
167                                         $item = Post::selectFirst([], ['uri-id' => $thrparentid, 'uid' => [0, $Notification->uid]], ['order' => ['uid' => true]]);
168                                         if (empty($item)) {
169                                                 $this->logger->info('Thread parent post not found', ['uri-id' => $thrparentid]);
170                                                 return $message;
171                                         }
172                                 }
173
174                                 if (($Notification->verb != Activity::POST) || !in_array($Notification->type, [Post\UserNotification::TYPE_DIRECT_THREAD_COMMENT, Post\UserNotification::TYPE_IMPLICIT_TAGGED])) {
175                                         $link_item = $item;
176                                 }
177                         }
178
179                         if (in_array($Notification->type, [Post\UserNotification::TYPE_COMMENT_PARTICIPATION, Post\UserNotification::TYPE_ACTIVITY_PARTICIPATION, Post\UserNotification::TYPE_FOLLOW, Post\UserNotification::TYPE_SHARED])) {
180                                 $author = Contact::getById($item['author-id'], ['id', 'name', 'url', 'contact-type']);
181                                 if (empty($author)) {
182                                         $this->logger->info('Author not found', ['author' => $item['author-id']]);
183                                         return $message;
184                                 }
185                         }
186
187                         // Final check on $link_item
188                         // @see https://github.com/friendica/friendica/issues/11632#issuecomment-1183365937
189                         if (empty($link_item)) {
190                                 $this->logger->info('Link item is still empty. Dumping whole Notification object:', [$Notification]);
191                                 return $message;
192                         }
193
194                         $link = $this->baseUrl . '/display/' . urlencode($link_item['guid']);
195
196                         $body = BBCode::toPlaintext($item['body'], false);
197                         $title = Plaintext::shorten($body, 70);
198                         if (!empty($title)) {
199                                 $title = '"' . trim(str_replace("\n", " ", $title)) . '"';
200                         }
201
202                         $this->logger->debug('Got verb and type', ['verb' => $Notification->verb, 'type' => $Notification->type, 'causer' => $causer['id'], 'author' => $author['id'], 'item' => $item['id'], 'uid' => $Notification->uid]);
203
204                         switch ($Notification->verb) {
205                                 case Activity::LIKE:
206                                         switch ($Notification->type) {
207                                                 case Post\UserNotification::TYPE_DIRECT_COMMENT:
208                                                         $msg = $l10n->t('%1$s liked your comment on %2$s');
209                                                         break;
210                                                 case Post\UserNotification::TYPE_DIRECT_THREAD_COMMENT:
211                                                         $msg = $l10n->t('%1$s liked your post %2$s');
212                                                         break;
213                                         }
214                                         break;
215                                 case Activity::DISLIKE:
216                                         switch ($Notification->type) {
217                                                 case Post\UserNotification::TYPE_DIRECT_COMMENT:
218                                                         $msg = $l10n->t('%1$s disliked your comment on %2$s');
219                                                         break;
220                                                 case Post\UserNotification::TYPE_DIRECT_THREAD_COMMENT:
221                                                         $msg = $l10n->t('%1$s disliked your post %2$s');
222                                                         break;
223                                         }
224                                         break;
225                                 case Activity::ANNOUNCE:
226                                         switch ($Notification->type) {
227                                                 case Post\UserNotification::TYPE_DIRECT_COMMENT:
228                                                         $msg = $l10n->t('%1$s shared your comment %2$s');
229                                                         break;
230                                                 case Post\UserNotification::TYPE_DIRECT_THREAD_COMMENT:
231                                                         $msg = $l10n->t('%1$s shared your post %2$s');
232                                                         break;
233                                                 case Post\UserNotification::TYPE_SHARED:
234                                                         if (($causer['id'] != $author['id']) && ($title != '')) {
235                                                                 $msg = $l10n->t('%1$s shared the post %2$s from %3$s');
236                                                         } elseif ($causer['id'] != $author['id']) {
237                                                                 $msg = $l10n->t('%1$s shared a post from %3$s');
238                                                         } elseif ($title != '') {
239                                                                 $msg = $l10n->t('%1$s shared the post %2$s');
240                                                         } else {
241                                                                 $msg = $l10n->t('%1$s shared a post');
242                                                         }
243                                                         break;
244                                         }
245                                         break;
246                                 case Activity::ATTEND:
247                                         switch ($Notification->type) {
248                                                 case Post\UserNotification::TYPE_DIRECT_THREAD_COMMENT:
249                                                         $msg = $l10n->t('%1$s wants to attend your event %2$s');
250                                                         break;
251                                         }
252                                         break;
253                                 case Activity::ATTENDNO:
254                                         switch ($Notification->type) {
255                                                 case Post\UserNotification::TYPE_DIRECT_THREAD_COMMENT:
256                                                         $msg = $l10n->t('%1$s does not want to attend your event %2$s');
257                                                         break;
258                                         }
259                                         break;
260                                 case Activity::ATTENDMAYBE:
261                                         switch ($Notification->type) {
262                                                 case Post\UserNotification::TYPE_DIRECT_THREAD_COMMENT:
263                                                         $msg = $l10n->t('%1$s maybe wants to attend your event %2$s');
264                                                         break;
265                                         }
266                                         break;
267                                 case Activity::POST:
268                                         switch ($Notification->type) {
269                                                 case Post\UserNotification::TYPE_EXPLICIT_TAGGED:
270                                                         $msg = $l10n->t('%1$s tagged you on %2$s');
271                                                         break;
272
273                                                 case Post\UserNotification::TYPE_IMPLICIT_TAGGED:
274                                                         $msg = $l10n->t('%1$s replied to you on %2$s');
275                                                         break;
276
277                                                 case Post\UserNotification::TYPE_THREAD_COMMENT:
278                                                         $msg = $l10n->t('%1$s commented in your thread %2$s');
279                                                         break;
280
281                                                 case Post\UserNotification::TYPE_DIRECT_COMMENT:
282                                                         $msg = $l10n->t('%1$s commented on your comment %2$s');
283                                                         break;
284
285                                                 case Post\UserNotification::TYPE_COMMENT_PARTICIPATION:
286                                                 case Post\UserNotification::TYPE_ACTIVITY_PARTICIPATION:
287                                                 case Post\UserNotification::TYPE_FOLLOW;
288                                                         if (($causer['id'] == $author['id']) && ($title != '')) {
289                                                                 $msg = $l10n->t('%1$s commented in their thread %2$s');
290                                                         } elseif ($causer['id'] == $author['id']) {
291                                                                 $msg = $l10n->t('%1$s commented in their thread');
292                                                         } elseif ($title != '') {
293                                                                 $msg = $l10n->t('%1$s commented in the thread %2$s from %3$s');
294                                                         } else {
295                                                                 $msg = $l10n->t('%1$s commented in the thread from %3$s');
296                                                         }
297                                                         break;
298
299                                                 case Post\UserNotification::TYPE_DIRECT_THREAD_COMMENT:
300                                                         $msg = $l10n->t('%1$s commented on your thread %2$s');
301                                                         break;
302
303                                                 case Post\UserNotification::TYPE_SHARED:
304                                                         if (($causer['id'] != $author['id']) && ($title != '')) {
305                                                                 $msg = $l10n->t('%1$s shared the post %2$s from %3$s');
306                                                         } elseif ($causer['id'] != $author['id']) {
307                                                                 $msg = $l10n->t('%1$s shared a post from %3$s');
308                                                         } elseif ($title != '') {
309                                                                 $msg = $l10n->t('%1$s shared the post %2$s');
310                                                         } else {
311                                                                 $msg = $l10n->t('%1$s shared a post');
312                                                         }
313                                                         break;
314
315                                                 case Post\UserNotification::TYPE_QUOTED:
316                                                         $msg = $l10n->t('%1$s shared your post %2$s');
317                                                         break;
318                                         }
319                                         break;
320                         }
321                 }
322
323                 if (!empty($msg)) {
324                         // Name of the notification's causer
325                         $message['causer'] = $causer['name'];
326                         // Format for the "ping" mechanism
327                         $message['notification'] = sprintf($msg, '{0}', $title, $author['name']);
328                         // Plain text for the web push api
329                         $message['plain'] = sprintf($msg, $causer['name'], $title, $author['name']);
330                         // Rich text for other purposes
331                         $message['rich'] = sprintf($msg,
332                                 '[url=' . $causer['url'] . ']' . $causer['name'] . '[/url]',
333                                 '[url=' . $link . ']' . $title . '[/url]',
334                                 '[url=' . $author['url'] . ']' . $author['name'] . '[/url]');
335                         $message['link'] = $link;
336                         $this->cache->set($cachekey, $message, Duration::HOUR);
337                 } else {
338                         $this->logger->debug('Unhandled notification', ['notification' => $Notification]);
339                 }
340
341                 return $message;
342         }
343 }