use Friendica\Network\Probe;
use Friendica\Protocol\Activity;
use Friendica\Protocol\ActivityPub;
-use Friendica\Protocol\DFRN;
use Friendica\Protocol\Diaspora;
use Friendica\Protocol\OStatus;
use Friendica\Protocol\Salmon;
$fields['gsid'] = GServer::getID($fields['baseurl'], true);
}
+ $fields['uri-id'] = ItemURI::getIdByURI($fields['url']);
+
if (empty($fields['created'])) {
$fields['created'] = DateTimeFormat::utcNow();
}
return false;
}
- $cdata = self::getPublicAndUserContacID($cid, $uid);
+ $cdata = self::getPublicAndUserContactID($cid, $uid);
if (empty($cdata['user'])) {
return false;
}
return false;
}
- $cdata = self::getPublicAndUserContacID($cid, $uid);
+ $cdata = self::getPublicAndUserContactID($cid, $uid);
if (empty($cdata['user'])) {
return false;
}
*/
public static function isLocal($url)
{
+ if (!parse_url($url, PHP_URL_SCHEME)) {
+ $addr_parts = explode('@', $url);
+ return (count($addr_parts) == 2) && ($addr_parts[1] == DI::baseUrl()->getHostname());
+ }
+
return Strings::compareLink(self::getBasepath($url, true), DI::baseUrl());
}
* @throws HTTPException\InternalServerErrorException
* @throws \ImagickException
*/
- public static function getPublicAndUserContacID($cid, $uid)
+ public static function getPublicAndUserContactID($cid, $uid)
+ {
+ // We have to use the legacy function as long as the post update hasn't finished
+ if (DI::config()->get('system', 'post_update_version') < 1427) {
+ return self::legacyGetPublicAndUserContactID($cid, $uid);
+ }
+
+ if (empty($uid) || empty($cid)) {
+ return [];
+ }
+
+ $contact = DBA::selectFirst('account-user-view', ['id', 'uid', 'pid'], ['id' => $cid]);
+ if (!DBA::isResult($contact) || !in_array($contact['uid'], [0, $uid])) {
+ return [];
+ }
+
+ $pcid = $contact['pid'];
+ if ($contact['uid'] == $uid) {
+ $ucid = $contact['id'];
+ } else {
+ $contact = DBA::selectFirst('account-user-view', ['id', 'uid'], ['pid' => $cid, 'uid' => $uid]);
+ if (DBA::isResult($contact)) {
+ $ucid = $contact['id'];
+ } else {
+ $ucid = 0;
+ }
+ }
+
+ return ['public' => $pcid, 'user' => $ucid];
+ }
+
+ /**
+ * Helper function for "getPublicAndUserContactID"
+ *
+ * @param int $cid Either public contact id or user's contact id
+ * @param int $uid User ID
+ *
+ * @return array with public and user's contact id
+ * @throws HTTPException\InternalServerErrorException
+ * @throws \ImagickException
+ */
+ private static function legacyGetPublicAndUserContactID($cid, $uid)
{
if (empty($uid) || empty($cid)) {
return [];
*/
public static function createSelfFromUserId($uid)
{
- // Only create the entry if it doesn't exist yet
- if (DBA::exists('contact', ['uid' => $uid, 'self' => true])) {
- return true;
- }
-
$user = DBA::selectFirst('user', ['uid', 'username', 'nickname', 'pubkey', 'prvkey'],
['uid' => $uid, 'account_expired' => false]);
if (!DBA::isResult($user)) {
return false;
}
- $return = DBA::insert('contact', [
+ $contact = [
'uid' => $user['uid'],
'created' => DateTimeFormat::utcNow(),
'self' => 1,
'nick' => $user['nickname'],
'pubkey' => $user['pubkey'],
'prvkey' => $user['prvkey'],
- 'photo' => DI::baseUrl() . '/photo/profile/' . $user['uid'] . '.jpg',
- 'thumb' => DI::baseUrl() . '/photo/avatar/' . $user['uid'] . '.jpg',
- 'micro' => DI::baseUrl() . '/photo/micro/' . $user['uid'] . '.jpg',
+ 'photo' => User::getAvatarUrlForId($user['uid']),
+ 'thumb' => User::getAvatarUrlForId($user['uid'], Proxy::SIZE_THUMB),
+ 'micro' => User::getAvatarUrlForId($user['uid'], Proxy::SIZE_MICRO),
'blocked' => 0,
'pending' => 0,
'url' => DI::baseUrl() . '/profile/' . $user['nickname'],
'uri-date' => DateTimeFormat::utcNow(),
'avatar-date' => DateTimeFormat::utcNow(),
'closeness' => 0
- ]);
+ ];
+
+ $return = true;
+
+ // Only create the entry if it doesn't exist yet
+ if (!DBA::exists('contact', ['uid' => $uid, 'self' => true])) {
+ $return = DBA::insert('contact', $contact);
+ }
+
+ // Create the public contact
+ if (!DBA::exists('contact', ['nurl' => $contact['nurl'], 'uid' => 0])) {
+ $contact['self'] = false;
+ $contact['uid'] = 0;
+ $contact['prvkey'] = null;
+
+ DBA::insert('contact', $contact, Database::INSERT_IGNORE);
+ }
return $return;
}
*
* @param int $uid
* @param boolean $update_avatar Force the avatar update
+ * @return bool "true" if updated
* @throws HTTPException\InternalServerErrorException
*/
public static function updateSelfFromUserID($uid, $update_avatar = false)
{
$fields = ['id', 'name', 'nick', 'location', 'about', 'keywords', 'avatar', 'prvkey', 'pubkey',
- 'xmpp', 'contact-type', 'forum', 'prv', 'avatar-date', 'url', 'nurl', 'unsearchable',
- 'photo', 'thumb', 'micro', 'addr', 'request', 'notify', 'poll', 'confirm', 'poco'];
+ 'xmpp', 'matrix', 'contact-type', 'forum', 'prv', 'avatar-date', 'url', 'nurl', 'unsearchable',
+ 'photo', 'thumb', 'micro', 'addr', 'request', 'notify', 'poll', 'confirm', 'poco', 'network'];
$self = DBA::selectFirst('contact', $fields, ['uid' => $uid, 'self' => true]);
if (!DBA::isResult($self)) {
- return;
+ return false;
}
$fields = ['nickname', 'page-flags', 'account-type', 'prvkey', 'pubkey'];
$user = DBA::selectFirst('user', $fields, ['uid' => $uid, 'account_expired' => false]);
if (!DBA::isResult($user)) {
- return;
+ return false;
}
$fields = ['name', 'photo', 'thumb', 'about', 'address', 'locality', 'region',
- 'country-name', 'pub_keywords', 'xmpp', 'net-publish'];
+ 'country-name', 'pub_keywords', 'xmpp', 'matrix', 'net-publish'];
$profile = DBA::selectFirst('profile', $fields, ['uid' => $uid]);
if (!DBA::isResult($profile)) {
- return;
+ return false;
}
$file_suffix = 'jpg';
'avatar-date' => $self['avatar-date'], 'location' => Profile::formatLocation($profile),
'about' => $profile['about'], 'keywords' => $profile['pub_keywords'],
'contact-type' => $user['account-type'], 'prvkey' => $user['prvkey'],
- 'pubkey' => $user['pubkey'], 'xmpp' => $profile['xmpp']];
+ 'pubkey' => $user['pubkey'], 'xmpp' => $profile['xmpp'], 'matrix' => $profile['matrix'], 'network' => Protocol::DFRN];
// it seems as if ported accounts can have wrong values, so we make sure that now everything is fine.
$fields['url'] = DI::baseUrl() . '/profile/' . $user['nickname'];
$fields['micro'] = self::getDefaultAvatar($fields, Proxy::SIZE_MICRO);
}
- $fields['avatar'] = DI::baseUrl() . '/photo/profile/' .$uid . '.' . $file_suffix;
+ $fields['avatar'] = User::getAvatarUrlForId($uid);
$fields['forum'] = $user['page-flags'] == User::PAGE_FLAGS_COMMUNITY;
$fields['prv'] = $user['page-flags'] == User::PAGE_FLAGS_PRVGROUP;
$fields['unsearchable'] = !$profile['net-publish'];
DBA::update('contact', $fields, ['id' => $self['id']]);
// Update the public contact as well
+ $fields['prvkey'] = null;
+ $fields['self'] = false;
DBA::update('contact', $fields, ['uid' => 0, 'nurl' => $self['nurl']]);
// Update the profile
- $fields = ['photo' => DI::baseUrl() . '/photo/profile/' .$uid . '.' . $file_suffix,
- 'thumb' => DI::baseUrl() . '/photo/avatar/' . $uid .'.' . $file_suffix];
+ $fields = [
+ 'photo' => User::getAvatarUrlForId($uid),
+ 'thumb' => User::getAvatarUrlForId($uid, Proxy::SIZE_THUMB)
+ ];
+
DBA::update('profile', $fields, ['uid' => $uid]);
}
+
+ return $update;
}
/**
}
$protocol = $contact['network'];
- if (($protocol == Protocol::DFRN) && !self::isLegacyDFRNContact($contact)) {
- $protocol = Protocol::ACTIVITYPUB;
+ if (($protocol == Protocol::DFRN) && !empty($contact['protocol'])) {
+ $protocol = $contact['protocol'];
}
- if (($protocol == Protocol::DFRN) && $dissolve) {
- DFRN::deliver($user, $contact, 'placeholder', true);
- } elseif (in_array($protocol, [Protocol::OSTATUS, Protocol::DFRN])) {
+ if (in_array($protocol, [Protocol::OSTATUS, Protocol::DFRN])) {
// create an unfollow slap
$item = [];
$item['verb'] = Activity::O_UNFOLLOW;
if ($dissolve) {
ActivityPub\Transmitter::sendContactReject($contact['url'], $contact['hub-verify'], $user['uid']);
}
+ } else {
+ $hook_data = [
+ 'contact' => $contact,
+ 'dissolve' => $dissolve,
+ ];
+ Hook::callAll('unfollow', $hook_data);
}
}
return 0;
}
- $contact = self::getByURL($url, false, ['id', 'network'], $uid);
+ $contact = self::getByURL($url, false, ['id', 'network', 'uri-id'], $uid);
if (!empty($contact)) {
$contact_id = $contact["id"];
- if (empty($update)) {
+ if (empty($update) && (!empty($contact['uri-id']) || is_bool($update))) {
Logger::debug('Contact found', ['url' => $url, 'uid' => $uid, 'update' => $update, 'cid' => $contact_id]);
return $contact_id;
}
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',
+ $fields = ['name', 'nick', 'url', 'addr', 'alias', 'avatar', 'header', 'contact-type',
'keywords', 'location', 'about', 'unsearchable', 'batch', 'notify', 'poll',
'request', 'confirm', 'poco', 'subscribe', 'network', 'baseurl', 'gsid'];
if ($thread_mode) {
$items = Post::toArray(Post::selectForUser(local_user(), ['uri-id', 'gravity', 'parent-uri-id', 'thr-parent-id', 'author-id'], $condition, $params));
- $o .= conversation($a, $items, 'contacts', $update, false, 'commented', local_user());
+ $o .= DI::conversation()->create($items, 'contacts', $update, false, 'commented', local_user());
} else {
$items = Post::toArray(Post::selectForUser(local_user(), Item::DISPLAY_FIELDLIST, $condition, $params));
- $o .= conversation($a, $items, 'contact-posts', $update);
+ $o .= DI::conversation()->create($items, 'contact-posts', $update);
}
if (!$update) {
* @param bool $no_update Don't perfom an update if no cached avatar was found
* @return string photo path
*/
- private static function getAvatarPath(array $contact, string $field, string $size, string $avatar, $no_update = false)
+ private static function getAvatarPath(array $contact, string $size, $no_update = false)
{
- if (!empty($contact)) {
- $contact = self::checkAvatarCacheByArray($contact, $no_update);
- if (!empty($contact[$field])) {
- $avatar = $contact[$field];
- }
- }
-
- if ($no_update && empty($avatar) && !empty($contact['avatar'])) {
- $avatar = $contact['avatar'];
- }
-
- if (empty($avatar)) {
- $avatar = self::getDefaultAvatar([], $size);
- }
-
- if (Proxy::isLocalImage($avatar)) {
- return $avatar;
- } else {
- return Proxy::proxifyUrl($avatar, false, $size);
- }
+ $contact = self::checkAvatarCacheByArray($contact, $no_update);
+ return self::getAvatarUrlForId($contact['id'], $size, $contact['updated'] ?? '');
}
/**
* Return the photo path for a given contact array
*
* @param array $contact Contact array
- * @param string $avatar Avatar path that is displayed when no photo had been found
* @param bool $no_update Don't perfom an update if no cached avatar was found
* @return string photo path
*/
- public static function getPhoto(array $contact, string $avatar = '', bool $no_update = false)
+ public static function getPhoto(array $contact, bool $no_update = false)
{
- return self::getAvatarPath($contact, 'photo', Proxy::SIZE_SMALL, $avatar, $no_update);
+ return self::getAvatarPath($contact, Proxy::SIZE_SMALL, $no_update);
}
/**
* Return the photo path (thumb size) for a given contact array
*
* @param array $contact Contact array
- * @param string $avatar Avatar path that is displayed when no photo had been found
* @param bool $no_update Don't perfom an update if no cached avatar was found
* @return string photo path
*/
- public static function getThumb(array $contact, string $avatar = '', bool $no_update = false)
+ public static function getThumb(array $contact, bool $no_update = false)
{
- return self::getAvatarPath($contact, 'thumb', Proxy::SIZE_THUMB, $avatar, $no_update);
+ return self::getAvatarPath($contact, Proxy::SIZE_THUMB, $no_update);
}
/**
* Return the photo path (micro size) for a given contact array
*
* @param array $contact Contact array
- * @param string $avatar Avatar path that is displayed when no photo had been found
* @param bool $no_update Don't perfom an update if no cached avatar was found
* @return string photo path
*/
- public static function getMicro(array $contact, string $avatar = '', bool $no_update = false)
+ public static function getMicro(array $contact, bool $no_update = false)
{
- return self::getAvatarPath($contact, 'micro', Proxy::SIZE_MICRO, $avatar, $no_update);
+ return self::getAvatarPath($contact, Proxy::SIZE_MICRO, $no_update);
}
/**
*
* @param array $contact contact array
* @param string $size Size of the avatar picture
- * @return void
+ * @return string avatar URL
*/
public static function getDefaultAvatar(array $contact, string $size)
{
return DI::baseUrl() . $default;
}
+ /**
+ * Get avatar link for given contact id
+ *
+ * @param integer $cid contact id
+ * @param string $size One of the ProxyUtils::SIZE_* constants
+ * @param string $updated Contact update date
+ * @return string avatar link
+ */
+ public static function getAvatarUrlForId(int $cid, string $size = '', string $updated = ''):string
+ {
+ // We have to fetch the "updated" variable when it wasn't provided
+ // The parameter can be provided to improve performance
+ if (empty($updated)) {
+ $contact = self::getById($cid, ['updated']);
+ $updated = $contact['updated'] ?? '';
+ }
+
+ $url = DI::baseUrl() . '/photo/contact/';
+ switch ($size) {
+ case Proxy::SIZE_MICRO:
+ $url .= Proxy::PIXEL_MICRO . '/';
+ break;
+ case Proxy::SIZE_THUMB:
+ $url .= Proxy::PIXEL_THUMB . '/';
+ break;
+ case Proxy::SIZE_SMALL:
+ $url .= Proxy::PIXEL_SMALL . '/';
+ break;
+ case Proxy::SIZE_MEDIUM:
+ $url .= Proxy::PIXEL_MEDIUM . '/';
+ break;
+ case Proxy::SIZE_LARGE:
+ $url .= Proxy::PIXEL_LARGE . '/';
+ break;
+ }
+ return $url . $cid . ($updated ? '?ts=' . strtotime($updated) : '');
+ }
+
+ /**
+ * Get avatar link for given contact URL
+ *
+ * @param string $url contact url
+ * @param integer $uid user id
+ * @param string $size One of the ProxyUtils::SIZE_* constants
+ * @return string avatar link
+ */
+ public static function getAvatarUrlForUrl(string $url, int $uid, string $size = ''):string
+ {
+ $condition = ["`nurl` = ? AND ((`uid` = ? AND `network` IN (?, ?)) OR `uid` = ?)",
+ Strings::normaliseLink($url), $uid, Protocol::FEED, Protocol::MAIL, 0];
+ $contact = self::selectFirst(['id', 'updated'], $condition, ['order' => ['uid' => true]]);
+ return self::getAvatarUrlForId($contact['id'] ?? 0, $size, $contact['updated'] ?? '');
+ }
+
+ /**
+ * Get header link for given contact id
+ *
+ * @param integer $cid contact id
+ * @param string $size One of the ProxyUtils::SIZE_* constants
+ * @param string $updated Contact update date
+ * @return string header link
+ */
+ public static function getHeaderUrlForId(int $cid, string $size = '', string $updated = ''):string
+ {
+ // We have to fetch the "updated" variable when it wasn't provided
+ // The parameter can be provided to improve performance
+ if (empty($updated)) {
+ $contact = self::getById($cid, ['updated']);
+ $updated = $contact['updated'] ?? '';
+ }
+
+ $url = DI::baseUrl() . '/photo/header/';
+ switch ($size) {
+ case Proxy::SIZE_MICRO:
+ $url .= Proxy::PIXEL_MICRO . '/';
+ break;
+ case Proxy::SIZE_THUMB:
+ $url .= Proxy::PIXEL_THUMB . '/';
+ break;
+ case Proxy::SIZE_SMALL:
+ $url .= Proxy::PIXEL_SMALL . '/';
+ break;
+ case Proxy::SIZE_MEDIUM:
+ $url .= Proxy::PIXEL_MEDIUM . '/';
+ break;
+ case Proxy::SIZE_LARGE:
+ $url .= Proxy::PIXEL_LARGE . '/';
+ break;
+ }
+
+ return $url . $cid . ($updated ? '?ts=' . strtotime($updated) : '');
+ }
+
/**
* Updates the avatar links in a contact only if needed
*
// 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);
+ $pcid = self::getIdForURL($contact['url'], 0, 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);
*/
private static function updateContact(int $id, int $uid, string $old_url, string $new_url, array $fields)
{
- if (Strings::normaliseLink($new_url) != Strings::normaliseLink($old_url)) {
- Logger::notice('New URL differs from old URL', ['old' => $old_url, 'new' => $new_url]);
- // @todo It is to decide what to do when the URL is changed
- }
-
if (!DBA::update('contact', $fields, ['id' => $id])) {
Logger::info('Couldn\'t update contact.', ['id' => $id, 'fields' => $fields]);
return;
*/
// These fields aren't updated by this routine:
- // 'xmpp', 'sensitive'
+ // 'sensitive'
- $fields = ['uid', 'avatar', 'name', 'nick', 'location', 'keywords', 'about', 'subscribe', 'manually-approve',
- 'unsearchable', 'url', 'addr', 'batch', 'notify', 'poll', 'request', 'confirm', 'poco',
- 'network', 'alias', 'baseurl', 'gsid', 'forum', 'prv', 'contact-type', 'pubkey', 'last-item'];
+ $fields = ['uid', 'uri-id', 'avatar', 'header', 'name', 'nick', 'location', 'keywords', 'about', 'subscribe',
+ 'manually-approve', 'unsearchable', 'url', 'addr', 'batch', 'notify', 'poll', 'request', 'confirm', 'poco',
+ 'network', 'alias', 'baseurl', 'gsid', 'forum', 'prv', 'contact-type', 'pubkey', 'last-item', 'xmpp', 'matrix'];
$contact = DBA::selectFirst('contact', $fields, ['id' => $id]);
if (!DBA::isResult($contact)) {
return false;
}
if (self::isLocal($ret['url'])) {
- Logger::info('Local contacts are not updated here.');
+ if ($contact['uid'] == 0) {
+ Logger::info('Local contacts are not updated here.');
+ } else {
+ self::updateFromPublicContact($id, $contact);
+ }
return true;
}
$uid = $contact['uid'];
unset($contact['uid']);
+ $uriid = $contact['uri-id'];
+ unset($contact['uri-id']);
+
$pubkey = $contact['pubkey'];
unset($contact['pubkey']);
$updated = DateTimeFormat::utcNow();
+ if (Strings::normaliseLink($contact['url']) != Strings::normaliseLink($ret['url'])) {
+ Logger::notice('New URL differs from old URL', ['id' => $id, 'uid' => $uid, 'old' => $contact['url'], 'new' => $ret['url']]);
+ self::updateContact($id, $uid, $contact['url'], $ret['url'], ['failed' => true, 'last-update' => $updated, 'failure_update' => $updated]);
+ return false;
+ }
+
// We must not try to update relay contacts via probe. They are no real contacts.
// We check after the probing to be able to correct falsely detected contact types.
if (($contact['contact-type'] == self::TYPE_RELAY) &&
return false;
}
+ if (Strings::normaliseLink($ret['url']) != Strings::normaliseLink($contact['url'])) {
+ $cid = self::getIdForURL($ret['url'], 0, false);
+ if (!empty($cid) && ($cid != $id)) {
+ Logger::notice('URL of contact changed.', ['id' => $id, 'new_id' => $cid, 'old' => $contact['url'], 'new' => $ret['url']]);
+ return self::updateFromProbeArray($cid, $ret);
+ }
+ }
+
if (isset($ret['hide']) && is_bool($ret['hide'])) {
$ret['unsearchable'] = $ret['hide'];
}
}
$update = false;
+ $guid = $ret['guid'] ?? '';
// make sure to not overwrite existing values with blank entries except some technical fields
$keep = ['batch', 'notify', 'poll', 'request', 'confirm', 'poco', 'baseurl'];
unset($ret['last-item']);
}
+ if (empty($uriid)) {
+ $update = true;
+ }
+
if (!empty($ret['photo']) && ($ret['network'] != Protocol::FEED)) {
self::updateAvatar($id, $ret['photo'], $update);
}
return true;
}
- $ret['nurl'] = Strings::normaliseLink($ret['url']);
+ if (empty($guid)) {
+ $ret['uri-id'] = ItemURI::getIdByURI($ret['url']);
+ } else {
+ $ret['uri-id'] = ItemURI::insert(['uri' => $ret['url'], 'guid' => $guid]);
+ }
+
+ $ret['nurl'] = Strings::normaliseLink($ret['url']);
$ret['updated'] = $updated;
- $ret['failed'] = false;
+ $ret['failed'] = false;
// Only fill the pubkey if it had been empty before. We have to prevent identity theft.
if (empty($pubkey) && !empty($new_pubkey)) {
}
if ((!empty($ret['addr']) && ($ret['addr'] != $contact['addr'])) || (!empty($ret['alias']) && ($ret['alias'] != $contact['alias']))) {
- $ret['uri-date'] = DateTimeFormat::utcNow();
+ $ret['uri-date'] = $updated;
}
- if (($ret['name'] != $contact['name']) || ($ret['nick'] != $contact['nick'])) {
+ if ((!empty($ret['name']) && ($ret['name'] != $contact['name'])) || (!empty($ret['nick']) && ($ret['nick'] != $contact['nick']))) {
$ret['name-date'] = $updated;
}
return true;
}
+ private static function updateFromPublicContact(int $id, array $contact)
+ {
+ $public = self::getByURL($contact['url'], false);
+
+ $fields = [];
+
+ foreach ($contact as $field => $value) {
+ if ($field == 'uid') {
+ continue;
+ }
+ if ($public[$field] != $value) {
+ $fields[$field] = $public[$field];
+ }
+ }
+ if (!empty($fields)) {
+ DBA::update('contact', $fields, ['id' => $id, 'self' => false]);
+ Logger::info('Updating local contact', ['id' => $id]);
+ }
+ }
+
/**
* @param integer $url contact url
* @return integer Contact id
return $id;
}
- /**
- * Detects if a given contact array belongs to a legacy DFRN connection
- *
- * @param array $contact
- * @return boolean
- */
- public static function isLegacyDFRNContact($contact)
- {
- // Newer Friendica contacts are connected via AP, then these fields aren't set
- return !empty($contact['dfrn-id']) || !empty($contact['issued-id']);
- }
-
/**
* Detects the communication protocol for a given contact url.
* This is used to detect Friendica contacts that we can communicate via AP.
*
* Takes a $uid and a url/handle and adds a new contact
*
- * @param array $user The user the contact should be created for
+ * @param int $uid The user id the contact should be created for
* @param string $url The profile URL of the contact
- * @param bool $interactive
* @param string $network
* @return array
* @throws HTTPException\InternalServerErrorException
* @throws HTTPException\NotFoundException
* @throws \ImagickException
*/
- public static function createFromProbe(array $user, $url, $interactive = false, $network = '')
+ public static function createFromProbeForUser(int $uid, $url, $network = '')
{
$result = ['cid' => -1, 'success' => false, 'message' => ''];
}
if (!empty($arr['contact']['name'])) {
+ $probed = false;
$ret = $arr['contact'];
} else {
- $ret = Probe::uri($url, $network, $user['uid']);
+ $probed = true;
+ $ret = Probe::uri($url, $network, $uid);
}
if (($network != '') && ($ret['network'] != $network)) {
// the poll url is more reliable than the profile url, as we may have
// indirect links or webfinger links
- $condition = ['uid' => $user['uid'], 'poll' => [$ret['poll'], Strings::normaliseLink($ret['poll'])], 'network' => $ret['network'], 'pending' => false];
+ $condition = ['uid' => $uid, 'poll' => [$ret['poll'], Strings::normaliseLink($ret['poll'])], 'network' => $ret['network'], 'pending' => false];
$contact = DBA::selectFirst('contact', ['id', 'rel'], $condition);
if (!DBA::isResult($contact)) {
- $condition = ['uid' => $user['uid'], 'nurl' => Strings::normaliseLink($ret['url']), 'network' => $ret['network'], 'pending' => false];
+ $condition = ['uid' => $uid, 'nurl' => Strings::normaliseLink($ret['url']), 'network' => $ret['network'], 'pending' => false];
$contact = DBA::selectFirst('contact', ['id', 'rel'], $condition);
}
$protocol = self::getProtocol($ret['url'], $ret['network']);
- if (($protocol === Protocol::DFRN) && !DBA::isResult($contact)) {
- if ($interactive) {
- if (strlen(DI::baseUrl()->getUrlPath())) {
- $myaddr = bin2hex(DI::baseUrl() . '/profile/' . $user['nickname']);
- } else {
- $myaddr = bin2hex($user['nickname'] . '@' . DI::baseUrl()->getHostname());
- }
-
- DI::baseUrl()->redirect($ret['request'] . "&addr=$myaddr");
-
- // NOTREACHED
- }
- } elseif (DI::config()->get('system', 'dfrn_only') && ($ret['network'] != Protocol::DFRN)) {
- $result['message'] = DI::l10n()->t('This site is not configured to allow communications with other networks.') . EOL;
- $result['message'] .= DI::l10n()->t('No compatible communication protocols or feeds were discovered.') . EOL;
- return $result;
- }
-
// This extra param just confuses things, remove it
if ($protocol === Protocol::DIASPORA) {
$ret['url'] = str_replace('?absolute=true', '', $ret['url']);
// create contact record
self::insert([
- 'uid' => $user['uid'],
+ 'uid' => $uid,
'created' => DateTimeFormat::utcNow(),
'url' => $ret['url'],
'nurl' => Strings::normaliseLink($ret['url']),
]);
}
- $contact = DBA::selectFirst('contact', [], ['url' => $ret['url'], 'network' => $ret['network'], 'uid' => $user['uid']]);
+ $contact = DBA::selectFirst('contact', [], ['url' => $ret['url'], 'network' => $ret['network'], 'uid' => $uid]);
if (!DBA::isResult($contact)) {
$result['message'] .= DI::l10n()->t('Unable to retrieve contact information.') . EOL;
return $result;
$contact_id = $contact['id'];
$result['cid'] = $contact_id;
- Group::addMember(User::getDefaultGroup($user['uid'], $contact["network"]), $contact_id);
+ Group::addMember(User::getDefaultGroup($uid, $contact["network"]), $contact_id);
// Update the avatar
self::updateAvatar($contact_id, $ret['photo']);
// pull feed and consume it, which should subscribe to the hub.
if ($contact['network'] == Protocol::OSTATUS) {
Worker::add(PRIORITY_HIGH, 'OnePoll', $contact_id, 'force');
+ }
+
+ if ($probed) {
+ self::updateFromProbeArray($contact_id, $ret);
} else {
Worker::add(PRIORITY_HIGH, 'UpdateContact', $contact_id);
}
- $owner = User::getOwnerDataById($user['uid']);
+ $owner = User::getOwnerDataById($uid);
if (DBA::isResult($owner)) {
if (in_array($protocol, [Protocol::OSTATUS, Protocol::DFRN])) {
return false;
}
- $ret = ActivityPub\Transmitter::sendActivity('Follow', $contact['url'], $user['uid'], $activity_id);
+ $ret = ActivityPub\Transmitter::sendActivity('Follow', $contact['url'], $uid, $activity_id);
Logger::log('Follow returns: ' . $ret);
}
}
*/
public static function follow(int $cid, int $uid)
{
- $user = User::getById($uid);
- if (empty($user)) {
- return false;
- }
-
$contact = self::getById($cid, ['url']);
- $result = self::createFromProbe($user, $contact['url'], false);
+ $result = self::createFromProbeForUser($uid, $contact['url']);
return $result['cid'];
}
*/
public static function unfollow(int $cid, int $uid)
{
- $cdata = self::getPublicAndUserContacID($cid, $uid);
+ $cdata = self::getPublicAndUserContactID($cid, $uid);
if (empty($cdata['user'])) {
return false;
}
// Ensure to always have the correct network type, independent from the connection request method
self::updateFromProbe($contact['id']);
+ Post\UserNotification::insertNotication($contact['id'], Verb::getID(Activity::FOLLOW), $importer['uid']);
+
return true;
} else {
// send email notification to owner?
self::updateAvatar($contact_id, $photo, true);
+ Post\UserNotification::insertNotication($contact_id, Verb::getID(Activity::FOLLOW), $importer['uid']);
+
$contact_record = DBA::selectFirst('contact', ['id', 'network', 'name', 'url', 'photo'], ['id' => $contact_id]);
/// @TODO Encapsulate this into a function/method
}
} elseif (DBA::isResult($user) && in_array($user['page-flags'], [User::PAGE_FLAGS_SOAPBOX, User::PAGE_FLAGS_FREELOVE, User::PAGE_FLAGS_COMMUNITY])) {
if (($user['page-flags'] == User::PAGE_FLAGS_FREELOVE) && ($network != Protocol::DIASPORA)) {
- self::createFromProbe($importer, $url, false, $network);
+ self::createFromProbeForUser($importer['uid'], $url, $network);
}
$condition = ['uid' => $importer['uid'], 'url' => $url, 'pending' => true];
return null;
}
- public static function removeFollower($importer, $contact)
+ public static function removeFollower(array $contact)
{
- if (($contact['rel'] == self::FRIEND) || ($contact['rel'] == self::SHARING)) {
+ if (in_array($contact['rel'] ?? [], [self::FRIEND, self::SHARING])) {
DBA::update('contact', ['rel' => self::SHARING], ['id' => $contact['id']]);
- } else {
+ } elseif (!empty($contact['id'])) {
self::remove($contact['id']);
+ } else {
+ DI::logger()->info('Couldn\'t remove follower because of invalid contact array', ['contact' => $contact, 'callstack' => System::callstack()]);
}
}
$search .= '%';
$results = DBA::p("SELECT * FROM `contact`
- WHERE NOT `unsearchable` AND `network` IN (?, ?, ?, ?) AND
+ WHERE (NOT `unsearchable` OR `nurl` IN (SELECT `nurl` FROM `owner-view` where `publish` OR `net-publish`))
+ AND `network` IN (?, ?, ?, ?) AND
NOT `failed` AND `uid` = ? AND
(`addr` LIKE ? OR `name` LIKE ? OR `nick` LIKE ?) $extra_sql
ORDER BY `nurl` DESC LIMIT 1000",