2017-11-08 00:37:53 +00:00
|
|
|
<?php
|
2020-02-09 14:45:36 +00:00
|
|
|
/**
|
2021-03-29 06:40:20 +00:00
|
|
|
* @copyright Copyright (C) 2010-2021, the Friendica project
|
2020-02-09 14:45:36 +00:00
|
|
|
*
|
|
|
|
* @license GNU AGPL version 3 or any later version
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Affero General Public License as
|
|
|
|
* published by the Free Software Foundation, either version 3 of the
|
|
|
|
* License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU Affero General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Affero General Public License
|
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
*
|
|
|
|
*/
|
2019-10-26 02:03:27 +00:00
|
|
|
|
2019-10-26 00:01:46 +00:00
|
|
|
namespace Friendica\Model;
|
2017-11-08 00:37:53 +00:00
|
|
|
|
2020-01-25 01:01:49 +00:00
|
|
|
use Friendica\BaseModel;
|
2018-02-15 02:33:55 +00:00
|
|
|
use Friendica\Content\Text\BBCode;
|
2021-08-19 13:58:55 +00:00
|
|
|
use Friendica\Content\Text\Plaintext;
|
|
|
|
use Friendica\Core\Logger;
|
2019-10-26 02:03:27 +00:00
|
|
|
use Friendica\Database\Database;
|
2021-08-19 13:58:55 +00:00
|
|
|
use Friendica\DI;
|
2020-01-25 01:01:49 +00:00
|
|
|
use Friendica\Network\HTTPException\InternalServerErrorException;
|
2021-08-15 16:18:25 +00:00
|
|
|
use Friendica\Protocol\Activity;
|
2019-10-26 02:03:27 +00:00
|
|
|
use Psr\Log\LoggerInterface;
|
2017-11-08 00:37:53 +00:00
|
|
|
|
|
|
|
/**
|
2020-01-25 01:01:49 +00:00
|
|
|
* Model for an entry in the notify table
|
|
|
|
*
|
|
|
|
* @property string hash
|
|
|
|
* @property integer type
|
|
|
|
* @property string name Full name of the contact subject
|
|
|
|
* @property string url Profile page URL of the contact subject
|
|
|
|
* @property string photo Profile photo URL of the contact subject
|
|
|
|
* @property string date YYYY-MM-DD hh:mm:ss local server time
|
|
|
|
* @property string msg
|
|
|
|
* @property integer uid Owner User Id
|
|
|
|
* @property string link Notification URL
|
|
|
|
* @property integer iid Item Id
|
|
|
|
* @property integer parent Parent Item Id
|
|
|
|
* @property boolean seen Whether the notification was read or not.
|
|
|
|
* @property string verb Verb URL (@see http://activitystrea.ms)
|
2021-01-17 20:32:13 +00:00
|
|
|
* @property string otype Subject type ('item', 'intro' or 'mail')
|
2020-01-25 01:01:49 +00:00
|
|
|
*
|
|
|
|
* @property-read string name_cache Full name of the contact subject
|
|
|
|
* @property-read string msg_cache Plaintext version of the notification text with a placeholder (`{0}`) for the subject contact's name.
|
2017-11-08 00:37:53 +00:00
|
|
|
*/
|
2021-01-23 09:53:44 +00:00
|
|
|
class Notification extends BaseModel
|
2017-11-08 22:02:50 +00:00
|
|
|
{
|
2021-01-23 09:53:44 +00:00
|
|
|
/** @var \Friendica\Repository\Notification */
|
2020-01-25 01:01:49 +00:00
|
|
|
private $repo;
|
2019-10-26 02:33:59 +00:00
|
|
|
|
2021-01-23 09:53:44 +00:00
|
|
|
public function __construct(Database $dba, LoggerInterface $logger, \Friendica\Repository\Notification $repo, array $data = [])
|
2019-10-26 02:03:27 +00:00
|
|
|
{
|
2020-01-25 01:01:49 +00:00
|
|
|
parent::__construct($dba, $logger, $data);
|
2019-10-26 02:03:27 +00:00
|
|
|
|
2020-01-25 01:01:49 +00:00
|
|
|
$this->repo = $repo;
|
2019-10-26 02:03:27 +00:00
|
|
|
|
2020-01-25 01:01:49 +00:00
|
|
|
$this->setNameCache();
|
2020-01-28 20:28:57 +00:00
|
|
|
$this->setMsgCache();
|
2017-11-08 00:37:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-01-25 01:01:49 +00:00
|
|
|
* Sets the pre-formatted name (caching)
|
2017-11-08 00:37:53 +00:00
|
|
|
*/
|
2020-01-25 01:01:49 +00:00
|
|
|
private function setNameCache()
|
2017-11-08 22:02:50 +00:00
|
|
|
{
|
2020-01-28 20:28:57 +00:00
|
|
|
try {
|
2020-06-08 23:15:08 +00:00
|
|
|
$this->name_cache = strip_tags(BBCode::convert($this->source_name));
|
2020-01-28 20:28:57 +00:00
|
|
|
} catch (InternalServerErrorException $e) {
|
|
|
|
}
|
2017-11-08 00:37:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-01-28 20:28:57 +00:00
|
|
|
* Sets the pre-formatted msg (caching)
|
2017-11-08 00:37:53 +00:00
|
|
|
*/
|
2020-01-28 20:28:57 +00:00
|
|
|
private function setMsgCache()
|
2017-11-08 22:02:50 +00:00
|
|
|
{
|
2020-01-25 01:01:49 +00:00
|
|
|
try {
|
|
|
|
$this->msg_cache = self::formatMessage($this->name_cache, strip_tags(BBCode::convert($this->msg)));
|
|
|
|
} catch (InternalServerErrorException $e) {
|
2017-11-08 00:37:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-25 01:01:49 +00:00
|
|
|
public function __set($name, $value)
|
2017-11-08 22:02:50 +00:00
|
|
|
{
|
2020-01-25 01:01:49 +00:00
|
|
|
parent::__set($name, $value);
|
2018-06-15 05:50:28 +00:00
|
|
|
|
2020-01-25 01:01:49 +00:00
|
|
|
if ($name == 'msg') {
|
2020-01-28 20:28:57 +00:00
|
|
|
$this->setMsgCache();
|
2017-11-08 22:02:50 +00:00
|
|
|
}
|
2017-11-08 00:37:53 +00:00
|
|
|
|
2020-01-25 01:01:49 +00:00
|
|
|
if ($name == 'source_name') {
|
|
|
|
$this->setNameCache();
|
2017-11-08 22:02:50 +00:00
|
|
|
}
|
2017-11-08 00:37:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-01-25 01:01:49 +00:00
|
|
|
* Formats a notification message with the notification author
|
2017-11-08 00:37:53 +00:00
|
|
|
*
|
2020-01-25 01:01:49 +00:00
|
|
|
* Replace the name with {0} but ensure to make that only once. The {0} is used
|
|
|
|
* later and prints the name in bold.
|
2017-11-08 00:37:53 +00:00
|
|
|
*
|
2020-01-25 01:01:49 +00:00
|
|
|
* @param string $name
|
|
|
|
* @param string $message
|
2019-10-26 02:03:27 +00:00
|
|
|
*
|
2020-01-25 01:01:49 +00:00
|
|
|
* @return string Formatted message
|
2017-11-08 00:37:53 +00:00
|
|
|
*/
|
2020-01-25 01:01:49 +00:00
|
|
|
public static function formatMessage($name, $message)
|
2017-11-19 19:15:25 +00:00
|
|
|
{
|
2020-01-25 01:01:49 +00:00
|
|
|
if ($name != '') {
|
|
|
|
$pos = strpos($message, $name);
|
2019-09-09 05:29:33 +00:00
|
|
|
} else {
|
2020-01-25 01:01:49 +00:00
|
|
|
$pos = false;
|
2017-11-08 00:37:53 +00:00
|
|
|
}
|
|
|
|
|
2020-01-25 01:01:49 +00:00
|
|
|
if ($pos !== false) {
|
|
|
|
$message = substr_replace($message, '{0}', $pos, strlen($name));
|
2017-11-08 00:37:53 +00:00
|
|
|
}
|
|
|
|
|
2020-01-25 01:01:49 +00:00
|
|
|
return $message;
|
2017-11-08 00:37:53 +00:00
|
|
|
}
|
2021-08-15 16:18:25 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Fetch the notification type for the given notification
|
|
|
|
*
|
2021-08-19 13:58:55 +00:00
|
|
|
* @param array $notification
|
2021-08-15 16:18:25 +00:00
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public static function getType(array $notification): string
|
|
|
|
{
|
|
|
|
if (($notification['vid'] == Verb::getID(Activity::FOLLOW)) && ($notification['type'] == Post\UserNotification::NOTIF_NONE)) {
|
|
|
|
$contact = Contact::getById($notification['actor-id'], ['pending']);
|
2021-08-19 13:58:55 +00:00
|
|
|
$type = $contact['pending'] ? 'follow_request' : 'follow';
|
2021-08-15 16:18:25 +00:00
|
|
|
} elseif (($notification['vid'] == Verb::getID(Activity::ANNOUNCE)) &&
|
|
|
|
in_array($notification['type'], [Post\UserNotification::NOTIF_DIRECT_COMMENT, Post\UserNotification::NOTIF_DIRECT_THREAD_COMMENT])) {
|
|
|
|
$type = 'reblog';
|
|
|
|
} elseif (in_array($notification['vid'], [Verb::getID(Activity::LIKE), Verb::getID(Activity::DISLIKE)]) &&
|
|
|
|
in_array($notification['type'], [Post\UserNotification::NOTIF_DIRECT_COMMENT, Post\UserNotification::NOTIF_DIRECT_THREAD_COMMENT])) {
|
|
|
|
$type = 'favourite';
|
|
|
|
} elseif ($notification['type'] == Post\UserNotification::NOTIF_SHARED) {
|
|
|
|
$type = 'status';
|
|
|
|
} elseif (in_array($notification['type'], [Post\UserNotification::NOTIF_EXPLICIT_TAGGED,
|
|
|
|
Post\UserNotification::NOTIF_IMPLICIT_TAGGED, Post\UserNotification::NOTIF_DIRECT_COMMENT,
|
|
|
|
Post\UserNotification::NOTIF_DIRECT_THREAD_COMMENT, Post\UserNotification::NOTIF_THREAD_COMMENT])) {
|
|
|
|
$type = 'mention';
|
|
|
|
} else {
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
|
|
|
|
return $type;
|
|
|
|
}
|
2021-08-19 13:58:55 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a notification message for the given notification
|
|
|
|
*
|
|
|
|
* @param array $notification
|
2021-08-19 14:57:35 +00:00
|
|
|
* @return array with the elements "causer", "notification", "plain" and "rich"
|
2021-08-19 13:58:55 +00:00
|
|
|
*/
|
|
|
|
public static function getMessage(array $notification)
|
|
|
|
{
|
|
|
|
$message = [];
|
|
|
|
|
|
|
|
if ($notification['type'] == Post\UserNotification::NOTIF_NONE) {
|
|
|
|
return $message;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (empty($notification['target-uri-id'])) {
|
|
|
|
return $message;
|
|
|
|
}
|
|
|
|
|
|
|
|
$user = User::getById($notification['uid']);
|
|
|
|
if (empty($user)) {
|
|
|
|
Logger::info('User not found', ['application' => $notification['uid']]);
|
|
|
|
return $message;
|
|
|
|
}
|
|
|
|
|
2021-08-21 20:35:04 +00:00
|
|
|
$causer = $contact = Contact::getById($notification['actor-id'], ['id', 'name', 'url']);
|
2021-08-19 13:58:55 +00:00
|
|
|
if (empty($contact)) {
|
|
|
|
Logger::info('Contact not found', ['contact' => $notification['actor-id']]);
|
|
|
|
return $message;
|
|
|
|
}
|
|
|
|
|
|
|
|
$like = Verb::getID(Activity::LIKE);
|
|
|
|
$dislike = Verb::getID(Activity::DISLIKE);
|
|
|
|
$announce = Verb::getID(Activity::ANNOUNCE);
|
|
|
|
$post = Verb::getID(Activity::POST);
|
|
|
|
|
2021-08-21 20:35:04 +00:00
|
|
|
if (in_array($notification['type'], [Post\UserNotification::NOTIF_THREAD_COMMENT, Post\UserNotification::NOTIF_COMMENT_PARTICIPATION, Post\UserNotification::NOTIF_EXPLICIT_TAGGED])) {
|
2021-08-19 13:58:55 +00:00
|
|
|
$item = Post::selectFirst([], ['uri-id' => $notification['parent-uri-id'], 'uid' => [0, $notification['uid']]]);
|
|
|
|
if (empty($item)) {
|
|
|
|
Logger::info('Parent post not found', ['uri-id' => $notification['parent-uri-id']]);
|
|
|
|
return $message;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$item = Post::selectFirst([], ['uri-id' => $notification['target-uri-id'], 'uid' => [0, $notification['uid']]]);
|
|
|
|
if (empty($item)) {
|
|
|
|
Logger::info('Post not found', ['uri-id' => $notification['target-uri-id']]);
|
|
|
|
return $message;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($notification['vid'] == $post) {
|
|
|
|
$item = Post::selectFirst([], ['uri-id' => $item['thr-parent-id'], 'uid' => [0, $notification['uid']]]);
|
|
|
|
if (empty($item)) {
|
|
|
|
Logger::info('Thread parent post not found', ['uri-id' => $item['thr-parent-id']]);
|
|
|
|
return $message;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-21 20:35:04 +00:00
|
|
|
if (($notification['type'] == Post\UserNotification::NOTIF_SHARED) && !empty($item['causer-id'])) {
|
|
|
|
$causer = Contact::getById($item['causer-id'], ['id', 'name', 'url']);
|
|
|
|
if (empty($contact)) {
|
|
|
|
Logger::info('Causer not found', ['causer' => $item['causer-id']]);
|
|
|
|
return $message;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-19 13:58:55 +00:00
|
|
|
$link = DI::baseUrl() . '/display/' . urlencode($item['guid']);
|
|
|
|
|
|
|
|
$content = Plaintext::getPost($item, 70);
|
|
|
|
if (!empty($content['text'])) {
|
|
|
|
$title = '"' . trim(str_replace("\n", " ", $content['text'])) . '"';
|
|
|
|
} else {
|
|
|
|
$title = '';
|
|
|
|
}
|
|
|
|
|
|
|
|
$l10n = DI::l10n()->withLang($user['language']);
|
|
|
|
|
|
|
|
switch ($notification['vid']) {
|
|
|
|
case $like:
|
|
|
|
switch ($notification['type']) {
|
|
|
|
case Post\UserNotification::NOTIF_DIRECT_COMMENT:
|
|
|
|
$msg = $l10n->t('%1$s liked your comment %2$s');
|
|
|
|
break;
|
|
|
|
case Post\UserNotification::NOTIF_DIRECT_THREAD_COMMENT:
|
|
|
|
$msg = $l10n->t('%1$s liked your post %2$s');
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case $dislike:
|
|
|
|
switch ($notification['type']) {
|
|
|
|
case Post\UserNotification::NOTIF_DIRECT_COMMENT:
|
|
|
|
$msg = $l10n->t('%1$s disliked your comment %2$s');
|
|
|
|
break;
|
|
|
|
case Post\UserNotification::NOTIF_DIRECT_THREAD_COMMENT:
|
|
|
|
$msg = $l10n->t('%1$s disliked your post %2$s');
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case $announce:
|
|
|
|
switch ($notification['type']) {
|
|
|
|
case Post\UserNotification::NOTIF_DIRECT_COMMENT:
|
|
|
|
$msg = $l10n->t('%1$s shared your comment %2$s');
|
|
|
|
break;
|
|
|
|
case Post\UserNotification::NOTIF_DIRECT_THREAD_COMMENT:
|
|
|
|
$msg = $l10n->t('%1$s shared your post %2$s');
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case $post:
|
|
|
|
switch ($notification['type']) {
|
|
|
|
case Post\UserNotification::NOTIF_EXPLICIT_TAGGED:
|
|
|
|
$msg = $l10n->t('%1$s tagged you on %2$s');
|
|
|
|
break;
|
|
|
|
|
|
|
|
case Post\UserNotification::NOTIF_IMPLICIT_TAGGED:
|
|
|
|
$msg = $l10n->t('%1$s replied to you on %2$s');
|
|
|
|
break;
|
|
|
|
|
|
|
|
case Post\UserNotification::NOTIF_THREAD_COMMENT:
|
|
|
|
$msg = $l10n->t('%1$s commented in your thread %2$s');
|
|
|
|
break;
|
|
|
|
|
|
|
|
case Post\UserNotification::NOTIF_DIRECT_COMMENT:
|
|
|
|
$msg = $l10n->t('%1$s commented on your comment %2$s');
|
|
|
|
break;
|
|
|
|
|
|
|
|
case Post\UserNotification::NOTIF_COMMENT_PARTICIPATION:
|
|
|
|
$msg = $l10n->t('%1$s commented in the thread %2$s');
|
|
|
|
break;
|
|
|
|
|
|
|
|
case Post\UserNotification::NOTIF_ACTIVITY_PARTICIPATION:
|
|
|
|
// Unhandled
|
|
|
|
break;
|
|
|
|
|
|
|
|
case Post\UserNotification::NOTIF_DIRECT_THREAD_COMMENT:
|
|
|
|
$msg = $l10n->t('%1$s commented on your thread %2$s');
|
|
|
|
break;
|
|
|
|
|
|
|
|
case Post\UserNotification::NOTIF_SHARED:
|
2021-08-21 20:35:04 +00:00
|
|
|
if (($causer['id'] != $contact['id']) && ($title != '')) {
|
|
|
|
$msg = $l10n->t('%1$s shared the post %2$s from %3$s');
|
|
|
|
} elseif ($causer['id'] != $contact['id']) {
|
|
|
|
$msg = $l10n->t('%1$s shared a post from %3$s');
|
|
|
|
} elseif ($title != '') {
|
2021-08-19 13:58:55 +00:00
|
|
|
$msg = $l10n->t('%1$s shared the post %2$s');
|
|
|
|
} else {
|
|
|
|
$msg = $l10n->t('%1$s shared a post');
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!empty($msg)) {
|
2021-08-19 14:57:35 +00:00
|
|
|
// Name of the notification's causer
|
2021-08-21 20:35:04 +00:00
|
|
|
$message['causer'] = $causer['name'];
|
2021-08-19 14:57:35 +00:00
|
|
|
// Format for the "ping" mechanism
|
2021-08-21 20:35:04 +00:00
|
|
|
$message['notification'] = sprintf($msg, '{0}', $title, $contact['name']);
|
2021-08-19 14:57:35 +00:00
|
|
|
// Plain text for the web push api
|
2021-08-21 20:35:04 +00:00
|
|
|
$message['plain'] = sprintf($msg, $causer['name'], $title, $contact['name']);
|
2021-08-19 14:57:35 +00:00
|
|
|
// Rich text for other purposes
|
|
|
|
$message['rich'] = sprintf($msg,
|
2021-08-21 20:35:04 +00:00
|
|
|
'[url=' . $causer['url'] . ']' . $causer['name'] . '[/url]',
|
|
|
|
'[url=' . $link . ']' . $title . '[/url]',
|
|
|
|
'[url=' . $contact['url'] . ']' . $contact['name'] . '[/url]');
|
2021-08-19 13:58:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return $message;
|
|
|
|
}
|
2017-11-08 00:37:53 +00:00
|
|
|
}
|