]> git.mxchange.org Git - friendica.git/commitdiff
Remove dependency to App in Contact::createFromProbe
authorHypolite Petovan <hypolite@mrpetovan.com>
Wed, 10 Jun 2020 14:36:42 +0000 (10:36 -0400)
committerHypolite Petovan <hypolite@mrpetovan.com>
Wed, 10 Jun 2020 14:36:42 +0000 (10:36 -0400)
- Address https://github.com/friendica/friendica/issues/8473#issuecomment-641259906

mod/follow.php
mod/ostatus_subscribe.php
mod/repair_ostatus.php
mod/salmon.php
src/Model/Contact.php
src/Module/Contact.php
src/Worker/AddContact.php

index 58419dfd3ba54836f144ae3b3ff2d3148df98c50..8910f54c0c086ebb1cdeaf3f579ac9b514e8f3ec 100644 (file)
@@ -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
index bdf362e1cee2983fdf1ea1057ce3b930b939e552..e5c4fa7b0161016ded2e621e58b1c0d63fa8fd47 100644 (file)
@@ -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 {
index 6ceba80553fbe40be7417cfb657b43217a307c7b..33e97499e570bd0a0242ea10e95a43e922b5f015 100644 (file)
@@ -70,7 +70,7 @@ function repair_ostatus_content(App $a) {
 
        $o .= "<p>".DI::l10n()->t("Keep this window open until done.")."</p>";
 
-       Contact::createFromProbe($uid, $r[0]["url"], true);
+       Contact::createFromProbe($a->user, $r[0]["url"], true);
 
        DI::page()['htmlhead'] = '<meta http-equiv="refresh" content="1; URL=' . DI::baseUrl() . '/repair_ostatus?counter='.$counter.'">';
 
index 6eea57f6a7e3a3d70066e83ea49aabf240e311f9..1932d3ba4175599f990121a0608382562e44b90f 100644 (file)
@@ -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')
index 02f9e829f29f69c341935caf3678617f492fea7f..ce4c0d1403efb2fc8e5e34f1b6c752798dc79699 100644 (file)
@@ -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];
index 1fb071e53152f9c29beff18c21bce66ed3a56714..99a299b57129b0092b90ea9e007b95dee9337c1e 100644 (file)
@@ -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]);
index 700802ca8bd6d7548f944c4a3e9fde01e30e2a10..6bb8a41b59ce3fa50928785249b09e1d53df3253 100644 (file)
@@ -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]);
        }
 }