X-Git-Url: https://git.mxchange.org/?a=blobdiff_plain;f=include%2Fapi.php;h=11950f52f3cbf4d3a20cc05f2f4af1d77e8fc0c4;hb=7a2d5f6a8eea1babea3450af0f62f3f692f91348;hp=7656f5029c3226dc1040315759dc7a71511804cc;hpb=6cba99f9c948e60abc18e1f2176b5fbaf1f7cece;p=friendica.git diff --git a/include/api.php b/include/api.php index 7656f5029c..11950f52f3 100644 --- a/include/api.php +++ b/include/api.php @@ -24,15 +24,14 @@ */ use Friendica\App; +use Friendica\Collection\Api\Notifications as ApiNotifications; use Friendica\Content\ContactSelector; use Friendica\Content\Text\BBCode; use Friendica\Content\Text\HTML; use Friendica\Core\Hook; use Friendica\Core\Logger; use Friendica\Core\Protocol; -use Friendica\Core\Session; use Friendica\Core\System; -use Friendica\Core\Worker; use Friendica\Database\DBA; use Friendica\DI; use Friendica\Model\Contact; @@ -54,6 +53,7 @@ use Friendica\Network\HTTPException\MethodNotAllowedException; use Friendica\Network\HTTPException\NotFoundException; use Friendica\Network\HTTPException\TooManyRequestsException; use Friendica\Network\HTTPException\UnauthorizedException; +use Friendica\Object\Api\Friendica\Notification as ApiNotification; use Friendica\Object\Image; use Friendica\Protocol\Activity; use Friendica\Protocol\Diaspora; @@ -2193,11 +2193,14 @@ function api_statuses_mentions($type) (SELECT `uri-id` FROM `post-user-notification` WHERE `uid` = ? AND `notification-type` & ? != 0 ORDER BY `uri-id`) AND (`uid` = 0 OR (`uid` = ? AND NOT `global`)) AND `id` > ?"; - $condition = [GRAVITY_PARENT, GRAVITY_COMMENT, api_user(), - Post\UserNotification::NOTIF_EXPLICIT_TAGGED | Post\UserNotification::NOTIF_IMPLICIT_TAGGED | - Post\UserNotification::NOTIF_THREAD_COMMENT | Post\UserNotification::NOTIF_DIRECT_COMMENT | - Post\UserNotification::NOTIF_DIRECT_THREAD_COMMENT, - api_user(), $since_id]; + $condition = [ + GRAVITY_PARENT, GRAVITY_COMMENT, + api_user(), + Post\UserNotification::TYPE_EXPLICIT_TAGGED | Post\UserNotification::TYPE_IMPLICIT_TAGGED | + Post\UserNotification::TYPE_THREAD_COMMENT | Post\UserNotification::TYPE_DIRECT_COMMENT | + Post\UserNotification::TYPE_DIRECT_THREAD_COMMENT, + api_user(), $since_id, + ]; if ($max_id > 0) { $query .= " AND `id` <= ?"; @@ -3778,11 +3781,11 @@ api_register_func('api/direct_messages/destroy', 'api_direct_messages_destroy', * * @param string $type Known types are 'atom', 'rss', 'xml' and 'json' * @return string|array - * @throws BadRequestException - * @throws ForbiddenException - * @throws ImagickException - * @throws InternalServerErrorException - * @throws NotFoundException + * @throws HTTPException\BadRequestException + * @throws HTTPException\ExpectationFailedException + * @throws HTTPException\ForbiddenException + * @throws HTTPException\InternalServerErrorException + * @throws HTTPException\NotFoundException * @see https://developer.twitter.com/en/docs/accounts-and-users/follow-search-get-users/api-reference/post-friendships-destroy.html */ function api_friendships_destroy($type) @@ -3790,25 +3793,31 @@ function api_friendships_destroy($type) $uid = api_user(); if ($uid === false) { - throw new ForbiddenException(); + throw new HTTPException\ForbiddenException(); + } + + $owner = User::getOwnerDataById($uid); + if (!$owner) { + Logger::notice(API_LOG_PREFIX . 'No owner {uid} found', ['module' => 'api', 'action' => 'friendships_destroy', 'uid' => $uid]); + throw new HTTPException\NotFoundException('Error Processing Request'); } $contact_id = $_REQUEST['user_id'] ?? 0; if (empty($contact_id)) { Logger::notice(API_LOG_PREFIX . 'No user_id specified', ['module' => 'api', 'action' => 'friendships_destroy']); - throw new BadRequestException("no user_id specified"); + throw new HTTPException\BadRequestException('no user_id specified'); } // Get Contact by given id $contact = DBA::selectFirst('contact', ['url'], ['id' => $contact_id, 'uid' => 0, 'self' => false]); if(!DBA::isResult($contact)) { - Logger::notice(API_LOG_PREFIX . 'No contact found for ID {contact}', ['module' => 'api', 'action' => 'friendships_destroy', 'contact' => $contact_id]); - throw new NotFoundException("no contact found to given ID"); + Logger::notice(API_LOG_PREFIX . 'No public contact found for ID {contact}', ['module' => 'api', 'action' => 'friendships_destroy', 'contact' => $contact_id]); + throw new HTTPException\NotFoundException('no contact found to given ID'); } - $url = $contact["url"]; + $url = $contact['url']; $condition = ["`uid` = ? AND (`rel` = ? OR `rel` = ?) AND (`nurl` = ? OR `alias` = ? OR `alias` = ?)", $uid, Contact::SHARING, Contact::FRIEND, Strings::normaliseLink($url), @@ -3817,40 +3826,33 @@ function api_friendships_destroy($type) if (!DBA::isResult($contact)) { Logger::notice(API_LOG_PREFIX . 'Not following contact', ['module' => 'api', 'action' => 'friendships_destroy']); - throw new NotFoundException("Not following Contact"); - } - - if (!in_array($contact['network'], Protocol::NATIVE_SUPPORT)) { - Logger::notice(API_LOG_PREFIX . 'Not supported for {network}', ['module' => 'api', 'action' => 'friendships_destroy', 'network' => $contact['network']]); - throw new ExpectationFailedException("Not supported"); + throw new HTTPException\NotFoundException('Not following Contact'); } - $dissolve = ($contact['rel'] == Contact::SHARING); + try { + $result = Contact::terminateFriendship($owner, $contact); - $owner = User::getOwnerDataById($uid); - if ($owner) { - Contact::terminateFriendship($owner, $contact, $dissolve); - } - else { - Logger::notice(API_LOG_PREFIX . 'No owner {uid} found', ['module' => 'api', 'action' => 'friendships_destroy', 'uid' => $uid]); - throw new NotFoundException("Error Processing Request"); - } + if ($result === null) { + Logger::notice(API_LOG_PREFIX . 'Not supported for {network}', ['module' => 'api', 'action' => 'friendships_destroy', 'network' => $contact['network']]); + throw new HTTPException\ExpectationFailedException('Unfollowing is currently not supported by this contact\'s network.'); + } - // Sharing-only contacts get deleted as there no relationship any more - if ($dissolve) { - Contact::remove($contact['id']); - } else { - Contact::update(['rel' => Contact::FOLLOWER], ['id' => $contact['id']]); + if ($result === false) { + throw new HTTPException\ServiceUnavailableException('Unable to unfollow this contact, please retry in a few minutes or contact your administrator.'); + } + } catch (Exception $e) { + Logger::error(API_LOG_PREFIX . $e->getMessage(), ['owner' => $owner, 'contact' => $contact]); + throw new HTTPException\InternalServerErrorException('Unable to unfollow this contact, please contact your administrator'); } // "uid" and "self" are only needed for some internal stuff, so remove it from here - unset($contact["uid"]); - unset($contact["self"]); + unset($contact['uid']); + unset($contact['self']); // Set screen_name since Twidere requests it - $contact["screen_name"] = $contact["nick"]; + $contact['screen_name'] = $contact['nick']; - return api_format_data("friendships-destroy", $type, ['user' => $contact]); + return api_format_data('friendships-destroy', $type, ['user' => $contact]); } api_register_func('api/friendships/destroy', 'api_friendships_destroy', true, API_METHOD_POST); @@ -5581,23 +5583,24 @@ api_register_func('api/friendica/activity/unattendmaybe', 'api_friendica_activit */ function api_friendica_notification($type) { - $a = DI::app(); - if (api_user() === false) { throw new ForbiddenException(); } if (DI::args()->getArgc()!==3) { - throw new BadRequestException("Invalid argument count"); + throw new BadRequestException('Invalid argument count'); } - $notifications = DI::notification()->getApiList(local_user()); + $Notifies = DI::notify()->selectAllForUser(local_user(), ['order' => ['seen' => 'ASC', 'date' => 'DESC'], 'limit' => 50]); - if ($type == "xml") { - $xmlnotes = false; - if (!empty($notifications)) { - foreach ($notifications as $notification) { - $xmlnotes[] = ["@attributes" => $notification->toArray()]; - } + $notifications = new ApiNotifications(); + foreach ($Notifies as $Notify) { + $notifications[] = new ApiNotification($Notify); + } + + if ($type == 'xml') { + $xmlnotes = []; + foreach ($notifications as $notification) { + $xmlnotes[] = ['@attributes' => $notification->toArray()]; } $result = $xmlnotes; @@ -5607,7 +5610,7 @@ function api_friendica_notification($type) $result = false; } - return api_format_data("notes", $type, ['note' => $result]); + return api_format_data('notes', $type, ['note' => $result]); } /** @@ -5632,26 +5635,36 @@ function api_friendica_notification_seen($type) throw new ForbiddenException(); } if (DI::args()->getArgc() !== 4) { - throw new BadRequestException("Invalid argument count"); + throw new BadRequestException('Invalid argument count'); } - $id = (!empty($_REQUEST['id']) ? intval($_REQUEST['id']) : 0); + $id = intval($_REQUEST['id'] ?? 0); try { - $notify = DI::notify()->getByID($id, api_user()); - DI::notify()->setSeen(true, $notify); + $Notify = DI::notify()->selectOneById($id); + if ($Notify->uid !== api_user()) { + throw new NotFoundException(); + } - if ($notify->otype === Notification\ObjectType::ITEM) { - $item = Post::selectFirstForUser(api_user(), [], ['id' => $notify->iid, 'uid' => api_user()]); + if ($Notify->uriId) { + DI::notification()->setAllSeenForUser($Notify->uid, ['target-uri-id' => $Notify->uriId]); + } + + $Notify->setSeen(); + DI::notify()->save($Notify); + + if ($Notify->otype === Notification\ObjectType::ITEM) { + $item = Post::selectFirstForUser(api_user(), [], ['id' => $Notify->iid, 'uid' => api_user()]); if (DBA::isResult($item)) { // we found the item, return it to the user $ret = api_format_items([$item], $user_info, false, $type); $data = ['status' => $ret]; - return api_format_data("status", $type, $data); + return api_format_data('status', $type, $data); } // the item can't be found, but we set the notification as seen, so we count this as a success } - return api_format_data('result', $type, ['result' => "success"]); + + return api_format_data('result', $type, ['result' => 'success']); } catch (NotFoundException $e) { throw new BadRequestException('Invalid argument', $e); } catch (Exception $e) {