]> git.mxchange.org Git - friendica.git/blob - src/Navigation/Notifications/Factory/Notification.php
Merge pull request #11334 from annando/guid-style
[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\Plaintext;
29 use Friendica\Core\L10n;
30 use Friendica\Model\Contact;
31 use Friendica\Model\Post;
32 use Friendica\Model\Verb;
33 use Friendica\Navigation\Notifications\Entity;
34 use Friendica\Network\HTTPException;
35 use Friendica\Protocol\Activity;
36 use Psr\Log\LoggerInterface;
37
38 class Notification extends BaseFactory implements ICanCreateFromTableRow
39 {
40         /** @var BaseURL */
41         private $baseUrl;
42         /** @var L10n */
43         private $l10n;
44         /** @var LocalRelationship */
45         private $localRelationshipRepo;
46
47         public function __construct(\Friendica\App\BaseURL $baseUrl, \Friendica\Core\L10n $l10n, \Friendica\Contact\LocalRelationship\Repository\LocalRelationship $localRelationshipRepo, LoggerInterface $logger)
48         {
49                 parent::__construct($logger);
50
51                 $this->baseUrl = $baseUrl;
52                 $this->l10n = $l10n;
53                 $this->localRelationshipRepo = $localRelationshipRepo;
54         }
55
56         public function createFromTableRow(array $row): Entity\Notification
57         {
58                 return new Entity\Notification(
59                         $row['uid'] ?? 0,
60                         Verb::getByID($row['vid']),
61                         $row['type'],
62                         $row['actor-id'],
63                         $row['target-uri-id'],
64                         $row['parent-uri-id'],
65                         new \DateTime($row['created'], new \DateTimeZone('UTC')),
66                         $row['seen'],
67                         $row['dismissed'],
68                         $row['id'],
69                 );
70         }
71
72         public function createForUser(int $uid, int $vid, int $type, int $actorId, int $targetUriId, int $parentUriId): Entity\Notification
73         {
74                 return new Entity\Notification(
75                         $uid,
76                         Verb::getByID($vid),
77                         $type,
78                         $actorId,
79                         $targetUriId,
80                         $parentUriId
81                 );
82         }
83
84         /**
85          * @param int    $uid
86          * @param int    $contactId Public contact id
87          * @param string $verb
88          * @return Entity\Notification
89          */
90         public function createForRelationship(int $uid, int $contactId, string $verb): Entity\Notification
91         {
92                 return new Entity\Notification(
93                         $uid,
94                         $verb,
95                         Post\UserNotification::TYPE_NONE,
96                         $contactId
97                 );
98         }
99
100         /**
101          * @param Entity\Notification $Notification
102          * @return array
103          * @throws HTTPException\InternalServerErrorException
104          * @throws HTTPException\NotFoundException
105          */
106         public function getMessageFromNotification(Entity\Notification $Notification): array
107         {
108                 $message = [];
109
110                 $causer = $author = Contact::getById($Notification->actorId, ['id', 'name', 'url', 'contact-type', 'pending']);
111                 if (empty($causer)) {
112                         $this->logger->info('Causer not found', ['contact' => $Notification->actorId]);
113                         return $message;
114                 }
115
116                 if ($Notification->type === Post\UserNotification::TYPE_NONE) {
117                         $localRelationship = $this->localRelationshipRepo->getForUserContact($Notification->uid, $Notification->actorId);
118                         if ($localRelationship->pending) {
119                                 $msg = $this->l10n->t('%1$s wants to follow you');
120                         } else {
121                                 $msg = $this->l10n->t('%1$s has started following you');
122                         }
123
124                         $title = $causer['name'];
125                         $link  = $this->baseUrl . '/contact/' . $causer['id'];
126                 } else {
127                         if (!$Notification->targetUriId) {
128                                 return $message;
129                         }
130
131                         $item = Post::selectFirst([], ['uri-id' => $Notification->targetUriId, 'uid' => [0, $Notification->uid]], ['order' => ['uid' => true]]);
132                         if (empty($item)) {
133                                 $this->logger->info('Post not found', ['uri-id' => $Notification->targetUriId]);
134                                 return $message;
135                         }
136
137                         if ($Notification->type == Post\UserNotification::TYPE_ACTIVITY_PARTICIPATION) {
138                                 $thrParentId = $item['thr-parent-id'];
139                                 $item = Post::selectFirst([], ['uri-id' => $thrParentId, 'uid' => [0, $Notification->uid]], ['order' => ['uid' => true]]);
140                                 if (empty($item)) {
141                                         $this->logger->info('Thread parent post not found', ['uri-id' => $thrParentId]);
142                                         return $message;
143                                 }
144                         }
145
146                         $parent = $item;
147                         if ($Notification->targetUriId != $Notification->parentUriId) {
148                                 $parent = Post::selectFirst([], ['uri-id' => $Notification->parentUriId, 'uid' => [0, $Notification->uid]], ['order' => ['uid' => true]]);
149                                 if (empty($parent)) {
150                                         $this->logger->info('Top level post not found', ['uri-id' => $Notification->parentUriId]);
151                                         return $message;
152                                 }
153                         }
154
155                         if (in_array($Notification->type, [Post\UserNotification::TYPE_COMMENT_PARTICIPATION, Post\UserNotification::TYPE_ACTIVITY_PARTICIPATION, Post\UserNotification::TYPE_SHARED])) {
156                                 $author = Contact::getById($item['author-id'], ['id', 'name', 'url', 'contact-type']);
157                                 if (empty($author)) {
158                                         $this->logger->info('Author not found', ['author' => $item['author-id']]);
159                                         return $message;
160                                 }
161                         }
162
163                         $link = $this->baseUrl . '/display/' . urlencode($item['guid']);
164
165                         $content = Plaintext::getPost($parent, 70);
166                         if (!empty($content['text'])) {
167                                 $title = '"' . trim(str_replace("\n", " ", $content['text'])) . '"';
168                         } else {
169                                 $title = '';
170                         }
171
172                         $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]);
173
174                         switch ($Notification->verb) {
175                                 case Activity::LIKE:
176                                         switch ($Notification->type) {
177                                                 case Post\UserNotification::TYPE_DIRECT_COMMENT:
178                                                         $msg = $this->l10n->t('%1$s liked your comment on %2$s');
179                                                         break;
180                                                 case Post\UserNotification::TYPE_DIRECT_THREAD_COMMENT:
181                                                         $msg = $this->l10n->t('%1$s liked your post %2$s');
182                                                         break;
183                                         }
184                                         break;
185                                 case Activity::DISLIKE:
186                                         switch ($Notification->type) {
187                                                 case Post\UserNotification::TYPE_DIRECT_COMMENT:
188                                                         $msg = $this->l10n->t('%1$s disliked your comment on %2$s');
189                                                         break;
190                                                 case Post\UserNotification::TYPE_DIRECT_THREAD_COMMENT:
191                                                         $msg = $this->l10n->t('%1$s disliked your post %2$s');
192                                                         break;
193                                         }
194                                         break;
195                                 case Activity::ANNOUNCE:
196                                         switch ($Notification->type) {
197                                                 case Post\UserNotification::TYPE_DIRECT_COMMENT:
198                                                         $msg = $this->l10n->t('%1$s shared your comment %2$s');
199                                                         break;
200                                                 case Post\UserNotification::TYPE_DIRECT_THREAD_COMMENT:
201                                                         $msg = $this->l10n->t('%1$s shared your post %2$s');
202                                                         break;
203                                                 case Post\UserNotification::TYPE_SHARED:
204                                                         if (($causer['id'] != $author['id']) && ($title != '')) {
205                                                                 $msg = $this->l10n->t('%1$s shared the post %2$s from %3$s');
206                                                         } elseif ($causer['id'] != $author['id']) {
207                                                                 $msg = $this->l10n->t('%1$s shared a post from %3$s');
208                                                         } elseif ($title != '') {
209                                                                 $msg = $this->l10n->t('%1$s shared the post %2$s');
210                                                         } else {
211                                                                 $msg = $this->l10n->t('%1$s shared a post');
212                                                         }
213                                                         break;
214                                         }
215                                         break;
216                                 case Activity::ATTEND:
217                                         switch ($Notification->type) {
218                                                 case Post\UserNotification::TYPE_DIRECT_THREAD_COMMENT:
219                                                         $msg = $this->l10n->t('%1$s wants to attend your event %2$s');
220                                                         break;
221                                         }
222                                         break;
223                                 case Activity::ATTENDNO:
224                                         switch ($Notification->type) {
225                                                 case Post\UserNotification::TYPE_DIRECT_THREAD_COMMENT:
226                                                         $msg = $this->l10n->t('%1$s does not want to attend your event %2$s');
227                                                         break;
228                                         }
229                                         break;
230                                 case Activity::ATTENDMAYBE:
231                                         switch ($Notification->type) {
232                                                 case Post\UserNotification::TYPE_DIRECT_THREAD_COMMENT:
233                                                         $msg = $this->l10n->t('%1$s maybe wants to attend your event %2$s');
234                                                         break;
235                                         }
236                                         break;
237                                 case Activity::POST:
238                                         switch ($Notification->type) {
239                                                 case Post\UserNotification::TYPE_EXPLICIT_TAGGED:
240                                                         $msg = $this->l10n->t('%1$s tagged you on %2$s');
241                                                         break;
242
243                                                 case Post\UserNotification::TYPE_IMPLICIT_TAGGED:
244                                                         $msg = $this->l10n->t('%1$s replied to you on %2$s');
245                                                         break;
246
247                                                 case Post\UserNotification::TYPE_THREAD_COMMENT:
248                                                         $msg = $this->l10n->t('%1$s commented in your thread %2$s');
249                                                         break;
250
251                                                 case Post\UserNotification::TYPE_DIRECT_COMMENT:
252                                                         $msg = $this->l10n->t('%1$s commented on your comment %2$s');
253                                                         break;
254
255                                                 case Post\UserNotification::TYPE_COMMENT_PARTICIPATION:
256                                                 case Post\UserNotification::TYPE_ACTIVITY_PARTICIPATION:
257                                                         if (($causer['id'] == $author['id']) && ($title != '')) {
258                                                                 $msg = $this->l10n->t('%1$s commented in their thread %2$s');
259                                                         } elseif ($causer['id'] == $author['id']) {
260                                                                 $msg = $this->l10n->t('%1$s commented in their thread');
261                                                         } elseif ($title != '') {
262                                                                 $msg = $this->l10n->t('%1$s commented in the thread %2$s from %3$s');
263                                                         } else {
264                                                                 $msg = $this->l10n->t('%1$s commented in the thread from %3$s');
265                                                         }
266                                                         break;
267
268                                                 case Post\UserNotification::TYPE_DIRECT_THREAD_COMMENT:
269                                                         $msg = $this->l10n->t('%1$s commented on your thread %2$s');
270                                                         break;
271
272                                                 case Post\UserNotification::TYPE_SHARED:
273                                                         if (($causer['id'] != $author['id']) && ($title != '')) {
274                                                                 $msg = $this->l10n->t('%1$s shared the post %2$s from %3$s');
275                                                         } elseif ($causer['id'] != $author['id']) {
276                                                                 $msg = $this->l10n->t('%1$s shared a post from %3$s');
277                                                         } elseif ($title != '') {
278                                                                 $msg = $this->l10n->t('%1$s shared the post %2$s');
279                                                         } else {
280                                                                 $msg = $this->l10n->t('%1$s shared a post');
281                                                         }
282                                                         break;
283                                         }
284                                         break;
285                         }
286                 }
287
288                 if (!empty($msg)) {
289                         // Name of the notification's causer
290                         $message['causer'] = $causer['name'];
291                         // Format for the "ping" mechanism
292                         $message['notification'] = sprintf($msg, '{0}', $title, $author['name']);
293                         // Plain text for the web push api
294                         $message['plain'] = sprintf($msg, $causer['name'], $title, $author['name']);
295                         // Rich text for other purposes
296                         $message['rich'] = sprintf($msg,
297                                 '[url=' . $causer['url'] . ']' . $causer['name'] . '[/url]',
298                                 '[url=' . $link . ']' . $title . '[/url]',
299                                 '[url=' . $author['url'] . ']' . $author['name'] . '[/url]');
300                         $message['link'] = $link;
301                 }
302
303                 return $message;
304         }
305 }