]> git.mxchange.org Git - friendica.git/blob - src/Object/Api/Friendica/Notification.php
Refactor API notification usage
[friendica.git] / src / Object / Api / Friendica / Notification.php
1 <?php
2
3 namespace Friendica\Object\Api\Friendica;
4
5 use Friendica\BaseEntity;
6 use Friendica\Content\Text\BBCode;
7 use Friendica\Content\Text\HTML;
8 use Friendica\Model\Notify;
9 use Friendica\Util\DateTimeFormat;
10 use Friendica\Util\Temporal;
11
12 /**
13  * Friendica Notification
14  *
15  * @see https://github.com/friendica/friendica/blob/develop/doc/API-Entities.md#notification
16  */
17 class Notification extends BaseEntity
18 {
19         /** @var integer */
20         protected $id;
21         /** @var string */
22         protected $hash;
23         /** @var integer */
24         protected $type;
25         /** @var string Full name of the contact subject */
26         protected $name;
27         /** @var string Profile page URL of the contact subject */
28         protected $url;
29         /** @var string Profile photo URL of the contact subject */
30         protected $photo;
31         /** @var string YYYY-MM-DD hh:mm:ss local server time */
32         protected $date;
33         /** @var string The message (BBCode) */
34         protected $msg;
35         /** @var integer Owner User Id */
36         protected $uid;
37         /** @var string Notification URL */
38         protected $link;
39         /** @var integer Item Id */
40         protected $iid;
41         /** @var integer Parent Item Id */
42         protected $parent;
43         /** @var boolean  Whether the notification was read or not. */
44         protected $seen;
45         /** @var string Verb URL @see http://activitystrea.ms */
46         protected $verb;
47         /** @var string Subject type (`item`, `intro` or `mail`) */
48         protected $otype;
49         /** @var string Full name of the contact subject (HTML) */
50         protected $name_cache;
51         /** @var string Plaintext version of the notification text with a placeholder (`{0}`) for the subject contact's name. (Plaintext) */
52         protected $msg_cache;
53         /** @var integer  Unix timestamp */
54         protected $timestamp;
55         /** @var string Time since the note was posted, eg "1 hour ago" */
56         protected $date_rel;
57         /** @var string Message (HTML) */
58         protected $msg_html;
59         /** @var string Message (Plaintext) */
60         protected $msg_plain;
61
62         public function __construct(Notify $notify)
63         {
64                 // map each notify attribute to the entity
65                 foreach ($notify->toArray() as $key => $value) {
66                         $this->{$key} = $value;
67                 }
68
69                 // add additional attributes for the API
70                 try {
71                         $this->timestamp = strtotime(DateTimeFormat::local($this->date));
72                         $this->msg_html  = BBCode::convert($this->msg, false);
73                         $this->msg_plain = explode("\n", trim(HTML::toPlaintext($this->msg_html, 0)))[0];
74                 } catch (\Exception $e) {
75                 }
76
77                 $this->date_rel = Temporal::getRelativeDate($this->date);
78         }
79 }