]> git.mxchange.org Git - friendica.git/blob - src/Model/Notification.php
Rename Model\Post\UserNotifications type constants
[friendica.git] / src / Model / Notification.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2010-2021, 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\Model;
23
24 use Friendica\BaseModel;
25 use Friendica\Content\Text\BBCode;
26 use Friendica\Content\Text\Plaintext;
27 use Friendica\Core\Logger;
28 use Friendica\Database\Database;
29 use Friendica\DI;
30 use Friendica\Network\HTTPException\InternalServerErrorException;
31 use Friendica\Protocol\Activity;
32 use Psr\Log\LoggerInterface;
33
34 /**
35  * Model for an entry in the notify table
36  */
37 class Notification extends BaseModel
38 {
39         /**
40          * Fetch the notification type for the given notification
41          *
42          * @param array $notification
43          * @return string
44          */
45         public static function getType(array $notification): string
46         {
47                 if (($notification['vid'] == Verb::getID(Activity::FOLLOW)) && ($notification['type'] == Post\UserNotification::TYPE_NONE)) {
48                         $contact = Contact::getById($notification['actor-id'], ['pending']);
49                         $type = $contact['pending'] ? 'follow_request' : 'follow';
50                 } elseif (($notification['vid'] == Verb::getID(Activity::ANNOUNCE)) &&
51                         in_array($notification['type'], [Post\UserNotification::TYPE_DIRECT_COMMENT, Post\UserNotification::TYPE_DIRECT_THREAD_COMMENT])) {
52                         $type = 'reblog';
53                 } elseif (in_array($notification['vid'], [Verb::getID(Activity::LIKE), Verb::getID(Activity::DISLIKE)]) &&
54                         in_array($notification['type'], [Post\UserNotification::TYPE_DIRECT_COMMENT, Post\UserNotification::TYPE_DIRECT_THREAD_COMMENT])) {
55                         $type = 'favourite';
56                 } elseif ($notification['type'] == Post\UserNotification::TYPE_SHARED) {
57                         $type = 'status';
58                 } elseif (in_array($notification['type'], [
59                         Post\UserNotification::TYPE_EXPLICIT_TAGGED,
60             Post\UserNotification::TYPE_IMPLICIT_TAGGED,
61                         Post\UserNotification::TYPE_DIRECT_COMMENT,
62                         Post\UserNotification::TYPE_DIRECT_THREAD_COMMENT,
63                         Post\UserNotification::TYPE_THREAD_COMMENT
64                 ])) {
65                         $type = 'mention';
66                 } else {
67                         return '';
68                 }
69
70                 return $type;
71         }
72
73         /**
74          * Create a notification message for the given notification
75          *
76          * @param array $notification
77          * @return array with the elements "causer", "notification", "plain" and "rich"
78          */
79         public static function getMessage(array $notification)
80         {
81                 $message = [];
82
83                 $user = User::getById($notification['uid']);
84                 if (empty($user)) {
85                         Logger::info('User not found', ['application' => $notification['uid']]);
86                         return $message;
87                 }
88
89                 $l10n = DI::l10n()->withLang($user['language']);
90
91                 $causer = $contact = Contact::getById($notification['actor-id'], ['id', 'name', 'url', 'pending']);
92                 if (empty($contact)) {
93                         Logger::info('Contact not found', ['contact' => $notification['actor-id']]);
94                         return $message;
95                 }
96
97                 if ($notification['type'] == Post\UserNotification::TYPE_NONE) {
98                         if ($contact['pending']) {
99                                 $msg = $l10n->t('%1$s wants to follow you');
100                         } else {
101                                 $msg = $l10n->t('%1$s had started following you');
102                         }
103                         $title = $contact['name'];
104                         $link = DI::baseUrl() . '/contact/' . $contact['id'];
105                 } else {
106                         if (empty($notification['target-uri-id'])) {
107                                 return $message;
108                         }
109
110                         $like     = Verb::getID(Activity::LIKE);
111                         $dislike  = Verb::getID(Activity::DISLIKE);
112                         $announce = Verb::getID(Activity::ANNOUNCE);
113                         $post     = Verb::getID(Activity::POST);
114
115                         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])) {
116                                 $item = Post::selectFirst([], ['uri-id' => $notification['parent-uri-id'], 'uid' => [0, $notification['uid']]], ['order' => ['uid' => true]]);
117                                 if (empty($item)) {
118                                         Logger::info('Parent post not found', ['uri-id' => $notification['parent-uri-id']]);
119                                         return $message;
120                                 }
121                         } else {
122                                 $item = Post::selectFirst([], ['uri-id' => $notification['target-uri-id'], 'uid' => [0, $notification['uid']]], ['order' => ['uid' => true]]);
123                                 if (empty($item)) {
124                                         Logger::info('Post not found', ['uri-id' => $notification['target-uri-id']]);
125                                         return $message;
126                                 }
127
128                                 if ($notification['vid'] == $post) {
129                                         $item = Post::selectFirst([], ['uri-id' => $item['thr-parent-id'], 'uid' => [0, $notification['uid']]], ['order' => ['uid' => true]]);
130                                         if (empty($item)) {
131                                                 Logger::info('Thread parent post not found', ['uri-id' => $item['thr-parent-id']]);
132                                                 return $message;
133                                         }
134                                 }
135                         }
136
137                         if ($item['owner-id'] != $item['author-id']) {
138                                 $cid = $item['owner-id'];
139                         }
140                         if (!empty($item['causer-id']) && ($item['causer-id'] != $item['author-id'])) {
141                                 $cid = $item['causer-id'];
142                         }
143
144                         if (($notification['type'] == Post\UserNotification::TYPE_SHARED) && !empty($cid)) {
145                                 $causer = Contact::getById($cid, ['id', 'name', 'url']);
146                                 if (empty($contact)) {
147                                         Logger::info('Causer not found', ['causer' => $cid]);
148                                         return $message;
149                                 }
150                         } elseif (in_array($notification['type'], [Post\UserNotification::TYPE_COMMENT_PARTICIPATION, Post\UserNotification::TYPE_ACTIVITY_PARTICIPATION])) {
151                                 $contact = Contact::getById($item['author-id'], ['id', 'name', 'url']);
152                                 if (empty($contact)) {
153                                         Logger::info('Author not found', ['author' => $item['author-id']]);
154                                         return $message;
155                                 }
156                         }
157
158                         $link = DI::baseUrl() . '/display/' . urlencode($item['guid']);
159
160                         $content = Plaintext::getPost($item, 70);
161                         if (!empty($content['text'])) {
162                                 $title = '"' . trim(str_replace("\n", " ", $content['text'])) . '"';
163                         } else {
164                                 $title = '';
165                         }
166
167                         switch ($notification['vid']) {
168                                 case $like:
169                                         switch ($notification['type']) {
170                                                 case Post\UserNotification::TYPE_DIRECT_COMMENT:
171                                                         $msg = $l10n->t('%1$s liked your comment %2$s');
172                                                         break;
173                                                 case Post\UserNotification::TYPE_DIRECT_THREAD_COMMENT:
174                                                         $msg = $l10n->t('%1$s liked your post %2$s');
175                                                         break;
176                                                 }
177                                         break;
178                                 case $dislike:
179                                         switch ($notification['type']) {
180                                                 case Post\UserNotification::TYPE_DIRECT_COMMENT:
181                                                         $msg = $l10n->t('%1$s disliked your comment %2$s');
182                                                         break;
183                                                 case Post\UserNotification::TYPE_DIRECT_THREAD_COMMENT:
184                                                         $msg = $l10n->t('%1$s disliked your post %2$s');
185                                                         break;
186                                         }
187                                         break;
188                                 case $announce:
189                                         switch ($notification['type']) {
190                                                 case Post\UserNotification::TYPE_DIRECT_COMMENT:
191                                                         $msg = $l10n->t('%1$s shared your comment %2$s');
192                                                         break;
193                                                 case Post\UserNotification::TYPE_DIRECT_THREAD_COMMENT:
194                                                         $msg = $l10n->t('%1$s shared your post %2$s');
195                                                         break;
196                                                 }
197                                         break;
198                                 case $post:
199                                         switch ($notification['type']) {
200                                                 case Post\UserNotification::TYPE_EXPLICIT_TAGGED:
201                                                         $msg = $l10n->t('%1$s tagged you on %2$s');
202                                                         break;
203
204                                                 case Post\UserNotification::TYPE_IMPLICIT_TAGGED:
205                                                         $msg = $l10n->t('%1$s replied to you on %2$s');
206                                                         break;
207
208                                                 case Post\UserNotification::TYPE_THREAD_COMMENT:
209                                                         $msg = $l10n->t('%1$s commented in your thread %2$s');
210                                                         break;
211
212                                                 case Post\UserNotification::TYPE_DIRECT_COMMENT:
213                                                         $msg = $l10n->t('%1$s commented on your comment %2$s');
214                                                         break;
215
216                                                 case Post\UserNotification::TYPE_COMMENT_PARTICIPATION:
217                                                 case Post\UserNotification::TYPE_ACTIVITY_PARTICIPATION:
218                                                         if (($causer['id'] == $contact['id']) && ($title != '')) {
219                                                                 $msg = $l10n->t('%1$s commented in their thread %2$s');
220                                                         } elseif ($causer['id'] == $contact['id']) {
221                                                                 $msg = $l10n->t('%1$s commented in their thread');
222                                                         } elseif ($title != '') {
223                                                                 $msg = $l10n->t('%1$s commented in the thread %2$s from %3$s');
224                                                         } else {
225                                                                 $msg = $l10n->t('%1$s commented in the thread from %3$s');
226                                                         }
227                                                         break;
228
229                                                 case Post\UserNotification::TYPE_DIRECT_THREAD_COMMENT:
230                                                         $msg = $l10n->t('%1$s commented on your thread %2$s');
231                                                         break;
232
233                                                 case Post\UserNotification::TYPE_SHARED:
234                                                         if (($causer['id'] != $contact['id']) && ($title != '')) {
235                                                                 $msg = $l10n->t('%1$s shared the post %2$s from %3$s');
236                                                         } elseif ($causer['id'] != $contact['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                         }
247                 }
248
249                 if (!empty($msg)) {
250                         // Name of the notification's causer
251                         $message['causer'] = $causer['name'];
252                         // Format for the "ping" mechanism
253                         $message['notification'] = sprintf($msg, '{0}', $title, $contact['name']);
254                         // Plain text for the web push api
255                         $message['plain']        = sprintf($msg, $causer['name'], $title, $contact['name']);
256                         // Rich text for other purposes
257                         $message['rich']         = sprintf($msg,
258                                 '[url=' . $causer['url'] . ']' . $causer['name'] . '[/url]',
259                                 '[url=' . $link . ']' . $title . '[/url]',
260                                 '[url=' . $contact['url'] . ']' . $contact['name'] . '[/url]');
261                 }
262
263                 return $message;
264         }
265 }