friendica/src/Module/Api/Friendica/Notification/Seen.php

87 lines
2.9 KiB
PHP
Raw Normal View History

2021-11-24 23:03:34 +00:00
<?php
/**
* @copyright Copyright (C) 2010-2021, the Friendica project
*
* @license GNU AGPL version 3 or any later version
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
*/
namespace Friendica\Module\Api\Friendica\Notification;
use Exception;
use Friendica\Database\DBA;
use Friendica\DI;
use Friendica\Model\Notification;
use Friendica\Model\Post;
use Friendica\Module\BaseApi;
use Friendica\Network\HTTPException\BadRequestException;
use Friendica\Network\HTTPException\InternalServerErrorException;
use Friendica\Network\HTTPException\NotFoundException;
/**
* Set notification as seen and returns associated item (if possible)
*
* POST request with 'id' param as notification id
*/
class Seen extends BaseApi
{
2021-11-30 21:41:10 +00:00
protected function post(array $request = [])
2021-11-24 23:03:34 +00:00
{
BaseApi::checkAllowedScope(BaseApi::SCOPE_WRITE);
$uid = BaseApi::getCurrentUserID();
2021-11-24 23:06:28 +00:00
2021-11-24 23:03:34 +00:00
if (DI::args()->getArgc() !== 4) {
throw new BadRequestException('Invalid argument count');
}
2021-11-24 23:06:28 +00:00
2021-11-24 23:03:34 +00:00
$id = intval($_REQUEST['id'] ?? 0);
2021-11-24 23:06:28 +00:00
2021-11-24 23:03:34 +00:00
try {
$Notify = DI::notify()->selectOneById($id);
if ($Notify->uid !== $uid) {
throw new NotFoundException();
}
2021-11-24 23:06:28 +00:00
2021-11-24 23:03:34 +00:00
if ($Notify->uriId) {
DI::notification()->setAllSeenForUser($Notify->uid, ['target-uri-id' => $Notify->uriId]);
}
2021-11-24 23:06:28 +00:00
2021-11-24 23:03:34 +00:00
$Notify->setSeen();
DI::notify()->save($Notify);
2021-11-24 23:06:28 +00:00
2021-11-24 23:03:34 +00:00
if ($Notify->otype === Notification\ObjectType::ITEM) {
$item = Post::selectFirstForUser($uid, [], ['id' => $Notify->iid, 'uid' => $uid]);
if (DBA::isResult($item)) {
$include_entities = strtolower(($_REQUEST['include_entities'] ?? 'false') == 'true');
2021-11-24 23:06:28 +00:00
2021-11-24 23:03:34 +00:00
// we found the item, return it to the user
2021-11-24 23:20:42 +00:00
$ret = [DI::twitterStatus()->createFromUriId($item['uri-id'], $item['uid'], $include_entities)->toArray()];
2021-11-24 23:03:34 +00:00
$data = ['status' => $ret];
$this->response->exit('statuses', $data, $this->parameters['extension'] ?? null);
2021-11-29 06:09:28 +00:00
return;
2021-11-24 23:03:34 +00:00
}
// the item can't be found, but we set the notification as seen, so we count this as a success
}
2021-11-24 23:06:28 +00:00
$this->response->exit('statuses', ['result' => 'success'], $this->parameters['extension'] ?? null);
2021-11-24 23:03:34 +00:00
} catch (NotFoundException $e) {
throw new BadRequestException('Invalid argument', $e);
} catch (Exception $e) {
throw new InternalServerErrorException('Internal Server exception', $e);
}
}
}