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::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])) {
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])) {
56 } elseif ($notification['type'] == Post\UserNotification::TYPE_SHARED) {
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
74 * Create a notification message for the given notification
76 * @param array $notification
77 * @return array with the elements "causer", "notification", "plain" and "rich"
79 public static function getMessage(array $notification)
83 $user = User::getById($notification['uid']);
85 Logger::info('User not found', ['application' => $notification['uid']]);
89 $l10n = DI::l10n()->withLang($user['language']);
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']]);
97 if ($notification['type'] == Post\UserNotification::TYPE_NONE) {
98 if ($contact['pending']) {
99 $msg = $l10n->t('%1$s wants to follow you');
101 $msg = $l10n->t('%1$s had started following you');
103 $title = $contact['name'];
104 $link = DI::baseUrl() . '/contact/' . $contact['id'];
106 if (empty($notification['target-uri-id'])) {
110 $like = Verb::getID(Activity::LIKE);
111 $dislike = Verb::getID(Activity::DISLIKE);
112 $announce = Verb::getID(Activity::ANNOUNCE);
113 $post = Verb::getID(Activity::POST);
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]]);
118 Logger::info('Parent post not found', ['uri-id' => $notification['parent-uri-id']]);
122 $item = Post::selectFirst([], ['uri-id' => $notification['target-uri-id'], 'uid' => [0, $notification['uid']]], ['order' => ['uid' => true]]);
124 Logger::info('Post not found', ['uri-id' => $notification['target-uri-id']]);
128 if ($notification['vid'] == $post) {
129 $item = Post::selectFirst([], ['uri-id' => $item['thr-parent-id'], 'uid' => [0, $notification['uid']]], ['order' => ['uid' => true]]);
131 Logger::info('Thread parent post not found', ['uri-id' => $item['thr-parent-id']]);
137 if ($item['owner-id'] != $item['author-id']) {
138 $cid = $item['owner-id'];
140 if (!empty($item['causer-id']) && ($item['causer-id'] != $item['author-id'])) {
141 $cid = $item['causer-id'];
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]);
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']]);
158 $link = DI::baseUrl() . '/display/' . urlencode($item['guid']);
160 $content = Plaintext::getPost($item, 70);
161 if (!empty($content['text'])) {
162 $title = '"' . trim(str_replace("\n", " ", $content['text'])) . '"';
167 switch ($notification['vid']) {
169 switch ($notification['type']) {
170 case Post\UserNotification::TYPE_DIRECT_COMMENT:
171 $msg = $l10n->t('%1$s liked your comment %2$s');
173 case Post\UserNotification::TYPE_DIRECT_THREAD_COMMENT:
174 $msg = $l10n->t('%1$s liked your post %2$s');
179 switch ($notification['type']) {
180 case Post\UserNotification::TYPE_DIRECT_COMMENT:
181 $msg = $l10n->t('%1$s disliked your comment %2$s');
183 case Post\UserNotification::TYPE_DIRECT_THREAD_COMMENT:
184 $msg = $l10n->t('%1$s disliked your post %2$s');
189 switch ($notification['type']) {
190 case Post\UserNotification::TYPE_DIRECT_COMMENT:
191 $msg = $l10n->t('%1$s shared your comment %2$s');
193 case Post\UserNotification::TYPE_DIRECT_THREAD_COMMENT:
194 $msg = $l10n->t('%1$s shared your post %2$s');
199 switch ($notification['type']) {
200 case Post\UserNotification::TYPE_EXPLICIT_TAGGED:
201 $msg = $l10n->t('%1$s tagged you on %2$s');
204 case Post\UserNotification::TYPE_IMPLICIT_TAGGED:
205 $msg = $l10n->t('%1$s replied to you on %2$s');
208 case Post\UserNotification::TYPE_THREAD_COMMENT:
209 $msg = $l10n->t('%1$s commented in your thread %2$s');
212 case Post\UserNotification::TYPE_DIRECT_COMMENT:
213 $msg = $l10n->t('%1$s commented on your comment %2$s');
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');
225 $msg = $l10n->t('%1$s commented in the thread from %3$s');
229 case Post\UserNotification::TYPE_DIRECT_THREAD_COMMENT:
230 $msg = $l10n->t('%1$s commented on your thread %2$s');
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');
241 $msg = $l10n->t('%1$s shared a post');
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]');