Merge pull request #12320 from annando/issue-11553a
Issue 11553: Reliably return the user's contacts
This commit is contained in:
commit
8cb7d4a9bd
5 changed files with 189 additions and 47 deletions
|
@ -136,6 +136,18 @@ class Contact
|
|||
return $contact;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $fields Array of selected fields, empty for all
|
||||
* @param array $condition Array of fields for condition
|
||||
* @param array $params Array of several parameters
|
||||
* @return array
|
||||
* @throws \Exception
|
||||
*/
|
||||
public static function selectAccountToArray(array $fields = [], array $condition = [], array $params = []): array
|
||||
{
|
||||
return DBA::selectToArray('account-user-view', $fields, $condition, $params);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $fields Array of selected fields, empty for all
|
||||
* @param array $condition Array of fields for condition
|
||||
|
@ -3154,6 +3166,8 @@ class Contact
|
|||
return;
|
||||
}
|
||||
|
||||
Worker::add(Worker::PRIORITY_LOW, 'ContactDiscoveryForUser', $contact['uid']);
|
||||
|
||||
self::clearFollowerFollowingEndpointCache($contact['uid']);
|
||||
|
||||
$cdata = self::getPublicAndUserContactID($contact['id'], $contact['uid']);
|
||||
|
@ -3177,6 +3191,8 @@ class Contact
|
|||
} else {
|
||||
self::update(['rel' => self::FOLLOWER], ['id' => $contact['id']]);
|
||||
}
|
||||
|
||||
Worker::add(Worker::PRIORITY_LOW, 'ContactDiscoveryForUser', $contact['uid']);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -68,6 +68,26 @@ class Relation
|
|||
DBA::insert('contact-relation', ['last-interaction' => $interaction_date, 'cid' => $target, 'relation-cid' => $actor], Database::INSERT_UPDATE);
|
||||
}
|
||||
|
||||
/**
|
||||
* Fetch the followers of a given user
|
||||
*
|
||||
* @param integer $uid User ID
|
||||
* @return void
|
||||
*/
|
||||
public static function discoverByUser(int $uid)
|
||||
{
|
||||
$contact = Contact::selectFirst(['id', 'url'], ['uid' => $uid, 'self' => true]);
|
||||
if (empty($contact)) {
|
||||
Logger::warning('Self contact for user not found', ['uid' => $uid]);
|
||||
return;
|
||||
}
|
||||
|
||||
$followers = self::getContacts($uid, [Contact::FOLLOWER, Contact::FRIEND]);
|
||||
$followings = self::getContacts($uid, [Contact::SHARING, Contact::FRIEND]);
|
||||
|
||||
self::updateFollowersFollowings($contact, $followers, $followings);
|
||||
}
|
||||
|
||||
/**
|
||||
* Fetches the followers of a given profile and adds them
|
||||
*
|
||||
|
@ -113,13 +133,27 @@ class Relation
|
|||
$followings = [];
|
||||
}
|
||||
|
||||
self::updateFollowersFollowings($contact, $followers, $followings);
|
||||
}
|
||||
|
||||
/**
|
||||
* Update followers and followings for the given contact
|
||||
*
|
||||
* @param array $contact
|
||||
* @param array $followers
|
||||
* @param array $followings
|
||||
* @return void
|
||||
*/
|
||||
private static function updateFollowersFollowings(array $contact, array $followers, array $followings)
|
||||
{
|
||||
if (empty($followers) && empty($followings)) {
|
||||
Contact::update(['last-discovery' => DateTimeFormat::utcNow()], ['id' => $contact['id']]);
|
||||
Logger::info('The contact does not offer discoverable data', ['id' => $contact['id'], 'url' => $url, 'network' => $contact['network']]);
|
||||
Logger::info('The contact does not offer discoverable data', ['id' => $contact['id'], 'url' => $contact['url'], 'network' => $contact['network']]);
|
||||
return;
|
||||
}
|
||||
|
||||
$target = $contact['id'];
|
||||
$url = $contact['url'];
|
||||
|
||||
if (!empty($followers)) {
|
||||
// Clear the follower list, since it will be recreated in the next step
|
||||
|
|
|
@ -24,6 +24,7 @@ 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;
|
||||
|
||||
/**
|
||||
|
@ -55,6 +56,32 @@ class Followers extends BaseApi
|
|||
'limit' => 40, // Maximum number of results to return. Defaults to 40.
|
||||
], $request);
|
||||
|
||||
if ($id == Contact::getPublicIdByUserId($uid)) {
|
||||
$params = ['order' => ['pid' => true], 'limit' => $request['limit']];
|
||||
|
||||
$condition = ['uid' => $uid, 'self' => false, 'rel' => [Contact::FOLLOWER, Contact::FRIEND]];
|
||||
|
||||
if (!empty($request['max_id'])) {
|
||||
$condition = DBA::mergeConditions($condition, ["`pid` < ?", $request['max_id']]);
|
||||
}
|
||||
|
||||
if (!empty($request['since_id'])) {
|
||||
$condition = DBA::mergeConditions($condition, ["`pid` > ?", $request['since_id']]);
|
||||
}
|
||||
|
||||
if (!empty($request['min_id'])) {
|
||||
$condition = DBA::mergeConditions($condition, ["`pid` > ?", $request['min_id']]);
|
||||
|
||||
$params['order'] = ['pid'];
|
||||
}
|
||||
|
||||
$accounts = [];
|
||||
|
||||
foreach (Contact::selectAccountToArray(['pid'], $condition, $params) as $follower) {
|
||||
self::setBoundaries($follower['pid']);
|
||||
$accounts[] = DI::mstdnAccount()->createFromContactId($follower['pid'], $uid);
|
||||
}
|
||||
} else {
|
||||
$params = ['order' => ['relation-cid' => true], 'limit' => $request['limit']];
|
||||
|
||||
$condition = ['cid' => $id, 'follows' => true];
|
||||
|
@ -70,7 +97,7 @@ class Followers extends BaseApi
|
|||
if (!empty($request['min_id'])) {
|
||||
$condition = DBA::mergeConditions($condition, ["`relation-cid` > ?", $request['min_id']]);
|
||||
|
||||
$params['order'] = ['cid'];
|
||||
$params['order'] = ['relation-cid'];
|
||||
}
|
||||
|
||||
$accounts = [];
|
||||
|
@ -81,6 +108,7 @@ class Followers extends BaseApi
|
|||
$accounts[] = DI::mstdnAccount()->createFromContactId($follower['relation-cid'], $uid);
|
||||
}
|
||||
DBA::close($followers);
|
||||
}
|
||||
|
||||
if (!empty($request['min_id'])) {
|
||||
$accounts = array_reverse($accounts);
|
||||
|
|
|
@ -24,6 +24,7 @@ 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;
|
||||
|
||||
/**
|
||||
|
@ -55,6 +56,32 @@ class Following extends BaseApi
|
|||
'limit' => 40, // Maximum number of results to return. Defaults to 40.
|
||||
], $request);
|
||||
|
||||
if ($id == Contact::getPublicIdByUserId($uid)) {
|
||||
$params = ['order' => ['pid' => true], 'limit' => $request['limit']];
|
||||
|
||||
$condition = ['uid' => $uid, 'self' => false, 'rel' => [Contact::SHARING, Contact::FRIEND]];
|
||||
|
||||
if (!empty($request['max_id'])) {
|
||||
$condition = DBA::mergeConditions($condition, ["`pid` < ?", $request['max_id']]);
|
||||
}
|
||||
|
||||
if (!empty($request['since_id'])) {
|
||||
$condition = DBA::mergeConditions($condition, ["`pid` > ?", $request['since_id']]);
|
||||
}
|
||||
|
||||
if (!empty($request['min_id'])) {
|
||||
$condition = DBA::mergeConditions($condition, ["`pid` > ?", $request['min_id']]);
|
||||
|
||||
$params['order'] = ['pid'];
|
||||
}
|
||||
|
||||
$accounts = [];
|
||||
|
||||
foreach (Contact::selectAccountToArray(['pid'], $condition, $params) as $follower) {
|
||||
self::setBoundaries($follower['pid']);
|
||||
$accounts[] = DI::mstdnAccount()->createFromContactId($follower['pid'], $uid);
|
||||
}
|
||||
} else {
|
||||
$params = ['order' => ['cid' => true], 'limit' => $request['limit']];
|
||||
|
||||
$condition = ['relation-cid' => $id, 'follows' => true];
|
||||
|
@ -81,6 +108,7 @@ class Following extends BaseApi
|
|||
$accounts[] = DI::mstdnAccount()->createFromContactId($follower['cid'], $uid);
|
||||
}
|
||||
DBA::close($followers);
|
||||
}
|
||||
|
||||
if (!empty($request['min_id'])) {
|
||||
$accounts = array_reverse($accounts);
|
||||
|
|
36
src/Worker/ContactDiscoveryForUser.php
Normal file
36
src/Worker/ContactDiscoveryForUser.php
Normal file
|
@ -0,0 +1,36 @@
|
|||
<?php
|
||||
/**
|
||||
* @copyright Copyright (C) 2010-2022, 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\Worker;
|
||||
|
||||
use Friendica\Model\Contact;
|
||||
|
||||
class ContactDiscoveryForUser
|
||||
{
|
||||
/**
|
||||
* Discover contact relations
|
||||
* @param string $url
|
||||
*/
|
||||
public static function execute(int $uid)
|
||||
{
|
||||
Contact\Relation::discoverByUser($uid);
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue