]> git.mxchange.org Git - friendica.git/commitdiff
Merge pull request #7095 from annando/ap-connect
authorHypolite Petovan <hypolite@mrpetovan.com>
Sun, 5 May 2019 13:37:43 +0000 (09:37 -0400)
committerGitHub <noreply@github.com>
Sun, 5 May 2019 13:37:43 +0000 (09:37 -0400)
Switching from legacy DFRN connect to ActivityPub

config/dbstructure.config.php
database.sql
mod/dfrn_confirm.php
mod/follow.php
src/Model/APContact.php
src/Model/Contact.php
src/Protocol/ActivityPub/Processor.php
src/Worker/OnePoll.php

index 3996205db0d50ad5d800b98719437c1afd05ca81..86965a2019045d7c61ca7aa8bc68d92f9acce4e9 100755 (executable)
@@ -34,7 +34,7 @@
 use Friendica\Database\DBA;
 
 if (!defined('DB_UPDATE_VERSION')) {
-       define('DB_UPDATE_VERSION', 1310);
+       define('DB_UPDATE_VERSION', 1311);
 }
 
 return [
@@ -74,6 +74,7 @@ return [
                        "alias" => ["type" => "varchar(255)", "comment" => ""],
                        "pubkey" => ["type" => "text", "comment" => ""],
                        "baseurl" => ["type" => "varchar(255)", "comment" => "baseurl of the ap contact"],
+                       "generator" => ["type" => "varchar(255)", "comment" => "Name of the contact's system"],
                        "updated" => ["type" => "datetime", "not null" => "1", "default" => DBA::NULL_DATETIME, "comment" => ""]
                ],
                "indexes" => [
@@ -185,7 +186,8 @@ return [
                        "remote_self" => ["type" => "boolean", "not null" => "1", "default" => "0", "comment" => ""],
                        "rel" => ["type" => "tinyint unsigned", "not null" => "1", "default" => "0", "comment" => "The kind of the relation between the user and the contact"],
                        "duplex" => ["type" => "boolean", "not null" => "1", "default" => "0", "comment" => ""],
-                       "network" => ["type" => "char(4)", "not null" => "1", "default" => "", "comment" => "Network protocol of the contact"],
+                       "network" => ["type" => "char(4)", "not null" => "1", "default" => "", "comment" => "Network of the contact"],
+                       "protocol" => ["type" => "char(4)", "not null" => "1", "default" => "", "comment" => "Protocol of the contact"],
                        "name" => ["type" => "varchar(255)", "not null" => "1", "default" => "", "comment" => "Name that this contact is known by"],
                        "nick" => ["type" => "varchar(255)", "not null" => "1", "default" => "", "comment" => "Nick- and user name of the contact"],
                        "location" => ["type" => "varchar(255)", "default" => "", "comment" => ""],
index b9e70097eec314d2dae4a47748dfe8bc8c8c1174..dde37cd3762bc8a49e2b61d6d7a88d9881fb3732 100644 (file)
@@ -1,6 +1,6 @@
 -- ------------------------------------------
 -- Friendica 2019.06-dev (Dalmatian Bellflower)
--- DB_UPDATE_VERSION 1310
+-- DB_UPDATE_VERSION 1311
 -- ------------------------------------------
 
 
@@ -40,6 +40,7 @@ CREATE TABLE IF NOT EXISTS `apcontact` (
        `alias` varchar(255) COMMENT '',
        `pubkey` text COMMENT '',
        `baseurl` varchar(255) COMMENT 'baseurl of the ap contact',
+       `generator` varchar(255) COMMENT 'Name of the contact\'s system',
        `updated` datetime NOT NULL DEFAULT '0001-01-01 00:00:00' COMMENT '',
         PRIMARY KEY(`url`),
         INDEX `addr` (`addr`(32)),
@@ -143,7 +144,8 @@ CREATE TABLE IF NOT EXISTS `contact` (
        `remote_self` boolean NOT NULL DEFAULT '0' COMMENT '',
        `rel` tinyint unsigned NOT NULL DEFAULT 0 COMMENT 'The kind of the relation between the user and the contact',
        `duplex` boolean NOT NULL DEFAULT '0' COMMENT '',
-       `network` char(4) NOT NULL DEFAULT '' COMMENT 'Network protocol of the contact',
+       `network` char(4) NOT NULL DEFAULT '' COMMENT 'Network of the contact',
+       `protocol` char(4) NOT NULL DEFAULT '' COMMENT 'Protocol of the contact',
        `name` varchar(255) NOT NULL DEFAULT '' COMMENT 'Name that this contact is known by',
        `nick` varchar(255) NOT NULL DEFAULT '' COMMENT 'Nick- and user name of the contact',
        `location` varchar(255) DEFAULT '' COMMENT '',
index 97d19f1b71babd53956b24cb1d3917bdc32d2a46..fe7323a33a78b152827a50e4db5b2d015ad67f7f 100644 (file)
@@ -24,7 +24,6 @@ use Friendica\Core\Logger;
 use Friendica\Core\Protocol;
 use Friendica\Core\System;
 use Friendica\Database\DBA;
-use Friendica\Model\APContact;
 use Friendica\Model\Contact;
 use Friendica\Model\Group;
 use Friendica\Model\User;
@@ -138,21 +137,16 @@ function dfrn_confirm_post(App $a, $handsfree = null)
                $dfrn_confirm = $contact['confirm'];
                $aes_allow    = $contact['aes_allow'];
 
-               $network = ((strlen($contact['issued-id'])) ? Protocol::DFRN : Protocol::OSTATUS);
-
-               if ($contact['network']) {
-                       $network = $contact['network'];
-               }
-
                // an empty DFRN-ID tells us that it had been a request via AP from a Friendica contact
-               if (($network === Protocol::DFRN) && empty($dfrn_id) && !empty($contact['hub-verify'])) {
-                       $apcontact = APContact::getByURL($contact['url']);
-                       if (!empty($apcontact)) {
-                               $network = Protocol::ACTIVITYPUB;
-                       }
+               if (!empty($contact['protocol'])) {
+                       $protocol = $contact['protocol'];
+               } elseif (($contact['network'] === Protocol::DFRN) && empty($dfrn_id)) {
+                       $protocol = Contact::getProtocol($contact['url'], $contact['network']);
+               } else {
+                       $protocol = $contact['network'];
                }
 
-               if ($network === Protocol::DFRN) {
+               if ($protocol === Protocol::DFRN) {
                        /*
                         * Generate a key pair for all further communications with this person.
                         * We have a keypair for every contact, and a site key for unknown people.
@@ -166,11 +160,8 @@ function dfrn_confirm_post(App $a, $handsfree = null)
                        $public_key  = $res['pubkey'];
 
                        // Save the private key. Send them the public key.
-                       q("UPDATE `contact` SET `prvkey` = '%s' WHERE `id` = %d AND `uid` = %d",
-                               DBA::escape($private_key),
-                               intval($contact_id),
-                               intval($uid)
-                       );
+                       $fields = ['prvkey' => $private_key, 'protocol' => Protocol::DFRN];
+                       DBA::update('contact', $fields, ['id' => $contact_id]);
 
                        $params = [];
 
@@ -302,6 +293,8 @@ function dfrn_confirm_post(App $a, $handsfree = null)
                        if ($status != 0) {
                                return;
                        }
+               } else {
+                       DBA::update('contact', ['protocol' => $protocol], ['id' => $contact_id]);
                }
 
                /*
@@ -315,7 +308,7 @@ function dfrn_confirm_post(App $a, $handsfree = null)
 
                Logger::log('dfrn_confirm: confirm - imported photos');
 
-               if ($network === Protocol::DFRN) {
+               if ($protocol === Protocol::DFRN) {
                        $new_relation = Contact::FOLLOWER;
 
                        if (($relation == Contact::SHARING) || ($duplex)) {
@@ -344,7 +337,7 @@ function dfrn_confirm_post(App $a, $handsfree = null)
                                intval($contact_id)
                        );
                } else {
-                       if ($network == Protocol::ACTIVITYPUB) {
+                       if ($protocol == Protocol::ACTIVITYPUB) {
                                ActivityPub\Transmitter::sendContactAccept($contact['url'], $contact['hub-verify'], $uid);
                                // Setting "pending" to true on a bidirectional contact request could create a problem when it isn't accepted on the other side
                                // Then we have got a situation where - although one direction is accepted - the contact still appears as pending.
@@ -356,10 +349,7 @@ function dfrn_confirm_post(App $a, $handsfree = null)
                                $pending = false;
                        }
 
-                       // $network !== Protocol::DFRN
-                       $network = defaults($contact, 'network', Protocol::OSTATUS);
-
-                       $arr = Probe::uri($contact['url'], $network);
+                       $arr = Probe::uri($contact['url'], $protocol);
 
                        $notify  = defaults($contact, 'notify' , $arr['notify']);
                        $poll    = defaults($contact, 'poll'   , $arr['poll']);
@@ -369,7 +359,7 @@ function dfrn_confirm_post(App $a, $handsfree = null)
                        $new_relation = $contact['rel'];
                        $writable = $contact['writable'];
 
-                       if (in_array($network, [Protocol::DIASPORA, Protocol::ACTIVITYPUB])) {
+                       if (in_array($protocol, [Protocol::DIASPORA, Protocol::ACTIVITYPUB])) {
                                if ($duplex) {
                                        $new_relation = Contact::FRIEND;
                                } else {
@@ -386,7 +376,7 @@ function dfrn_confirm_post(App $a, $handsfree = null)
                        $fields = ['name-date' => DateTimeFormat::utcNow(),
                                'uri-date' => DateTimeFormat::utcNow(), 'addr' => $addr,
                                'notify' => $notify, 'poll' => $poll, 'blocked' => false,
-                               'pending' => $pending, 'network' => $network,
+                               'pending' => $pending, 'protocol' => $protocol,
                                'writable' => $writable, 'hidden' => $hidden, 'rel' => $new_relation];
                        DBA::update('contact', $fields, ['id' => $contact_id]);
                }
@@ -397,7 +387,7 @@ function dfrn_confirm_post(App $a, $handsfree = null)
 
                // reload contact info
                $contact = DBA::selectFirst('contact', [], ['id' => $contact_id]);
-               if ((isset($new_relation) && $new_relation == Contact::FRIEND)) {
+               if (isset($new_relation) && ($new_relation == Contact::FRIEND)) {
                        if (DBA::isResult($contact) && ($contact['network'] === Protocol::DIASPORA)) {
                                $ret = Diaspora::sendShare($user, $contact);
                                Logger::log('share returns: ' . $ret);
@@ -406,7 +396,7 @@ function dfrn_confirm_post(App $a, $handsfree = null)
 
                Group::addMember(User::getDefaultGroup($uid, $contact["network"]), $contact['id']);
 
-               if ($network == Protocol::ACTIVITYPUB && $duplex) {
+               if (($protocol == Protocol::ACTIVITYPUB) && $duplex) {
                        ActivityPub\Transmitter::sendActivity('Follow', $contact['url'], $uid);
                }
 
index e0b6e1733b7386b420f0efb221e9a9b59abed75f..8a00e0559ce3d4132dca07cc3f8084638e981dc7 100644 (file)
@@ -91,32 +91,34 @@ function follow_content(App $a)
 
        $ret = Probe::uri($url);
 
-       if (($ret['network'] == Protocol::DIASPORA) && !Config::get('system', 'diaspora_enabled')) {
+       $protocol = Contact::getProtocol($ret['url'], $ret['network']);
+
+       if (($protocol == Protocol::DIASPORA) && !Config::get('system', 'diaspora_enabled')) {
                notice(L10n::t("Diaspora support isn't enabled. Contact can't be added."));
                $submit = '';
                //$a->internalRedirect($_SESSION['return_path']);
                // NOTREACHED
        }
 
-       if (($ret['network'] == Protocol::OSTATUS) && Config::get('system', 'ostatus_disabled')) {
+       if (($protocol == Protocol::OSTATUS) && Config::get('system', 'ostatus_disabled')) {
                notice(L10n::t("OStatus support is disabled. Contact can't be added."));
                $submit = '';
                //$a->internalRedirect($_SESSION['return_path']);
                // NOTREACHED
        }
 
-       if ($ret['network'] == Protocol::PHANTOM) {
+       if ($protocol == Protocol::PHANTOM) {
                notice(L10n::t("The network type couldn't be detected. Contact can't be added."));
                $submit = '';
                //$a->internalRedirect($_SESSION['return_path']);
                // NOTREACHED
        }
 
-       if ($ret['network'] == Protocol::MAIL) {
+       if ($protocol == Protocol::MAIL) {
                $ret['url'] = $ret['addr'];
        }
 
-       if (($ret['network'] === Protocol::DFRN) && !DBA::isResult($r)) {
+       if (($protocol === Protocol::DFRN) && !DBA::isResult($r)) {
                $request = $ret['request'];
                $tpl = Renderer::getMarkupTemplate('dfrn_request.tpl');
        } else {
@@ -147,7 +149,7 @@ function follow_content(App $a)
                $gcontact_id = $r[0]['id'];
        }
 
-       if ($ret['network'] === Protocol::DIASPORA) {
+       if ($protocol === Protocol::DIASPORA) {
                $r[0]['location'] = '';
                $r[0]['about'] = '';
        }
index 47bbe7d59f137d21d1c17ad38ccd7a3d19823f41..cf1f9b72313c44baee97083b4b91a3b76255017e 100644 (file)
@@ -167,10 +167,15 @@ class APContact extends BaseObject
 
                $apcontact['manually-approve'] = (int)JsonLD::fetchElement($compacted, 'as:manuallyApprovesFollowers');
 
+               if (!empty($compacted['as:generator'])) {
+                       $apcontact['baseurl'] = JsonLD::fetchElement($compacted['as:generator'], 'as:url', '@id');
+                       $apcontact['generator'] = JsonLD::fetchElement($compacted['as:generator'], 'as:name', '@value');
+               }
+
                // To-Do
 
                // Unhandled
-               // @context, tag, attachment, image, nomadicLocations, signature, following, followers, featured, movedTo, liked
+               // tag, attachment, image, nomadicLocations, signature, featured, movedTo, liked
 
                // Unhandled from Misskey
                // sharedInbox, isCat
@@ -185,6 +190,9 @@ class APContact extends BaseObject
                        $apcontact['baseurl'] = Network::unparseURL($parts);
                } else {
                        $apcontact['addr'] = null;
+               }
+
+               if (empty($apcontact['baseurl'])) {
                        $apcontact['baseurl'] = null;
                }
 
index f4c62fea809d554e220e180b292a9e1004f9268f..27ac3d33e1fec3669d71e66ef00177022879906a 100644 (file)
@@ -689,9 +689,15 @@ class Contact extends BaseObject
                if (empty($contact['network'])) {
                        return;
                }
-               if (($contact['network'] == Protocol::DFRN) && $dissolve) {
+
+               $protocol = $contact['network'];
+               if (($protocol == Protocol::DFRN) && !self::isLegacyDFRNContact($contact)) {
+                       $protocol = Protocol::ACTIVITYPUB;
+               }
+
+               if (($protocol == Protocol::DFRN) && $dissolve) {
                        DFRN::deliver($user, $contact, 'placeholder', true);
-               } elseif (in_array($contact['network'], [Protocol::OSTATUS, Protocol::DFRN])) {
+               } elseif (in_array($protocol, [Protocol::OSTATUS, Protocol::DFRN])) {
                        // create an unfollow slap
                        $item = [];
                        $item['verb'] = NAMESPACE_OSTATUS . "/unfollow";
@@ -706,9 +712,9 @@ class Contact extends BaseObject
                        if (!empty($contact['notify'])) {
                                Salmon::slapper($user, $contact['notify'], $slap);
                        }
-               } elseif ($contact['network'] == Protocol::DIASPORA) {
+               } elseif ($protocol == Protocol::DIASPORA) {
                        Diaspora::sendUnshare($user, $contact);
-               } elseif ($contact['network'] == Protocol::ACTIVITYPUB) {
+               } elseif ($protocol == Protocol::ACTIVITYPUB) {
                        ActivityPub\Transmitter::sendContactUndo($contact['url'], $contact['id'], $user['uid']);
 
                        if ($dissolve) {
@@ -1804,6 +1810,40 @@ class Contact extends BaseObject
                return true;
        }
 
+       /**
+        * 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.
+        *
+        * @param string $url contact url
+        * @param string $network Network of that contact
+        * @return string with protocol
+        */
+       public static function getProtocol($url, $network)
+       {
+               if ($network != Protocol::DFRN) {
+                       return $network;
+               }
+
+               $apcontact = APContact::getByURL($url);
+               if (!empty($apcontact) && !empty($apcontact['generator'])) {
+                       return Protocol::ACTIVITYPUB;
+               } else {
+                       return $network;
+               }
+       }
+
        /**
         * Takes a $uid and a url/handle and adds a new contact
         * Currently if the contact is DFRN, interactive needs to be true, to redirect to the
@@ -1879,7 +1919,9 @@ class Contact extends BaseObject
                        $contact = DBA::selectFirst('contact', ['id', 'rel'], $condition);
                }
 
-               if (($ret['network'] === Protocol::DFRN) && !DBA::isResult($contact)) {
+               $protocol = self::getProtocol($url, $ret['network']);
+
+               if (($protocol === Protocol::DFRN) && !DBA::isResult($contact)) {
                        if ($interactive) {
                                if (strlen($a->getURLPath())) {
                                        $myaddr = bin2hex(System::baseUrl() . '/profile/' . $a->user['nickname']);
@@ -1898,7 +1940,7 @@ class Contact extends BaseObject
                }
 
                // This extra param just confuses things, remove it
-               if ($ret['network'] === Protocol::DIASPORA) {
+               if ($protocol === Protocol::DIASPORA) {
                        $ret['url'] = str_replace('?absolute=true', '', $ret['url']);
                }
 
@@ -1921,7 +1963,7 @@ class Contact extends BaseObject
                        return $result;
                }
 
-               if ($ret['network'] === Protocol::OSTATUS && Config::get('system', 'ostatus_disabled')) {
+               if ($protocol === Protocol::OSTATUS && Config::get('system', 'ostatus_disabled')) {
                        $result['message'] .= L10n::t('The profile address specified belongs to a network which has been disabled on this site.') . EOL;
                        $ret['notify'] = '';
                }
@@ -1930,15 +1972,15 @@ class Contact extends BaseObject
                        $result['message'] .= L10n::t('Limited profile. This person will be unable to receive direct/personal notifications from you.') . EOL;
                }
 
-               $writeable = ((($ret['network'] === Protocol::OSTATUS) && ($ret['notify'])) ? 1 : 0);
+               $writeable = ((($protocol === Protocol::OSTATUS) && ($ret['notify'])) ? 1 : 0);
 
-               $subhub = (($ret['network'] === Protocol::OSTATUS) ? true : false);
+               $subhub = (($protocol === Protocol::OSTATUS) ? true : false);
 
-               $hidden = (($ret['network'] === Protocol::MAIL) ? 1 : 0);
+               $hidden = (($protocol === Protocol::MAIL) ? 1 : 0);
 
-               $pending = in_array($ret['network'], [Protocol::ACTIVITYPUB]);
+               $pending = in_array($protocol, [Protocol::ACTIVITYPUB]);
 
-               if (in_array($ret['network'], [Protocol::MAIL, Protocol::DIASPORA, Protocol::ACTIVITYPUB])) {
+               if (in_array($protocol, [Protocol::MAIL, Protocol::DIASPORA, Protocol::ACTIVITYPUB])) {
                        $writeable = 1;
                }
 
@@ -1949,7 +1991,7 @@ class Contact extends BaseObject
                        $fields = ['rel' => $new_relation, 'subhub' => $subhub, 'readonly' => false];
                        DBA::update('contact', $fields, ['id' => $contact['id']]);
                } else {
-                       $new_relation = (in_array($ret['network'], [Protocol::MAIL]) ? self::FRIEND : self::SHARING);
+                       $new_relation = (in_array($protocol, [Protocol::MAIL]) ? self::FRIEND : self::SHARING);
 
                        // create contact record
                        DBA::insert('contact', [
@@ -1966,6 +2008,7 @@ class Contact extends BaseObject
                                'name'    => $ret['name'],
                                'nick'    => $ret['nick'],
                                'network' => $ret['network'],
+                               'protocol' => $protocol,
                                'pubkey'  => $ret['pubkey'],
                                'rel'     => $new_relation,
                                'priority'=> $ret['priority'],
@@ -1999,7 +2042,7 @@ class Contact extends BaseObject
                $owner = User::getOwnerDataById($uid);
 
                if (DBA::isResult($owner)) {
-                       if (in_array($contact['network'], [Protocol::OSTATUS, Protocol::DFRN])) {
+                       if (in_array($protocol, [Protocol::OSTATUS, Protocol::DFRN])) {
                                // create a follow slap
                                $item = [];
                                $item['verb'] = ACTIVITY_FOLLOW;
@@ -2015,10 +2058,10 @@ class Contact extends BaseObject
                                if (!empty($contact['notify'])) {
                                        Salmon::slapper($owner, $contact['notify'], $slap);
                                }
-                       } elseif ($contact['network'] == Protocol::DIASPORA) {
+                       } elseif ($protocol == Protocol::DIASPORA) {
                                $ret = Diaspora::sendShare($a->user, $contact);
                                Logger::log('share returns: ' . $ret);
-                       } elseif ($contact['network'] == Protocol::ACTIVITYPUB) {
+                       } elseif ($protocol == Protocol::ACTIVITYPUB) {
                                $activity_id = ActivityPub\Transmitter::activityIDFromContact($contact_id);
                                if (empty($activity_id)) {
                                        // This really should never happen
@@ -2076,7 +2119,7 @@ class Contact extends BaseObject
                return $contact;
        }
 
-       public static function addRelationship($importer, $contact, $datarray, $item = '', $sharing = false) {
+       public static function addRelationship($importer, $contact, $datarray, $item = '', $sharing = false, $note = '') {
                // Should always be set
                if (empty($datarray['author-id'])) {
                        return;
@@ -2096,18 +2139,25 @@ class Contact extends BaseObject
                $network = $pub_contact['network'];
 
                if (is_array($contact)) {
+                       // Make sure that the existing contact isn't archived
+                       self::unmarkForArchival($contact);
+
+                       $protocol = self::getProtocol($url, $contact['network']);
+
                        if (($contact['rel'] == self::SHARING)
                                || ($sharing && $contact['rel'] == self::FOLLOWER)) {
-                               DBA::update('contact', ['rel' => self::FRIEND, 'writable' => true],
+                               DBA::update('contact', ['rel' => self::FRIEND, 'writable' => true, 'pending' => false],
                                                ['id' => $contact['id'], 'uid' => $importer['uid']]);
                        }
 
-                       if ($contact['network'] == Protocol::ACTIVITYPUB) {
+                       if ($protocol == Protocol::ACTIVITYPUB) {
                                ActivityPub\Transmitter::sendContactAccept($contact['url'], $contact['hub-verify'], $importer['uid']);
                        }
 
                        // send email notification to owner?
                } else {
+                       $protocol = self::getProtocol($url, $network);
+
                        if (DBA::exists('contact', ['nurl' => Strings::normaliseLink($url), 'uid' => $importer['uid'], 'pending' => true])) {
                                Logger::log('ignoring duplicated connection request from pending contact ' . $url);
                                return;
@@ -2146,7 +2196,7 @@ class Contact extends BaseObject
 
                                if (is_array($contact_record)) {
                                        DBA::insert('intro', ['uid' => $importer['uid'], 'contact-id' => $contact_record['id'],
-                                                               'blocked' => false, 'knowyou' => false,
+                                                               'blocked' => false, 'knowyou' => false, 'note' => $note,
                                                                'hash' => $hash, 'datetime' => DateTimeFormat::utcNow()]);
                                }
 
@@ -2176,8 +2226,9 @@ class Contact extends BaseObject
                                DBA::update('contact', ['pending' => false], $condition);
 
                                $contact = DBA::selectFirst('contact', ['url', 'network', 'hub-verify'], ['id' => $contact_record['id']]);
+                               $protocol = self::getProtocol($contact['url'], $contact['network']);
 
-                               if ($contact['network'] == Protocol::ACTIVITYPUB) {
+                               if ($protocol == Protocol::ACTIVITYPUB) {
                                        ActivityPub\Transmitter::sendContactAccept($contact['url'], $contact['hub-verify'], $importer['uid']);
                                }
                        }
index 475ccfc0b22048f66a4aa1b6b43c3b547a380530..3bb0639ab05cd349931475695d2115b03818cad0 100644 (file)
@@ -454,7 +454,7 @@ class Processor
                $cid = Contact::getIdForURL($activity['actor'], $uid);
                if (!empty($cid)) {
                        self::switchContact($cid);
-                       DBA::update('contact', ['hub-verify' => $activity['id']], ['id' => $cid]);
+                       DBA::update('contact', ['hub-verify' => $activity['id'], 'protocol' => Protocol::ACTIVITYPUB], ['id' => $cid]);
                        $contact = DBA::selectFirst('contact', [], ['id' => $cid, 'network' => Protocol::NATIVE_SUPPORT]);
                } else {
                        $contact = false;
@@ -463,17 +463,19 @@ class Processor
                $item = ['author-id' => Contact::getIdForURL($activity['actor']),
                        'author-link' => $activity['actor']];
 
+               $note = Strings::escapeTags(trim(defaults($activity, 'content', '')));
+
                // Ensure that the contact has got the right network type
                self::switchContact($item['author-id']);
 
-               Contact::addRelationship($owner, $contact, $item);
+               Contact::addRelationship($owner, $contact, $item, '', false, $note);
                $cid = Contact::getIdForURL($activity['actor'], $uid);
                if (empty($cid)) {
                        return;
                }
 
                if (empty($contact)) {
-                       DBA::update('contact', ['hub-verify' => $activity['id']], ['id' => $cid]);
+                       DBA::update('contact', ['hub-verify' => $activity['id'], 'protocol' => Protocol::ACTIVITYPUB], ['id' => $cid]);
                }
 
                Logger::log('Follow user ' . $uid . ' from contact ' . $cid . ' with id ' . $activity['id']);
@@ -578,7 +580,7 @@ class Processor
 
                self::switchContact($cid);
 
-               if (DBA::exists('contact', ['id' => $cid, 'rel' => Contact::SHARING, 'pending' => true])) {
+               if (DBA::exists('contact', ['id' => $cid, 'rel' => Contact::SHARING])) {
                        Contact::remove($cid);
                        Logger::log('Rejected contact request from contact ' . $cid . ' for user ' . $uid . ' - contact had been removed.', Logger::DEBUG);
                } else {
@@ -653,7 +655,7 @@ class Processor
        private static function switchContact($cid)
        {
                $contact = DBA::selectFirst('contact', ['network'], ['id' => $cid, 'network' => Protocol::NATIVE_SUPPORT]);
-               if (!DBA::isResult($contact) || ($contact['network'] == Protocol::ACTIVITYPUB)) {
+               if (!DBA::isResult($contact) || in_array($contact['network'], [Protocol::ACTIVITYPUB, Protocol::DFRN])) {
                        return;
                }
 
index 8523ce2e0b8d01db801a3f58be2a3df4ab47a189..8f494af53a9afc4130251c0603f06bb9dc52d0ff 100644 (file)
@@ -48,20 +48,27 @@ class OnePoll
                        return;
                }
 
+               if (($contact['network'] == Protocol::DFRN) && !Contact::isLegacyDFRNContact($contact)) {
+                       $protocol = Protocol::ACTIVITYPUB;
+               } else {
+                       $protocol = $contact['network'];
+               }
+
                $importer_uid = $contact['uid'];
 
                // Possibly switch the remote contact to AP
-               if ($contact['network'] === Protocol::OSTATUS) {
+               if ($protocol === Protocol::OSTATUS) {
                        ActivityPub\Receiver::switchContact($contact['id'], $importer_uid, $contact['url']);
                        $contact = DBA::selectFirst('contact', [], ['id' => $contact_id]);
                }
 
+               $updated = DateTimeFormat::utcNow();
+
                // These three networks can be able to speak AP, so we are trying to fetch AP profile data here
-               if (in_array($contact['network'], [Protocol::ACTIVITYPUB, Protocol::DIASPORA, Protocol::DFRN])) {
+               if (in_array($protocol, [Protocol::ACTIVITYPUB, Protocol::DIASPORA, Protocol::DFRN])) {
                        $apcontact = APContact::getByURL($contact['url'], true);
 
-                       $updated = DateTimeFormat::utcNow();
-                       if (($contact['network'] === Protocol::ACTIVITYPUB) && empty($apcontact)) {
+                       if (($protocol === Protocol::ACTIVITYPUB) && empty($apcontact)) {
                                self::updateContact($contact, ['last-update' => $updated, 'failure_update' => $updated]);
                                Contact::markForArchival($contact);
                                Logger::log('Contact archived');
@@ -74,9 +81,8 @@ class OnePoll
                }
 
                // Diaspora users, archived users and followers are only checked if they still exist.
-               if (($contact['network'] != Protocol::ACTIVITYPUB) && ($contact['archive'] || ($contact["network"] == Protocol::DIASPORA) || ($contact["rel"] == Contact::FOLLOWER))) {
+               if (($protocol != Protocol::ACTIVITYPUB) && ($contact['archive'] || ($contact["network"] == Protocol::DIASPORA) || ($contact["rel"] == Contact::FOLLOWER))) {
                        $last_updated = PortableContact::lastUpdated($contact["url"], true);
-                       $updated = DateTimeFormat::utcNow();
 
                        if ($last_updated) {
                                Logger::log('Contact '.$contact['id'].' had last update on '.$last_updated, Logger::DEBUG);
@@ -98,10 +104,9 @@ class OnePoll
                }
 
                // Update the contact entry
-               if (in_array($contact['network'], [Protocol::ACTIVITYPUB, Protocol::OSTATUS, Protocol::DIASPORA, Protocol::DFRN])) {
-                       $updated = DateTimeFormat::utcNow();
+               if (in_array($protocol, [Protocol::ACTIVITYPUB, Protocol::OSTATUS, Protocol::DIASPORA, Protocol::DFRN])) {
                        // Currently we can't check every AP implementation, so we don't do it at all
-                       if (($contact['network'] != Protocol::ACTIVITYPUB) && !PortableContact::reachable($contact['url'])) {
+                       if (($protocol != Protocol::ACTIVITYPUB) && !PortableContact::reachable($contact['url'])) {
                                Logger::log("Skipping probably dead contact ".$contact['url']);
 
                                // set the last-update so we don't keep polling
@@ -140,25 +145,25 @@ class OnePoll
                        Logger::log("Don't poll follower");
 
                        // set the last-update so we don't keep polling
-                       DBA::update('contact', ['last-update' => DateTimeFormat::utcNow()], ['id' => $contact['id']]);
+                       DBA::update('contact', ['last-update' => $updated], ['id' => $contact['id']]);
                        return;
                }
 
                // Don't poll if polling is deactivated (But we poll feeds and mails anyway)
-               if (!in_array($contact['network'], [Protocol::FEED, Protocol::MAIL]) && Config::get('system', 'disable_polling')) {
+               if (!in_array($protocol, [Protocol::FEED, Protocol::MAIL]) && Config::get('system', 'disable_polling')) {
                        Logger::log('Polling is disabled');
 
                        // set the last-update so we don't keep polling
-                       DBA::update('contact', ['last-update' => DateTimeFormat::utcNow()], ['id' => $contact['id']]);
+                       DBA::update('contact', ['last-update' => $updated], ['id' => $contact['id']]);
                        return;
                }
 
                // We don't poll AP contacts by now
-               if ($contact['network'] === Protocol::ACTIVITYPUB) {
+               if ($protocol === Protocol::ACTIVITYPUB) {
                        Logger::log("Don't poll AP contact");
 
                        // set the last-update so we don't keep polling
-                       DBA::update('contact', ['last-update' => DateTimeFormat::utcNow()], ['id' => $contact['id']]);
+                       DBA::update('contact', ['last-update' => $updated], ['id' => $contact['id']]);
                        return;
                }
 
@@ -166,7 +171,7 @@ class OnePoll
                        Logger::log('Ignore public contacts');
 
                        // set the last-update so we don't keep polling
-                       DBA::update('contact', ['last-update' => DateTimeFormat::utcNow()], ['id' => $contact['id']]);
+                       DBA::update('contact', ['last-update' => $updated], ['id' => $contact['id']]);
                        return;
                }
 
@@ -178,7 +183,7 @@ class OnePoll
                        Logger::log('No self contact for user '.$importer_uid);
 
                        // set the last-update so we don't keep polling
-                       DBA::update('contact', ['last-update' => DateTimeFormat::utcNow()], ['id' => $contact['id']]);
+                       DBA::update('contact', ['last-update' => $updated], ['id' => $contact['id']]);
                        return;
                }
 
@@ -203,9 +208,9 @@ class OnePoll
                        : DateTimeFormat::utc($contact['last-update'], DateTimeFormat::ATOM)
                );
 
-               Logger::log("poll: ({$contact['network']}-{$contact['id']}) IMPORTER: {$importer['name']}, CONTACT: {$contact['name']}");
+               Logger::log("poll: ({$protocol}-{$contact['id']}) IMPORTER: {$importer['name']}, CONTACT: {$contact['name']}");
 
-               if ($contact['network'] === Protocol::DFRN) {
+               if ($protocol === Protocol::DFRN) {
                        $idtosend = $orig_id = (($contact['dfrn-id']) ? $contact['dfrn-id'] : $contact['issued-id']);
                        if (intval($contact['duplex']) && $contact['dfrn-id']) {
                                $idtosend = '0:' . $orig_id;
@@ -233,7 +238,7 @@ class OnePoll
 
                        if (!$curlResult->isSuccess() && ($curlResult->getErrorNumber() == CURLE_OPERATION_TIMEDOUT)) {
                                // set the last-update so we don't keep polling
-                               self::updateContact($contact, ['last-update' => DateTimeFormat::utcNow()]);
+                               self::updateContact($contact, ['last-update' => $updated]);
                                Contact::markForArchival($contact);
                                Logger::log('Contact archived');
                                return;
@@ -251,7 +256,7 @@ class OnePoll
                                Logger::log("$url appears to be dead - marking for death ");
 
                                // set the last-update so we don't keep polling
-                               $fields = ['last-update' => DateTimeFormat::utcNow(), 'failure_update' => DateTimeFormat::utcNow()];
+                               $fields = ['last-update' => $updated, 'failure_update' => $updated];
                                self::updateContact($contact, $fields);
                                Contact::markForArchival($contact);
                                return;
@@ -260,7 +265,7 @@ class OnePoll
                        if (!strstr($handshake_xml, '<')) {
                                Logger::log('response from ' . $url . ' did not contain XML.');
 
-                               $fields = ['last-update' => DateTimeFormat::utcNow(), 'failure_update' => DateTimeFormat::utcNow()];
+                               $fields = ['last-update' => $updated, 'failure_update' => $updated];
                                self::updateContact($contact, $fields);
                                Contact::markForArchival($contact);
                                return;
@@ -274,7 +279,7 @@ class OnePoll
                                Logger::log("$url replied status 1 - marking for death ");
 
                                // set the last-update so we don't keep polling
-                               $fields = ['last-update' => DateTimeFormat::utcNow(), 'failure_update' => DateTimeFormat::utcNow()];
+                               $fields = ['last-update' => $updated, 'failure_update' => $updated];
                                self::updateContact($contact, $fields);
                                Contact::markForArchival($contact);
                        } elseif ($contact['term-date'] > DBA::NULL_DATETIME) {
@@ -283,7 +288,7 @@ class OnePoll
 
                        if ((intval($res->status) != 0) || !strlen($res->challenge) || !strlen($res->dfrn_id)) {
                                // set the last-update so we don't keep polling
-                               DBA::update('contact', ['last-update' => DateTimeFormat::utcNow()], ['id' => $contact['id']]);
+                               DBA::update('contact', ['last-update' => $updated], ['id' => $contact['id']]);
                                Logger::log('Contact status is ' . $res->status);
                                return;
                        }
@@ -326,7 +331,7 @@ class OnePoll
                                Logger::log('ID did not decode: ' . $contact['id'] . ' orig: ' . $orig_id . ' final: ' . $final_dfrn_id);
 
                                // set the last-update so we don't keep polling
-                               DBA::update('contact', ['last-update' => DateTimeFormat::utcNow()], ['id' => $contact['id']]);
+                               DBA::update('contact', ['last-update' => $updated], ['id' => $contact['id']]);
                                Contact::markForArchival($contact);
                                return;
                        }
@@ -337,9 +342,9 @@ class OnePoll
 
                        $xml = Network::post($contact['poll'], $postvars)->getBody();
 
-               } elseif (($contact['network'] === Protocol::OSTATUS)
-                       || ($contact['network'] === Protocol::DIASPORA)
-                       || ($contact['network'] === Protocol::FEED)) {
+               } elseif (($protocol === Protocol::OSTATUS)
+                       || ($protocol === Protocol::DIASPORA)
+                       || ($protocol === Protocol::FEED)) {
 
                        // Upgrading DB fields from an older Friendica version
                        // Will only do this once per notify-enabled OStatus contact
@@ -348,7 +353,7 @@ class OnePoll
                        $stat_writeable = ((($contact['notify']) && ($contact['rel'] == Contact::FOLLOWER || $contact['rel'] == Contact::FRIEND)) ? 1 : 0);
 
                        // Contacts from OStatus are always writable
-                       if ($contact['network'] === Protocol::OSTATUS) {
+                       if ($protocol === Protocol::OSTATUS) {
                                $stat_writeable = 1;
                        }
 
@@ -360,7 +365,7 @@ class OnePoll
                        // Are we allowed to import from this person?
                        if ($contact['rel'] == Contact::FOLLOWER || $contact['blocked']) {
                                // set the last-update so we don't keep polling
-                               DBA::update('contact', ['last-update' => DateTimeFormat::utcNow()], ['id' => $contact['id']]);
+                               DBA::update('contact', ['last-update' => $updated], ['id' => $contact['id']]);
                                Logger::log('Contact is blocked or only a follower');
                                return;
                        }
@@ -371,7 +376,7 @@ class OnePoll
 
                        if ($curlResult->isTimeout()) {
                                // set the last-update so we don't keep polling
-                               self::updateContact($contact, ['last-update' => DateTimeFormat::utcNow()]);
+                               self::updateContact($contact, ['last-update' => $updated]);
                                Contact::markForArchival($contact);
                                Logger::log('Contact archived');
                                return;
@@ -379,13 +384,13 @@ class OnePoll
 
                        $xml = $curlResult->getBody();
 
-               } elseif ($contact['network'] === Protocol::MAIL) {
+               } elseif ($protocol === Protocol::MAIL) {
                        Logger::log("Mail: Fetching for ".$contact['addr'], Logger::DEBUG);
 
                        $mail_disabled = ((function_exists('imap_open') && !Config::get('system', 'imap_disabled')) ? 0 : 1);
                        if ($mail_disabled) {
                                // set the last-update so we don't keep polling
-                               self::updateContact($contact, ['last-update' => DateTimeFormat::utcNow()]);
+                               self::updateContact($contact, ['last-update' => $updated]);
                                Contact::markForArchival($contact);
                                Logger::log('Contact archived');
                                return;
@@ -406,7 +411,7 @@ class OnePoll
                                unset($password);
                                Logger::log("Mail: Connect to " . $mailconf['user']);
                                if ($mbox) {
-                                       $fields = ['last_check' => DateTimeFormat::utcNow()];
+                                       $fields = ['last_check' => $updated];
                                        DBA::update('mailacct', $fields, ['id' => $mailconf['id']]);
                                        Logger::log("Mail: Connected to " . $mailconf['user']);
                                } else {
@@ -448,7 +453,7 @@ class OnePoll
                                                                // Only delete when mails aren't automatically moved or deleted
                                                                if (($mailconf['action'] != 1) && ($mailconf['action'] != 3))
                                                                        if ($meta->deleted && ! $item['deleted']) {
-                                                                               $fields = ['deleted' => true, 'changed' => DateTimeFormat::utcNow()];
+                                                                               $fields = ['deleted' => true, 'changed' => $updated];
                                                                                Item::update($fields, ['id' => $item['id']]);
                                                                        }
 
@@ -579,7 +584,7 @@ class OnePoll
                                                        if ($datarray['parent-uri'] === $datarray['uri']) {
                                                                $datarray['private'] = 1;
                                                        }
-                                                       if (($contact['network'] === Protocol::MAIL) && !PConfig::get($importer_uid, 'system', 'allow_public_email_replies')) {
+                                                       if (($protocol === Protocol::MAIL) && !PConfig::get($importer_uid, 'system', 'allow_public_email_replies')) {
                                                                $datarray['private'] = 1;
                                                                $datarray['allow_cid'] = '<' . $contact['id'] . '>';
                                                        }
@@ -622,7 +627,7 @@ class OnePoll
                        if (!strstr($xml, '<')) {
                                Logger::log('post_handshake: response from ' . $url . ' did not contain XML.');
 
-                               $fields = ['last-update' => DateTimeFormat::utcNow(), 'failure_update' => DateTimeFormat::utcNow()];
+                               $fields = ['last-update' => $updated, 'failure_update' => $updated];
                                self::updateContact($contact, $fields);
                                Contact::markForArchival($contact);
                                return;
@@ -634,16 +639,16 @@ class OnePoll
                        consume_feed($xml, $importer, $contact, $hub);
 
                        // do it a second time for DFRN so that any children find their parents.
-                       if ($contact['network'] === Protocol::DFRN) {
+                       if ($protocol === Protocol::DFRN) {
                                consume_feed($xml, $importer, $contact, $hub);
                        }
 
                        $hubmode = 'subscribe';
-                       if ($contact['network'] === Protocol::DFRN || $contact['blocked']) {
+                       if ($protocol === Protocol::DFRN || $contact['blocked']) {
                                $hubmode = 'unsubscribe';
                        }
 
-                       if (($contact['network'] === Protocol::OSTATUS ||  $contact['network'] == Protocol::FEED) && (! $contact['hub-verify'])) {
+                       if (($protocol === Protocol::OSTATUS ||  $protocol == Protocol::FEED) && (! $contact['hub-verify'])) {
                                $hub_update = true;
                        }
 
@@ -651,9 +656,9 @@ class OnePoll
                                $hub_update = true;
                        }
 
-                       Logger::log("Contact ".$contact['id']." returned hub: ".$hub." Network: ".$contact['network']." Relation: ".$contact['rel']." Update: ".$hub_update);
+                       Logger::log("Contact ".$contact['id']." returned hub: ".$hub." Network: ".$protocol." Relation: ".$contact['rel']." Update: ".$hub_update);
 
-                       if (strlen($hub) && $hub_update && (($contact['rel'] != Contact::FOLLOWER) || $contact['network'] == Protocol::FEED)) {
+                       if (strlen($hub) && $hub_update && (($contact['rel'] != Contact::FOLLOWER) || $protocol == Protocol::FEED)) {
                                Logger::log('hub ' . $hubmode . ' : ' . $hub . ' contact name : ' . $contact['name'] . ' local user : ' . $importer['name']);
                                $hubs = explode(',', $hub);
 
@@ -670,19 +675,15 @@ class OnePoll
                                }
                        }
 
-                       $updated = DateTimeFormat::utcNow();
-
                        self::updateContact($contact, ['last-update' => $updated, 'success_update' => $updated]);
                        DBA::update('gcontact', ['last_contact' => $updated], ['nurl' => $contact['nurl']]);
                        Contact::unmarkForArchival($contact);
                } elseif (in_array($contact["network"], [Protocol::DFRN, Protocol::DIASPORA, Protocol::OSTATUS, Protocol::FEED])) {
-                       $updated = DateTimeFormat::utcNow();
-
                        self::updateContact($contact, ['last-update' => $updated, 'failure_update' => $updated]);
                        DBA::update('gcontact', ['last_failure' => $updated], ['nurl' => $contact['nurl']]);
                        Contact::markForArchival($contact);
                } else {
-                       self::updateContact($contact, ['last-update' => DateTimeFormat::utcNow()]);
+                       self::updateContact($contact, ['last-update' => $updated]);
                }
 
                Logger::log('End');