Merge pull request #12320 from annando/issue-11553a

Issue 11553: Reliably return the user's contacts
This commit is contained in:
Hypolite Petovan 2022-12-03 12:20:34 -05:00 committed by GitHub
commit 8cb7d4a9bd
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 189 additions and 47 deletions

View file

@ -136,6 +136,18 @@ class Contact
return $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 $fields Array of selected fields, empty for all
* @param array $condition Array of fields for condition * @param array $condition Array of fields for condition
@ -3154,6 +3166,8 @@ class Contact
return; return;
} }
Worker::add(Worker::PRIORITY_LOW, 'ContactDiscoveryForUser', $contact['uid']);
self::clearFollowerFollowingEndpointCache($contact['uid']); self::clearFollowerFollowingEndpointCache($contact['uid']);
$cdata = self::getPublicAndUserContactID($contact['id'], $contact['uid']); $cdata = self::getPublicAndUserContactID($contact['id'], $contact['uid']);
@ -3177,6 +3191,8 @@ class Contact
} else { } else {
self::update(['rel' => self::FOLLOWER], ['id' => $contact['id']]); self::update(['rel' => self::FOLLOWER], ['id' => $contact['id']]);
} }
Worker::add(Worker::PRIORITY_LOW, 'ContactDiscoveryForUser', $contact['uid']);
} }
/** /**

View file

@ -68,6 +68,26 @@ class Relation
DBA::insert('contact-relation', ['last-interaction' => $interaction_date, 'cid' => $target, 'relation-cid' => $actor], Database::INSERT_UPDATE); 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 * Fetches the followers of a given profile and adds them
* *
@ -113,13 +133,27 @@ class Relation
$followings = []; $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)) { if (empty($followers) && empty($followings)) {
Contact::update(['last-discovery' => DateTimeFormat::utcNow()], ['id' => $contact['id']]); 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; return;
} }
$target = $contact['id']; $target = $contact['id'];
$url = $contact['url'];
if (!empty($followers)) { if (!empty($followers)) {
// Clear the follower list, since it will be recreated in the next step // Clear the follower list, since it will be recreated in the next step

View file

@ -24,6 +24,7 @@ namespace Friendica\Module\Api\Mastodon\Accounts;
use Friendica\Core\System; use Friendica\Core\System;
use Friendica\Database\DBA; use Friendica\Database\DBA;
use Friendica\DI; use Friendica\DI;
use Friendica\Model\Contact;
use Friendica\Module\BaseApi; use Friendica\Module\BaseApi;
/** /**
@ -55,33 +56,60 @@ class Followers extends BaseApi
'limit' => 40, // Maximum number of results to return. Defaults to 40. 'limit' => 40, // Maximum number of results to return. Defaults to 40.
], $request); ], $request);
$params = ['order' => ['relation-cid' => true], 'limit' => $request['limit']]; if ($id == Contact::getPublicIdByUserId($uid)) {
$params = ['order' => ['pid' => true], 'limit' => $request['limit']];
$condition = ['cid' => $id, 'follows' => true]; $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']];
if (!empty($request['max_id'])) { $condition = ['cid' => $id, 'follows' => true];
$condition = DBA::mergeConditions($condition, ["`relation-cid` < ?", $request['max_id']]);
if (!empty($request['max_id'])) {
$condition = DBA::mergeConditions($condition, ["`relation-cid` < ?", $request['max_id']]);
}
if (!empty($request['since_id'])) {
$condition = DBA::mergeConditions($condition, ["`relation-cid` > ?", $request['since_id']]);
}
if (!empty($request['min_id'])) {
$condition = DBA::mergeConditions($condition, ["`relation-cid` > ?", $request['min_id']]);
$params['order'] = ['relation-cid'];
}
$accounts = [];
$followers = DBA::select('contact-relation', ['relation-cid'], $condition, $params);
while ($follower = DBA::fetch($followers)) {
self::setBoundaries($follower['relation-cid']);
$accounts[] = DI::mstdnAccount()->createFromContactId($follower['relation-cid'], $uid);
}
DBA::close($followers);
} }
if (!empty($request['since_id'])) {
$condition = DBA::mergeConditions($condition, ["`relation-cid` > ?", $request['since_id']]);
}
if (!empty($request['min_id'])) {
$condition = DBA::mergeConditions($condition, ["`relation-cid` > ?", $request['min_id']]);
$params['order'] = ['cid'];
}
$accounts = [];
$followers = DBA::select('contact-relation', ['relation-cid'], $condition, $params);
while ($follower = DBA::fetch($followers)) {
self::setBoundaries($follower['relation-cid']);
$accounts[] = DI::mstdnAccount()->createFromContactId($follower['relation-cid'], $uid);
}
DBA::close($followers);
if (!empty($request['min_id'])) { if (!empty($request['min_id'])) {
$accounts = array_reverse($accounts); $accounts = array_reverse($accounts);
} }

View file

@ -24,6 +24,7 @@ namespace Friendica\Module\Api\Mastodon\Accounts;
use Friendica\Core\System; use Friendica\Core\System;
use Friendica\Database\DBA; use Friendica\Database\DBA;
use Friendica\DI; use Friendica\DI;
use Friendica\Model\Contact;
use Friendica\Module\BaseApi; use Friendica\Module\BaseApi;
/** /**
@ -55,33 +56,60 @@ class Following extends BaseApi
'limit' => 40, // Maximum number of results to return. Defaults to 40. 'limit' => 40, // Maximum number of results to return. Defaults to 40.
], $request); ], $request);
$params = ['order' => ['cid' => true], 'limit' => $request['limit']]; if ($id == Contact::getPublicIdByUserId($uid)) {
$params = ['order' => ['pid' => true], 'limit' => $request['limit']];
$condition = ['relation-cid' => $id, 'follows' => true]; $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']];
if (!empty($request['max_id'])) { $condition = ['relation-cid' => $id, 'follows' => true];
$condition = DBA::mergeConditions($condition, ["`cid` < ?", $request['max_id']]);
if (!empty($request['max_id'])) {
$condition = DBA::mergeConditions($condition, ["`cid` < ?", $request['max_id']]);
}
if (!empty($request['since_id'])) {
$condition = DBA::mergeConditions($condition, ["`cid` > ?", $request['since_id']]);
}
if (!empty($request['min_id'])) {
$condition = DBA::mergeConditions($condition, ["`cid` > ?", $request['min_id']]);
$params['order'] = ['cid'];
}
$accounts = [];
$followers = DBA::select('contact-relation', ['cid'], $condition, $params);
while ($follower = DBA::fetch($followers)) {
self::setBoundaries($follower['cid']);
$accounts[] = DI::mstdnAccount()->createFromContactId($follower['cid'], $uid);
}
DBA::close($followers);
} }
if (!empty($request['since_id'])) {
$condition = DBA::mergeConditions($condition, ["`cid` > ?", $request['since_id']]);
}
if (!empty($request['min_id'])) {
$condition = DBA::mergeConditions($condition, ["`cid` > ?", $request['min_id']]);
$params['order'] = ['cid'];
}
$accounts = [];
$followers = DBA::select('contact-relation', ['cid'], $condition, $params);
while ($follower = DBA::fetch($followers)) {
self::setBoundaries($follower['cid']);
$accounts[] = DI::mstdnAccount()->createFromContactId($follower['cid'], $uid);
}
DBA::close($followers);
if (!empty($request['min_id'])) { if (!empty($request['min_id'])) {
$accounts = array_reverse($accounts); $accounts = array_reverse($accounts);
} }

View 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);
}
}