]> git.mxchange.org Git - friendica.git/blobdiff - src/Model/Notification.php
Merge pull request #10283 from very-ape/fix-message-button
[friendica.git] / src / Model / Notification.php
index 11105d798392f65723c81bf65ee1b6c0a6a0ccea..cf2c754631c6760c0a8fcf72a9c88efe08ecf9a1 100644 (file)
@@ -1,23 +1,34 @@
 <?php
+/**
+ * @copyright Copyright (C) 2010-2021, the Friendica project
+ *
+ * @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/>.
+ *
+ */
 
 namespace Friendica\Model;
 
-use Exception;
 use Friendica\BaseModel;
 use Friendica\Content\Text\BBCode;
-use Friendica\Content\Text\HTML;
 use Friendica\Database\Database;
 use Friendica\Network\HTTPException\InternalServerErrorException;
-use Friendica\Util\DateTimeFormat;
-use Friendica\Util\Temporal;
 use Psr\Log\LoggerInterface;
 
 /**
  * Model for an entry in the notify table
- * - 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
  *
  * @property string  hash
  * @property integer type
@@ -32,22 +43,15 @@ use Psr\Log\LoggerInterface;
  * @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 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
  */
 class Notification extends BaseModel
 {
        /** @var \Friendica\Repository\Notification */
        private $repo;
-       /** @var $this */
-       private $parentInst;
 
        public function __construct(Database $dba, LoggerInterface $logger, \Friendica\Repository\Notification $repo, array $data = [])
        {
@@ -56,100 +60,37 @@ class Notification extends BaseModel
                $this->repo = $repo;
 
                $this->setNameCache();
-               $this->setTimestamp();
-               $this->setMsg();
-       }
-
-       /**
-        * Set the notification as seen
-        *
-        * @param bool $seen true, if seen
-        *
-        * @return bool True, if the seen state could be saved
-        */
-       public function setSeen(bool $seen = true)
-       {
-               $this->seen = $seen;
-               try {
-                       return $this->repo->update($this);
-               } catch (Exception $e) {
-                       $this->logger->warning('Update failed.', ['$this' => $this, 'exception' => $e]);
-                       return false;
-               }
-       }
-
-       /**
-        * Set some extra properties to the notification from db:
-        *  - timestamp as int in default TZ
-        *  - date_rel : relative date string
-        */
-       private function setTimestamp()
-       {
-               try {
-                       $this->timestamp = strtotime(DateTimeFormat::local($this->date));
-               } catch (Exception $e) {
-               }
-               $this->dateRel = Temporal::getRelativeDate($this->date);
+               $this->setMsgCache();
        }
 
        /**
         * Sets the pre-formatted name (caching)
-        *
-        * @throws InternalServerErrorException
         */
        private function setNameCache()
        {
-               $this->name_cache = strip_tags(BBCode::convert($this->source_name ?? ''));
+               try {
+                       $this->name_cache = strip_tags(BBCode::convert($this->source_name));
+               } catch (InternalServerErrorException $e) {
+               }
        }
 
        /**
-        * 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)
+        * Sets the pre-formatted msg (caching)
         */
-       private function setMsg()
+       private function setMsgCache()
        {
                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) {
                }
        }
 
-       public function __get($name)
-       {
-               $this->checkValid();
-
-               $return = null;
-
-               switch ($name) {
-                       case 'parent':
-                               if (!empty($this->parent)) {
-                                       $this->parentInst = $this->parentInst ?? $this->repo->getByID($this->parent);
-
-                                       $return = $this->parentInst;
-                               }
-                               break;
-                       default:
-                               $return = parent::__get($name);
-                               break;
-               }
-
-               return $return;
-       }
-
        public function __set($name, $value)
        {
                parent::__set($name, $value);
 
-               if ($name == 'date') {
-                       $this->setTimestamp();
-               }
-
                if ($name == 'msg') {
-                       $this->setMsg();
+                       $this->setMsgCache();
                }
 
                if ($name == 'source_name') {