namespace Friendica\Model;
-use DOMDocument;
-use DOMXPath;
use Friendica\App\BaseURL;
use Friendica\Content\Pager;
use Friendica\Core\Hook;
*/
class Contact
{
+ const DEFAULT_AVATAR_PHOTO = '/images/person-300.jpg';
+ const DEFAULT_AVATAR_THUMB = '/images/person-80.jpg';
+ const DEFAULT_AVATAR_MICRO = '/images/person-48.jpg';
+
/**
* @deprecated since version 2019.03
* @see User::PAGE_FLAGS_NORMAL
// Add internal fields
$removal = [];
if (!empty($fields)) {
- foreach (['id', 'updated', 'network'] as $internal) {
+ foreach (['id', 'avatar', 'updated', 'last-update', 'success_update', 'failure_update', 'network'] as $internal) {
if (!in_array($internal, $fields)) {
$fields[] = $internal;
$removal[] = $internal;
}
// Update the contact in the background if needed
- if ((($contact['updated'] < DateTimeFormat::utc('now -7 days')) || empty($contact['avatar'])) &&
+ $updated = max($contact['success_update'], $contact['updated'], $contact['last-update'], $contact['failure_update']);
+ if ((($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' : ''));
+ Worker::add(PRIORITY_LOW, "UpdateContact", $contact['id']);
}
// Remove the internal fields
*/
public static function isFollowerByURL($url, $uid)
{
- $cid = self::getIdForURL($url, $uid, false);
+ $cid = self::getIdForURL($url, $uid);
if (empty($cid)) {
return false;
*/
public static function isSharingByURL($url, $uid)
{
- $cid = self::getIdForURL($url, $uid, false);
+ $cid = self::getIdForURL($url, $uid);
if (empty($cid)) {
return false;
}
// Update the existing contact
- self::updateFromProbe($contact['id'], '', true);
+ self::updateFromProbe($contact['id']);
// And fetch the result
$contact = DBA::selectFirst('contact', ['baseurl'], ['id' => $contact['id']]);
if (!DBA::isResult($self)) {
return false;
}
- return self::getIdForURL($self['url'], 0, false);
+ return self::getIdForURL($self['url']);
}
/**
$ucid = $contact['id'];
} else {
$pcid = $contact['id'];
- $ucid = Contact::getIdForURL($contact['url'], $uid, false);
+ $ucid = Contact::getIdForURL($contact['url'], $uid);
}
return ['public' => $pcid, 'user' => $ucid];
$fields['micro'] = $prefix . '6' . $suffix;
} else {
// We hadn't found a photo entry, so we use the default avatar
- $fields['photo'] = DI::baseUrl() . '/images/person-300.jpg';
- $fields['thumb'] = DI::baseUrl() . '/images/person-80.jpg';
- $fields['micro'] = DI::baseUrl() . '/images/person-48.jpg';
+ $fields['photo'] = DI::baseUrl() . self::DEFAULT_AVATAR_PHOTO;
+ $fields['thumb'] = DI::baseUrl() . self::DEFAULT_AVATAR_THUMB;
+ $fields['micro'] = DI::baseUrl() . self::DEFAULT_AVATAR_MICRO;
}
$fields['avatar'] = DI::baseUrl() . '/photo/profile/' .$uid . '.' . $file_suffix;
return $menucondensed;
}
- /**
- * Have a look at all contact tables for a given profile url.
- * This function works as a replacement for probing the contact.
- *
- * @param string $url Contact URL
- * @param integer $cid Contact ID
- *
- * @return array Contact array in the "probe" structure
- */
- private static function getProbeDataFromDatabase($url, $cid = null)
- {
- // The link could be provided as http although we stored it as https
- $ssl_url = str_replace('http://', 'https://', $url);
-
- $fields = ['id', 'uid', 'url', 'addr', 'alias', 'notify', 'poll', 'name', 'nick',
- 'photo', 'keywords', 'location', 'about', 'network',
- 'priority', 'batch', 'request', 'confirm', 'poco'];
-
- if (!empty($cid)) {
- $data = DBA::selectFirst('contact', $fields, ['id' => $cid]);
- if (DBA::isResult($data)) {
- return $data;
- }
- }
-
- $data = DBA::selectFirst('contact', $fields, ['nurl' => Strings::normaliseLink($url)]);
-
- if (!DBA::isResult($data)) {
- $condition = ['alias' => [$url, Strings::normaliseLink($url), $ssl_url]];
- $data = DBA::selectFirst('contact', $fields, $condition);
- }
-
- if (DBA::isResult($data)) {
- // For security reasons we don't fetch key data from our users
- $data["pubkey"] = '';
- return $data;
- }
-
- $fields = ['url', 'addr', 'alias', 'notify', 'name', 'nick',
- 'photo', 'keywords', 'location', 'about', 'network'];
- $condition = ['alias' => [$url, Strings::normaliseLink($url), $ssl_url]];
- $data = DBA::selectFirst('contact', $fields, $condition);
-
- if (DBA::isResult($data)) {
- $data["pubkey"] = '';
- $data["poll"] = '';
- $data["priority"] = 0;
- $data["batch"] = '';
- $data["request"] = '';
- $data["confirm"] = '';
- $data["poco"] = '';
- return $data;
- }
-
- $data = ActivityPub::probeProfile($url, false);
- if (!empty($data)) {
- return $data;
- }
-
- $fields = ['url', 'addr', 'alias', 'notify', 'poll', 'name', 'nick',
- 'photo', 'network', 'priority', 'batch', 'request', 'confirm'];
- $data = DBA::selectFirst('fcontact', $fields, ['url' => $url]);
-
- if (!DBA::isResult($data)) {
- $condition = ['alias' => [$url, Strings::normaliseLink($url), $ssl_url]];
- $data = DBA::selectFirst('contact', $fields, $condition);
- }
-
- if (DBA::isResult($data)) {
- $data["pubkey"] = '';
- $data["keywords"] = '';
- $data["location"] = '';
- $data["about"] = '';
- $data["poco"] = '';
- return $data;
- }
-
- return [];
- }
-
/**
* Fetch the contact id for a given URL and user
*
*
* @param string $url Contact URL
* @param integer $uid The user id for the contact (0 = public 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 boolean $update true = always update, false = never update, null = update when not found
+ * @param array $default Default value for creating the contact when everything else fails
*
* @return integer Contact ID
* @throws HTTPException\InternalServerErrorException
*/
public static function getIdForURL($url, $uid = 0, $update = null, $default = [])
{
- Logger::info('Get contact data', ['url' => $url, 'user' => $uid]);
-
$contact_id = 0;
if ($url == '') {
+ Logger::notice('Empty url, quitting', ['url' => $url, 'user' => $uid, 'default' => $default]);
return 0;
}
- $contact = self::getByURL($url, false, ['id', 'avatar', 'updated', 'network'], $uid);
+ $contact = self::getByURL($url, false, ['id', 'network'], $uid);
if (!empty($contact)) {
$contact_id = $contact["id"];
- if (empty($default) && in_array($contact['network'], [Protocol::MAIL, Protocol::PHANTOM]) && ($uid == 0)) {
- // Update public mail accounts via their user's accounts
- $fields = ['network', 'addr', 'name', 'nick', 'avatar', 'photo', 'thumb', 'micro'];
- $mailcontact = DBA::selectFirst('contact', $fields, ["`addr` = ? AND `network` = ? AND `uid` != 0", $url, Protocol::MAIL]);
- if (!DBA::isResult($mailcontact)) {
- $mailcontact = DBA::selectFirst('contact', $fields, ["`nurl` = ? AND `network` = ? AND `uid` != 0", $url, Protocol::MAIL]);
- }
-
- if (DBA::isResult($mailcontact)) {
- DBA::update('contact', $mailcontact, ['id' => $contact_id]);
- }
- }
-
if (empty($update)) {
+ Logger::debug('Contact found', ['url' => $url, 'uid' => $uid, 'update' => $update, 'cid' => $contact_id]);
return $contact_id;
}
} elseif ($uid != 0) {
- // Non-existing user-specific contact, exiting
+ Logger::debug('Contact does not exist for the user', ['url' => $url, 'uid' => $uid, 'update' => $update]);
+ return 0;
+ } elseif (empty($default) && !is_null($update) && !$update) {
+ Logger::info('Contact not found, update not desired', ['url' => $url, 'uid' => $uid, 'update' => $update]);
return 0;
}
- if (!$update && empty($default)) {
- // When we don't want to update, we look if we know this contact in any way
- $data = self::getProbeDataFromDatabase($url, $contact_id);
- $background_update = true;
- } elseif (!$update && !empty($default['network'])) {
- // If there are default values, take these
- $data = $default;
- $background_update = false;
- } else {
- $data = [];
- $background_update = false;
- }
+ $data = [];
- if ((empty($data) && is_null($update)) || $update) {
+ if (empty($default['network']) || $update) {
$data = Probe::uri($url, "", $uid);
- }
- // Take the default values when probing failed
- if (!empty($default) && (empty($data['network']) || !in_array($data["network"], array_merge(Protocol::NATIVE_SUPPORT, [Protocol::PUMPIO])))) {
- $data = array_merge($data, $default);
+ // Take the default values when probing failed
+ if (!empty($default) && !in_array($data["network"], array_merge(Protocol::NATIVE_SUPPORT, [Protocol::PUMPIO]))) {
+ $data = array_merge($data, $default);
+ }
+ } elseif (!empty($default['network'])) {
+ $data = $default;
}
- if (empty($data['network']) || ($data['network'] == Protocol::PHANTOM)) {
- Logger::info('No valid network found', ['url' => $url, 'data' => $data, 'callstack' => System::callstack(20)]);
- return 0;
- }
+ if (($uid == 0) && (empty($data['network']) || ($data['network'] == Protocol::PHANTOM))) {
+ // Fetch data for the public contact via the first found personal contact
+ /// @todo Check if this case can happen at all (possibly with mail accounts?)
+ $fields = ['name', 'nick', 'url', 'addr', 'alias', 'avatar', 'contact-type',
+ 'keywords', 'location', 'about', 'unsearchable', 'batch', 'notify', 'poll',
+ 'request', 'confirm', 'poco', 'subscribe', 'network', 'baseurl', 'gsid'];
- if (!empty($data['baseurl'])) {
- $data['baseurl'] = GServer::cleanURL($data['baseurl']);
- }
+ $personal_contact = DBA::selectFirst('contact', $fields, ["`addr` = ? AND `uid` != 0", $url]);
+ if (!DBA::isResult($personal_contact)) {
+ $personal_contact = DBA::selectFirst('contact', $fields, ["`nurl` = ? AND `uid` != 0", Strings::normaliseLink($url)]);
+ }
- if (!empty($data['baseurl']) && empty($data['gsid'])) {
- $data['gsid'] = GServer::getID($data['baseurl']);
+ if (DBA::isResult($personal_contact)) {
+ Logger::info('Take contact data from personal contact', ['url' => $url, 'update' => $update, 'contact' => $personal_contact, 'callstack' => System::callstack(20)]);
+ $data = $personal_contact;
+ $data['photo'] = $personal_contact['avatar'];
+ $data['account-type'] = $personal_contact['contact-type'];
+ $data['hide'] = $personal_contact['unsearchable'];
+ unset($data['avatar']);
+ unset($data['contact-type']);
+ unset($data['unsearchable']);
+ }
}
- if (!$contact_id && !empty($data['alias']) && ($data['alias'] != $data['url'])) {
- $contact = self::getByURL($data['alias'], false, ['id']);
- if (!empty($contact['id'])) {
- $contact_id = $contact['id'];
- Logger::info('Fetched id by alias', ['cid' => $contact_id, 'url' => $url, 'probed_url' => $data['url'], 'alias' => $data['alias']]);
- }
+ if (empty($data['network']) || ($data['network'] == Protocol::PHANTOM)) {
+ Logger::notice('No valid network found', ['url' => $url, 'uid' => $uid, 'default' => $default, 'update' => $update, 'callstack' => System::callstack(20)]);
+ return 0;
}
- // Possibly there is a contact entry with the probed URL
- if (!$contact_id && ($url != $data['url']) && ($url != $data['alias'])) {
- $contact = self::getByURL($data['url'], false, ['id']);
+ if (!$contact_id) {
+ $urls = [Strings::normaliseLink($url), Strings::normaliseLink($data['url'])];
+ if (!empty($data['alias'])) {
+ $urls[] = Strings::normaliseLink($data['alias']);
+ }
+ $contact = self::selectFirst(['id'], ['nurl' => $urls, 'uid' => $uid]);
if (!empty($contact['id'])) {
$contact_id = $contact['id'];
- Logger::info('Fetched id by url', ['cid' => $contact_id, 'url' => $url, 'probed_url' => $data['url'], 'alias' => $data['alias']]);
+ Logger::info('Fetched id by url', ['cid' => $contact_id, 'uid' => $uid, 'url' => $url, 'probed_url' => $data['url'], 'alias' => $data['alias'], 'addr' => $data['addr']]);
}
}
- if ($uid == 0) {
- $data['last-item'] = Probe::getLastUpdate($data);
- Logger::info('Fetched last item', ['url' => $url, 'probed_url' => $data['url'], 'last-item' => $data['last-item'], 'callstack' => System::callstack(20)]);
- }
-
if (!$contact_id) {
+ // We only insert the basic data. The rest will be done in "updateFromProbeArray"
$fields = [
'uid' => $uid,
- 'created' => DateTimeFormat::utcNow(),
'url' => $data['url'],
'nurl' => Strings::normaliseLink($data['url']),
- 'addr' => $data['addr'] ?? '',
- 'alias' => $data['alias'] ?? '',
- 'notify' => $data['notify'] ?? '',
- 'poll' => $data['poll'] ?? '',
- 'name' => $data['name'] ?? '',
- 'nick' => $data['nick'] ?? '',
- 'keywords' => $data['keywords'] ?? '',
- 'location' => $data['location'] ?? '',
- 'about' => $data['about'] ?? '',
'network' => $data['network'],
- 'pubkey' => $data['pubkey'] ?? '',
+ 'created' => DateTimeFormat::utcNow(),
'rel' => self::SHARING,
- 'priority' => $data['priority'] ?? 0,
- 'batch' => $data['batch'] ?? '',
- 'request' => $data['request'] ?? '',
- 'confirm' => $data['confirm'] ?? '',
- 'poco' => $data['poco'] ?? '',
- 'baseurl' => $data['baseurl'] ?? '',
- 'gsid' => $data['gsid'] ?? null,
- 'name-date' => DateTimeFormat::utcNow(),
- 'uri-date' => DateTimeFormat::utcNow(),
- 'avatar-date' => DateTimeFormat::utcNow(),
'writable' => 1,
'blocked' => 0,
'readonly' => 0,
'pending' => 0];
- if (!empty($data['last-item'])) {
- $fields['last-item'] = $data['last-item'];
- }
-
$condition = ['nurl' => Strings::normaliseLink($data["url"]), 'uid' => $uid, 'deleted' => false];
// Before inserting we do check if the entry does exist now.
+ DBA::lock('contact');
$contact = DBA::selectFirst('contact', ['id'], $condition, ['order' => ['id']]);
- if (!DBA::isResult($contact)) {
- Logger::info('Create new contact', $fields);
-
- self::insert($fields);
-
- // We intentionally aren't using lastInsertId here. There is a chance for duplicates.
- $contact = DBA::selectFirst('contact', ['id'], $condition, ['order' => ['id']]);
- if (!DBA::isResult($contact)) {
- Logger::info('Contact creation failed', $fields);
- // Shouldn't happen
- return 0;
- }
+ if (DBA::isResult($contact)) {
+ $contact_id = $contact['id'];
+ Logger::notice('Contact had been created (shortly) before', ['id' => $contact_id, 'url' => $url, 'uid' => $uid]);
} else {
- Logger::info('Contact had been created before', ['id' => $contact["id"], 'url' => $url, 'contact' => $fields]);
+ DBA::insert('contact', $fields);
+ $contact_id = DBA::lastInsertId();
+ if ($contact_id) {
+ Logger::info('Contact inserted', ['id' => $contact_id, 'url' => $url, 'uid' => $uid]);
+ }
}
-
- $contact_id = $contact["id"];
- }
-
- if (!empty($data['photo']) && ($data['network'] != Protocol::FEED)) {
- self::updateAvatar($contact_id, $data['photo']);
- }
-
- if (in_array($data["network"], array_merge(Protocol::NATIVE_SUPPORT, [Protocol::PUMPIO]))) {
- if ($background_update) {
- // Update in the background when we fetched the data solely from the database
- Worker::add(PRIORITY_MEDIUM, "UpdateContact", $contact_id, ($uid == 0 ? 'force' : ''));
- } else {
- // Else do a direct update
- self::updateFromProbe($contact_id, '', false);
+ DBA::unlock();
+ if (!$contact_id) {
+ Logger::info('Contact was not inserted', ['url' => $url, 'uid' => $uid]);
+ return 0;
}
} else {
- $fields = ['url', 'nurl', 'addr', 'alias', 'name', 'nick', 'keywords', 'location', 'about', 'avatar-date', 'baseurl', 'gsid', 'last-item'];
- $contact = DBA::selectFirst('contact', $fields, ['id' => $contact_id]);
-
- // This condition should always be true
- if (!DBA::isResult($contact)) {
- return $contact_id;
- }
-
- $updated = [
- 'url' => $data['url'],
- 'nurl' => Strings::normaliseLink($data['url']),
- 'updated' => DateTimeFormat::utcNow(),
- 'failed' => false
- ];
-
- $fields = ['addr', 'alias', 'name', 'nick', 'keywords', 'location', 'about', 'baseurl', 'gsid'];
-
- foreach ($fields as $field) {
- $updated[$field] = ($data[$field] ?? '') ?: $contact[$field];
- }
-
- if (!empty($data['last-item']) && ($contact['last-item'] < $data['last-item'])) {
- $updated['last-item'] = $data['last-item'];
- }
-
- if (($updated['addr'] != $contact['addr']) || (!empty($data['alias']) && ($data['alias'] != $contact['alias']))) {
- $updated['uri-date'] = DateTimeFormat::utcNow();
- }
-
- if (($data['name'] != $contact['name']) || ($data['nick'] != $contact['nick'])) {
- $updated['name-date'] = DateTimeFormat::utcNow();
- }
-
- DBA::update('contact', $updated, ['id' => $contact_id], $contact);
+ Logger::info('Contact will be updated', ['url' => $url, 'uid' => $uid, 'update' => $update, 'cid' => $contact_id]);
}
+ self::updateFromProbeArray($contact_id, $data);
+
return $contact_id;
}
$contact_field = ((($contact["contact-type"] == self::TYPE_COMMUNITY) || ($contact['network'] == Protocol::MAIL)) ? 'owner-id' : 'author-id');
if ($thread_mode) {
- $condition = ["`$contact_field` = ? AND `gravity` = ? AND " . $sql,
- $cid, GRAVITY_PARENT, local_user()];
+ $condition = ["`$contact_field` = ? AND (`gravity` = ? OR (`gravity` = ? AND `vid` = ?)) AND " . $sql,
+ $cid, GRAVITY_PARENT, GRAVITY_ACTIVITY, Verb::getID(Activity::ANNOUNCE), local_user()];
} else {
$condition = ["`$contact_field` = ? AND `gravity` IN (?, ?) AND " . $sql,
$cid, GRAVITY_PARENT, GRAVITY_COMMENT, local_user()];
'limit' => [$pager->getStart(), $pager->getItemsPerPage()]];
if ($thread_mode) {
- $r = Item::selectThreadForUser(local_user(), ['uri'], $condition, $params);
-
- $items = Item::inArray($r);
+ $r = Item::selectForUser(local_user(), ['uri', 'gravity', 'parent-uri'], $condition, $params);
+ $items = [];
+ while ($item = DBA::fetch($r)) {
+ if ($item['gravity'] != GRAVITY_PARENT) {
+ $item['uri'] = $item['parent-uri'];
+ }
+ unset($item['parent-uri']);
+ unset($item['gravity']);
+
+ $items[] = $item;
+ }
+ DBA::close($r);
$o = conversation($a, $items, 'contacts', $update, false, 'commented', local_user());
} else {
*/
public static function getPhoto(array $contact, string $avatar = '')
{
- return self::getAvatarPath($contact, 'photo', DI::baseUrl() . '/images/person-300.jpg', Proxy::SIZE_SMALL, $avatar);
+ return self::getAvatarPath($contact, 'photo', DI::baseUrl() . self::DEFAULT_AVATAR_PHOTO, Proxy::SIZE_SMALL, $avatar);
}
/**
*/
public static function getThumb(array $contact, string $avatar = '')
{
- return self::getAvatarPath($contact, 'thumb', DI::baseUrl() . '/images/person-80.jpg', Proxy::SIZE_THUMB, $avatar);
+ return self::getAvatarPath($contact, 'thumb', DI::baseUrl() . self::DEFAULT_AVATAR_THUMB, Proxy::SIZE_THUMB, $avatar);
}
/**
*/
public static function getMicro(array $contact, string $avatar = '')
{
- return self::getAvatarPath($contact, 'micro', DI::baseUrl() . '/images/person-48.jpg', Proxy::SIZE_MICRO, $avatar);
+ return self::getAvatarPath($contact, 'micro', DI::baseUrl() . self::DEFAULT_AVATAR_MICRO, Proxy::SIZE_MICRO, $avatar);
}
/**
/// add the default avatars if the fields aren't filled
if (isset($contact['photo']) && empty($contact['photo'])) {
- $contact['photo'] = DI::baseUrl() . '/images/person-300.jpg';
+ $contact['photo'] = DI::baseUrl() . self::DEFAULT_AVATAR_PHOTO;
}
if (isset($contact['thumb']) && empty($contact['thumb'])) {
- $contact['thumb'] = DI::baseUrl() . '/images/person-80.jpg';
+ $contact['thumb'] = DI::baseUrl() . self::DEFAULT_AVATAR_THUMB;
}
if (isset($contact['micro']) && empty($contact['micro'])) {
- $contact['micro'] = DI::baseUrl() . '/images/person-48.jpg';
+ $contact['micro'] = DI::baseUrl() . self::DEFAULT_AVATAR_MICRO;
}
return $contact;
/**
* Updates the avatar links in a contact only if needed
*
- * @param int $cid Contact id
- * @param string $avatar Link to avatar picture
- * @param bool $force force picture update
+ * @param int $cid Contact id
+ * @param string $avatar Link to avatar picture
+ * @param bool $force force picture update
+ * @param bool $create_cache Enforces the creation of cached avatar fields
*
* @return void
* @throws HTTPException\InternalServerErrorException
* @throws HTTPException\NotFoundException
* @throws \ImagickException
*/
- public static function updateAvatar(int $cid, string $avatar, bool $force = false)
+ public static function updateAvatar(int $cid, string $avatar, bool $force = false, bool $create_cache = false)
{
- $contact = DBA::selectFirst('contact', ['uid', 'avatar', 'photo', 'thumb', 'micro', 'nurl'], ['id' => $cid, 'self' => false]);
+ $contact = DBA::selectFirst('contact', ['uid', 'avatar', 'photo', 'thumb', 'micro', 'nurl', 'url', 'network'], ['id' => $cid, 'self' => false]);
if (!DBA::isResult($contact)) {
return;
}
$uid = $contact['uid'];
// Only update the cached photo links of public contacts when they already are cached
- if (($uid == 0) && !$force && empty($contact['thumb']) && empty($contact['micro'])) {
+ if (($uid == 0) && !$force && empty($contact['thumb']) && empty($contact['micro']) && !$create_cache) {
if ($contact['avatar'] != $avatar) {
DBA::update('contact', ['avatar' => $avatar], ['id' => $cid]);
Logger::info('Only update the avatar', ['id' => $cid, 'avatar' => $avatar, 'contact' => $contact]);
return;
}
- $data = [
- $contact['photo'] ?? '',
- $contact['thumb'] ?? '',
- $contact['micro'] ?? '',
- ];
+ // User contacts use are updated through the public contacts
+ if (($uid != 0) && !in_array($contact['network'], [Protocol::FEED, Protocol::MAIL])) {
+ $pcid = self::getIdForURL($contact['url'], false);
+ if (!empty($pcid)) {
+ Logger::debug('Update the private contact via the public contact', ['id' => $cid, 'uid' => $uid, 'public' => $pcid]);
+ self::updateAvatar($pcid, $avatar, $force, true);
+ return;
+ }
+ }
+
+ // Replace cached avatar pictures from the default avatar with the default avatars in different sizes
+ if (strpos($avatar, self::DEFAULT_AVATAR_PHOTO)) {
+ $fields = ['avatar' => $avatar, 'avatar-date' => DateTimeFormat::utcNow(),
+ 'photo' => DI::baseUrl() . self::DEFAULT_AVATAR_PHOTO,
+ 'thumb' => DI::baseUrl() . self::DEFAULT_AVATAR_THUMB,
+ 'micro' => DI::baseUrl() . self::DEFAULT_AVATAR_MICRO];
+ Logger::debug('Use default avatar', ['id' => $cid, 'uid' => $uid]);
+ }
- $update = ($contact['avatar'] != $avatar) || $force;
+ // Use the data from the self account
+ if (empty($fields)) {
+ $local_uid = User::getIdForURL($contact['url']);
+ if (!empty($local_uid)) {
+ $fields = self::selectFirst(['avatar', 'avatar-date', 'photo', 'thumb', 'micro'], ['self' => true, 'uid' => $local_uid]);
+ Logger::debug('Use owner data', ['id' => $cid, 'uid' => $uid, 'owner-uid' => $local_uid]);
+ }
+ }
- 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 (empty($fields)) {
+ $update = ($contact['avatar'] != $avatar) || $force;
+
+ if (!$update) {
+ $data = [
+ $contact['photo'] ?? '',
+ $contact['thumb'] ?? '',
+ $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::debug('Regenerating avatar', ['contact uid' => $uid, 'cid' => $cid, 'missing photo' => $image_rid, 'avatar' => $contact['avatar']]);
+ $update = true;
+ }
}
}
+
+ 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()];
+ $update = !empty($fields);
+ Logger::debug('Created new cached avatars', ['id' => $cid, 'uid' => $uid, 'owner-uid' => $local_uid]);
+ } else {
+ $update = false;
+ }
+ }
+ } else {
+ $update = ($fields['photo'] . $fields['thumb'] . $fields['micro'] != $contact['photo'] . $contact['thumb'] . $contact['micro']) || $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]);
- } 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]);
+ if (!$update) {
+ return;
+ }
+
+ $cids = [];
+ $uids = [];
+ if (($uid == 0) && !in_array($contact['network'], [Protocol::FEED, Protocol::MAIL])) {
+ // Collect all user contacts of the given public contact
+ $personal_contacts = DBA::select('contact', ['id', 'uid'],
+ ["`nurl` = ? AND `id` != ? AND NOT `self`", $contact['nurl'], $cid]);
+ while ($personal_contact = DBA::fetch($personal_contacts)) {
+ $cids[] = $personal_contact['id'];
+ $uids[] = $personal_contact['uid'];
+ }
+ DBA::close($personal_contacts);
+
+ if (!empty($cids)) {
+ // Delete possibly existing cached user contact avatars
+ Photo::delete(['uid' => $uids, 'contact-id' => $cids, 'album' => Photo::CONTACT_PHOTOS]);
}
}
+
+ $cids[] = $cid;
+ $uids[] = $uid;
+ Logger::info('Updating cached contact avatars', ['cid' => $cids, 'uid' => $uids, 'fields' => $fields]);
+ DBA::update('contact', $fields, ['id' => $cids]);
}
/**
/**
* @param integer $id contact id
* @param string $network Optional network we are probing for
- * @param boolean $force Optional forcing of network probing (otherwise we use the cached data)
* @return boolean
* @throws HTTPException\InternalServerErrorException
* @throws \ImagickException
*/
- public static function updateFromProbe(int $id, string $network = '', bool $force = false)
+ public static function updateFromProbe(int $id, string $network = '')
+ {
+ $contact = DBA::selectFirst('contact', ['uid', 'url'], ['id' => $id]);
+ if (!DBA::isResult($contact)) {
+ return false;
+ }
+
+ $ret = Probe::uri($contact['url'], $network, $contact['uid']);
+ return self::updateFromProbeArray($id, $ret);
+ }
+
+ /**
+ * @param integer $id contact id
+ * @param array $ret Probed data
+ * @return boolean
+ * @throws HTTPException\InternalServerErrorException
+ * @throws \ImagickException
+ */
+ private static function updateFromProbeArray(int $id, array $ret)
{
/*
Warning: Never ever fetch the public key via Probe::uri and write it into the contacts.
$fields = ['uid', 'avatar', 'name', 'nick', 'location', 'keywords', 'about', 'subscribe',
'unsearchable', 'url', 'addr', 'batch', 'notify', 'poll', 'request', 'confirm', 'poco',
- 'network', 'alias', 'baseurl', 'gsid', 'forum', 'prv', 'contact-type', 'pubkey'];
+ 'network', 'alias', 'baseurl', 'gsid', 'forum', 'prv', 'contact-type', 'pubkey', 'last-item'];
$contact = DBA::selectFirst('contact', $fields, ['id' => $id]);
if (!DBA::isResult($contact)) {
return false;
$contact['photo'] = $contact['avatar'];
unset($contact['avatar']);
- $ret = Probe::uri($contact['url'], $network, $uid, !$force);
-
$updated = DateTimeFormat::utcNow();
// We must not try to update relay contacts via probe. They are no real contacts.
// If Probe::uri fails the network code will be different ("feed" or "unkn")
if (in_array($ret['network'], [Protocol::FEED, Protocol::PHANTOM]) && ($ret['network'] != $contact['network'])) {
- if ($force && ($uid == 0)) {
+ if ($uid == 0) {
self::updateContact($id, $uid, $ret['url'], ['failed' => true, 'last-update' => $updated, 'failure_update' => $updated]);
}
return false;
}
}
- $new_pubkey = $ret['pubkey'];
+ $new_pubkey = $ret['pubkey'] ?? '';
+
+ if ($uid == 0) {
+ $ret['last-item'] = Probe::getLastUpdate($ret);
+ Logger::info('Fetched last item', ['id' => $id, 'probed_url' => $ret['url'], 'last-item' => $ret['last-item'], 'callstack' => System::callstack(20)]);
+ }
$update = false;
}
}
+ if (!empty($ret['last-item']) && ($contact['last-item'] < $ret['last-item'])) {
+ $update = true;
+ } else {
+ unset($ret['last-item']);
+ }
+
if (!empty($ret['photo']) && ($ret['network'] != Protocol::FEED)) {
- self::updateAvatar($id, $ret['photo'], $update || $force);
+ self::updateAvatar($id, $ret['photo'], $update);
}
if (!$update) {
- if ($force) {
- self::updateContact($id, $uid, $ret['url'], ['failed' => false, 'last-update' => $updated, 'success_update' => $updated]);
- }
+ self::updateContact($id, $uid, $ret['url'], ['failed' => false, 'last-update' => $updated, 'success_update' => $updated]);
// Update the public contact
if ($uid != 0) {
- self::updateFromProbeByURL($ret['url']);
+ $contact = self::getByURL($ret['url'], false, ['id']);
+ if (!empty($contact['id'])) {
+ self::updateFromProbeArray($contact['id'], $ret);
+ }
}
return true;
$ret['name-date'] = $updated;
}
- if ($force && ($uid == 0)) {
+ if ($uid == 0) {
$ret['last-update'] = $updated;
$ret['success_update'] = $updated;
$ret['failed'] = false;
return true;
}
- public static function updateFromProbeByURL($url, $force = false)
+ /**
+ * @param integer $url contact url
+ * @return integer Contact id
+ * @throws HTTPException\InternalServerErrorException
+ * @throws \ImagickException
+ */
+ public static function updateFromProbeByURL($url)
{
$id = self::getIdForURL($url);
return $id;
}
- self::updateFromProbe($id, '', $force);
+ self::updateFromProbe($id);
return $id;
}
if (!empty($arr['contact']['name'])) {
$ret = $arr['contact'];
} else {
- $ret = Probe::uri($url, $network, $user['uid'], false);
+ $ret = Probe::uri($url, $network, $user['uid']);
}
if (($network != '') && ($ret['network'] != $network)) {
}
// Ensure to always have the correct network type, independent from the connection request method
- self::updateFromProbe($contact['id'], '', true);
+ self::updateFromProbe($contact['id']);
return true;
} else {
$contact_id = DBA::lastInsertId();
// Ensure to always have the correct network type, independent from the connection request method
- self::updateFromProbe($contact_id, '', true);
+ self::updateFromProbe($contact_id);
self::updateAvatar($contact_id, $photo, true);
return $url ?: $contact_url; // Equivalent to: ($url != '') ? $url : $contact_url;
}
- $data = self::getProbeDataFromDatabase($contact_url);
- if (empty($data)) {
+ $contact = self::getByURL($contact_url, false);
+ if (empty($contact)) {
return $url ?: $contact_url; // Equivalent to: ($url != '') ? $url : $contact_url;
}
// Prevents endless loop in case only a non-public contact exists for the contact URL
- unset($data['uid']);
+ unset($contact['uid']);
- return self::magicLinkByContact($data, $url ?: $contact_url);
+ return self::magicLinkByContact($contact, $url ?: $contact_url);
}
/**