2017-11-08 00:37:53 +00:00
|
|
|
<?php
|
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
|
|
|
|
2019-10-26 02:03:27 +00:00
|
|
|
use Exception;
|
2020-01-25 01:01:49 +00:00
|
|
|
use Friendica\BaseModel;
|
2018-02-15 02:33:55 +00:00
|
|
|
use Friendica\Content\Text\BBCode;
|
2018-03-08 19:58:35 +00:00
|
|
|
use Friendica\Content\Text\HTML;
|
2019-10-26 02:03:27 +00:00
|
|
|
use Friendica\Database\Database;
|
2020-01-25 01:01:49 +00:00
|
|
|
use Friendica\Network\HTTPException\InternalServerErrorException;
|
2018-01-27 02:38:34 +00:00
|
|
|
use Friendica\Util\DateTimeFormat;
|
2018-02-03 17:25:58 +00:00
|
|
|
use Friendica\Util\Temporal;
|
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
|
2020-01-25 14:31:25 +00:00
|
|
|
* - Including additional, calculated properties
|
|
|
|
*
|
|
|
|
* Is used either for frontend interactions or for API-based interaction
|
|
|
|
* @see https://github.com/friendica/friendica/blob/develop/doc/API-Entities.md#notification
|
2020-01-25 01:01:49 +00:00
|
|
|
*
|
|
|
|
* @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)
|
|
|
|
* @property string otype Subject type (`item`, `intro` or `mail`)
|
|
|
|
*
|
|
|
|
* @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.
|
|
|
|
*
|
|
|
|
* @property-read integer timestamp Unix timestamp
|
|
|
|
* @property-read string dateRel Time since the note was posted, eg "1 hour ago"
|
|
|
|
* @property-read string $msg_html
|
|
|
|
* @property-read string $msg_plain
|
2017-11-08 00:37:53 +00:00
|
|
|
*/
|
2020-01-25 01:01:49 +00:00
|
|
|
class Notification extends BaseModel
|
2017-11-08 22:02:50 +00:00
|
|
|
{
|
2020-01-26 13:30:32 +00:00
|
|
|
const OTYPE_ITEM = 'item';
|
|
|
|
const OTYPE_INTRO = 'intro';
|
|
|
|
const OTYPE_MAIL = 'mail';
|
|
|
|
const OTYPE_PERSON = 'person';
|
|
|
|
|
2020-01-25 01:01:49 +00:00
|
|
|
/** @var \Friendica\Repository\Notification */
|
|
|
|
private $repo;
|
2019-10-26 02:33:59 +00:00
|
|
|
|
2020-01-25 01:01:49 +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();
|
|
|
|
$this->setTimestamp();
|
|
|
|
$this->setMsg();
|
2017-11-08 00:37:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-01-25 01:01:49 +00:00
|
|
|
* Set the notification as seen
|
2017-11-08 00:37:53 +00:00
|
|
|
*
|
2020-01-25 01:01:49 +00:00
|
|
|
* @param bool $seen true, if seen
|
2017-11-08 00:37:53 +00:00
|
|
|
*
|
2020-01-25 01:01:49 +00:00
|
|
|
* @return bool True, if the seen state could be saved
|
2017-11-08 00:37:53 +00:00
|
|
|
*/
|
2020-01-25 01:01:49 +00:00
|
|
|
public function setSeen(bool $seen = true)
|
2017-11-08 22:02:50 +00:00
|
|
|
{
|
2020-01-25 01:01:49 +00:00
|
|
|
$this->seen = $seen;
|
|
|
|
try {
|
|
|
|
return $this->repo->update($this);
|
|
|
|
} catch (Exception $e) {
|
|
|
|
$this->logger->warning('Update failed.', ['$this' => $this, 'exception' => $e]);
|
|
|
|
return false;
|
2017-11-08 22:02:50 +00:00
|
|
|
}
|
2017-11-08 00:37:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-01-25 01:01:49 +00:00
|
|
|
* Set some extra properties to the notification from db:
|
|
|
|
* - timestamp as int in default TZ
|
|
|
|
* - date_rel : relative date string
|
2017-11-08 00:37:53 +00:00
|
|
|
*/
|
2020-01-25 01:01:49 +00:00
|
|
|
private function setTimestamp()
|
2017-11-08 22:02:50 +00:00
|
|
|
{
|
2020-01-25 01:01:49 +00:00
|
|
|
try {
|
|
|
|
$this->timestamp = strtotime(DateTimeFormat::local($this->date));
|
|
|
|
} catch (Exception $e) {
|
2017-11-08 00:37:53 +00:00
|
|
|
}
|
2020-01-25 01:01:49 +00:00
|
|
|
$this->dateRel = Temporal::getRelativeDate($this->date);
|
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
|
|
|
* @throws InternalServerErrorException
|
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-25 01:01:49 +00:00
|
|
|
$this->name_cache = strip_tags(BBCode::convert($this->source_name ?? ''));
|
2017-11-08 00:37:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-01-25 01:01:49 +00:00
|
|
|
* Set some extra properties to the notification from db:
|
|
|
|
* - msg_html: message as html string
|
|
|
|
* - msg_plain: message as plain text string
|
|
|
|
* - msg_cache: The pre-formatted message (caching)
|
2017-11-08 00:37:53 +00:00
|
|
|
*/
|
2020-01-25 01:01:49 +00:00
|
|
|
private function setMsg()
|
2017-11-08 22:02:50 +00:00
|
|
|
{
|
2020-01-25 01:01:49 +00:00
|
|
|
try {
|
|
|
|
$this->msg_html = BBCode::convert($this->msg, false);
|
|
|
|
$this->msg_plain = explode("\n", trim(HTML::toPlaintext($this->msg_html, 0)))[0];
|
|
|
|
$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 == 'date') {
|
|
|
|
$this->setTimestamp();
|
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 == 'msg') {
|
|
|
|
$this->setMsg();
|
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
|
|
|
}
|
|
|
|
}
|