Rename notify classes according the feature name, not the table name
[friendica.git/.git] / src / Object / Api / Friendica / Notification.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2020, Friendica
4  *
5  * @license GNU AGPL version 3 or any later version
6  *
7  * This program is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU Affero General Public License as
9  * published by the Free Software Foundation, either version 3 of the
10  * License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU Affero General Public License for more details.
16  *
17  * You should have received a copy of the GNU Affero General Public License
18  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
19  *
20  */
21
22 namespace Friendica\Object\Api\Friendica;
23
24 use Friendica\BaseEntity;
25 use Friendica\Content\Text\BBCode;
26 use Friendica\Content\Text\HTML;
27 use Friendica\Model\Notification as NotificationModel;
28 use Friendica\Util\DateTimeFormat;
29 use Friendica\Util\Temporal;
30
31 /**
32  * Friendica Notification
33  *
34  * @see https://github.com/friendica/friendica/blob/develop/doc/API-Entities.md#notification
35  */
36 class Notification extends BaseEntity
37 {
38         /** @var integer */
39         protected $id;
40         /** @var string */
41         protected $hash;
42         /** @var integer */
43         protected $type;
44         /** @var string Full name of the contact subject */
45         protected $name;
46         /** @var string Profile page URL of the contact subject */
47         protected $url;
48         /** @var string Profile photo URL of the contact subject */
49         protected $photo;
50         /** @var string YYYY-MM-DD hh:mm:ss local server time */
51         protected $date;
52         /** @var string The message (BBCode) */
53         protected $msg;
54         /** @var integer Owner User Id */
55         protected $uid;
56         /** @var string Notification URL */
57         protected $link;
58         /** @var integer Item Id */
59         protected $iid;
60         /** @var integer Parent Item Id */
61         protected $parent;
62         /** @var boolean  Whether the notification was read or not. */
63         protected $seen;
64         /** @var string Verb URL @see http://activitystrea.ms */
65         protected $verb;
66         /** @var string Subject type ('item', 'intro' or 'mail') */
67         protected $otype;
68         /** @var string Full name of the contact subject (HTML) */
69         protected $name_cache;
70         /** @var string Plaintext version of the notification text with a placeholder (`{0}`) for the subject contact's name. (Plaintext) */
71         protected $msg_cache;
72         /** @var integer  Unix timestamp */
73         protected $timestamp;
74         /** @var string Time since the note was posted, eg "1 hour ago" */
75         protected $date_rel;
76         /** @var string Message (HTML) */
77         protected $msg_html;
78         /** @var string Message (Plaintext) */
79         protected $msg_plain;
80
81         public function __construct(NotificationModel $notify)
82         {
83                 // map each notify attribute to the entity
84                 foreach ($notify->toArray() as $key => $value) {
85                         $this->{$key} = $value;
86                 }
87
88                 // add additional attributes for the API
89                 try {
90                         $this->timestamp = strtotime(DateTimeFormat::local($this->date));
91                         $this->msg_html  = BBCode::convert($this->msg, false);
92                         $this->msg_plain = explode("\n", trim(HTML::toPlaintext($this->msg_html, 0)))[0];
93                 } catch (\Exception $e) {
94                 }
95
96                 $this->date_rel = Temporal::getRelativeDate($this->date);
97         }
98 }