mirror of
https://github.com/friendica/friendica
synced 2025-04-22 03:10:10 +00:00
commit
23c56b108b
7 changed files with 382 additions and 755 deletions
|
@ -3,11 +3,14 @@
|
|||
namespace Friendica\Module\Api;
|
||||
|
||||
use Friendica\App\Arguments;
|
||||
use Friendica\App\BaseURL;
|
||||
use Friendica\Core\L10n;
|
||||
use Friendica\Util\Arrays;
|
||||
use Friendica\Util\DateTimeFormat;
|
||||
use Friendica\Util\HTTPInputData;
|
||||
use Friendica\Util\XML;
|
||||
use Psr\Log\LoggerInterface;
|
||||
use Friendica\Factory\Api\Twitter\User as TwitterUser;
|
||||
|
||||
/**
|
||||
* This class is used to format and return API responses
|
||||
|
@ -26,11 +29,13 @@ class ApiResponse
|
|||
* @param Arguments $args
|
||||
* @param LoggerInterface $logger
|
||||
*/
|
||||
public function __construct(L10n $l10n, Arguments $args, LoggerInterface $logger)
|
||||
public function __construct(L10n $l10n, Arguments $args, LoggerInterface $logger, BaseURL $baseurl, TwitterUser $twitteruser)
|
||||
{
|
||||
$this->l10n = $l10n;
|
||||
$this->args = $args;
|
||||
$this->logger = $logger;
|
||||
$this->l10n = $l10n;
|
||||
$this->args = $args;
|
||||
$this->logger = $logger;
|
||||
$this->baseurl = $baseurl;
|
||||
$this->twitterUser = $twitteruser;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -102,20 +107,51 @@ class ApiResponse
|
|||
return XML::fromArray($data3, $xml, false, $namespaces);
|
||||
}
|
||||
|
||||
/**
|
||||
* Set values for RSS template
|
||||
*
|
||||
* @param array $arr Array to be passed to template
|
||||
* @param int $cid Contact ID of template
|
||||
* @return array
|
||||
*/
|
||||
private function addRSSValues(array $arr, int $cid)
|
||||
{
|
||||
if (empty($cid)) {
|
||||
return $arr;
|
||||
}
|
||||
|
||||
$user_info = $this->twitterUser->createFromContactId($cid)->toArray();
|
||||
|
||||
$arr['$user'] = $user_info;
|
||||
$arr['$rss'] = [
|
||||
'alternate' => $user_info['url'],
|
||||
'self' => $this->baseurl . '/' . $this->args->getQueryString(),
|
||||
'base' => $this->baseurl,
|
||||
'updated' => DateTimeFormat::utc(null, DateTimeFormat::API),
|
||||
'atom_updated' => DateTimeFormat::utcNow(DateTimeFormat::ATOM),
|
||||
'language' => $user_info['lang'],
|
||||
'logo' => $this->baseurl . '/images/friendica-32.png',
|
||||
];
|
||||
|
||||
return $arr;
|
||||
}
|
||||
|
||||
/**
|
||||
* Formats the data according to the data type
|
||||
*
|
||||
* @param string $root_element Name of the root element
|
||||
* @param string $type Return type (atom, rss, xml, json)
|
||||
* @param array $data JSON style array
|
||||
* @param int $cid ID of the contact for RSS
|
||||
*
|
||||
* @return array|string (string|array) XML data or JSON data
|
||||
*/
|
||||
public function formatData(string $root_element, string $type, array $data)
|
||||
public function formatData(string $root_element, string $type, array $data, int $cid = 0)
|
||||
{
|
||||
switch ($type) {
|
||||
case 'atom':
|
||||
case 'rss':
|
||||
$data = $this->addRSSValues($data, $cid);
|
||||
case 'atom':
|
||||
case 'xml':
|
||||
return $this->createXML($data, $root_element);
|
||||
case 'json':
|
||||
|
|
|
@ -23,6 +23,7 @@ namespace Friendica\Module\Api\Friendica;
|
|||
|
||||
use Friendica\DI;
|
||||
use Friendica\Module\BaseApi;
|
||||
require_once __DIR__ . '/../../../../include/api.php';
|
||||
|
||||
/**
|
||||
* api/friendica
|
||||
|
|
|
@ -27,6 +27,7 @@ use Friendica\Core\System;
|
|||
use Friendica\DI;
|
||||
use Friendica\Model\Contact;
|
||||
use Friendica\Model\Post;
|
||||
use Friendica\Model\User;
|
||||
use Friendica\Network\HTTPException;
|
||||
use Friendica\Security\BasicAuth;
|
||||
use Friendica\Security\OAuth;
|
||||
|
@ -292,12 +293,23 @@ class BaseApi extends BaseModule
|
|||
}
|
||||
}
|
||||
|
||||
public static function getContactIDForSearchterm($searchterm)
|
||||
public static function getContactIDForSearchterm(string $screen_name, int $cid, int $uid)
|
||||
{
|
||||
if (intval($searchterm) == 0) {
|
||||
$cid = Contact::getIdForURL($searchterm, 0, false);
|
||||
if (!empty($cid)) {
|
||||
return $cid;
|
||||
}
|
||||
|
||||
if (strpos($screen_name, '@') !== false) {
|
||||
$cid = Contact::getIdForURL($screen_name, 0, false);
|
||||
} else {
|
||||
$cid = intval($searchterm);
|
||||
$user = User::getByNickname($screen_name, ['uid']);
|
||||
if (!empty($user['uid'])) {
|
||||
$cid = Contact::getPublicIdByUserId($user['uid']);
|
||||
}
|
||||
}
|
||||
|
||||
if (empty($cid) && ($uid != 0)) {
|
||||
$cid = Contact::getPublicIdByUserId($uid);
|
||||
}
|
||||
|
||||
return $cid;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue