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;
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 [];
'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'],
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',
+ '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)) {
}
$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 false;
'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'], 'network' => Protocol::DFRN];
+ '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, ['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]);
}
}
$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 ($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) {
{
$condition = ["`nurl` = ? AND ((`uid` = ? AND `network` IN (?, ?)) OR `uid` = ?)",
Strings::normaliseLink($url), $uid, Protocol::FEED, Protocol::MAIL, 0];
- $contact = self::selectFirst(['id', 'updated'], $condition);
+ $contact = self::selectFirst(['id', 'updated'], $condition, ['order' => ['uid' => true]]);
return self::getAvatarUrlForId($contact['id'] ?? 0, $size, $contact['updated'] ?? '');
}
// 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]);
- return;
- }
-
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', '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'];
+ '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;
$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) &&
}
if (Strings::normaliseLink($ret['url']) != Strings::normaliseLink($contact['url'])) {
- $cid = self::getIdForURL($ret['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 (empty($guid)) {
$ret['uri-id'] = ItemURI::getIdByURI($ret['url']);
} else {
- $ret['uri-id'] = ItemURI::insert(['uri' => $ret['uri'], 'guid' => $guid]);
+ $ret['uri-id'] = ItemURI::insert(['uri' => $ret['url'], 'guid' => $guid]);
}
$ret['nurl'] = Strings::normaliseLink($ret['url']);
}
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 $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' => ''];
$probed = false;
$ret = $arr['contact'];
} else {
- $probed = true;
- $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']);
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;
}
}
} 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()]);
}
}