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 * @property string hash
38 * @property integer type
39 * @property string name Full name of the contact subject
40 * @property string url Profile page URL of the contact subject
41 * @property string photo Profile photo URL of the contact subject
42 * @property string date YYYY-MM-DD hh:mm:ss local server time
43 * @property string msg
44 * @property integer uid Owner User Id
45 * @property string link Notification URL
46 * @property integer iid Item Id
47 * @property integer parent Parent Item Id
48 * @property boolean seen Whether the notification was read or not.
49 * @property string verb Verb URL (@see http://activitystrea.ms)
50 * @property string otype Subject type ('item', 'intro' or 'mail')
52 * @property-read string name_cache Full name of the contact subject
53 * @property-read string msg_cache Plaintext version of the notification text with a placeholder (`{0}`) for the subject contact's name.
55 class Notification extends BaseModel
57 /** @var \Friendica\Repository\Notification */
60 public function __construct(Database $dba, LoggerInterface $logger, \Friendica\Repository\Notification $repo, array $data = [])
62 parent::__construct($dba, $logger, $data);
66 $this->setNameCache();
71 * Sets the pre-formatted name (caching)
73 private function setNameCache()
76 $this->name_cache = strip_tags(BBCode::convert($this->source_name));
77 } catch (InternalServerErrorException $e) {
82 * Sets the pre-formatted msg (caching)
84 private function setMsgCache()
87 $this->msg_cache = self::formatMessage($this->name_cache, strip_tags(BBCode::convert($this->msg)));
88 } catch (InternalServerErrorException $e) {
92 public function __set($name, $value)
94 parent::__set($name, $value);
100 if ($name == 'source_name') {
101 $this->setNameCache();
106 * Formats a notification message with the notification author
108 * Replace the name with {0} but ensure to make that only once. The {0} is used
109 * later and prints the name in bold.
111 * @param string $name
112 * @param string $message
114 * @return string Formatted message
116 public static function formatMessage($name, $message)
119 $pos = strpos($message, $name);
124 if ($pos !== false) {
125 $message = substr_replace($message, '{0}', $pos, strlen($name));
132 * Fetch the notification type for the given notification
134 * @param array $notification
137 public static function getType(array $notification): string
139 if (($notification['vid'] == Verb::getID(Activity::FOLLOW)) && ($notification['type'] == Post\UserNotification::NOTIF_NONE)) {
140 $contact = Contact::getById($notification['actor-id'], ['pending']);
141 $type = $contact['pending'] ? 'follow_request' : 'follow';
142 } elseif (($notification['vid'] == Verb::getID(Activity::ANNOUNCE)) &&
143 in_array($notification['type'], [Post\UserNotification::NOTIF_DIRECT_COMMENT, Post\UserNotification::NOTIF_DIRECT_THREAD_COMMENT])) {
145 } elseif (in_array($notification['vid'], [Verb::getID(Activity::LIKE), Verb::getID(Activity::DISLIKE)]) &&
146 in_array($notification['type'], [Post\UserNotification::NOTIF_DIRECT_COMMENT, Post\UserNotification::NOTIF_DIRECT_THREAD_COMMENT])) {
148 } elseif ($notification['type'] == Post\UserNotification::NOTIF_SHARED) {
150 } elseif (in_array($notification['type'], [Post\UserNotification::NOTIF_EXPLICIT_TAGGED,
151 Post\UserNotification::NOTIF_IMPLICIT_TAGGED, Post\UserNotification::NOTIF_DIRECT_COMMENT,
152 Post\UserNotification::NOTIF_DIRECT_THREAD_COMMENT, Post\UserNotification::NOTIF_THREAD_COMMENT])) {
162 * Create a notification message for the given notification
164 * @param array $notification
165 * @return array with the elements "causer", "notification", "plain" and "rich"
167 public static function getMessage(array $notification)
171 if ($notification['type'] == Post\UserNotification::NOTIF_NONE) {
175 if (empty($notification['target-uri-id'])) {
179 $user = User::getById($notification['uid']);
181 Logger::info('User not found', ['application' => $notification['uid']]);
185 $contact = Contact::getById($notification['actor-id']);
186 if (empty($contact)) {
187 Logger::info('Contact not found', ['contact' => $notification['actor-id']]);
191 $like = Verb::getID(Activity::LIKE);
192 $dislike = Verb::getID(Activity::DISLIKE);
193 $announce = Verb::getID(Activity::ANNOUNCE);
194 $post = Verb::getID(Activity::POST);
196 if (in_array($notification['type'], [Post\UserNotification::NOTIF_THREAD_COMMENT, Post\UserNotification::NOTIF_COMMENT_PARTICIPATION])) {
197 $item = Post::selectFirst([], ['uri-id' => $notification['parent-uri-id'], 'uid' => [0, $notification['uid']]]);
199 Logger::info('Parent post not found', ['uri-id' => $notification['parent-uri-id']]);
203 $item = Post::selectFirst([], ['uri-id' => $notification['target-uri-id'], 'uid' => [0, $notification['uid']]]);
205 Logger::info('Post not found', ['uri-id' => $notification['target-uri-id']]);
209 if ($notification['vid'] == $post) {
210 $item = Post::selectFirst([], ['uri-id' => $item['thr-parent-id'], 'uid' => [0, $notification['uid']]]);
212 Logger::info('Thread parent post not found', ['uri-id' => $item['thr-parent-id']]);
218 $link = DI::baseUrl() . '/display/' . urlencode($item['guid']);
220 $content = Plaintext::getPost($item, 70);
221 if (!empty($content['text'])) {
222 $title = '"' . trim(str_replace("\n", " ", $content['text'])) . '"';
227 $l10n = DI::l10n()->withLang($user['language']);
229 switch ($notification['vid']) {
231 switch ($notification['type']) {
232 case Post\UserNotification::NOTIF_DIRECT_COMMENT:
233 $msg = $l10n->t('%1$s liked your comment %2$s');
235 case Post\UserNotification::NOTIF_DIRECT_THREAD_COMMENT:
236 $msg = $l10n->t('%1$s liked your post %2$s');
241 switch ($notification['type']) {
242 case Post\UserNotification::NOTIF_DIRECT_COMMENT:
243 $msg = $l10n->t('%1$s disliked your comment %2$s');
245 case Post\UserNotification::NOTIF_DIRECT_THREAD_COMMENT:
246 $msg = $l10n->t('%1$s disliked your post %2$s');
251 switch ($notification['type']) {
252 case Post\UserNotification::NOTIF_DIRECT_COMMENT:
253 $msg = $l10n->t('%1$s shared your comment %2$s');
255 case Post\UserNotification::NOTIF_DIRECT_THREAD_COMMENT:
256 $msg = $l10n->t('%1$s shared your post %2$s');
261 switch ($notification['type']) {
262 case Post\UserNotification::NOTIF_EXPLICIT_TAGGED:
263 $msg = $l10n->t('%1$s tagged you on %2$s');
266 case Post\UserNotification::NOTIF_IMPLICIT_TAGGED:
267 $msg = $l10n->t('%1$s replied to you on %2$s');
270 case Post\UserNotification::NOTIF_THREAD_COMMENT:
271 $msg = $l10n->t('%1$s commented in your thread %2$s');
274 case Post\UserNotification::NOTIF_DIRECT_COMMENT:
275 $msg = $l10n->t('%1$s commented on your comment %2$s');
278 case Post\UserNotification::NOTIF_COMMENT_PARTICIPATION:
279 $msg = $l10n->t('%1$s commented in the thread %2$s');
282 case Post\UserNotification::NOTIF_ACTIVITY_PARTICIPATION:
286 case Post\UserNotification::NOTIF_DIRECT_THREAD_COMMENT:
287 $msg = $l10n->t('%1$s commented on your thread %2$s');
290 case Post\UserNotification::NOTIF_SHARED:
292 $msg = $l10n->t('%1$s shared the post %2$s');
294 $msg = $l10n->t('%1$s shared a post');
302 // Name of the notification's causer
303 $message['causer'] = $contact['name'];
304 // Format for the "ping" mechanism
305 $message['notification'] = sprintf($msg, '{0}', $title);
306 // Plain text for the web push api
307 $message['plain'] = sprintf($msg, $contact['name'], $title);
308 // Rich text for other purposes
309 $message['rich'] = sprintf($msg,
310 '[url=' . $contact['url'] . ']' . $contact['name'] . '[/url]',
311 '[url=' . $link . ']' . $title . '[/url]');