Fetch followers/followings of contacts
This commit is contained in:
parent
eaf159fc20
commit
18617f6c48
10 changed files with 219 additions and 208 deletions
|
@ -1418,7 +1418,6 @@ class Contact
|
|||
'poll' => $data['poll'] ?? '',
|
||||
'name' => $data['name'] ?? '',
|
||||
'nick' => $data['nick'] ?? '',
|
||||
'photo' => $data['photo'] ?? '',
|
||||
'keywords' => $data['keywords'] ?? '',
|
||||
'location' => $data['location'] ?? '',
|
||||
'about' => $data['about'] ?? '',
|
||||
|
@ -1474,14 +1473,6 @@ class Contact
|
|||
} else {
|
||||
// Else do a direct update
|
||||
self::updateFromProbe($contact_id, '', false);
|
||||
|
||||
// Update the gcontact entry
|
||||
if ($uid == 0) {
|
||||
GContact::updateFromPublicContactID($contact_id);
|
||||
if (($data['network'] == Protocol::ACTIVITYPUB) && in_array(DI::config()->get('system', 'gcontact_discovery'), [GContact::DISCOVERY_DIRECT, GContact::DISCOVERY_RECURSIVE])) {
|
||||
GContact::discoverFollowers($data['url']);
|
||||
}
|
||||
}
|
||||
}
|
||||
} else {
|
||||
$fields = ['url', 'nurl', 'addr', 'alias', 'name', 'nick', 'keywords', 'location', 'about', 'avatar-date', 'baseurl', 'gsid'];
|
||||
|
@ -1818,9 +1809,11 @@ class Contact
|
|||
$uid = $contact['uid'];
|
||||
|
||||
// Only update the cached photo links of public contacts when they already are cached
|
||||
if (($uid == 0) && !$force && empty($contact['photo']) && empty($contact['thumb']) && empty($contact['micro'])) {
|
||||
DBA::update('contact', ['avatar' => $avatar], ['id' => $cid]);
|
||||
Logger::info('Only update the avatar', ['id' => $cid, 'avatar' => $avatar, 'contact' => $contact]);
|
||||
if (($uid == 0) && !$force && empty($contact['thumb']) && empty($contact['micro'])) {
|
||||
if ($contact['avatar'] != $avatar) {
|
||||
DBA::update('contact', ['avatar' => $avatar], ['id' => $cid]);
|
||||
Logger::info('Only update the avatar', ['id' => $cid, 'avatar' => $avatar, 'contact' => $contact]);
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
|
@ -1830,28 +1823,27 @@ class Contact
|
|||
$contact['micro'] ?? '',
|
||||
];
|
||||
|
||||
foreach ($data as $image_uri) {
|
||||
$image_rid = Photo::ridFromURI($image_uri);
|
||||
if ($image_rid && !Photo::exists(['resource-id' => $image_rid, 'uid' => $uid])) {
|
||||
Logger::info('Regenerating avatar', ['contact uid' => $uid, 'cid' => $cid, 'missing photo' => $image_rid, 'avatar' => $contact['avatar']]);
|
||||
$force = true;
|
||||
$update = ($contact['avatar'] != $avatar) || $force;
|
||||
|
||||
if (!$update) {
|
||||
foreach ($data as $image_uri) {
|
||||
$image_rid = Photo::ridFromURI($image_uri);
|
||||
if ($image_rid && !Photo::exists(['resource-id' => $image_rid, 'uid' => $uid])) {
|
||||
Logger::info('Regenerating avatar', ['contact uid' => $uid, 'cid' => $cid, 'missing photo' => $image_rid, 'avatar' => $contact['avatar']]);
|
||||
$update = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (($contact["avatar"] != $avatar) || $force) {
|
||||
if ($update) {
|
||||
$photos = Photo::importProfilePhoto($avatar, $uid, $cid, true);
|
||||
|
||||
if ($photos) {
|
||||
$fields = ['avatar' => $avatar, 'photo' => $photos[0], 'thumb' => $photos[1], 'micro' => $photos[2], 'avatar-date' => DateTimeFormat::utcNow()];
|
||||
DBA::update('contact', $fields, ['id' => $cid]);
|
||||
|
||||
// Update the public contact (contact id = 0)
|
||||
if ($uid != 0) {
|
||||
$pcontact = DBA::selectFirst('contact', ['id'], ['nurl' => $contact['nurl'], 'uid' => 0]);
|
||||
if (DBA::isResult($pcontact)) {
|
||||
DBA::update('contact', $fields, ['id' => $pcontact['id']]);
|
||||
}
|
||||
}
|
||||
} elseif (empty($contact['avatar'])) {
|
||||
// Ensure that the avatar field is set
|
||||
DBA::update('contact', ['avatar' => $avatar], ['id' => $cid]);
|
||||
Logger::info('Failed profile import', ['id' => $cid, 'force' => $force, 'avatar' => $avatar, 'contact' => $contact]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -2035,6 +2027,13 @@ class Contact
|
|||
|
||||
$new_pubkey = $ret['pubkey'];
|
||||
|
||||
// Update the gcontact entry
|
||||
if ($uid == 0) {
|
||||
GContact::updateFromPublicContactID($id);
|
||||
}
|
||||
|
||||
ContactRelation::discoverByUrl($ret['url']);
|
||||
|
||||
$update = false;
|
||||
|
||||
// make sure to not overwrite existing values with blank entries except some technical fields
|
||||
|
@ -2508,7 +2507,6 @@ class Contact
|
|||
'nurl' => Strings::normaliseLink($url),
|
||||
'name' => $name,
|
||||
'nick' => $nick,
|
||||
'photo' => $photo,
|
||||
'network' => $network,
|
||||
'rel' => self::FOLLOWER,
|
||||
'blocked' => 0,
|
||||
|
|
163
src/Model/ContactRelation.php
Normal file
163
src/Model/ContactRelation.php
Normal file
|
@ -0,0 +1,163 @@
|
|||
<?php
|
||||
/**
|
||||
* @copyright Copyright (C) 2020, Friendica
|
||||
*
|
||||
* @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\Model;
|
||||
|
||||
use Friendica\Core\Logger;
|
||||
use Friendica\Database\DBA;
|
||||
use Friendica\DI;
|
||||
use Friendica\Protocol\ActivityPub;
|
||||
use Friendica\Util\DateTimeFormat;
|
||||
use Friendica\Util\Strings;
|
||||
|
||||
class ContactRelation
|
||||
{
|
||||
/**
|
||||
* No discovery of followers/followings
|
||||
*/
|
||||
const DISCOVERY_NONE = 0;
|
||||
/**
|
||||
* Discover followers/followings of local contacts
|
||||
*/
|
||||
const DISCOVERY_LOCAL = 1;
|
||||
/**
|
||||
* Discover followers/followings of local contacts and contacts that visibly interacted on the system
|
||||
*/
|
||||
const DISCOVERY_INTERACTOR = 2;
|
||||
/**
|
||||
* Discover followers/followings of all contacts
|
||||
*/
|
||||
const DISCOVERY_ALL = 3;
|
||||
|
||||
public static function store(int $target, int $actor, string $interaction_date)
|
||||
{
|
||||
if ($actor == $target) {
|
||||
return;
|
||||
}
|
||||
|
||||
DBA::update('contact-relation', ['last-interaction' => $interaction_date], ['cid' => $target, 'relation-cid' => $actor], true);
|
||||
}
|
||||
|
||||
/**
|
||||
* Fetches the followers of a given profile and adds them
|
||||
*
|
||||
* @param string $url URL of a profile
|
||||
* @return void
|
||||
*/
|
||||
public static function discoverByUrl(string $url)
|
||||
{
|
||||
$contact_discovery = DI::config()->get('system', 'contact_discovery');
|
||||
|
||||
if ($contact_discovery == self::DISCOVERY_NONE) {
|
||||
return;
|
||||
}
|
||||
|
||||
$contact = Contact::getByURL($url);
|
||||
if (empty($contact)) {
|
||||
return;
|
||||
}
|
||||
|
||||
if ($contact['last-discovery'] > DateTimeFormat::utc('now - 1 month')) {
|
||||
Logger::info('Last discovery was less then a month before.', ['id' => $contact['id'], 'url' => $url, 'discovery' => $contact['last-discovery']]);
|
||||
return;
|
||||
}
|
||||
|
||||
if ($contact_discovery != self::DISCOVERY_ALL) {
|
||||
$local = DBA::exists('contact', ["`nurl` = ? AND `uid` != ?", Strings::normaliseLink($url), 0]);
|
||||
if (($contact_discovery == self::DISCOVERY_LOCAL) && !$local) {
|
||||
Logger::info('No discovery - This contact is not followed/following locally.', ['id' => $contact['id'], 'url' => $url]);
|
||||
return;
|
||||
}
|
||||
|
||||
if ($contact_discovery == self::DISCOVERY_INTERACTOR) {
|
||||
$interactor = DBA::exists('contact-relation', ["`relation-cid` = ? AND `last-interaction` > ?", $contact['id'], DBA::NULL_DATETIME]);
|
||||
if (!$local && !$interactor) {
|
||||
Logger::info('No discovery - This contact is not interacting locally.', ['id' => $contact['id'], 'url' => $url]);
|
||||
return;
|
||||
}
|
||||
}
|
||||
} elseif ($contact['created'] > DateTimeFormat::utc('now - 1 day')) {
|
||||
Logger::info('Newly created contacs are not discovered to avoid DDoS attacks.', ['id' => $contact['id'], 'url' => $url, 'discovery' => $contact['created']]);
|
||||
return;
|
||||
}
|
||||
|
||||
$apcontact = APContact::getByURL($url);
|
||||
|
||||
if (!empty($apcontact['followers']) && is_string($apcontact['followers'])) {
|
||||
$followers = ActivityPub::fetchItems($apcontact['followers']);
|
||||
} else {
|
||||
$followers = [];
|
||||
}
|
||||
|
||||
if (!empty($apcontact['following']) && is_string($apcontact['following'])) {
|
||||
$followings = ActivityPub::fetchItems($apcontact['following']);
|
||||
} else {
|
||||
$followings = [];
|
||||
}
|
||||
|
||||
if (empty($followers) && empty($followings)) {
|
||||
return;
|
||||
}
|
||||
|
||||
$target = $contact['id'];
|
||||
|
||||
if (!empty($followers)) {
|
||||
// Clear the follower list, since it will be recreated in the next step
|
||||
DBA::update('contact-relation', ['follows' => false], ['cid' => $target]);
|
||||
}
|
||||
|
||||
$contacts = [];
|
||||
foreach (array_merge($followers, $followings) as $contact) {
|
||||
if (is_string($contact)) {
|
||||
$contacts[] = $contact;
|
||||
} elseif (!empty($contact['url']) && is_string($contact['url'])) {
|
||||
$contacts[] = $contact['url'];
|
||||
}
|
||||
}
|
||||
$contacts = array_unique($contacts);
|
||||
|
||||
Logger::info('Discover contacts', ['id' => $target, 'url' => $url, 'contacts' => count($contacts)]);
|
||||
foreach ($contacts as $contact) {
|
||||
$actor = Contact::getIdForURL($contact);
|
||||
if (!empty($actor)) {
|
||||
$fields = [];
|
||||
if (in_array($contact, $followers)) {
|
||||
$fields = ['cid' => $target, 'relation-cid' => $actor];
|
||||
} elseif (in_array($contact, $followings)) {
|
||||
$fields = ['cid' => $actor, 'relation-cid' => $target];
|
||||
} else {
|
||||
continue;
|
||||
}
|
||||
|
||||
DBA::update('contact-relation', ['follows' => true, 'follow-updated' => DateTimeFormat::utcNow()], $fields, true);
|
||||
}
|
||||
}
|
||||
|
||||
if (!empty($followers)) {
|
||||
// Delete all followers that aren't followers anymore (and aren't interacting)
|
||||
DBA::delete('contact-relation', ['cid' => $target, 'follows' => false, 'last-interaction' => DBA::NULL_DATETIME]);
|
||||
}
|
||||
|
||||
DBA::update('contact', ['last-discovery' => DateTimeFormat::utcNow()], ['id' => $target]);
|
||||
Logger::info('Contacts discovery finished, "last-discovery" set', ['id' => $target, 'url' => $url]);
|
||||
return;
|
||||
}
|
||||
}
|
|
@ -43,19 +43,6 @@ use Friendica\Util\Strings;
|
|||
*/
|
||||
class GContact
|
||||
{
|
||||
/**
|
||||
* No discovery of followers/followings
|
||||
*/
|
||||
const DISCOVERY_NONE = 0;
|
||||
/**
|
||||
* Only discover followers/followings from direct contacts
|
||||
*/
|
||||
const DISCOVERY_DIRECT = 1;
|
||||
/**
|
||||
* Recursive discovery of followers/followings
|
||||
*/
|
||||
const DISCOVERY_RECURSIVE = 2;
|
||||
|
||||
/**
|
||||
* Search global contact table by nick or name
|
||||
*
|
||||
|
@ -1288,129 +1275,6 @@ class GContact
|
|||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Fetches the followers of a given profile and adds them
|
||||
*
|
||||
* @param string $url URL of a profile
|
||||
* @return void
|
||||
*/
|
||||
public static function discoverFollowers(string $url)
|
||||
{
|
||||
$gcontact = DBA::selectFirst('gcontact', ['id', 'last_discovery'], ['nurl' => Strings::normaliseLink(($url))]);
|
||||
if (!DBA::isResult($gcontact)) {
|
||||
return;
|
||||
}
|
||||
|
||||
if ($gcontact['last_discovery'] > DateTimeFormat::utc('now - 1 month')) {
|
||||
Logger::info('Last discovery was less then a month before.', ['url' => $url, 'discovery' => $gcontact['last_discovery']]);
|
||||
return;
|
||||
}
|
||||
|
||||
$gcid = $gcontact['id'];
|
||||
|
||||
$apcontact = APContact::getByURL($url);
|
||||
|
||||
if (!empty($apcontact['followers']) && is_string($apcontact['followers'])) {
|
||||
$followers = ActivityPub::fetchItems($apcontact['followers']);
|
||||
} else {
|
||||
$followers = [];
|
||||
}
|
||||
|
||||
if (!empty($apcontact['following']) && is_string($apcontact['following'])) {
|
||||
$followings = ActivityPub::fetchItems($apcontact['following']);
|
||||
} else {
|
||||
$followings = [];
|
||||
}
|
||||
|
||||
if (!empty($followers) || !empty($followings)) {
|
||||
if (!empty($followers)) {
|
||||
// Clear the follower list, since it will be recreated in the next step
|
||||
DBA::update('gfollower', ['deleted' => true], ['gcid' => $gcid]);
|
||||
}
|
||||
|
||||
$contacts = [];
|
||||
foreach (array_merge($followers, $followings) as $contact) {
|
||||
if (is_string($contact)) {
|
||||
$contacts[] = $contact;
|
||||
} elseif (!empty($contact['url']) && is_string($contact['url'])) {
|
||||
$contacts[] = $contact['url'];
|
||||
}
|
||||
}
|
||||
$contacts = array_unique($contacts);
|
||||
|
||||
Logger::info('Discover AP contacts', ['url' => $url, 'contacts' => count($contacts)]);
|
||||
foreach ($contacts as $contact) {
|
||||
$gcontact = DBA::selectFirst('gcontact', ['id'], ['nurl' => Strings::normaliseLink(($contact))]);
|
||||
if (DBA::isResult($gcontact)) {
|
||||
$fields = [];
|
||||
if (in_array($contact, $followers)) {
|
||||
$fields = ['gcid' => $gcid, 'follower-gcid' => $gcontact['id']];
|
||||
} elseif (in_array($contact, $followings)) {
|
||||
$fields = ['gcid' => $gcontact['id'], 'follower-gcid' => $gcid];
|
||||
}
|
||||
|
||||
if (!empty($fields)) {
|
||||
Logger::info('Set relation between contacts', $fields);
|
||||
DBA::update('gfollower', ['deleted' => false], $fields, true);
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
if (!Network::isUrlBlocked($contact)) {
|
||||
Logger::info('Discover new AP contact', ['url' => $contact]);
|
||||
Worker::add(PRIORITY_LOW, 'UpdateGContact', $contact, 'nodiscover');
|
||||
} else {
|
||||
Logger::info('No discovery, the URL is blocked.', ['url' => $contact]);
|
||||
}
|
||||
}
|
||||
if (!empty($followers)) {
|
||||
// Delete all followers that aren't undeleted
|
||||
DBA::delete('gfollower', ['gcid' => $gcid, 'deleted' => true]);
|
||||
}
|
||||
|
||||
DBA::update('gcontact', ['last_discovery' => DateTimeFormat::utcNow()], ['id' => $gcid]);
|
||||
Logger::info('AP contacts discovery finished, last discovery set', ['url' => $url]);
|
||||
return;
|
||||
}
|
||||
|
||||
$data = Probe::uri($url);
|
||||
if (empty($data['poco'])) {
|
||||
return;
|
||||
}
|
||||
|
||||
$curlResult = DI::httpRequest()->get($data['poco']);
|
||||
if (!$curlResult->isSuccess()) {
|
||||
return;
|
||||
}
|
||||
$poco = json_decode($curlResult->getBody(), true);
|
||||
if (empty($poco['entry'])) {
|
||||
return;
|
||||
}
|
||||
|
||||
Logger::info('PoCo Discovery started', ['url' => $url, 'contacts' => count($poco['entry'])]);
|
||||
|
||||
foreach ($poco['entry'] as $entries) {
|
||||
if (!empty($entries['urls'])) {
|
||||
foreach ($entries['urls'] as $entry) {
|
||||
if ($entry['type'] == 'profile') {
|
||||
if (DBA::exists('gcontact', ['nurl' => Strings::normaliseLink(($entry['value']))])) {
|
||||
continue;
|
||||
}
|
||||
if (!Network::isUrlBlocked($entry['value'])) {
|
||||
Logger::info('Discover new PoCo contact', ['url' => $entry['value']]);
|
||||
Worker::add(PRIORITY_LOW, 'UpdateGContact', $entry['value'], 'nodiscover');
|
||||
} else {
|
||||
Logger::info('No discovery, the URL is blocked.', ['url' => $entry['value']]);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
DBA::update('gcontact', ['last_discovery' => DateTimeFormat::utcNow()], ['id' => $gcid]);
|
||||
Logger::info('PoCo Discovery finished', ['url' => $url]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a random, global contact of the current node
|
||||
*
|
||||
|
|
|
@ -1554,9 +1554,7 @@ class Item
|
|||
}
|
||||
|
||||
// Update the contact relations
|
||||
if ($item['author-id'] != $parent['author-id']) {
|
||||
DBA::update('contact-relation', ['last-interaction' => $item['created']], ['cid' => $parent['author-id'], 'relation-cid' => $item['author-id']], true);
|
||||
}
|
||||
ContactRelation::store($parent['author-id'], $item['author-id'], $item['created']);
|
||||
}
|
||||
|
||||
return $item;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue