]> git.mxchange.org Git - friendica.git/blob - src/Module/Api/Friendica/Notification/Seen.php
Many API calls moved
[friendica.git] / src / Module / Api / Friendica / Notification / Seen.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\Module\Api\Friendica\Notification;
23
24 use Exception;
25 use Friendica\Database\DBA;
26 use Friendica\DI;
27 use Friendica\Model\Notification;
28 use Friendica\Model\Post;
29 use Friendica\Module\BaseApi;
30 use Friendica\Network\HTTPException\BadRequestException;
31 use Friendica\Network\HTTPException\InternalServerErrorException;
32 use Friendica\Network\HTTPException\NotFoundException;
33
34 /**
35  * Set notification as seen and returns associated item (if possible)
36  *
37  * POST request with 'id' param as notification id
38  */
39 class Seen extends BaseApi
40 {
41         public function rawContent()
42         {
43                 BaseApi::checkAllowedScope(BaseApi::SCOPE_WRITE);
44                 $uid = BaseApi::getCurrentUserID();
45         
46                 if (DI::args()->getArgc() !== 4) {
47                         throw new BadRequestException('Invalid argument count');
48                 }
49         
50                 $id = intval($_REQUEST['id'] ?? 0);
51         
52                 try {
53                         $Notify = DI::notify()->selectOneById($id);
54                         if ($Notify->uid !== $uid) {
55                                 throw new NotFoundException();
56                         }
57         
58                         if ($Notify->uriId) {
59                                 DI::notification()->setAllSeenForUser($Notify->uid, ['target-uri-id' => $Notify->uriId]);
60                         }
61         
62                         $Notify->setSeen();
63                         DI::notify()->save($Notify);
64         
65                         if ($Notify->otype === Notification\ObjectType::ITEM) {
66                                 $item = Post::selectFirstForUser($uid, [], ['id' => $Notify->iid, 'uid' => $uid]);
67                                 if (DBA::isResult($item)) {
68                                         $include_entities = strtolower(($_REQUEST['include_entities'] ?? 'false') == 'true');
69         
70                                         // we found the item, return it to the user
71                                         $ret = [DI::twitterStatus()->createFromUriId($item['uri-id'], $item['uid'], $include_entities)->toArray()];
72                                         $data = ['status' => $ret];
73                                         DI::apiResponse()->exit('statuses', $data, $this->parameters['extension'] ?? null);
74                                 }
75                                 // the item can't be found, but we set the notification as seen, so we count this as a success
76                         }
77         
78                         DI::apiResponse()->exit('statuses', ['result' => 'success'], $this->parameters['extension'] ?? null);
79                 } catch (NotFoundException $e) {
80                         throw new BadRequestException('Invalid argument', $e);
81                 } catch (Exception $e) {
82                         throw new InternalServerErrorException('Internal Server exception', $e);
83                 }
84         }
85 }