]> git.mxchange.org Git - friendica.git/blob - src/Navigation/Notifications/Factory/Notification.php
Fix wrong spelling
[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) || ($Notification->type === Post\UserNotification::TYPE_SHARED)) {
118                                         $item = Post::selectFirst([], ['uri-id' => $item['thr-parent-id'], 'uid' => [0, $Notification->uid]], ['order' => ['uid' => true]]);
119                                         if (empty($item)) {
120                                                 $this->logger->info('Thread parent post not found', ['uri-id' => $item['thr-parent-id']]);
121                                                 return $message;
122                                         }
123                                 }
124                         }
125
126                         if (in_array($Notification->type, [Post\UserNotification::TYPE_COMMENT_PARTICIPATION, Post\UserNotification::TYPE_ACTIVITY_PARTICIPATION, Post\UserNotification::TYPE_SHARED])) {
127                                 $author = Contact::getById($item['author-id'], ['id', 'name', 'url']);
128                                 if (empty($author)) {
129                                         $this->logger->info('Author not found', ['author' => $item['author-id']]);
130                                         return $message;
131                                 }
132                         }
133
134                         $link = $baseUrl . '/display/' . urlencode($item['guid']);
135
136                         $content = Plaintext::getPost($item, 70);
137                         if (!empty($content['text'])) {
138                                 $title = '"' . trim(str_replace("\n", " ", $content['text'])) . '"';
139                         } else {
140                                 $title = '';
141                         }
142
143                         $this->logger->debug('Got verb and type', ['verb' => $Notification->verb, 'type' => $Notification->type]);
144
145                         switch ($Notification->verb) {
146                                 case Activity::LIKE:
147                                         switch ($Notification->type) {
148                                                 case Post\UserNotification::TYPE_DIRECT_COMMENT:
149                                                         $msg = $userL10n->t('%1$s liked your comment %2$s');
150                                                         break;
151                                                 case Post\UserNotification::TYPE_DIRECT_THREAD_COMMENT:
152                                                         $msg = $userL10n->t('%1$s liked your post %2$s');
153                                                         break;
154                                         }
155                                         break;
156                                 case Activity::DISLIKE:
157                                         switch ($Notification->type) {
158                                                 case Post\UserNotification::TYPE_DIRECT_COMMENT:
159                                                         $msg = $userL10n->t('%1$s disliked your comment %2$s');
160                                                         break;
161                                                 case Post\UserNotification::TYPE_DIRECT_THREAD_COMMENT:
162                                                         $msg = $userL10n->t('%1$s disliked your post %2$s');
163                                                         break;
164                                         }
165                                         break;
166                                 case Activity::ANNOUNCE:
167                                         switch ($Notification->type) {
168                                                 case Post\UserNotification::TYPE_DIRECT_COMMENT:
169                                                         $msg = $userL10n->t('%1$s shared your comment %2$s');
170                                                         break;
171                                                 case Post\UserNotification::TYPE_DIRECT_THREAD_COMMENT:
172                                                         $msg = $userL10n->t('%1$s shared your post %2$s');
173                                                         break;
174                                                 case Post\UserNotification::TYPE_SHARED:
175                                                         if (($causer['id'] != $author['id']) && ($title != '')) {
176                                                                 $msg = $userL10n->t('%1$s shared the post %2$s from %3$s');
177                                                         } elseif ($causer['id'] != $author['id']) {
178                                                                 $msg = $userL10n->t('%1$s shared a post from %3$s');
179                                                         } elseif ($title != '') {
180                                                                 $msg = $userL10n->t('%1$s shared the post %2$s');
181                                                         } else {
182                                                                 $msg = $userL10n->t('%1$s shared a post');
183                                                         }
184                                                         break;
185                                         }
186                                         break;
187                                 case Activity::ATTEND:
188                                         switch ($Notification->type) {
189                                                 case Post\UserNotification::TYPE_DIRECT_THREAD_COMMENT:
190                                                         $msg = $userL10n->t('%1$s wants to attend your event %2$s');
191                                                         break;
192                                         }
193                                         break;
194                                 case Activity::ATTENDNO:
195                                         switch ($Notification->type) {
196                                                 case Post\UserNotification::TYPE_DIRECT_THREAD_COMMENT:
197                                                         $msg = $userL10n->t('%1$s does not want to attend your event %2$s');
198                                                         break;
199                                         }
200                                         break;
201                                 case Activity::ATTENDMAYBE:
202                                         switch ($Notification->type) {
203                                                 case Post\UserNotification::TYPE_DIRECT_THREAD_COMMENT:
204                                                         $msg = $userL10n->t('%1$s maybe wants to attend your event %2$s');
205                                                         break;
206                                         }
207                                         break;
208                                 case Activity::POST:
209                                         switch ($Notification->type) {
210                                                 case Post\UserNotification::TYPE_EXPLICIT_TAGGED:
211                                                         $msg = $userL10n->t('%1$s tagged you on %2$s');
212                                                         break;
213
214                                                 case Post\UserNotification::TYPE_IMPLICIT_TAGGED:
215                                                         $msg = $userL10n->t('%1$s replied to you on %2$s');
216                                                         break;
217
218                                                 case Post\UserNotification::TYPE_THREAD_COMMENT:
219                                                         $msg = $userL10n->t('%1$s commented in your thread %2$s');
220                                                         break;
221
222                                                 case Post\UserNotification::TYPE_DIRECT_COMMENT:
223                                                         $msg = $userL10n->t('%1$s commented on your comment %2$s');
224                                                         break;
225
226                                                 case Post\UserNotification::TYPE_COMMENT_PARTICIPATION:
227                                                 case Post\UserNotification::TYPE_ACTIVITY_PARTICIPATION:
228                                                         if (($causer['id'] == $author['id']) && ($title != '')) {
229                                                                 $msg = $userL10n->t('%1$s commented in their thread %2$s');
230                                                         } elseif ($causer['id'] == $author['id']) {
231                                                                 $msg = $userL10n->t('%1$s commented in their thread');
232                                                         } elseif ($title != '') {
233                                                                 $msg = $userL10n->t('%1$s commented in the thread %2$s from %3$s');
234                                                         } else {
235                                                                 $msg = $userL10n->t('%1$s commented in the thread from %3$s');
236                                                         }
237                                                         break;
238
239                                                 case Post\UserNotification::TYPE_DIRECT_THREAD_COMMENT:
240                                                         $msg = $userL10n->t('%1$s commented on your thread %2$s');
241                                                         break;
242                                         }
243                                         break;
244                         }
245                 }
246
247                 if (!empty($msg)) {
248                         // Name of the notification's causer
249                         $message['causer'] = $causer['name'];
250                         // Format for the "ping" mechanism
251                         $message['notification'] = sprintf($msg, '{0}', $title, $author['name']);
252                         // Plain text for the web push api
253                         $message['plain'] = sprintf($msg, $causer['name'], $title, $author['name']);
254                         // Rich text for other purposes
255                         $message['rich'] = sprintf($msg,
256                                 '[url=' . $causer['url'] . ']' . $causer['name'] . '[/url]',
257                                 '[url=' . $link . ']' . $title . '[/url]',
258                                 '[url=' . $author['url'] . ']' . $author['name'] . '[/url]');
259                 }
260
261                 return $message;
262         }
263 }