2017-11-07 19:37:53 -05:00
|
|
|
<?php
|
2020-02-09 09:45:36 -05:00
|
|
|
/**
|
|
|
|
* @copyright Copyright (C) 2020, Friendica
|
|
|
|
*
|
|
|
|
* @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-25 22:03:27 -04:00
|
|
|
|
2019-10-25 20:01:46 -04:00
|
|
|
namespace Friendica\Model;
|
2017-11-07 19:37:53 -05:00
|
|
|
|
2020-01-24 20:01:49 -05:00
|
|
|
use Friendica\BaseModel;
|
2018-02-14 21:33:55 -05:00
|
|
|
use Friendica\Content\Text\BBCode;
|
2019-10-25 22:03:27 -04:00
|
|
|
use Friendica\Database\Database;
|
2020-01-24 20:01:49 -05:00
|
|
|
use Friendica\Network\HTTPException\InternalServerErrorException;
|
2019-10-25 22:03:27 -04:00
|
|
|
use Psr\Log\LoggerInterface;
|
2017-11-07 19:37:53 -05:00
|
|
|
|
|
|
|
/**
|
2020-01-24 20:01:49 -05: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)
|
|
|
|
* @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.
|
2017-11-07 19:37:53 -05:00
|
|
|
*/
|
2020-01-26 14:30:24 -05:00
|
|
|
class Notify extends BaseModel
|
2017-11-08 17:02:50 -05:00
|
|
|
{
|
2020-01-26 08:30:32 -05:00
|
|
|
|
2020-01-26 14:30:24 -05:00
|
|
|
/** @var \Friendica\Repository\Notify */
|
2020-01-24 20:01:49 -05:00
|
|
|
private $repo;
|
2019-10-25 22:33:59 -04:00
|
|
|
|
2020-01-26 14:30:24 -05:00
|
|
|
public function __construct(Database $dba, LoggerInterface $logger, \Friendica\Repository\Notify $repo, array $data = [])
|
2019-10-25 22:03:27 -04:00
|
|
|
{
|
2020-01-24 20:01:49 -05:00
|
|
|
parent::__construct($dba, $logger, $data);
|
2019-10-25 22:03:27 -04:00
|
|
|
|
2020-01-24 20:01:49 -05:00
|
|
|
$this->repo = $repo;
|
2019-10-25 22:03:27 -04:00
|
|
|
|
2020-01-24 20:01:49 -05:00
|
|
|
$this->setNameCache();
|
2020-01-28 15:28:57 -05:00
|
|
|
$this->setMsgCache();
|
2017-11-07 19:37:53 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-01-24 20:01:49 -05:00
|
|
|
* Sets the pre-formatted name (caching)
|
2017-11-07 19:37:53 -05:00
|
|
|
*/
|
2020-01-24 20:01:49 -05:00
|
|
|
private function setNameCache()
|
2017-11-08 17:02:50 -05:00
|
|
|
{
|
2020-01-28 15:28:57 -05:00
|
|
|
try {
|
2020-06-08 19:15:08 -04:00
|
|
|
$this->name_cache = strip_tags(BBCode::convert($this->source_name));
|
2020-01-28 15:28:57 -05:00
|
|
|
} catch (InternalServerErrorException $e) {
|
|
|
|
}
|
2017-11-07 19:37:53 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-01-28 15:28:57 -05:00
|
|
|
* Sets the pre-formatted msg (caching)
|
2017-11-07 19:37:53 -05:00
|
|
|
*/
|
2020-01-28 15:28:57 -05:00
|
|
|
private function setMsgCache()
|
2017-11-08 17:02:50 -05:00
|
|
|
{
|
2020-01-24 20:01:49 -05:00
|
|
|
try {
|
|
|
|
$this->msg_cache = self::formatMessage($this->name_cache, strip_tags(BBCode::convert($this->msg)));
|
|
|
|
} catch (InternalServerErrorException $e) {
|
2017-11-07 19:37:53 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-24 20:01:49 -05:00
|
|
|
public function __set($name, $value)
|
2017-11-08 17:02:50 -05:00
|
|
|
{
|
2020-01-24 20:01:49 -05:00
|
|
|
parent::__set($name, $value);
|
2018-06-15 01:50:28 -04:00
|
|
|
|
2020-01-24 20:01:49 -05:00
|
|
|
if ($name == 'msg') {
|
2020-01-28 15:28:57 -05:00
|
|
|
$this->setMsgCache();
|
2017-11-08 17:02:50 -05:00
|
|
|
}
|
2017-11-07 19:37:53 -05:00
|
|
|
|
2020-01-24 20:01:49 -05:00
|
|
|
if ($name == 'source_name') {
|
|
|
|
$this->setNameCache();
|
2017-11-08 17:02:50 -05:00
|
|
|
}
|
2017-11-07 19:37:53 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-01-24 20:01:49 -05:00
|
|
|
* Formats a notification message with the notification author
|
2017-11-07 19:37:53 -05:00
|
|
|
*
|
2020-01-24 20:01:49 -05: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-07 19:37:53 -05:00
|
|
|
*
|
2020-01-24 20:01:49 -05:00
|
|
|
* @param string $name
|
|
|
|
* @param string $message
|
2019-10-25 22:03:27 -04:00
|
|
|
*
|
2020-01-24 20:01:49 -05:00
|
|
|
* @return string Formatted message
|
2017-11-07 19:37:53 -05:00
|
|
|
*/
|
2020-01-24 20:01:49 -05:00
|
|
|
public static function formatMessage($name, $message)
|
2017-11-19 14:15:25 -05:00
|
|
|
{
|
2020-01-24 20:01:49 -05:00
|
|
|
if ($name != '') {
|
|
|
|
$pos = strpos($message, $name);
|
2019-09-09 01:29:33 -04:00
|
|
|
} else {
|
2020-01-24 20:01:49 -05:00
|
|
|
$pos = false;
|
2017-11-07 19:37:53 -05:00
|
|
|
}
|
|
|
|
|
2020-01-24 20:01:49 -05:00
|
|
|
if ($pos !== false) {
|
|
|
|
$message = substr_replace($message, '{0}', $pos, strlen($name));
|
2017-11-07 19:37:53 -05:00
|
|
|
}
|
|
|
|
|
2020-01-24 20:01:49 -05:00
|
|
|
return $message;
|
2017-11-07 19:37:53 -05:00
|
|
|
}
|
|
|
|
}
|