API: added account related endpoints

This commit is contained in:
Michael 2021-05-09 09:35:51 +00:00
parent 6511493fdf
commit 5bb5c44bd9
16 changed files with 599 additions and 13 deletions

View file

@ -81,6 +81,10 @@ class Followers extends BaseApi
}
DBA::close($followers);
if (!empty($min_id)) {
array_reverse($accounts);
}
System::jsonExit($accounts);
}
}

View file

@ -81,6 +81,10 @@ class Following extends BaseApi
}
DBA::close($followers);
if (!empty($min_id)) {
array_reverse($accounts);
}
System::jsonExit($accounts);
}
}

View file

@ -0,0 +1,66 @@
<?php
/**
* @copyright Copyright (C) 2010-2021, the Friendica project
*
* @license GNU AGPL version 3 or any later version
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
*/
namespace Friendica\Module\Api\Mastodon\Accounts;
use Friendica\Core\System;
use Friendica\Database\DBA;
use Friendica\DI;
use Friendica\Model\Contact;
use Friendica\Module\BaseApi;
/**
* @see https://docs.joinmastodon.org/methods/accounts/
*/
class Lists extends BaseApi
{
/**
* @param array $parameters
* @throws \Friendica\Network\HTTPException\InternalServerErrorException
*/
public static function rawContent(array $parameters = [])
{
self::login();
$uid = self::getCurrentUserID();
if (empty($parameters['id'])) {
DI::mstdnError()->RecordNotFound();
}
$id = $parameters['id'];
if (!DBA::exists('contact', ['id' => $id, 'uid' => 0])) {
DI::mstdnError()->RecordNotFound();
}
$lists = [];
$cdata = Contact::getPublicAndUserContacID($id, $uid);
if (!empty($cdata['user'])) {
$groups = DBA::select('group_member', ['gid'], ['contact-id' => $cdata['user']]);
while ($group = DBA::fetch($groups)) {
$lists[] = DI::mstdnList()->create($group['gid']);
}
DBA::close($groups);
}
System::jsonExit($lists);
}
}

View file

@ -0,0 +1,98 @@
<?php
/**
* @copyright Copyright (C) 2010-2021, the Friendica project
*
* @license GNU AGPL version 3 or any later version
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
*/
namespace Friendica\Module\Api\Mastodon\Accounts;
use Friendica\Core\Search as CoreSearch;
use Friendica\Core\System;
use Friendica\Database\DBA;
use Friendica\DI;
use Friendica\Model\Contact;
use Friendica\Module\BaseApi;
use Friendica\Object\Search\ContactResult;
/**
* @see https://docs.joinmastodon.org/methods/accounts/
*/
class Search extends BaseApi
{
/**
* @param array $parameters
* @throws \Friendica\Network\HTTPException\InternalServerErrorException
*/
public static function rawContent(array $parameters = [])
{
self::login();
$uid = self::getCurrentUserID();
// What to search for
$q = (int)!isset($_REQUEST['q']) ? 0 : $_REQUEST['q'];
// Maximum number of results. Defaults to 40.
$limit = (int)!isset($_REQUEST['limit']) ? 40 : $_REQUEST['limit'];
// Attempt WebFinger lookup. Defaults to false. Use this when q is an exact address.
$resolve = (int)!isset($_REQUEST['resolve']) ? 0 : $_REQUEST['resolve'];
// Only who the user is following. Defaults to false.
$following = (int)!isset($_REQUEST['following']) ? 0 : $_REQUEST['following'];
$accounts = [];
if (!$following) {
if ((strrpos($q, '@') > 0) && $resolve) {
$results = CoreSearch::getContactsFromProbe($q);
}
if (empty($results)) {
if (DI::config()->get('system', 'poco_local_search')) {
$results = CoreSearch::getContactsFromLocalDirectory($q, CoreSearch::TYPE_ALL, 0, $limit);
} elseif (!empty(DI::config()->get('system', 'directory'))) {
$results = CoreSearch::getContactsFromGlobalDirectory($q, CoreSearch::TYPE_ALL, 1);
}
}
if (!empty($results)) {
$counter = 0;
foreach ($results->getResults() as $result) {
if (++$counter > $limit) {
continue;
}
if ($result instanceof ContactResult) {
$id = Contact::getIdForURL($result->getUrl(), 0, false);
$accounts[] = DI::mstdnAccount()->createFromContactId($id, $uid);
}
}
}
} else {
$contacts = Contact::searchByName($q, '', $uid);
$counter = 0;
foreach ($contacts as $contact) {
if (!in_array($contact['rel'], [Contact::SHARING, Contact::FRIEND])) {
continue;
}
if (++$counter > $limit) {
continue;
}
$accounts[] = DI::mstdnAccount()->createFromContactId($contact['id'], $uid);
}
DBA::close($contacts);
}
System::jsonExit($accounts);
}
}

View file

@ -80,6 +80,10 @@ class Blocks extends BaseApi
}
DBA::close($followers);
if (!empty($min_id)) {
array_reverse($accounts);
}
System::jsonExit($accounts);
}
}

View file

@ -0,0 +1,76 @@
<?php
/**
* @copyright Copyright (C) 2010-2021, the Friendica project
*
* @license GNU AGPL version 3 or any later version
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
*/
namespace Friendica\Module\Api\Mastodon;
use Friendica\Core\System;
use Friendica\Database\DBA;
use Friendica\DI;
use Friendica\Module\BaseApi;
/**
* @see https://docs.joinmastodon.org/methods/timelines/lists/
*/
class Lists extends BaseApi
{
public static function delete(array $parameters = [])
{
self::unsupported('delete');
}
public static function post(array $parameters = [])
{
self::unsupported('post');
}
public static function put(array $parameters = [])
{
self::unsupported('put');
}
/**
* @param array $parameters
* @throws \Friendica\Network\HTTPException\InternalServerErrorException
*/
public static function rawContent(array $parameters = [])
{
self::login();
$uid = self::getCurrentUserID();
if (empty($parameters['id'])) {
$lists = [];
$groups = DBA::select('group', ['id'], ['uid' => $uid, 'deleted' => false]);
while ($group = DBA::fetch($groups)) {
$lists[] = DI::mstdnList()->create($group['id']);
}
DBA::close($groups);
} else {
$id = $parameters['id'];
if (!DBA::exists('group',['uid' => $uid, 'deleted' => false])) {
DI::mstdnError()->RecordNotFound();
}
$lists = DI::mstdnList()->create($id);
}
System::jsonExit($lists);
}
}

View file

@ -0,0 +1,102 @@
<?php
/**
* @copyright Copyright (C) 2010-2021, the Friendica project
*
* @license GNU AGPL version 3 or any later version
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
*/
namespace Friendica\Module\Api\Mastodon\Lists;
use Friendica\Core\System;
use Friendica\Database\DBA;
use Friendica\DI;
use Friendica\Module\BaseApi;
/**
* @see https://docs.joinmastodon.org/methods/timelines/lists/
*
* Currently the output will be unordered since we use public contact ids in the api and not user contact ids.
*/
class Accounts extends BaseApi
{
public static function delete(array $parameters = [])
{
self::unsupported('delete');
}
public static function post(array $parameters = [])
{
self::unsupported('post');
}
/**
* @param array $parameters
* @throws \Friendica\Network\HTTPException\InternalServerErrorException
*/
public static function rawContent(array $parameters = [])
{
self::login();
$uid = self::getCurrentUserID();
if (empty($parameters['id'])) {
DI::mstdnError()->RecordNotFound();
}
$id = $parameters['id'];
if (!DBA::exists('group', ['id' => $id, 'uid' => $uid])) {
DI::mstdnError()->RecordNotFound();
}
// Return results older than this id
$max_id = (int)!isset($_REQUEST['max_id']) ? 0 : $_REQUEST['max_id'];
// Return results newer than this id
$since_id = (int)!isset($_REQUEST['since_id']) ? 0 : $_REQUEST['since_id'];
// Maximum number of results to return. Defaults to 20.
$limit = (int)!isset($_REQUEST['limit']) ? 40 : $_REQUEST['limit'];
$params = ['order' => ['contact-id' => true], 'limit' => $limit];
$condition = ['gid' => $id];
if (!empty($max_id)) {
$condition = DBA::mergeConditions($condition, ["`contact-id` < ?", $max_id]);
}
if (!empty($since_id)) {
$condition = DBA::mergeConditions($condition, ["`contact-id` > ?", $since_id]);
}
if (!empty($min_id)) {
$condition = DBA::mergeConditions($condition, ["`contact-id` > ?", $min_id]);
$params['order'] = ['contact-id'];
}
$members = DBA::select('group_member', ['contact-id'], $condition, $params);
while ($member = DBA::fetch($members)) {
$accounts[] = DI::mstdnAccount()->createFromContactId($member['contact-id'], $uid);
}
DBA::close($members);
if (!empty($min_id)) {
array_reverse($accounts);
}
System::jsonExit($accounts);
}
}

View file

@ -80,6 +80,10 @@ class Mutes extends BaseApi
}
DBA::close($followers);
if (!empty($min_id)) {
array_reverse($accounts);
}
System::jsonExit($accounts);
}
}

View file

@ -0,0 +1,61 @@
<?php
/**
* @copyright Copyright (C) 2010-2021, the Friendica project
*
* @license GNU AGPL version 3 or any later version
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
*/
namespace Friendica\Module\Api\Mastodon;
use Friendica\Core\System;
use Friendica\DI;
use Friendica\Model\User;
use Friendica\Module\BaseApi;
/**
* @see https://docs.joinmastodon.org/methods/accounts/preferences/
*/
class Preferences extends BaseApi
{
/**
* @param array $parameters
* @throws \Friendica\Network\HTTPException\InternalServerErrorException
*/
public static function rawContent(array $parameters = [])
{
self::login();
$uid = self::getCurrentUserID();
$user = User::getById($uid, ['language', 'allow_cid', 'allow_gid', 'deny_cid', 'deny_gid']);
if (!empty($user['allow_cid']) || !empty($user['allow_gid']) || !empty($user['deny_cid']) || !empty($user['deny_gid'])) {
$visibility = 'private';
} elseif (DI::pConfig()->get($uid, 'system', 'unlisted')) {
$visibility = 'unlisted';
} else {
$visibility = 'public';
}
$sensitive = false;
$language = $user['language'];
$media = DI::pConfig()->get($uid, 'nsfw', 'disable') ? 'show_all' : 'default';
$spoilers = DI::pConfig()->get($uid, 'system', 'disable_cw');
$preferences = new \Friendica\Object\Api\Mastodon\Preferences($visibility, $sensitive, $language, $media, $spoilers);
System::jsonExit($preferences);
}
}