Merge pull request #8179 from MrPetovan/bug/notices
authorPhilipp <admin+Github@philipp.info>
Sun, 26 Jan 2020 22:10:56 +0000 (23:10 +0100)
committerGitHub <noreply@github.com>
Sun, 26 Jan 2020 22:10:56 +0000 (23:10 +0100)
Various notice fixes

1  2 
include/api.php
mod/display.php

diff --combined include/api.php
@@@ -23,7 -23,6 +23,7 @@@ use Friendica\Model\Contact
  use Friendica\Model\Group;
  use Friendica\Model\Item;
  use Friendica\Model\Mail;
 +use Friendica\Model\Notify;
  use Friendica\Model\Photo;
  use Friendica\Model\Profile;
  use Friendica\Model\User;
@@@ -312,9 -311,7 +312,7 @@@ function api_call(App $a, App\Argument
                                }
  
                                $called_api = explode("/", $p);
-                               //unset($_SERVER['PHP_AUTH_USER']);
  
-                               /// @TODO should be "true ==[=] $info['auth']", if you miss only one = character, you assign a variable (only with ==). Let's make all this even.
                                if (!empty($info['auth']) && api_user() === false) {
                                        api_login($a);
                                }
@@@ -5028,6 -5025,9 +5026,9 @@@ function prepare_photo_data($type, $sca
        // retrieve item element for getting activities (like, dislike etc.) related to photo
        $condition = ['uid' => local_user(), 'resource-id' => $photo_id, 'type' => 'photo'];
        $item = Item::selectFirstForUser(local_user(), ['id'], $condition);
+       if (!DBA::isResult($item)) {
+               throw new NotFoundException('Photo-related item not found.');
+       }
  
        $data['photo']['friendica_activities'] = api_format_items_activities($item, $type);
  
@@@ -5906,25 -5906,19 +5907,25 @@@ function api_friendica_notification($ty
        if ($a->argc!==3) {
                throw new BadRequestException("Invalid argument count");
        }
 -      $notes = DI::notify()->getAll([], ['seen' => 'ASC', 'date' => 'DESC'], 50);
 +
 +      $notifications = DI::notify()->select(['uid' => api_user()], ['order' => ['seen' => 'ASC', 'date' => 'DESC'], 'limit' => 50]);
  
        if ($type == "xml") {
 -              $xmlnotes = [];
 -              if (!empty($notes)) {
 -                      foreach ($notes as $note) {
 -                              $xmlnotes[] = ["@attributes" => $note];
 +              $xmlnotes = false;
 +              if (!empty($notifications)) {
 +                      foreach ($notifications as $notification) {
 +                              $xmlnotes[] = ["@attributes" => $notification->toArray()];
                        }
                }
  
 -              $notes = $xmlnotes;
 +              $result = $xmlnotes;
 +      } elseif (count($notifications) > 0) {
 +              $result = $notifications->getArrayCopy();
 +      } else {
 +              $result = false;
        }
 -      return api_format_data("notes", $type, ['note' => $notes]);
 +
 +      return api_format_data("notes", $type, ['note' => $result]);
  }
  
  /**
   */
  function api_friendica_notification_seen($type)
  {
 -      $a = DI::app();
 +      $a         = DI::app();
        $user_info = api_get_user($a);
  
        if (api_user() === false || $user_info === false) {
                throw new ForbiddenException();
        }
 -      if ($a->argc!==4) {
 +      if ($a->argc !== 4) {
                throw new BadRequestException("Invalid argument count");
        }
  
        $id = (!empty($_REQUEST['id']) ? intval($_REQUEST['id']) : 0);
  
 -      $nm = DI::notify();
 -      $note = $nm->getByID($id);
 -      if (is_null($note)) {
 -              throw new BadRequestException("Invalid argument");
 -      }
 -
 -      $nm->setSeen($note);
 -      if ($note['otype']=='item') {
 -              // would be really better with an ItemsManager and $im->getByID() :-P
 -              $item = Item::selectFirstForUser(api_user(), [], ['id' => $note['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);
 +      try {
 +              $notification = DI::notify()->getByID($id);
 +              $notification->setSeen();
 +
 +              if ($notification->otype === Notify::OTYPE_ITEM) {
 +                      $item = Item::selectFirstForUser(api_user(), [], ['id' => $notification->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);
 +                      }
 +                      // the item can't be found, but we set the notification as seen, so we count this as a success
                }
 -              // the item can't be found, but we set the note as seen, so we count this as a success
 +              return api_format_data('result', $type, ['result' => "success"]);
 +      } catch (NotFoundException $e) {
 +              throw new BadRequestException('Invalid argument');
        }
 -      return api_format_data('result', $type, ['result' => "success"]);
  }
  
  /// @TODO move to top of file or somewhere better
diff --combined mod/display.php
@@@ -96,13 -96,13 +96,13 @@@ function display_init(App $a
  
        $profiledata = display_fetchauthor($a, $item);
  
-       if (strstr(Strings::normaliseLink($profiledata["url"]), Strings::normaliseLink(DI::baseUrl()))) {
-               $nickname = str_replace(Strings::normaliseLink(DI::baseUrl())."/profile/", "", Strings::normaliseLink($profiledata["url"]));
+       if (strstr(Strings::normaliseLink($profiledata['url']), Strings::normaliseLink(DI::baseUrl()))) {
+               $nickname = str_replace(Strings::normaliseLink(DI::baseUrl()) . '/profile/', '', Strings::normaliseLink($profiledata['url']));
  
-               if ($nickname != $a->user["nickname"]) {
+               if (!empty($a->user['nickname']) && $nickname != $a->user['nickname']) {
                        $profile = DBA::fetchFirst("SELECT `profile`.* , `contact`.`avatar-date` AS picdate, `user`.* FROM `profile`
                                INNER JOIN `contact` on `contact`.`uid` = `profile`.`uid` INNER JOIN `user` ON `profile`.`uid` = `user`.`uid`
 -                              WHERE `user`.`nickname` = ? AND `profile`.`is-default` AND `contact`.`self` LIMIT 1",
 +                              WHERE `user`.`nickname` = ? AND `contact`.`self` LIMIT 1",
                                $nickname
                        );
                        if (DBA::isResult($profile)) {