Merge pull request #12392 from annando/api-relations
API: Use "contact" for own relations
This commit is contained in:
commit
0e543b2d4a
6 changed files with 236 additions and 107 deletions
|
@ -23,6 +23,7 @@ namespace Friendica\Module\Api\Twitter\Followers;
|
|||
|
||||
use Friendica\Core\System;
|
||||
use Friendica\Database\DBA;
|
||||
use Friendica\Model\Contact;
|
||||
use Friendica\Module\Api\Twitter\ContactEndpoint;
|
||||
use Friendica\Module\BaseApi;
|
||||
|
||||
|
@ -47,6 +48,34 @@ class Ids extends ContactEndpoint
|
|||
$max_id = $this->getRequestValue($request, 'max_id', 0, 0);
|
||||
$min_id = $this->getRequestValue($request, 'min_id', 0, 0);
|
||||
|
||||
if ($cid == Contact::getPublicIdByUserId($uid)) {
|
||||
$params = ['order' => ['pid' => true], 'limit' => $count];
|
||||
|
||||
$condition = ['uid' => $uid, 'self' => false, 'pending' => false, 'rel' => [Contact::FOLLOWER, Contact::FRIEND]];
|
||||
|
||||
$total_count = (int)DBA::count('contact', $condition);
|
||||
|
||||
if (!empty($max_id)) {
|
||||
$condition = DBA::mergeConditions($condition, ["`pid` < ?", $max_id]);
|
||||
}
|
||||
|
||||
if (!empty($since_id)) {
|
||||
$condition = DBA::mergeConditions($condition, ["`pid` > ?", $since_id]);
|
||||
}
|
||||
|
||||
if (!empty($min_id)) {
|
||||
$condition = DBA::mergeConditions($condition, ["`pid` > ?", $min_id]);
|
||||
|
||||
$params['order'] = ['pid'];
|
||||
}
|
||||
|
||||
$ids = [];
|
||||
|
||||
foreach (Contact::selectAccountToArray(['pid'], $condition, $params) as $follower) {
|
||||
self::setBoundaries($follower['pid']);
|
||||
$ids[] = $follower['pid'];
|
||||
}
|
||||
} else {
|
||||
$params = ['order' => ['relation-cid' => true], 'limit' => $count];
|
||||
|
||||
$condition = ['cid' => $cid, 'follows' => true];
|
||||
|
@ -75,6 +104,7 @@ class Ids extends ContactEndpoint
|
|||
$ids[] = $follower['relation-cid'];
|
||||
}
|
||||
DBA::close($followers);
|
||||
}
|
||||
|
||||
if (!empty($min_id)) {
|
||||
$ids = array_reverse($ids);
|
||||
|
|
|
@ -21,8 +21,8 @@
|
|||
|
||||
namespace Friendica\Module\Api\Twitter\Followers;
|
||||
|
||||
use Friendica\Core\System;
|
||||
use Friendica\Database\DBA;
|
||||
use Friendica\Model\Contact;
|
||||
use Friendica\Module\Api\Twitter\ContactEndpoint;
|
||||
use Friendica\Module\BaseApi;
|
||||
|
||||
|
@ -48,6 +48,34 @@ class Lists extends ContactEndpoint
|
|||
$max_id = $this->getRequestValue($request, 'max_id', 0, 0);
|
||||
$min_id = $this->getRequestValue($request, 'min_id', 0, 0);
|
||||
|
||||
if ($cid == Contact::getPublicIdByUserId($uid)) {
|
||||
$params = ['order' => ['pid' => true], 'limit' => $count];
|
||||
|
||||
$condition = ['uid' => $uid, 'self' => false, 'pending' => false, 'rel' => [Contact::FOLLOWER, Contact::FRIEND]];
|
||||
|
||||
$total_count = (int)DBA::count('contact', $condition);
|
||||
|
||||
if (!empty($max_id)) {
|
||||
$condition = DBA::mergeConditions($condition, ["`pid` < ?", $max_id]);
|
||||
}
|
||||
|
||||
if (!empty($since_id)) {
|
||||
$condition = DBA::mergeConditions($condition, ["`pid` > ?", $since_id]);
|
||||
}
|
||||
|
||||
if (!empty($min_id)) {
|
||||
$condition = DBA::mergeConditions($condition, ["`pid` > ?", $min_id]);
|
||||
|
||||
$params['order'] = ['pid'];
|
||||
}
|
||||
|
||||
$ids = [];
|
||||
|
||||
foreach (Contact::selectAccountToArray(['pid'], $condition, $params) as $follower) {
|
||||
self::setBoundaries($follower['pid']);
|
||||
$ids[] = $follower['pid'];
|
||||
}
|
||||
} else {
|
||||
$params = ['order' => ['relation-cid' => true], 'limit' => $count];
|
||||
|
||||
$condition = ['cid' => $cid, 'follows' => true];
|
||||
|
@ -76,6 +104,7 @@ class Lists extends ContactEndpoint
|
|||
$ids[] = $follower['relation-cid'];
|
||||
}
|
||||
DBA::close($followers);
|
||||
}
|
||||
|
||||
if (!empty($min_id)) {
|
||||
$ids = array_reverse($ids);
|
||||
|
@ -83,7 +112,7 @@ class Lists extends ContactEndpoint
|
|||
|
||||
$return = self::list($ids, $total_count, $uid, $cursor, $count, $skip_status, $include_user_entities);
|
||||
|
||||
self::setLinkHeader();
|
||||
$this->response->setHeader(self::getLinkHeader());
|
||||
|
||||
$this->response->exit('lists', ['lists' => $return]);
|
||||
}
|
||||
|
|
|
@ -23,6 +23,7 @@ namespace Friendica\Module\Api\Twitter\Friends;
|
|||
|
||||
use Friendica\Core\System;
|
||||
use Friendica\Database\DBA;
|
||||
use Friendica\Model\Contact;
|
||||
use Friendica\Module\Api\Twitter\ContactEndpoint;
|
||||
use Friendica\Module\BaseApi;
|
||||
|
||||
|
@ -47,6 +48,34 @@ class Ids extends ContactEndpoint
|
|||
$max_id = $this->getRequestValue($request, 'max_id', 0, 0);
|
||||
$min_id = $this->getRequestValue($request, 'min_id', 0, 0);
|
||||
|
||||
if ($cid == Contact::getPublicIdByUserId($uid)) {
|
||||
$params = ['order' => ['pid' => true], 'limit' => $count];
|
||||
|
||||
$condition = ['uid' => $uid, 'self' => false, 'pending' => false, 'rel' => [Contact::SHARING, Contact::FRIEND]];
|
||||
|
||||
$total_count = (int)DBA::count('contact', $condition);
|
||||
|
||||
if (!empty($max_id)) {
|
||||
$condition = DBA::mergeConditions($condition, ["`pid` < ?", $max_id]);
|
||||
}
|
||||
|
||||
if (!empty($since_id)) {
|
||||
$condition = DBA::mergeConditions($condition, ["`pid` > ?", $since_id]);
|
||||
}
|
||||
|
||||
if (!empty($min_id)) {
|
||||
$condition = DBA::mergeConditions($condition, ["`pid` > ?", $min_id]);
|
||||
|
||||
$params['order'] = ['pid'];
|
||||
}
|
||||
|
||||
$ids = [];
|
||||
|
||||
foreach (Contact::selectAccountToArray(['pid'], $condition, $params) as $follower) {
|
||||
self::setBoundaries($follower['pid']);
|
||||
$ids[] = $follower['pid'];
|
||||
}
|
||||
} else {
|
||||
$params = ['order' => ['cid' => true], 'limit' => $count];
|
||||
|
||||
$condition = ['relation-cid' => $cid, 'follows' => true];
|
||||
|
@ -75,6 +104,7 @@ class Ids extends ContactEndpoint
|
|||
$ids[] = $follower['cid'];
|
||||
}
|
||||
DBA::close($followers);
|
||||
}
|
||||
|
||||
if (!empty($min_id)) {
|
||||
$ids = array_reverse($ids);
|
||||
|
|
|
@ -21,8 +21,8 @@
|
|||
|
||||
namespace Friendica\Module\Api\Twitter\Friends;
|
||||
|
||||
use Friendica\Core\System;
|
||||
use Friendica\Database\DBA;
|
||||
use Friendica\Model\Contact;
|
||||
use Friendica\Module\Api\Twitter\ContactEndpoint;
|
||||
use Friendica\Module\BaseApi;
|
||||
|
||||
|
@ -48,6 +48,34 @@ class Lists extends ContactEndpoint
|
|||
$max_id = $this->getRequestValue($request, 'max_id', 0, 0);
|
||||
$min_id = $this->getRequestValue($request, 'min_id', 0, 0);
|
||||
|
||||
if ($cid == Contact::getPublicIdByUserId($uid)) {
|
||||
$params = ['order' => ['pid' => true], 'limit' => $count];
|
||||
|
||||
$condition = ['uid' => $uid, 'self' => false, 'pending' => false, 'rel' => [Contact::SHARING, Contact::FRIEND]];
|
||||
|
||||
$total_count = (int)DBA::count('contact', $condition);
|
||||
|
||||
if (!empty($max_id)) {
|
||||
$condition = DBA::mergeConditions($condition, ["`pid` < ?", $max_id]);
|
||||
}
|
||||
|
||||
if (!empty($since_id)) {
|
||||
$condition = DBA::mergeConditions($condition, ["`pid` > ?", $since_id]);
|
||||
}
|
||||
|
||||
if (!empty($min_id)) {
|
||||
$condition = DBA::mergeConditions($condition, ["`pid` > ?", $min_id]);
|
||||
|
||||
$params['order'] = ['pid'];
|
||||
}
|
||||
|
||||
$ids = [];
|
||||
|
||||
foreach (Contact::selectAccountToArray(['pid'], $condition, $params) as $follower) {
|
||||
self::setBoundaries($follower['pid']);
|
||||
$ids[] = $follower['pid'];
|
||||
}
|
||||
} else {
|
||||
$params = ['order' => ['cid' => true], 'limit' => $count];
|
||||
|
||||
$condition = ['relation-cid' => $cid, 'follows' => true];
|
||||
|
@ -76,6 +104,7 @@ class Lists extends ContactEndpoint
|
|||
$ids[] = $follower['cid'];
|
||||
}
|
||||
DBA::close($followers);
|
||||
}
|
||||
|
||||
if (!empty($min_id)) {
|
||||
$ids = array_reverse($ids);
|
||||
|
@ -83,7 +112,7 @@ class Lists extends ContactEndpoint
|
|||
|
||||
$return = self::list($ids, $total_count, $uid, $cursor, $count, $skip_status, $include_user_entities);
|
||||
|
||||
self::setLinkHeader();
|
||||
$this->response->setHeader(self::getLinkHeader());
|
||||
|
||||
$this->response->exit('lists', ['lists' => $return]);
|
||||
}
|
||||
|
|
|
@ -21,7 +21,6 @@
|
|||
|
||||
namespace Friendica\Module\Api\Twitter\Friendships;
|
||||
|
||||
use Friendica\Core\System;
|
||||
use Friendica\Database\DBA;
|
||||
use Friendica\Module\Api\Twitter\ContactEndpoint;
|
||||
use Friendica\Module\BaseApi;
|
||||
|
@ -81,7 +80,7 @@ class Incoming extends ContactEndpoint
|
|||
|
||||
$return = self::ids($ids, $total_count, $cursor, $count, $stringify_ids);
|
||||
|
||||
self::setLinkHeader();
|
||||
$this->response->setHeader(self::getLinkHeader());
|
||||
|
||||
$this->response->exit('incoming', ['incoming' => $return]);
|
||||
}
|
||||
|
|
|
@ -139,13 +139,13 @@ class BaseApi extends BaseModule
|
|||
}
|
||||
|
||||
/**
|
||||
* Set the "link" header with "next" and "prev" links
|
||||
* @return void
|
||||
* Get the "link" header with "next" and "prev" links
|
||||
* @return string
|
||||
*/
|
||||
protected static function setLinkHeader()
|
||||
protected static function getLinkHeader(): string
|
||||
{
|
||||
if (empty(self::$boundaries)) {
|
||||
return;
|
||||
return '';
|
||||
}
|
||||
|
||||
$request = self::$request;
|
||||
|
@ -164,7 +164,19 @@ class BaseApi extends BaseModule
|
|||
$prev = $command . '?' . http_build_query($prev_request);
|
||||
$next = $command . '?' . http_build_query($next_request);
|
||||
|
||||
header('Link: <' . $next . '>; rel="next", <' . $prev . '>; rel="prev"');
|
||||
return 'Link: <' . $next . '>; rel="next", <' . $prev . '>; rel="prev"';
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the "link" header with "next" and "prev" links
|
||||
* @return void
|
||||
*/
|
||||
protected static function setLinkHeader()
|
||||
{
|
||||
$header = self::getLinkHeader();
|
||||
if (!empty($header)) {
|
||||
header($header);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
Loading…
Reference in a new issue