]> git.mxchange.org Git - friendica.git/blob - src/Model/Notification.php
Move notify to the new paradigm
[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::NOTIF_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::NOTIF_DIRECT_COMMENT, Post\UserNotification::NOTIF_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::NOTIF_DIRECT_COMMENT, Post\UserNotification::NOTIF_DIRECT_THREAD_COMMENT])) {
55                         $type = 'favourite';
56                 } elseif ($notification['type'] == Post\UserNotification::NOTIF_SHARED) {
57                         $type = 'status';
58                 } elseif (in_array($notification['type'], [Post\UserNotification::NOTIF_EXPLICIT_TAGGED,
59                         Post\UserNotification::NOTIF_IMPLICIT_TAGGED, Post\UserNotification::NOTIF_DIRECT_COMMENT,
60                         Post\UserNotification::NOTIF_DIRECT_THREAD_COMMENT, Post\UserNotification::NOTIF_THREAD_COMMENT])) {
61                         $type = 'mention';
62                 } else {
63                         return '';
64                 }
65
66                 return $type;
67         }
68
69         /**
70          * Create a notification message for the given notification
71          *
72          * @param array $notification
73          * @return array with the elements "causer", "notification", "plain" and "rich"
74          */
75         public static function getMessage(array $notification)
76         {
77                 $message = [];
78
79                 $user = User::getById($notification['uid']);
80                 if (empty($user)) {
81                         Logger::info('User not found', ['application' => $notification['uid']]);
82                         return $message;
83                 }
84
85                 $l10n = DI::l10n()->withLang($user['language']);
86
87                 $causer = $contact = Contact::getById($notification['actor-id'], ['id', 'name', 'url', 'pending']);
88                 if (empty($contact)) {
89                         Logger::info('Contact not found', ['contact' => $notification['actor-id']]);
90                         return $message;
91                 }
92
93                 if ($notification['type'] == Post\UserNotification::NOTIF_NONE) {
94                         if ($contact['pending']) {
95                                 $msg = $l10n->t('%1$s wants to follow you');
96                         } else {
97                                 $msg = $l10n->t('%1$s had started following you');
98                         }
99                         $title = $contact['name'];
100                         $link = DI::baseUrl() . '/contact/' . $contact['id'];
101                 } else {
102                         if (empty($notification['target-uri-id'])) {
103                                 return $message;
104                         }
105
106                         $like     = Verb::getID(Activity::LIKE);
107                         $dislike  = Verb::getID(Activity::DISLIKE);
108                         $announce = Verb::getID(Activity::ANNOUNCE);
109                         $post     = Verb::getID(Activity::POST);
110
111                         if (in_array($notification['type'], [Post\UserNotification::NOTIF_THREAD_COMMENT, Post\UserNotification::NOTIF_COMMENT_PARTICIPATION, Post\UserNotification::NOTIF_ACTIVITY_PARTICIPATION, Post\UserNotification::NOTIF_EXPLICIT_TAGGED])) {
112                                 $item = Post::selectFirst([], ['uri-id' => $notification['parent-uri-id'], 'uid' => [0, $notification['uid']]], ['order' => ['uid' => true]]);
113                                 if (empty($item)) {
114                                         Logger::info('Parent post not found', ['uri-id' => $notification['parent-uri-id']]);
115                                         return $message;
116                                 }
117                         } else {
118                                 $item = Post::selectFirst([], ['uri-id' => $notification['target-uri-id'], 'uid' => [0, $notification['uid']]], ['order' => ['uid' => true]]);
119                                 if (empty($item)) {
120                                         Logger::info('Post not found', ['uri-id' => $notification['target-uri-id']]);
121                                         return $message;
122                                 }
123
124                                 if ($notification['vid'] == $post) {
125                                         $item = Post::selectFirst([], ['uri-id' => $item['thr-parent-id'], 'uid' => [0, $notification['uid']]], ['order' => ['uid' => true]]);
126                                         if (empty($item)) {
127                                                 Logger::info('Thread parent post not found', ['uri-id' => $item['thr-parent-id']]);
128                                                 return $message;
129                                         }
130                                 }
131                         }
132
133                         if ($item['owner-id'] != $item['author-id']) {
134                                 $cid = $item['owner-id'];
135                         }
136                         if (!empty($item['causer-id']) && ($item['causer-id'] != $item['author-id'])) {
137                                 $cid = $item['causer-id'];
138                         }
139
140                         if (($notification['type'] == Post\UserNotification::NOTIF_SHARED) && !empty($cid)) {
141                                 $causer = Contact::getById($cid, ['id', 'name', 'url']);
142                                 if (empty($contact)) {
143                                         Logger::info('Causer not found', ['causer' => $cid]);
144                                         return $message;
145                                 }
146                         } elseif (in_array($notification['type'], [Post\UserNotification::NOTIF_COMMENT_PARTICIPATION, Post\UserNotification::NOTIF_ACTIVITY_PARTICIPATION])) {
147                                 $contact = Contact::getById($item['author-id'], ['id', 'name', 'url']);
148                                 if (empty($contact)) {
149                                         Logger::info('Author not found', ['author' => $item['author-id']]);
150                                         return $message;
151                                 }
152                         }
153
154                         $link = DI::baseUrl() . '/display/' . urlencode($item['guid']);
155
156                         $content = Plaintext::getPost($item, 70);
157                         if (!empty($content['text'])) {
158                                 $title = '"' . trim(str_replace("\n", " ", $content['text'])) . '"';
159                         } else {
160                                 $title = '';
161                         }
162
163                         switch ($notification['vid']) {
164                                 case $like:
165                                         switch ($notification['type']) {
166                                                 case Post\UserNotification::NOTIF_DIRECT_COMMENT:
167                                                         $msg = $l10n->t('%1$s liked your comment %2$s');
168                                                         break;
169                                                 case Post\UserNotification::NOTIF_DIRECT_THREAD_COMMENT:
170                                                         $msg = $l10n->t('%1$s liked your post %2$s');
171                                                         break;
172                                                 }
173                                         break;
174                                 case $dislike:
175                                         switch ($notification['type']) {
176                                                 case Post\UserNotification::NOTIF_DIRECT_COMMENT:
177                                                         $msg = $l10n->t('%1$s disliked your comment %2$s');
178                                                         break;
179                                                 case Post\UserNotification::NOTIF_DIRECT_THREAD_COMMENT:
180                                                         $msg = $l10n->t('%1$s disliked your post %2$s');
181                                                         break;
182                                         }
183                                         break;
184                                 case $announce:
185                                         switch ($notification['type']) {
186                                                 case Post\UserNotification::NOTIF_DIRECT_COMMENT:
187                                                         $msg = $l10n->t('%1$s shared your comment %2$s');
188                                                         break;
189                                                 case Post\UserNotification::NOTIF_DIRECT_THREAD_COMMENT:
190                                                         $msg = $l10n->t('%1$s shared your post %2$s');
191                                                         break;
192                                                 }
193                                         break;
194                                 case $post:
195                                         switch ($notification['type']) {
196                                                 case Post\UserNotification::NOTIF_EXPLICIT_TAGGED:
197                                                         $msg = $l10n->t('%1$s tagged you on %2$s');
198                                                         break;
199
200                                                 case Post\UserNotification::NOTIF_IMPLICIT_TAGGED:
201                                                         $msg = $l10n->t('%1$s replied to you on %2$s');
202                                                         break;
203
204                                                 case Post\UserNotification::NOTIF_THREAD_COMMENT:
205                                                         $msg = $l10n->t('%1$s commented in your thread %2$s');
206                                                         break;
207
208                                                 case Post\UserNotification::NOTIF_DIRECT_COMMENT:
209                                                         $msg = $l10n->t('%1$s commented on your comment %2$s');
210                                                         break;
211
212                                                 case Post\UserNotification::NOTIF_COMMENT_PARTICIPATION:
213                                                 case Post\UserNotification::NOTIF_ACTIVITY_PARTICIPATION:
214                                                         if (($causer['id'] == $contact['id']) && ($title != '')) {
215                                                                 $msg = $l10n->t('%1$s commented in their thread %2$s');
216                                                         } elseif ($causer['id'] == $contact['id']) {
217                                                                 $msg = $l10n->t('%1$s commented in their thread');
218                                                         } elseif ($title != '') {
219                                                                 $msg = $l10n->t('%1$s commented in the thread %2$s from %3$s');
220                                                         } else {
221                                                                 $msg = $l10n->t('%1$s commented in the thread from %3$s');
222                                                         }
223                                                         break;
224
225                                                 case Post\UserNotification::NOTIF_DIRECT_THREAD_COMMENT:
226                                                         $msg = $l10n->t('%1$s commented on your thread %2$s');
227                                                         break;
228
229                                                 case Post\UserNotification::NOTIF_SHARED:
230                                                         if (($causer['id'] != $contact['id']) && ($title != '')) {
231                                                                 $msg = $l10n->t('%1$s shared the post %2$s from %3$s');
232                                                         } elseif ($causer['id'] != $contact['id']) {
233                                                                 $msg = $l10n->t('%1$s shared a post from %3$s');
234                                                         } elseif ($title != '') {
235                                                                 $msg = $l10n->t('%1$s shared the post %2$s');
236                                                         } else {
237                                                                 $msg = $l10n->t('%1$s shared a post');
238                                                         }
239                                                         break;
240                                         }
241                                         break;
242                         }
243                 }
244
245                 if (!empty($msg)) {
246                         // Name of the notification's causer
247                         $message['causer'] = $causer['name'];
248                         // Format for the "ping" mechanism
249                         $message['notification'] = sprintf($msg, '{0}', $title, $contact['name']);
250                         // Plain text for the web push api
251                         $message['plain']        = sprintf($msg, $causer['name'], $title, $contact['name']);
252                         // Rich text for other purposes
253                         $message['rich']         = sprintf($msg,
254                                 '[url=' . $causer['url'] . ']' . $causer['name'] . '[/url]',
255                                 '[url=' . $link . ']' . $title . '[/url]',
256                                 '[url=' . $contact['url'] . ']' . $contact['name'] . '[/url]');
257                 }
258
259                 return $message;
260         }
261 }