in "getidforurl" "no update" is now "update"

This commit is contained in:
Michael 2020-07-15 21:08:42 +00:00
parent 631095eefd
commit b0086a49e2
14 changed files with 66 additions and 60 deletions

View file

@ -253,10 +253,10 @@ function public_contact()
if (!$public_contact_id && !empty($_SESSION['authenticated'])) { if (!$public_contact_id && !empty($_SESSION['authenticated'])) {
if (!empty($_SESSION['my_address'])) { if (!empty($_SESSION['my_address'])) {
// Local user // Local user
$public_contact_id = intval(Contact::getIdForURL($_SESSION['my_address'], 0, true)); $public_contact_id = intval(Contact::getIdForURL($_SESSION['my_address'], 0, false));
} elseif (!empty($_SESSION['visitor_home'])) { } elseif (!empty($_SESSION['visitor_home'])) {
// Remote user // Remote user
$public_contact_id = intval(Contact::getIdForURL($_SESSION['visitor_home'], 0, true)); $public_contact_id = intval(Contact::getIdForURL($_SESSION['visitor_home'], 0, false));
} }
} elseif (empty($_SESSION['authenticated'])) { } elseif (empty($_SESSION['authenticated'])) {
$public_contact_id = false; $public_contact_id = false;

View file

@ -654,8 +654,8 @@ function api_get_user(App $a, $contact_id = null)
'notifications' => false, 'notifications' => false,
'statusnet_profile_url' => $contact["url"], 'statusnet_profile_url' => $contact["url"],
'uid' => 0, 'uid' => 0,
'cid' => Contact::getIdForURL($contact["url"], api_user(), true), 'cid' => Contact::getIdForURL($contact["url"], api_user(), false),
'pid' => Contact::getIdForURL($contact["url"], 0, true), 'pid' => Contact::getIdForURL($contact["url"], 0, false),
'self' => 0, 'self' => 0,
'network' => $contact["network"], 'network' => $contact["network"],
]; ];
@ -679,7 +679,7 @@ function api_get_user(App $a, $contact_id = null)
$countfollowers = 0; $countfollowers = 0;
$starred = 0; $starred = 0;
$pcontact_id = Contact::getIdForURL($uinfo[0]['url'], 0, true); $pcontact_id = Contact::getIdForURL($uinfo[0]['url'], 0, false);
if (!empty($profile['about'])) { if (!empty($profile['about'])) {
$description = $profile['about']; $description = $profile['about'];
@ -731,7 +731,7 @@ function api_get_user(App $a, $contact_id = null)
'statusnet_profile_url' => $uinfo[0]['url'], 'statusnet_profile_url' => $uinfo[0]['url'],
'uid' => intval($uinfo[0]['uid']), 'uid' => intval($uinfo[0]['uid']),
'cid' => intval($uinfo[0]['cid']), 'cid' => intval($uinfo[0]['cid']),
'pid' => Contact::getIdForURL($uinfo[0]["url"], 0, true), 'pid' => Contact::getIdForURL($uinfo[0]["url"], 0, false),
'self' => $uinfo[0]['self'], 'self' => $uinfo[0]['self'],
'network' => $uinfo[0]['network'], 'network' => $uinfo[0]['network'],
]; ];
@ -5052,7 +5052,7 @@ function api_share_as_retweet(&$item)
$reshared_item["share-pre-body"] = $reshared['comment']; $reshared_item["share-pre-body"] = $reshared['comment'];
$reshared_item["body"] = $reshared['shared']; $reshared_item["body"] = $reshared['shared'];
$reshared_item["author-id"] = Contact::getIdForURL($reshared['profile'], 0, true); $reshared_item["author-id"] = Contact::getIdForURL($reshared['profile'], 0, false);
$reshared_item["author-name"] = $reshared['author']; $reshared_item["author-name"] = $reshared['author'];
$reshared_item["author-link"] = $reshared['profile']; $reshared_item["author-link"] = $reshared['profile'];
$reshared_item["author-avatar"] = $reshared['avatar']; $reshared_item["author-avatar"] = $reshared['avatar'];

View file

@ -325,7 +325,7 @@ function conv_get_blocklist()
foreach (explode(',', $str_blocked) as $entry) { foreach (explode(',', $str_blocked) as $entry) {
// The 4th parameter guarantees that there always will be a public contact entry // The 4th parameter guarantees that there always will be a public contact entry
$cid = Contact::getIdForURL(trim($entry), 0, true, ['url' => trim($entry)]); $cid = Contact::getIdForURL(trim($entry), 0, false, ['url' => trim($entry)]);
if (!empty($cid)) { if (!empty($cid)) {
$blocklist[] = $cid; $blocklist[] = $cid;
} }
@ -837,7 +837,7 @@ function item_photo_menu($item) {
$sparkle = (strpos($profile_link, 'redir/') === 0); $sparkle = (strpos($profile_link, 'redir/') === 0);
$cid = 0; $cid = 0;
$pcid = Contact::getIdForURL($item['author-link'], 0, true); $pcid = Contact::getIdForURL($item['author-link'], 0, false);
$network = ''; $network = '';
$rel = 0; $rel = 0;
$condition = ['uid' => local_user(), 'nurl' => Strings::normaliseLink($item['author-link'])]; $condition = ['uid' => local_user(), 'nurl' => Strings::normaliseLink($item['author-link'])];

View file

@ -89,7 +89,7 @@ function match_content(App $a)
$profile = $msearch->results[$i]; $profile = $msearch->results[$i];
// Already known contact // Already known contact
if (!$profile || Contact::getIdForURL($profile->url, local_user(), true)) { if (!$profile || Contact::getIdForURL($profile->url, local_user(), false)) {
continue; continue;
} }

View file

@ -471,7 +471,7 @@ class Widget
} }
if (Feature::isEnabled($uid, 'tagadelic')) { if (Feature::isEnabled($uid, 'tagadelic')) {
$owner_id = Contact::getIdForURL($a->profile['url'], 0, true); $owner_id = Contact::getIdForURL($a->profile['url'], 0, false);
if (!$owner_id) { if (!$owner_id) {
return ''; return '';

View file

@ -245,14 +245,14 @@ class PostUpdate
$default = ['url' => $item['author-link'], 'name' => $item['author-name'], $default = ['url' => $item['author-link'], 'name' => $item['author-name'],
'photo' => $item['author-avatar'], 'network' => $item['network']]; 'photo' => $item['author-avatar'], 'network' => $item['network']];
$item['author-id'] = Contact::getIdForURL($item["author-link"], 0, false, $default); $item['author-id'] = Contact::getIdForURL($item["author-link"], 0, null, $default);
} }
if (empty($item['owner-id'])) { if (empty($item['owner-id'])) {
$default = ['url' => $item['owner-link'], 'name' => $item['owner-name'], $default = ['url' => $item['owner-link'], 'name' => $item['owner-name'],
'photo' => $item['owner-avatar'], 'network' => $item['network']]; 'photo' => $item['owner-avatar'], 'network' => $item['network']];
$item['owner-id'] = Contact::getIdForURL($item["owner-link"], 0, false, $default); $item['owner-id'] = Contact::getIdForURL($item["owner-link"], 0, null, $default);
} }
if (empty($item['psid'])) { if (empty($item['psid'])) {

View file

@ -194,21 +194,30 @@ class Contact
* Fetches a contact by a given url * Fetches a contact by a given url
* *
* @param string $url profile url * @param string $url profile url
* @param integer $uid User ID of the contact
* @param array $fields Field list
* @param boolean $update true = always update, false = never update, null = update when not found or outdated * @param boolean $update true = always update, false = never update, null = update when not found or outdated
* @param array $fields Field list
* @param integer $uid User ID of the contact
* @return array contact array * @return array contact array
*/ */
public static function getByURL(string $url, $update = null, array $fields = [], int $uid = 0) public static function getByURL(string $url, $update = null, array $fields = [], int $uid = 0)
{ {
if ($update || is_null($update)) { if ($update || is_null($update)) {
$cid = self::getIdForURL($url, $uid, !($update ?? false)); $cid = self::getIdForURL($url, $uid, $update);
if (empty($cid)) { if (empty($cid)) {
return []; return [];
} }
return self::getById($cid, $fields); return self::getById($cid, $fields);
} }
// Add internal fields
$removal = [];
foreach (['id', 'updated', 'network'] as $internal) {
if (!in_array($internal, $fields)) {
$fields[] = $internal;
$removal[] = $internal;
}
}
// We first try the nurl (http://server.tld/nick), most common case // We first try the nurl (http://server.tld/nick), most common case
$options = ['order' => ['id']]; $options = ['order' => ['id']];
$contact = DBA::selectFirst('contact', $fields, ['nurl' => Strings::normaliseLink($url), 'uid' => $uid, 'deleted' => false], $options); $contact = DBA::selectFirst('contact', $fields, ['nurl' => Strings::normaliseLink($url), 'uid' => $uid, 'deleted' => false], $options);
@ -225,6 +234,18 @@ class Contact
$condition = ['`alias` IN (?, ?, ?) AND `uid` = ? AND NOT `deleted`', $url, Strings::normaliseLink($url), $ssl_url, $uid]; $condition = ['`alias` IN (?, ?, ?) AND `uid` = ? AND NOT `deleted`', $url, Strings::normaliseLink($url), $ssl_url, $uid];
$contact = DBA::selectFirst('contact', $fields, $condition, $options); $contact = DBA::selectFirst('contact', $fields, $condition, $options);
} }
// Update the contact in the background if needed
if ((($contact['updated'] < DateTimeFormat::utc('now -7 days')) || empty($contact['avatar'])) &&
in_array($contact['network'], Protocol::FEDERATED)) {
Worker::add(PRIORITY_LOW, "UpdateContact", $contact['id'], ($uid == 0 ? 'force' : ''));
}
// Remove the internal fields
foreach ($removal as $internal) {
unset($contact[$internal]);
}
return $contact; return $contact;
} }
@ -234,8 +255,8 @@ class Contact
* *
* @param string $url profile url * @param string $url profile url
* @param integer $uid User ID of the contact * @param integer $uid User ID of the contact
* @param array $fields Field list
* @param boolean $update true = always update, false = never update, null = update when not found or outdated * @param boolean $update true = always update, false = never update, null = update when not found or outdated
* @param array $fields Field list
* @return array contact array * @return array contact array
*/ */
public static function getByURLForUser(string $url, int $uid = 0, $update = false, array $fields = []) public static function getByURLForUser(string $url, int $uid = 0, $update = false, array $fields = [])
@ -296,7 +317,7 @@ class Contact
*/ */
public static function isFollowerByURL($url, $uid) public static function isFollowerByURL($url, $uid)
{ {
$cid = self::getIdForURL($url, $uid, true); $cid = self::getIdForURL($url, $uid, false);
if (empty($cid)) { if (empty($cid)) {
return false; return false;
@ -342,7 +363,7 @@ class Contact
*/ */
public static function isSharingByURL($url, $uid) public static function isSharingByURL($url, $uid)
{ {
$cid = self::getIdForURL($url, $uid, true); $cid = self::getIdForURL($url, $uid, false);
if (empty($cid)) { if (empty($cid)) {
return false; return false;
@ -437,7 +458,7 @@ class Contact
if (!DBA::isResult($self)) { if (!DBA::isResult($self)) {
return false; return false;
} }
return self::getIdForURL($self['url'], 0, true); return self::getIdForURL($self['url'], 0, false);
} }
/** /**
@ -467,14 +488,14 @@ class Contact
} }
if ($contact['uid'] != 0) { if ($contact['uid'] != 0) {
$pcid = Contact::getIdForURL($contact['url'], 0, true, ['url' => $contact['url']]); $pcid = Contact::getIdForURL($contact['url'], 0, false, ['url' => $contact['url']]);
if (empty($pcid)) { if (empty($pcid)) {
return []; return [];
} }
$ucid = $contact['id']; $ucid = $contact['id'];
} else { } else {
$pcid = $contact['id']; $pcid = $contact['id'];
$ucid = Contact::getIdForURL($contact['url'], $uid, true); $ucid = Contact::getIdForURL($contact['url'], $uid, false);
} }
return ['public' => $pcid, 'user' => $ucid]; return ['public' => $pcid, 'user' => $ucid];
@ -1300,7 +1321,7 @@ class Contact
* *
* @param string $url Contact URL * @param string $url Contact URL
* @param integer $uid The user id for the contact (0 = public contact) * @param integer $uid The user id for the contact (0 = public contact)
* @param boolean $no_update Don't update the contact * @param boolean $update true = always update, false = never update, null = update when not found or outdated
* @param array $default Default value for creating the contact when every else fails * @param array $default Default value for creating the contact when every else fails
* @param boolean $in_loop Internally used variable to prevent an endless loop * @param boolean $in_loop Internally used variable to prevent an endless loop
* *
@ -1308,7 +1329,7 @@ class Contact
* @throws HTTPException\InternalServerErrorException * @throws HTTPException\InternalServerErrorException
* @throws \ImagickException * @throws \ImagickException
*/ */
public static function getIdForURL($url, $uid = 0, $no_update = false, $default = [], $in_loop = false) public static function getIdForURL($url, $uid = 0, $update = null, $default = [], $in_loop = false)
{ {
Logger::info('Get contact data', ['url' => $url, 'user' => $uid]); Logger::info('Get contact data', ['url' => $url, 'user' => $uid]);
@ -1322,17 +1343,8 @@ class Contact
if (!empty($contact)) { if (!empty($contact)) {
$contact_id = $contact["id"]; $contact_id = $contact["id"];
$update_contact = false;
// Update the contact every 7 days (Don't update mail or feed contacts) if (empty($default) && in_array($contact['network'], [Protocol::MAIL, Protocol::PHANTOM]) && ($uid == 0)) {
if (in_array($contact['network'], Protocol::FEDERATED)) {
$update_contact = ($contact['updated'] < DateTimeFormat::utc('now -7 days'));
// We force the update if the avatar is empty
if (empty($contact['avatar'])) {
$update_contact = true;
}
} elseif (empty($default) && in_array($contact['network'], [Protocol::MAIL, Protocol::PHANTOM]) && ($uid == 0)) {
// Update public mail accounts via their user's accounts // Update public mail accounts via their user's accounts
$fields = ['network', 'addr', 'name', 'nick', 'avatar', 'photo', 'thumb', 'micro']; $fields = ['network', 'addr', 'name', 'nick', 'avatar', 'photo', 'thumb', 'micro'];
$mailcontact = DBA::selectFirst('contact', $fields, ["`addr` = ? AND `network` = ? AND `uid` != 0", $url, Protocol::MAIL]); $mailcontact = DBA::selectFirst('contact', $fields, ["`addr` = ? AND `network` = ? AND `uid` != 0", $url, Protocol::MAIL]);
@ -1345,12 +1357,7 @@ class Contact
} }
} }
// Update the contact in the background if needed but it is called by the frontend if (empty($update)) {
if ($update_contact && $no_update && in_array($contact['network'], Protocol::NATIVE_SUPPORT)) {
Worker::add(PRIORITY_LOW, "UpdateContact", $contact_id, ($uid == 0 ? 'force' : ''));
}
if (!$update_contact || $no_update) {
return $contact_id; return $contact_id;
} }
} elseif ($uid != 0) { } elseif ($uid != 0) {
@ -1358,11 +1365,11 @@ class Contact
return 0; return 0;
} }
if ($no_update && empty($default)) { if (!$update && empty($default)) {
// When we don't want to update, we look if we know this contact in any way // When we don't want to update, we look if we know this contact in any way
$data = self::getProbeDataFromDatabase($url, $contact_id); $data = self::getProbeDataFromDatabase($url, $contact_id);
$background_update = true; $background_update = true;
} elseif ($no_update && !empty($default['network'])) { } elseif (!$update && !empty($default['network'])) {
// If there are default values, take these // If there are default values, take these
$data = $default; $data = $default;
$background_update = false; $background_update = false;
@ -1371,7 +1378,7 @@ class Contact
$background_update = false; $background_update = false;
} }
if (empty($data)) { if ((empty($data) && is_null($update)) || $update) {
$data = Probe::uri($url, "", $uid); $data = Probe::uri($url, "", $uid);
} }
@ -1394,7 +1401,7 @@ class Contact
} }
if (!$contact_id && !empty($data['alias']) && ($data['alias'] != $data['url']) && !$in_loop) { if (!$contact_id && !empty($data['alias']) && ($data['alias'] != $data['url']) && !$in_loop) {
$contact_id = self::getIdForURL($data["alias"], $uid, true, $default, true); $contact_id = self::getIdForURL($data["alias"], $uid, false, $default, true);
} }
if (!$contact_id) { if (!$contact_id) {

View file

@ -1680,11 +1680,11 @@ class Item
$default = ['url' => $item['author-link'], 'name' => $item['author-name'], $default = ['url' => $item['author-link'], 'name' => $item['author-name'],
'photo' => $item['author-avatar'], 'network' => $item['network']]; 'photo' => $item['author-avatar'], 'network' => $item['network']];
$item['author-id'] = ($item['author-id'] ?? 0) ?: Contact::getIdForURL($item['author-link'], 0, false, $default); $item['author-id'] = ($item['author-id'] ?? 0) ?: Contact::getIdForURL($item['author-link'], 0, null, $default);
$default = ['url' => $item['owner-link'], 'name' => $item['owner-name'], $default = ['url' => $item['owner-link'], 'name' => $item['owner-name'],
'photo' => $item['owner-avatar'], 'network' => $item['network']]; 'photo' => $item['owner-avatar'], 'network' => $item['network']];
$item['owner-id'] = ($item['owner-id'] ?? 0) ?: Contact::getIdForURL($item['owner-link'], 0, false, $default); $item['owner-id'] = ($item['owner-id'] ?? 0) ?: Contact::getIdForURL($item['owner-link'], 0, null, $default);
// The contact-id should be set before "self::insert" was called - but there seems to be issues sometimes // The contact-id should be set before "self::insert" was called - but there seems to be issues sometimes
$item["contact-id"] = self::contactId($item); $item["contact-id"] = self::contactId($item);
@ -2976,7 +2976,7 @@ class Item
if (local_user() == $uid) { if (local_user() == $uid) {
$item_contact_id = $owner_self_contact['id']; $item_contact_id = $owner_self_contact['id'];
} else { } else {
$item_contact_id = Contact::getIdForURL($author_contact['url'], $uid, true); $item_contact_id = Contact::getIdForURL($author_contact['url'], $uid, false);
$item_contact = DBA::selectFirst('contact', [], ['id' => $item_contact_id]); $item_contact = DBA::selectFirst('contact', [], ['id' => $item_contact_id]);
if (!DBA::isResult($item_contact)) { if (!DBA::isResult($item_contact)) {
Logger::log('like: unknown item contact ' . $item_contact_id); Logger::log('like: unknown item contact ' . $item_contact_id);

View file

@ -111,7 +111,7 @@ class Tag
} }
} }
} else { } else {
$cid = Contact::getIdForURL($url, 0, true); $cid = Contact::getIdForURL($url, 0, false);
Logger::info('Got id by probing', ['cid' => $cid, 'url' => $url]); Logger::info('Got id by probing', ['cid' => $cid, 'url' => $url]);
} }

View file

@ -47,8 +47,7 @@ class Feed extends BaseModule
if (!empty($_REQUEST['url'])) { if (!empty($_REQUEST['url'])) {
$url = $_REQUEST['url']; $url = $_REQUEST['url'];
$contact_id = Model\Contact::getIdForURL($url, local_user(), true); $contact = Model\Contact::getByURLForUser($url, local_user(), false);
$contact = Model\Contact::getById($contact_id);
$xml = Network::fetchUrl($contact['poll']); $xml = Network::fetchUrl($contact['poll']);

View file

@ -445,9 +445,9 @@ class Processor
$item['network'] = Protocol::ACTIVITYPUB; $item['network'] = Protocol::ACTIVITYPUB;
$item['author-link'] = $activity['author']; $item['author-link'] = $activity['author'];
$item['author-id'] = Contact::getIdForURL($activity['author'], 0, true); $item['author-id'] = Contact::getIdForURL($activity['author'], 0, false);
$item['owner-link'] = $activity['actor']; $item['owner-link'] = $activity['actor'];
$item['owner-id'] = Contact::getIdForURL($activity['actor'], 0, true); $item['owner-id'] = Contact::getIdForURL($activity['actor'], 0, false);
if (in_array(0, $activity['receiver']) && !empty($activity['unlisted'])) { if (in_array(0, $activity['receiver']) && !empty($activity['unlisted'])) {
$item['private'] = Item::UNLISTED; $item['private'] = Item::UNLISTED;
@ -511,13 +511,13 @@ class Processor
$item['uid'] = $receiver; $item['uid'] = $receiver;
if ($isForum) { if ($isForum) {
$item['contact-id'] = Contact::getIdForURL($activity['actor'], $receiver, true); $item['contact-id'] = Contact::getIdForURL($activity['actor'], $receiver, false);
} else { } else {
$item['contact-id'] = Contact::getIdForURL($activity['author'], $receiver, true); $item['contact-id'] = Contact::getIdForURL($activity['author'], $receiver, false);
} }
if (($receiver != 0) && empty($item['contact-id'])) { if (($receiver != 0) && empty($item['contact-id'])) {
$item['contact-id'] = Contact::getIdForURL($activity['author'], 0, true); $item['contact-id'] = Contact::getIdForURL($activity['author'], 0, false);
} }
if (!empty($activity['directmessage'])) { if (!empty($activity['directmessage'])) {

View file

@ -150,7 +150,7 @@ class Transmitter
*/ */
public static function getOutbox($owner, $page = null) public static function getOutbox($owner, $page = null)
{ {
$public_contact = Contact::getIdForURL($owner['url'], 0, true); $public_contact = Contact::getIdForURL($owner['url'], 0, false);
$condition = ['uid' => 0, 'contact-id' => $public_contact, 'author-id' => $public_contact, $condition = ['uid' => 0, 'contact-id' => $public_contact, 'author-id' => $public_contact,
'private' => [Item::PUBLIC, Item::UNLISTED], 'gravity' => [GRAVITY_PARENT, GRAVITY_COMMENT], 'private' => [Item::PUBLIC, Item::UNLISTED], 'gravity' => [GRAVITY_PARENT, GRAVITY_COMMENT],

View file

@ -221,7 +221,7 @@ class OStatus
} }
// Ensure that we are having this contact (with uid=0) // Ensure that we are having this contact (with uid=0)
$cid = Contact::getIdForURL($aliaslink, 0, true); $cid = Contact::getIdForURL($aliaslink, 0, false);
if ($cid) { if ($cid) {
$fields = ['url', 'nurl', 'name', 'nick', 'alias', 'about', 'location']; $fields = ['url', 'nurl', 'name', 'nick', 'alias', 'about', 'location'];
@ -2220,7 +2220,7 @@ class OStatus
} }
$check_date = $feed_mode ? '' : DateTimeFormat::utc($last_update); $check_date = $feed_mode ? '' : DateTimeFormat::utc($last_update);
$authorid = Contact::getIdForURL($owner["url"], 0, true); $authorid = Contact::getIdForURL($owner["url"], 0, false);
$condition = ["`uid` = ? AND `received` > ? AND NOT `deleted` $condition = ["`uid` = ? AND `received` > ? AND NOT `deleted`
AND `private` != ? AND `visible` AND `wall` AND `parent-network` IN (?, ?)", AND `private` != ? AND `visible` AND `wall` AND `parent-network` IN (?, ?)",

View file

@ -203,7 +203,7 @@ function update_1260()
while ($item = DBA::fetch($items)) { while ($item = DBA::fetch($items)) {
$contact = ['url' => $item['owner-link'], 'name' => $item['owner-name'], $contact = ['url' => $item['owner-link'], 'name' => $item['owner-name'],
'photo' => $item['owner-avatar'], 'network' => $item['network']]; 'photo' => $item['owner-avatar'], 'network' => $item['network']];
$cid = Contact::getIdForURL($item['owner-link'], 0, false, $contact); $cid = Contact::getIdForURL($item['owner-link'], 0, null, $contact);
if (empty($cid)) { if (empty($cid)) {
continue; continue;
} }
@ -219,7 +219,7 @@ function update_1260()
while ($item = DBA::fetch($items)) { while ($item = DBA::fetch($items)) {
$contact = ['url' => $item['author-link'], 'name' => $item['author-name'], $contact = ['url' => $item['author-link'], 'name' => $item['author-name'],
'photo' => $item['author-avatar'], 'network' => $item['network']]; 'photo' => $item['author-avatar'], 'network' => $item['network']];
$cid = Contact::getIdForURL($item['author-link'], 0, false, $contact); $cid = Contact::getIdForURL($item['author-link'], 0, null, $contact);
if (empty($cid)) { if (empty($cid)) {
continue; continue;
} }