From a5a7855d39d342d6586f755c7c07664a0d817cb6 Mon Sep 17 00:00:00 2001 From: Hypolite Petovan Date: Wed, 10 Jun 2020 10:36:42 -0400 Subject: [PATCH] Remove dependency to App in Contact::createFromProbe - Address https://github.com/friendica/friendica/issues/8473#issuecomment-641259906 --- mod/follow.php | 3 +-- mod/ostatus_subscribe.php | 2 +- mod/repair_ostatus.php | 2 +- mod/salmon.php | 10 +++------- src/Model/Contact.php | 36 ++++++++++++++++++------------------ src/Module/Contact.php | 5 ++--- src/Worker/AddContact.php | 7 ++++++- 7 files changed, 32 insertions(+), 33 deletions(-) diff --git a/mod/follow.php b/mod/follow.php index 58419dfd3b..8910f54c0c 100644 --- a/mod/follow.php +++ b/mod/follow.php @@ -40,7 +40,6 @@ function follow_post(App $a) DI::baseUrl()->redirect('contact'); } - $uid = local_user(); $url = Probe::cleanURI($_REQUEST['url']); $return_path = 'follow?url=' . urlencode($url); @@ -48,7 +47,7 @@ function follow_post(App $a) // This is just a precaution if maybe this page is called somewhere directly via POST $_SESSION['fastlane'] = $url; - $result = Contact::createFromProbe($uid, $url, true); + $result = Contact::createFromProbe($a->user, $url, true); if ($result['success'] == false) { // Possibly it is a remote item and not an account diff --git a/mod/ostatus_subscribe.php b/mod/ostatus_subscribe.php index bdf362e1ce..e5c4fa7b01 100644 --- a/mod/ostatus_subscribe.php +++ b/mod/ostatus_subscribe.php @@ -91,7 +91,7 @@ function ostatus_subscribe_content(App $a) $probed = Probe::uri($url); if ($probed['network'] == Protocol::OSTATUS) { - $result = Contact::createFromProbe($uid, $url, true, Protocol::OSTATUS); + $result = Contact::createFromProbe($a->user, $url, true, Protocol::OSTATUS); if ($result['success']) { $o .= ' - ' . DI::l10n()->t('success'); } else { diff --git a/mod/repair_ostatus.php b/mod/repair_ostatus.php index 6ceba80553..33e97499e5 100644 --- a/mod/repair_ostatus.php +++ b/mod/repair_ostatus.php @@ -70,7 +70,7 @@ function repair_ostatus_content(App $a) { $o .= "

".DI::l10n()->t("Keep this window open until done.")."

"; - Contact::createFromProbe($uid, $r[0]["url"], true); + Contact::createFromProbe($a->user, $r[0]["url"], true); DI::page()['htmlhead'] = ''; diff --git a/mod/salmon.php b/mod/salmon.php index 6eea57f6a7..1932d3ba41 100644 --- a/mod/salmon.php +++ b/mod/salmon.php @@ -42,15 +42,11 @@ function salmon_post(App $a, $xml = '') { $nick = (($a->argc > 1) ? Strings::escapeTags(trim($a->argv[1])) : ''); - $r = q("SELECT * FROM `user` WHERE `nickname` = '%s' AND `account_expired` = 0 AND `account_removed` = 0 LIMIT 1", - DBA::escape($nick) - ); - if (! DBA::isResult($r)) { + $importer = DBA::selectFirst('user', [], ['nickname' => $nick, 'account_expired' => false, 'account_removed' => false]); + if (! DBA::isResult($importer)) { throw new \Friendica\Network\HTTPException\InternalServerErrorException(); } - $importer = $r[0]; - // parse the xml $dom = simplexml_load_string($xml,'SimpleXMLElement',0, ActivityNamespace::SALMON_ME); @@ -175,7 +171,7 @@ function salmon_post(App $a, $xml = '') { Logger::log('Author ' . $author_link . ' unknown to user ' . $importer['uid'] . '.'); if (DI::pConfig()->get($importer['uid'], 'system', 'ostatus_autofriend')) { - $result = Contact::createFromProbe($importer['uid'], $author_link); + $result = Contact::createFromProbe($importer, $author_link); if ($result['success']) { $r = q("SELECT * FROM `contact` WHERE `network` = '%s' AND ( `url` = '%s' OR `alias` = '%s') diff --git a/src/Model/Contact.php b/src/Model/Contact.php index 02f9e829f2..ce4c0d1403 100644 --- a/src/Model/Contact.php +++ b/src/Model/Contact.php @@ -2272,20 +2272,20 @@ class Contact * $return['message'] error text if success is false. * * Takes a $uid and a url/handle and adds a new contact - * @param int $uid - * @param string $url + * + * @param array $user The user 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($uid, $url, $interactive = false, $network = '') + public static function createFromProbe(array $user, $url, $interactive = false, $network = '') { $result = ['cid' => -1, 'success' => false, 'message' => '']; - $a = DI::app(); - // remove ajax junk, e.g. Twitter $url = str_replace('/#!/', '/', $url); @@ -2316,7 +2316,7 @@ class Contact if (!empty($arr['contact']['name'])) { $ret = $arr['contact']; } else { - $ret = Probe::uri($url, $network, $uid, false); + $ret = Probe::uri($url, $network, $user['uid'], false); } if (($network != '') && ($ret['network'] != $network)) { @@ -2328,10 +2328,10 @@ class Contact // the poll url is more reliable than the profile url, as we may have // indirect links or webfinger links - $condition = ['uid' => $uid, 'poll' => [$ret['poll'], Strings::normaliseLink($ret['poll'])], 'network' => $ret['network'], 'pending' => false]; + $condition = ['uid' => $user['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' => $uid, 'nurl' => Strings::normaliseLink($url), 'network' => $ret['network'], 'pending' => false]; + $condition = ['uid' => $user['uid'], 'nurl' => Strings::normaliseLink($url), 'network' => $ret['network'], 'pending' => false]; $contact = DBA::selectFirst('contact', ['id', 'rel'], $condition); } @@ -2340,9 +2340,9 @@ class Contact if (($protocol === Protocol::DFRN) && !DBA::isResult($contact)) { if ($interactive) { if (strlen(DI::baseUrl()->getUrlPath())) { - $myaddr = bin2hex(DI::baseUrl() . '/profile/' . $a->user['nickname']); + $myaddr = bin2hex(DI::baseUrl() . '/profile/' . $user['nickname']); } else { - $myaddr = bin2hex($a->user['nickname'] . '@' . DI::baseUrl()->getHostname()); + $myaddr = bin2hex($user['nickname'] . '@' . DI::baseUrl()->getHostname()); } DI::baseUrl()->redirect($ret['request'] . "&addr=$myaddr"); @@ -2417,7 +2417,7 @@ class Contact // create contact record self::insert([ - 'uid' => $uid, + 'uid' => $user['uid'], 'created' => DateTimeFormat::utcNow(), 'url' => $ret['url'], 'nurl' => Strings::normaliseLink($ret['url']), @@ -2445,7 +2445,7 @@ class Contact ]); } - $contact = DBA::selectFirst('contact', [], ['url' => $ret['url'], 'network' => $ret['network'], 'uid' => $uid]); + $contact = DBA::selectFirst('contact', [], ['url' => $ret['url'], 'network' => $ret['network'], 'uid' => $user['uid']]); if (!DBA::isResult($contact)) { $result['message'] .= DI::l10n()->t('Unable to retrieve contact information.') . EOL; return $result; @@ -2454,16 +2454,16 @@ class Contact $contact_id = $contact['id']; $result['cid'] = $contact_id; - Group::addMember(User::getDefaultGroup($uid, $contact["network"]), $contact_id); + Group::addMember(User::getDefaultGroup($user['uid'], $contact["network"]), $contact_id); // Update the avatar - self::updateAvatar($ret['photo'], $uid, $contact_id); + self::updateAvatar($ret['photo'], $user['uid'], $contact_id); // pull feed and consume it, which should subscribe to the hub. Worker::add(PRIORITY_HIGH, "OnePoll", $contact_id, "force"); - $owner = User::getOwnerDataById($uid); + $owner = User::getOwnerDataById($user['uid']); if (DBA::isResult($owner)) { if (in_array($protocol, [Protocol::OSTATUS, Protocol::DFRN])) { @@ -2483,7 +2483,7 @@ class Contact Salmon::slapper($owner, $contact['notify'], $slap); } } elseif ($protocol == Protocol::DIASPORA) { - $ret = Diaspora::sendShare($a->user, $contact); + $ret = Diaspora::sendShare($owner, $contact); Logger::log('share returns: ' . $ret); } elseif ($protocol == Protocol::ACTIVITYPUB) { $activity_id = ActivityPub\Transmitter::activityIDFromContact($contact_id); @@ -2492,7 +2492,7 @@ class Contact return false; } - $ret = ActivityPub\Transmitter::sendActivity('Follow', $contact['url'], $uid, $activity_id); + $ret = ActivityPub\Transmitter::sendActivity('Follow', $contact['url'], $user['uid'], $activity_id); Logger::log('Follow returns: ' . $ret); } } @@ -2677,7 +2677,7 @@ class Contact } } 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['uid'], $url, false, $network); + self::createFromProbe($importer, $url, false, $network); } $condition = ['uid' => $importer['uid'], 'url' => $url, 'pending' => true]; diff --git a/src/Module/Contact.php b/src/Module/Contact.php index 1fb071e531..99a299b571 100644 --- a/src/Module/Contact.php +++ b/src/Module/Contact.php @@ -167,10 +167,9 @@ class Contact extends BaseModule return; } - $uid = $contact['uid']; - if ($contact['network'] == Protocol::OSTATUS) { - $result = Model\Contact::createFromProbe($uid, $contact['url'], false, $contact['network']); + $user = Model\User::getById($contact['uid']); + $result = Model\Contact::createFromProbe($user, $contact['url'], false, $contact['network']); if ($result['success']) { DBA::update('contact', ['subhub' => 1], ['id' => $contact_id]); diff --git a/src/Worker/AddContact.php b/src/Worker/AddContact.php index 700802ca8b..6bb8a41b59 100644 --- a/src/Worker/AddContact.php +++ b/src/Worker/AddContact.php @@ -23,6 +23,7 @@ namespace Friendica\Worker; use Friendica\Core\Logger; use Friendica\Model\Contact; +use Friendica\Model\User; class AddContact { @@ -33,7 +34,11 @@ class AddContact */ public static function execute(int $uid, string $url) { - $result = Contact::createFromProbe($uid, $url, '', false); + $user = User::getById($uid); + if (empty($user)) { + return; + } + $result = Contact::createFromProbe($user, $url, '', false); Logger::info('Added contact', ['uid' => $uid, 'url' => $url, 'result' => $result]); } }