]> git.mxchange.org Git - friendica.git/blob - src/Navigation/Notifications/Factory/Notification.php
Improved notification for announced posts
[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\Content\Text\Plaintext;
28 use Friendica\Core\L10n;
29 use Friendica\Model\Contact;
30 use Friendica\Model\Post;
31 use Friendica\Model\Verb;
32 use Friendica\Navigation\Notifications\Entity;
33 use Friendica\Protocol\Activity;
34
35 class Notification extends BaseFactory implements ICanCreateFromTableRow
36 {
37         public function createFromTableRow(array $row): Entity\Notification
38         {
39                 return new Entity\Notification(
40                         $row['uid'] ?? 0,
41                         Verb::getByID($row['vid']),
42                         $row['type'],
43                         $row['actor-id'],
44                         $row['target-uri-id'],
45                         $row['parent-uri-id'],
46                         new \DateTime($row['created'], new \DateTimeZone('UTC')),
47                         $row['seen'],
48                         $row['id']
49                 );
50         }
51
52         public function createForUser(int $uid, int $vid, int $type, int $actorId, int $targetUriId, int $parentUriId): Entity\Notification
53         {
54                 return new Entity\Notification(
55                         $uid,
56                         Verb::getByID($vid),
57                         $type,
58                         $actorId,
59                         $targetUriId,
60                         $parentUriId
61                 );
62         }
63
64         public function createForRelationship(int $uid, int $contactId, string $verb): Entity\Notification
65         {
66                 return new Entity\Notification(
67                         $uid,
68                         $verb,
69                         Post\UserNotification::TYPE_NONE,
70                         $contactId
71                 );
72         }
73
74         /**
75          * @param Entity\Notification $Notification
76          * @param BaseURL             $baseUrl
77          * @param L10n                $userL10n Seeded with the language of the user we mean the notification for
78          * @return array
79          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
80          */
81         public function getMessageFromNotification(Entity\Notification $Notification, BaseURL $baseUrl, L10n $userL10n)
82         {
83                 $message = [];
84
85                 $causer = $author = Contact::getById($Notification->actorId, ['id', 'name', 'url', 'pending']);
86                 if (empty($causer)) {
87                         $this->logger->info('Causer not found', ['contact' => $Notification->actorId]);
88                         return $message;
89                 }
90
91                 if ($Notification->type === Post\UserNotification::TYPE_NONE) {
92                         if ($causer['pending']) {
93                                 $msg = $userL10n->t('%1$s wants to follow you');
94                         } else {
95                                 $msg = $userL10n->t('%1$s had started following you');
96                         }
97                         $title = $causer['name'];
98                         $link  = $baseUrl . '/contact/' . $causer['id'];
99                 } else {
100                         if (!$Notification->targetUriId) {
101                                 return $message;
102                         }
103
104                         if (in_array($Notification->type, [Post\UserNotification::TYPE_THREAD_COMMENT, Post\UserNotification::TYPE_COMMENT_PARTICIPATION, Post\UserNotification::TYPE_ACTIVITY_PARTICIPATION, Post\UserNotification::TYPE_EXPLICIT_TAGGED])) {
105                                 $item = Post::selectFirst([], ['uri-id' => $Notification->parentUriId, 'uid' => [0, $Notification->uid]], ['order' => ['uid' => true]]);
106                                 if (empty($item)) {
107                                         $this->logger->info('Parent post not found', ['uri-id' => $Notification->parentUriId]);
108                                         return $message;
109                                 }
110                         } else {
111                                 $item = Post::selectFirst([], ['uri-id' => $Notification->targetUriId, 'uid' => [0, $Notification->uid]], ['order' => ['uid' => true]]);
112                                 if (empty($item)) {
113                                         $this->logger->info('Post not found', ['uri-id' => $Notification->targetUriId]);
114                                         return $message;
115                                 }
116
117                                 if (($Notification->verb == Activity::POST) || 
118                                         (($Notification->type === Post\UserNotification::TYPE_SHARED) && ($Notification->verb == Activity::ANNOUNCE))) {
119                                         $item = Post::selectFirst([], ['uri-id' => $item['thr-parent-id'], 'uid' => [0, $Notification->uid]], ['order' => ['uid' => true]]);
120                                         if (empty($item)) {
121                                                 $this->logger->info('Thread parent post not found', ['uri-id' => $item['thr-parent-id']]);
122                                                 return $message;
123                                         }
124                                 }
125                         }
126
127                         if ($Notification->type === Post\UserNotification::TYPE_SHARED) {
128                                 $author = Contact::getById($item['author-id'], ['id', 'name', 'url']);
129                                 if (empty($author)) {
130                                         $this->logger->info('Author not found', ['author' => $item['author-id']]);
131                                         return $message;
132                                 }
133                         } elseif (in_array($Notification->type, [Post\UserNotification::TYPE_COMMENT_PARTICIPATION, Post\UserNotification::TYPE_ACTIVITY_PARTICIPATION])) {
134                                 $author = Contact::getById($item['author-id'], ['id', 'name', 'url']);
135                                 if (empty($author)) {
136                                         $this->logger->info('Author not found', ['author' => $item['author-id']]);
137                                         return $message;
138                                 }
139                         }
140
141                         $link = $baseUrl . '/display/' . urlencode($item['guid']);
142
143                         $content = Plaintext::getPost($item, 70);
144                         if (!empty($content['text'])) {
145                                 $title = '"' . trim(str_replace("\n", " ", $content['text'])) . '"';
146                         } else {
147                                 $title = '';
148                         }
149
150                         $this->logger->debug('Got verb and type', ['verb' => $Notification->verb, 'type' => $Notification->type]);
151
152                         switch ($Notification->verb) {
153                                 case Activity::LIKE:
154                                         switch ($Notification->type) {
155                                                 case Post\UserNotification::TYPE_DIRECT_COMMENT:
156                                                         $msg = $userL10n->t('%1$s liked your comment %2$s');
157                                                         break;
158                                                 case Post\UserNotification::TYPE_DIRECT_THREAD_COMMENT:
159                                                         $msg = $userL10n->t('%1$s liked your post %2$s');
160                                                         break;
161                                         }
162                                         break;
163                                 case Activity::DISLIKE:
164                                         switch ($Notification->type) {
165                                                 case Post\UserNotification::TYPE_DIRECT_COMMENT:
166                                                         $msg = $userL10n->t('%1$s disliked your comment %2$s');
167                                                         break;
168                                                 case Post\UserNotification::TYPE_DIRECT_THREAD_COMMENT:
169                                                         $msg = $userL10n->t('%1$s disliked your post %2$s');
170                                                         break;
171                                         }
172                                         break;
173                                 case Activity::ANNOUNCE:
174                                         switch ($Notification->type) {
175                                                 case Post\UserNotification::TYPE_DIRECT_COMMENT:
176                                                         $msg = $userL10n->t('%1$s shared your comment %2$s');
177                                                         break;
178                                                 case Post\UserNotification::TYPE_DIRECT_THREAD_COMMENT:
179                                                         $msg = $userL10n->t('%1$s shared your post %2$s');
180                                                         break;
181                                                 case Post\UserNotification::TYPE_SHARED:
182                                                         if (($causer['id'] != $author['id']) && ($title != '')) {
183                                                                 $msg = $userL10n->t('%1$s shared the post %2$s from %3$s');
184                                                         } elseif ($causer['id'] != $author['id']) {
185                                                                 $msg = $userL10n->t('%1$s shared a post from %3$s');
186                                                         } elseif ($title != '') {
187                                                                 $msg = $userL10n->t('%1$s shared the post %2$s');
188                                                         } else {
189                                                                 $msg = $userL10n->t('%1$s shared a post');
190                                                         }
191                                                         break;
192                                         }
193                                         break;
194                                 case Activity::POST:
195                                         switch ($Notification->type) {
196                                                 case Post\UserNotification::TYPE_EXPLICIT_TAGGED:
197                                                         $msg = $userL10n->t('%1$s tagged you on %2$s');
198                                                         break;
199
200                                                 case Post\UserNotification::TYPE_IMPLICIT_TAGGED:
201                                                         $msg = $userL10n->t('%1$s replied to you on %2$s');
202                                                         break;
203
204                                                 case Post\UserNotification::TYPE_THREAD_COMMENT:
205                                                         $msg = $userL10n->t('%1$s commented in your thread %2$s');
206                                                         break;
207
208                                                 case Post\UserNotification::TYPE_DIRECT_COMMENT:
209                                                         $msg = $userL10n->t('%1$s commented on your comment %2$s');
210                                                         break;
211
212                                                 case Post\UserNotification::TYPE_COMMENT_PARTICIPATION:
213                                                 case Post\UserNotification::TYPE_ACTIVITY_PARTICIPATION:
214                                                         if (($causer['id'] == $author['id']) && ($title != '')) {
215                                                                 $msg = $userL10n->t('%1$s commented in their thread %2$s');
216                                                         } elseif ($causer['id'] == $author['id']) {
217                                                                 $msg = $userL10n->t('%1$s commented in their thread');
218                                                         } elseif ($title != '') {
219                                                                 $msg = $userL10n->t('%1$s commented in the thread %2$s from %3$s');
220                                                         } else {
221                                                                 $msg = $userL10n->t('%1$s commented in the thread from %3$s');
222                                                         }
223                                                         break;
224
225                                                 case Post\UserNotification::TYPE_DIRECT_THREAD_COMMENT:
226                                                         $msg = $userL10n->t('%1$s commented on your thread %2$s');
227                                                         break;
228                                         }
229                                         break;
230                         }
231                 }
232
233                 if (!empty($msg)) {
234                         // Name of the notification's causer
235                         $message['causer'] = $causer['name'];
236                         // Format for the "ping" mechanism
237                         $message['notification'] = sprintf($msg, '{0}', $title, $author['name']);
238                         // Plain text for the web push api
239                         $message['plain'] = sprintf($msg, $causer['name'], $title, $author['name']);
240                         // Rich text for other purposes
241                         $message['rich'] = sprintf($msg,
242                                 '[url=' . $causer['url'] . ']' . $causer['name'] . '[/url]',
243                                 '[url=' . $link . ']' . $title . '[/url]',
244                                 '[url=' . $author['url'] . ']' . $author['name'] . '[/url]');
245                 }
246
247                 return $message;
248         }
249 }