]> git.mxchange.org Git - friendica.git/blob - src/Object/Api/Mastodon/Notification.php
Merge pull request #11128 from annando/diaspora-contact-details
[friendica.git] / src / Object / Api / Mastodon / Notification.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2010-2021, the Friendica project
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\Mastodon;
23
24 use Exception;
25 use Friendica\BaseDataTransferObject;
26 use Friendica\Network\HTTPException;
27 use Friendica\Util\DateTimeFormat;
28
29 /**
30  * Class Notification
31  *
32  * @see https://docs.joinmastodon.org/entities/notification/
33  */
34 class Notification extends BaseDataTransferObject
35 {
36         /* From the Mastodon documentation:
37          * - follow         = Someone followed you
38          * - follow_request = Someone requested to follow you
39          * - mention        = Someone mentioned you in their status
40          * - reblog         = Someone boosted one of your statuses
41          * - favourite      = Someone favourited one of your statuses
42          * - poll           = A poll you have voted in or created has ended
43          * - status         = Someone you enabled notifications for has posted a status
44          */
45         public const TYPE_FOLLOW       = 'follow';
46         public const TYPE_INTRODUCTION = 'follow_request';
47         public const TYPE_MENTION      = 'mention';
48         public const TYPE_RESHARE      = 'reblog';
49         public const TYPE_LIKE         = 'favourite';
50         public const TYPE_POLL         = 'poll';
51         public const TYPE_POST         = 'status';
52
53         /** @var string */
54         protected $id;
55         /** @var string One of the TYPE_* constant values */
56         protected $type;
57         /** @var string (Datetime) */
58         protected $created_at;
59         /** @var Account */
60         protected $account;
61         /** @var Status|null */
62         protected $status = null;
63
64         /**
65          * Creates a notification record
66          *
67          * @throws HttpException\InternalServerErrorException|Exception
68          */
69         public function __construct(int $id, string $type, \DateTime $created_at, Account $account = null, Status $status = null)
70         {
71                 $this->id         = (string)$id;
72                 $this->type       = $type;
73                 $this->created_at = $created_at->format(DateTimeFormat::JSON);
74                 $this->account    = $account->toArray();
75
76                 if (!empty($status)) {
77                         $this->status = $status->toArray();
78                 }
79         }
80
81         /**
82          * Returns the current entity as an array
83          *
84          * @return array
85          */
86         public function toArray(): array
87         {
88                 $notification = parent::toArray();
89
90                 if (!$notification['status']) {
91                         unset($notification['status']);
92                 }
93
94                 return $notification;
95         }
96 }