3 * @copyright Copyright (C) 2010-2021, the Friendica project
5 * @license GNU AGPL version 3 or any later version
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.
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.
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/>.
22 namespace Friendica\Model;
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;
30 use Friendica\Network\HTTPException\InternalServerErrorException;
31 use Friendica\Protocol\Activity;
32 use Psr\Log\LoggerInterface;
35 * Model for an entry in the notify table
37 class Notification extends BaseModel
40 * Fetch the notification type for the given notification
42 * @param array $notification
45 public static function getType(array $notification): string
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])) {
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])) {
56 } elseif ($notification['type'] == Post\UserNotification::NOTIF_SHARED) {
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])) {
70 * Create a notification message for the given notification
72 * @param array $notification
73 * @return array with the elements "causer", "notification", "plain" and "rich"
75 public static function getMessage(array $notification)
79 $user = User::getById($notification['uid']);
81 Logger::info('User not found', ['application' => $notification['uid']]);
85 $l10n = DI::l10n()->withLang($user['language']);
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']]);
93 if ($notification['type'] == Post\UserNotification::NOTIF_NONE) {
94 if ($contact['pending']) {
95 $msg = $l10n->t('%1$s wants to follow you');
97 $msg = $l10n->t('%1$s had started following you');
99 $title = $contact['name'];
100 $link = DI::baseUrl() . '/contact/' . $contact['id'];
102 if (empty($notification['target-uri-id'])) {
106 $like = Verb::getID(Activity::LIKE);
107 $dislike = Verb::getID(Activity::DISLIKE);
108 $announce = Verb::getID(Activity::ANNOUNCE);
109 $post = Verb::getID(Activity::POST);
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]]);
114 Logger::info('Parent post not found', ['uri-id' => $notification['parent-uri-id']]);
118 $item = Post::selectFirst([], ['uri-id' => $notification['target-uri-id'], 'uid' => [0, $notification['uid']]], ['order' => ['uid' => true]]);
120 Logger::info('Post not found', ['uri-id' => $notification['target-uri-id']]);
124 if ($notification['vid'] == $post) {
125 $item = Post::selectFirst([], ['uri-id' => $item['thr-parent-id'], 'uid' => [0, $notification['uid']]], ['order' => ['uid' => true]]);
127 Logger::info('Thread parent post not found', ['uri-id' => $item['thr-parent-id']]);
133 if ($item['owner-id'] != $item['author-id']) {
134 $cid = $item['owner-id'];
136 if (!empty($item['causer-id']) && ($item['causer-id'] != $item['author-id'])) {
137 $cid = $item['causer-id'];
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]);
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']]);
154 $link = DI::baseUrl() . '/display/' . urlencode($item['guid']);
156 $content = Plaintext::getPost($item, 70);
157 if (!empty($content['text'])) {
158 $title = '"' . trim(str_replace("\n", " ", $content['text'])) . '"';
163 switch ($notification['vid']) {
165 switch ($notification['type']) {
166 case Post\UserNotification::NOTIF_DIRECT_COMMENT:
167 $msg = $l10n->t('%1$s liked your comment %2$s');
169 case Post\UserNotification::NOTIF_DIRECT_THREAD_COMMENT:
170 $msg = $l10n->t('%1$s liked your post %2$s');
175 switch ($notification['type']) {
176 case Post\UserNotification::NOTIF_DIRECT_COMMENT:
177 $msg = $l10n->t('%1$s disliked your comment %2$s');
179 case Post\UserNotification::NOTIF_DIRECT_THREAD_COMMENT:
180 $msg = $l10n->t('%1$s disliked your post %2$s');
185 switch ($notification['type']) {
186 case Post\UserNotification::NOTIF_DIRECT_COMMENT:
187 $msg = $l10n->t('%1$s shared your comment %2$s');
189 case Post\UserNotification::NOTIF_DIRECT_THREAD_COMMENT:
190 $msg = $l10n->t('%1$s shared your post %2$s');
195 switch ($notification['type']) {
196 case Post\UserNotification::NOTIF_EXPLICIT_TAGGED:
197 $msg = $l10n->t('%1$s tagged you on %2$s');
200 case Post\UserNotification::NOTIF_IMPLICIT_TAGGED:
201 $msg = $l10n->t('%1$s replied to you on %2$s');
204 case Post\UserNotification::NOTIF_THREAD_COMMENT:
205 $msg = $l10n->t('%1$s commented in your thread %2$s');
208 case Post\UserNotification::NOTIF_DIRECT_COMMENT:
209 $msg = $l10n->t('%1$s commented on your comment %2$s');
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');
221 $msg = $l10n->t('%1$s commented in the thread from %3$s');
225 case Post\UserNotification::NOTIF_DIRECT_THREAD_COMMENT:
226 $msg = $l10n->t('%1$s commented on your thread %2$s');
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');
237 $msg = $l10n->t('%1$s shared a post');
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]');