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); } /** * Sets the pre-formatted name (caching) * * @throws InternalServerErrorException */ private function setNameCache() { $this->name_cache = strip_tags(BBCode::convert($this->source_name ?? '')); } /** * 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) */ private function setMsg() { 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 __set($name, $value) { parent::__set($name, $value); if ($name == 'date') { $this->setTimestamp(); } if ($name == 'msg') { $this->setMsg(); } if ($name == 'source_name') { $this->setNameCache(); } } /** * Formats a notification message with the notification author * * Replace the name with {0} but ensure to make that only once. The {0} is used * later and prints the name in bold. * * @param string $name * @param string $message * * @return string Formatted message */ public static function formatMessage($name, $message) { if ($name != '') { $pos = strpos($message, $name); } else { $pos = false; } if ($pos !== false) { $message = substr_replace($message, '{0}', $pos, strlen($name)); } return $message; } /** * {@inheritDoc} */ protected function mapFields(array $data) { return [ 'hash' => $data['hash'] ?? '', 'type' => $data['type'] ?? 0, 'name' => $data['name'] ?? '', 'url' => $data['url'] ?? '', 'photo' => $data['photo'] ?? '', 'date' => $data['date'] ?? DateTimeFormat::utcNow(), 'msg' => $data['msg'] ?? '', 'uid' => $data['uid'] ?? 0, 'link' => $data['link'] ?? '', 'iid' => $data['iid'] ?? 0, 'parent' => $data['parent'] ?? 0, 'seen' => $data['seen'] ?? false, 'verb' => $data['verb'] ?? '', 'otype' => $data['otype'] ?? '', 'name_cache' => $data['name_cache'] ?? null, 'msg_cache' => $data['msg_cache'] ?? null, ]; } }