]> git.mxchange.org Git - friendica.git/commitdiff
Merge pull request #7943 from MrPetovan/bug/7920-api-fix-followers-friends-ids
authorMichael Vogel <icarus@dabo.de>
Fri, 13 Dec 2019 20:34:09 +0000 (21:34 +0100)
committerGitHub <noreply@github.com>
Fri, 13 Dec 2019 20:34:09 +0000 (21:34 +0100)
 Add relationship filter to api_friends_ids and api_followers_ids

1  2 
include/api.php

diff --combined include/api.php
index f5f9b50c3485959154c39ec740ce797e95763853,5329e0b1f3053df119c2b9a5e5284069a42f9ec8..b1030335d65fcc619acee1ecbbddfa97f8bfdeff
@@@ -1416,37 -1416,32 +1416,37 @@@ function api_users_search($type
        $userlist = [];
  
        if (!empty($_GET['q'])) {
 -              $r = q("SELECT id FROM `contact` WHERE `uid` = 0 AND `name` = '%s'", DBA::escape($_GET["q"]));
 -
 -              if (!DBA::isResult($r)) {
 -                      $r = q("SELECT `id` FROM `contact` WHERE `uid` = 0 AND `nick` = '%s'", DBA::escape($_GET["q"]));
 -              }
 +              $contacts = Contact::selectToArray(
 +                      ['id'],
 +                      [
 +                              '`uid` = 0 AND (`name` = ? OR `nick` = ? OR `url` = ? OR `addr` = ?)',
 +                              $_GET['q'],
 +                              $_GET['q'],
 +                              $_GET['q'],
 +                              $_GET['q'],
 +                      ]
 +              );
  
 -              if (DBA::isResult($r)) {
 +              if (DBA::isResult($contacts)) {
                        $k = 0;
 -                      foreach ($r as $user) {
 -                              $user_info = api_get_user($a, $user["id"]);
 +                      foreach ($contacts as $contact) {
 +                              $user_info = api_get_user($a, $contact['id']);
  
 -                              if ($type == "xml") {
 -                                      $userlist[$k++.":user"] = $user_info;
 +                              if ($type == 'xml') {
 +                                      $userlist[$k++ . ':user'] = $user_info;
                                } else {
                                        $userlist[] = $user_info;
                                }
                        }
 -                      $userlist = ["users" => $userlist];
 +                      $userlist = ['users' => $userlist];
                } else {
 -                      throw new BadRequestException("User ".$_GET["q"]." not found.");
 +                      throw new NotFoundException('User ' . $_GET['q'] . ' not found.');
                }
        } else {
 -              throw new BadRequestException("No user specified.");
 +              throw new BadRequestException('No search term specified.');
        }
  
 -      return api_format_data("users", $type, $userlist);
 +      return api_format_data('users', $type, $userlist);
  }
  
  /// @TODO move to top of file or somewhere better
@@@ -1507,9 -1502,7 +1507,9 @@@ function api_search($type
        $a = \get_app();
        $user_info = api_get_user($a);
  
 -      if (api_user() === false || $user_info === false) { throw new ForbiddenException(); }
 +      if (api_user() === false || $user_info === false) {
 +              throw new ForbiddenException();
 +      }
  
        if (empty($_REQUEST['q'])) {
                throw new BadRequestException('q parameter is required.');
                }
        }
  
 -      $statuses = Item::selectForUser(api_user(), [], $condition, $params);
 +      $statuses = [];
 +
 +      if (parse_url($searchTerm, PHP_URL_SCHEME) != '') {
 +              $id = Item::fetchByLink($searchTerm, api_user());
 +              if (!$id) {
 +                      // Public post
 +                      $id = Item::fetchByLink($searchTerm);
 +              }
 +
 +              if (!empty($id)) {
 +                      $statuses = Item::select([], ['id' => $id]);
 +              }
 +      }
 +
 +      $statuses = $statuses ?: Item::selectForUser(api_user(), [], $condition, $params);
  
        $data['status'] = api_format_items(Item::inArray($statuses), $user_info);
  
@@@ -2504,9 -2483,6 +2504,9 @@@ function api_format_messages($item, $re
  function api_convert_item($item)
  {
        $body = $item['body'];
 +      $entities = api_get_entitities($statustext, $body);
 +
 +      // Add pictures to the attachment array and remove them from the body
        $attachments = api_get_attachments($body);
  
        // Workaround for ostatus messages where the title is identically to the body
                $statushtml .= BBCode::convert($item['plink']);
        }
  
 -      $entities = api_get_entitities($statustext, $body);
 -
        return [
                "text" => $statustext,
                "html" => $statushtml,
   */
  function api_get_attachments(&$body)
  {
 -      $text = $body;
 -      $text = preg_replace("/\[img\=([0-9]*)x([0-9]*)\](.*?)\[\/img\]/ism", '[img]$3[/img]', $text);
 -      $text = preg_replace("/\[img\=(.*?)\](.*?)\[\/img\]/ism", '[img]$1[/img]', $text);
 +      $body = preg_replace("/\[img\=([0-9]*)x([0-9]*)\](.*?)\[\/img\]/ism", '[img]$3[/img]', $body);
 +      $body = preg_replace("/\[img\=(.*?)\](.*?)\[\/img\]/ism", '[img]$1[/img]', $body);
  
        $URLSearchString = "^\[\]";
 -      $ret = preg_match_all("/\[img\]([$URLSearchString]*)\[\/img\]/ism", $text, $images);
 -
 -      if (!$ret) {
 +      if (!preg_match_all("/\[img\]([$URLSearchString]*)\[\/img\]/ism", $body, $images)) {
                return [];
        }
  
 +      // Remove all embedded pictures, since they are added as attachments
 +      foreach ($images[0] as $orig) {
 +              $body = str_replace($orig, '', $body);
 +      }
 +
        $attachments = [];
  
        foreach ($images[1] as $image) {
                }
        }
  
 -      if (strstr($_SERVER['HTTP_USER_AGENT'] ?? '', 'AndStatus')) {
 -              foreach ($images[0] as $orig) {
 -                      $body = str_replace($orig, "", $body);
 -              }
 -      }
 -
        return $attachments;
  }
  
@@@ -2645,6 -2627,7 +2645,6 @@@ function api_get_entitities(&$text, $bb
        $bbcode = preg_replace("/#\[url\=([$URLSearchString]*)\](.*?)\[\/url\]/ism", '#$2', $bbcode);
  
        $bbcode = preg_replace("/\[bookmark\=([$URLSearchString]*)\](.*?)\[\/bookmark\]/ism", '[url=$1]$2[/url]', $bbcode);
 -      //$bbcode = preg_replace("/\[url\](.*?)\[\/url\]/ism",'[url=$1]$1[/url]',$bbcode);
        $bbcode = preg_replace("/\[video\](.*?)\[\/video\]/ism", '[url=$1]$1[/url]', $bbcode);
  
        $bbcode = preg_replace(
  
        $bbcode = preg_replace("/\[img\=([0-9]*)x([0-9]*)\](.*?)\[\/img\]/ism", '[img]$3[/img]', $bbcode);
  
 -      //preg_match_all("/\[url\]([$URLSearchString]*)\[\/url\]/ism", $bbcode, $urls1);
        preg_match_all("/\[url\=([$URLSearchString]*)\](.*?)\[\/url\]/ism", $bbcode, $urls);
  
        $ordered_urls = [];
        foreach ($urls[1] as $id => $url) {
 -              //$start = strpos($text, $url, $offset);
                $start = iconv_strpos($text, $url, 0, "UTF-8");
                if (!($start === false)) {
                        $ordered_urls[$start] = ["url" => $url, "title" => $urls[2][$id]];
        ksort($ordered_urls);
  
        $offset = 0;
 -      //foreach ($urls[1] AS $id=>$url) {
 +
        foreach ($ordered_urls as $url) {
                if ((substr($url["title"], 0, 7) != "http://") && (substr($url["title"], 0, 8) != "https://")
                        && !strpos($url["title"], "http://") && !strpos($url["title"], "https://")
                        }
                }
  
 -              //$start = strpos($text, $url, $offset);
                $start = iconv_strpos($text, $url["url"], $offset, "UTF-8");
                if (!($start === false)) {
                        $entities["urls"][] = ["url" => $url["url"],
                        $ordered_images[$start] = ['url' => $image, 'alt' => ''];
                }
        }
 -      //$entities["media"] = array();
 +
        $offset = 0;
  
        foreach ($ordered_images as $image) {
@@@ -3086,31 -3072,22 +3086,31 @@@ function api_format_item($item, $type 
        }
  
        if (!empty($quoted_item)) {
 -              $conv_quoted = api_convert_item($quoted_item);
 -              $quoted_status = $status;
 +              if ($quoted_item['id'] != $item['id']) {
 +                      $quoted_status = api_format_item($quoted_item);
 +                      /// @todo Only remove the attachments that are also contained in the quotes status
 +                      unset($status['attachments']);
 +                      unset($status['entities']);
 +              } else {
 +                      $conv_quoted = api_convert_item($quoted_item);
 +                      $quoted_status = $status;
 +                      unset($quoted_status['attachments']);
 +                      unset($quoted_status['entities']);
 +                      unset($quoted_status['statusnet_conversation_id']);
 +                      $quoted_status['text'] = $conv_quoted['text'];
 +                      $quoted_status['statusnet_html'] = $conv_quoted['html'];
 +                      try {
 +                              $quoted_status["user"] = api_get_user($a, $quoted_item["author-id"]);
 +                      } catch (BadRequestException $e) {
 +                              // user not found. should be found?
 +                              /// @todo check if the user should be always found
 +                              $quoted_status["user"] = [];
 +                      }
 +              }
                unset($quoted_status['friendica_author']);
                unset($quoted_status['friendica_owner']);
                unset($quoted_status['friendica_activities']);
                unset($quoted_status['friendica_private']);
 -              unset($quoted_status['statusnet_conversation_id']);
 -              $quoted_status['text'] = $conv_quoted['text'];
 -              $quoted_status['statusnet_html'] = $conv_quoted['html'];
 -              try {
 -                      $quoted_status["user"] = api_get_user($a, $quoted_item["author-id"]);
 -              } catch (BadRequestException $e) {
 -                      // user not found. should be found?
 -                      /// @todo check if the user should be always found
 -                      $quoted_status["user"] = [];
 -              }
        }
  
        if (!empty($retweeted_item)) {
@@@ -3626,6 -3603,7 +3626,7 @@@ api_register_func('api/statusnet/versio
   *
   * @param string $type Return type (atom, rss, xml, json)
   *
+  * @param int $rel A contact relationship constant
   * @return array|string|void
   * @throws BadRequestException
   * @throws ForbiddenException
   * @throws UnauthorizedException
   * @todo use api_format_data() to return data
   */
- function api_ff_ids($type)
+ function api_ff_ids($type, int $rel)
  {
        if (!api_user()) {
                throw new ForbiddenException();
  
        $stringify_ids = $_REQUEST['stringify_ids'] ?? false;
  
-       $r = q(
-               "SELECT `pcontact`.`id` FROM `contact`
-                       INNER JOIN `contact` AS `pcontact` ON `contact`.`nurl` = `pcontact`.`nurl` AND `pcontact`.`uid` = 0
-                       WHERE `contact`.`uid` = %s AND NOT `contact`.`self`",
-               intval(api_user())
+       $contacts = DBA::p("SELECT `pcontact`.`id`
+               FROM `contact`
+               INNER JOIN `contact` AS `pcontact`
+                   ON `contact`.`nurl` = `pcontact`.`nurl`
+                   AND `pcontact`.`uid` = 0
+               WHERE `contact`.`uid` = ?
+               AND NOT `contact`.`self`
+               AND `contact`.`rel` IN (?, ?)",
+               api_user(),
+               $rel,
+               Contact::FRIEND
        );
-       if (!DBA::isResult($r)) {
-               return;
-       }
  
        $ids = [];
-       foreach ($r as $rr) {
+       foreach (DBA::toArray($contacts) as $contact) {
                if ($stringify_ids) {
-                       $ids[] = $rr['id'];
+                       $ids[] = $contact['id'];
                } else {
-                       $ids[] = intval($rr['id']);
+                       $ids[] = intval($contact['id']);
                }
        }
  
-       return api_format_data("ids", $type, ['id' => $ids]);
+       return api_format_data('ids', $type, ['id' => $ids]);
  }
  
  /**
   * @return array|string
   * @throws BadRequestException
   * @throws ForbiddenException
+  * @throws ImagickException
+  * @throws InternalServerErrorException
+  * @throws UnauthorizedException
   * @see https://developer.twitter.com/en/docs/accounts-and-users/follow-search-get-users/api-reference/get-friends-ids
   */
  function api_friends_ids($type)
  {
-       return api_ff_ids($type);
+       return api_ff_ids($type, Contact::SHARING);
  }
  
  /**
   * @return array|string
   * @throws BadRequestException
   * @throws ForbiddenException
+  * @throws ImagickException
+  * @throws InternalServerErrorException
+  * @throws UnauthorizedException
   * @see https://developer.twitter.com/en/docs/accounts-and-users/follow-search-get-users/api-reference/get-followers-ids
   */
  function api_followers_ids($type)
  {
-       return api_ff_ids($type);
+       return api_ff_ids($type, Contact::FOLLOWER);
  }
  
  /// @TODO move to top of file or somewhere better
@@@ -5204,22 -5191,6 +5214,22 @@@ function api_share_as_retweet(&$item
        $reshared_item["created"] = $reshared['posted'];
        $reshared_item["edited"] = $reshared['posted'];
  
 +      // Try to fetch the original item
 +      if (!empty($reshared['guid'])) {
 +              $condition = ['guid' => $reshared['guid'], 'uid' => [0, $item['uid']]];
 +      } elseif (!empty($reshared_item['plink']) && ($original_id = Item::searchByLink($reshared_item['plink']))) {
 +              $condition = ['id' => $original_id];
 +      } else {
 +              $condition = [];
 +      }
 +
 +      if (!empty($condition)) {
 +              $original_item = Item::selectFirst([], $condition);
 +              if (DBA::isResult($original_item)) {
 +                      $reshared_item = array_merge($reshared_item, $original_item);
 +              }
 +      }
 +
        return $reshared_item;
  }