3 * @copyright Copyright (C) 2010-2021, the Friendica project
5 * @license GNU AGPL version 3 or any later version
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.
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.
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/>.
22 namespace Friendica\Module\Api\Friendica\Notification;
25 use Friendica\Database\DBA;
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;
35 * Set notification as seen and returns associated item (if possible)
37 * POST request with 'id' param as notification id
39 class Seen extends BaseApi
41 protected function rawContent(array $request = [])
43 BaseApi::checkAllowedScope(BaseApi::SCOPE_WRITE);
44 $uid = BaseApi::getCurrentUserID();
46 if (DI::args()->getArgc() !== 4) {
47 throw new BadRequestException('Invalid argument count');
50 $id = intval($_REQUEST['id'] ?? 0);
53 $Notify = DI::notify()->selectOneById($id);
54 if ($Notify->uid !== $uid) {
55 throw new NotFoundException();
59 DI::notification()->setAllSeenForUser($Notify->uid, ['target-uri-id' => $Notify->uriId]);
63 DI::notify()->save($Notify);
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');
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);
75 // the item can't be found, but we set the notification as seen, so we count this as a success
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);