]> git.mxchange.org Git - friendica.git/blobdiff - src/Protocol/Diaspora.php
Merge pull request #7570 from nupplaphil/bug/friendica-7298
[friendica.git] / src / Protocol / Diaspora.php
index 751033e9600c6dc5fb4453e95562177a732f2dcb..d578ba4545264085a265dd6ac3f574d721547707 100644 (file)
@@ -180,22 +180,28 @@ class Diaspora
        public static function setRelayContact($server_url, array $network_fields = [])
        {
                $fields = ['created' => DateTimeFormat::utcNow(),
-                       'name' => 'relay', 'nick' => 'relay',
-                       'url' => $server_url, 'network' => Protocol::DIASPORA,
+                       'name' => 'relay', 'nick' => 'relay', 'url' => $server_url,
+                       'nurl' => Strings::normaliseLink($server_url),
+                       'network' => Protocol::DIASPORA, 'uid' => 0,
                        'batch' => $server_url . '/receive/public',
                        'rel' => Contact::FOLLOWER, 'blocked' => false,
-                       'pending' => false, 'writable' => true];
+                       'pending' => false, 'writable' => true,
+                       'baseurl' => $server_url, 'contact-type' => Contact::TYPE_RELAY];
 
                $fields = array_merge($fields, $network_fields);
 
-               $condition = ['uid' => 0, 'nurl' => Strings::normaliseLink($server_url),
-                       'contact-type' => Contact::TYPE_RELAY];
-
-               if (DBA::exists('contact', $condition)) {
+               $condition = ['uid' => 0, 'nurl' => Strings::normaliseLink($server_url)];
+               $old = DBA::selectFirst('contact', [], $condition);
+               if (DBA::isResult($old)) {
                        unset($fields['created']);
-               }
+                       $condition = ['id' => $old['id']];
 
-               DBA::update('contact', $fields, $condition, true);
+                       Logger::info('Update relay contact', ['fields' => $fields, 'condition' => $condition]);
+                       DBA::update('contact', $fields, $condition, $old);
+               } else {
+                       Logger::info('Create relay contact', ['fields' => $fields]);
+                       DBA::insert('contact', $fields);
+               }
        }
 
        /**
@@ -217,7 +223,7 @@ class Diaspora
                                `fcontact`.`batch` AS `fbatch`, `fcontact`.`network` AS `fnetwork` FROM `participation`
                                INNER JOIN `contact` ON `contact`.`id` = `participation`.`cid`
                                INNER JOIN `fcontact` ON `fcontact`.`id` = `participation`.`fid`
-                               WHERE `participation`.`iid` = ?", $thread);
+                               WHERE `participation`.`iid` = ? AND NOT `contact`.`archive`", $thread);
 
                while ($contact = DBA::fetch($r)) {
                        if (!empty($contact['fnetwork'])) {
@@ -936,31 +942,41 @@ class Diaspora
         * @brief Fetches data for a given handle
         *
         * @param string $handle The handle
+        * @param boolean $update true = always update, false = never update, null = update when not found or outdated
         *
         * @return array the queried data
         * @throws \Friendica\Network\HTTPException\InternalServerErrorException
         * @throws \ImagickException
         */
-       public static function personByHandle($handle)
+       public static function personByHandle($handle, $update = null)
        {
-               $update = false;
-
                $person = DBA::selectFirst('fcontact', [], ['network' => Protocol::DIASPORA, 'addr' => $handle]);
+               if (!DBA::isResult($person)) {
+                       $urls = [$handle, str_replace('http://', 'https://', $handle), Strings::normaliseLink($handle)];
+                       $person = DBA::selectFirst('fcontact', [], ['network' => Protocol::DIASPORA, 'url' => $urls]);
+               }
+
                if (DBA::isResult($person)) {
                        Logger::debug("In cache " . print_r($person, true));
 
-                       // update record occasionally so it doesn't get stale
-                       $d = strtotime($person["updated"]." +00:00");
-                       if ($d < strtotime("now - 14 days")) {
-                               $update = true;
-                       }
+                       if (is_null($update)) {
+                               // update record occasionally so it doesn't get stale
+                               $d = strtotime($person["updated"]." +00:00");
+                               if ($d < strtotime("now - 14 days")) {
+                                       $update = true;
+                               }
 
-                       if ($person["guid"] == "") {
-                               $update = true;
+                               if ($person["guid"] == "") {
+                                       $update = true;
+                               }
                        }
+               } elseif (is_null($update)) {
+                       $update = !DBA::isResult($person);
+               } else {
+                       $person = [];
                }
 
-               if (!DBA::isResult($person) || $update) {
+               if ($update) {
                        Logger::log("create or refresh", Logger::DEBUG);
                        $r = Probe::uri($handle, Protocol::DIASPORA);
 
@@ -969,12 +985,7 @@ class Diaspora
                        if ($r && ($r["network"] === Protocol::DIASPORA)) {
                                self::updateFContact($r);
 
-                               // Fetch the updated or added contact
-                               $person = DBA::selectFirst('fcontact', [], ['network' => Protocol::DIASPORA, 'addr' => $handle]);
-                               if (!DBA::isResult($person)) {
-                                       $person = $r;
-                                       $person['id'] = 0;
-                               }
+                               $person = self::personByHandle($handle, false);
                        }
                }
 
@@ -1111,6 +1122,20 @@ class Diaspora
                return $contact;
        }
 
+       /**
+        * Checks if the given contact url does support ActivityPub
+        *
+        * @param string  $url    profile url
+        * @param boolean $update true = always update, false = never update, null = update when not found or outdated
+        * @return boolean
+        * @throws \Friendica\Network\HTTPException\InternalServerErrorException
+        * @throws \ImagickException
+        */
+       public static function isSupportedByContactUrl($url, $update = null)
+       {
+               return !empty(self::personByHandle($url, $update));
+       }
+
        /**
         * @brief Check if posting is allowed for this contact
         *
@@ -1408,6 +1433,39 @@ class Diaspora
                return $msg;
        }
 
+       /**
+        * @brief Fetches an item with a given URL
+        *
+        * @param string $url the message url
+        *
+        * @return int the message id of the stored message or false
+        * @throws \Friendica\Network\HTTPException\InternalServerErrorException
+        * @throws \ImagickException
+        */
+       public static function fetchByURL($url, $uid = 0)
+       {
+               // Check for Diaspora (and Friendica) typical paths
+               if (!preg_match("=(https?://.+)/(?:posts|display)/([a-zA-Z0-9-_@.:%]+[a-zA-Z0-9])=i", $url, $matches)) {
+                       return false;
+               }
+
+               $guid = urldecode($matches[2]);
+
+               $item = Item::selectFirst(['id'], ['guid' => $guid, 'uid' => $uid]);
+               if (DBA::isResult($item)) {
+                       return $item['id'];
+               }
+
+               self::storeByGuid($guid, $matches[1], $uid);
+
+               $item = Item::selectFirst(['id'], ['guid' => $guid, 'uid' => $uid]);
+               if (DBA::isResult($item)) {
+                       return $item['id'];
+               } else {
+                       return false;
+               }
+       }
+
        /**
         * @brief Fetches the item record of a given guid
         *
@@ -2258,8 +2316,8 @@ class Diaspora
                $fields = ['name' => $name, 'location' => $location,
                        'name-date' => DateTimeFormat::utcNow(),
                        'about' => $about, 'gender' => $gender,
-                       'addr' => $author, 'nick' => $nick,
-                       'keywords' => $keywords];
+                       'addr' => $author, 'nick' => $nick, 'keywords' => $keywords,
+                       'unsearchable' => !$searchable, 'sensitive' => $nsfw];
 
                if (!empty($birthday)) {
                        $fields['bd'] = $birthday;
@@ -2267,6 +2325,8 @@ class Diaspora
 
                DBA::update('contact', $fields, ['id' => $contact['id']]);
 
+               // @todo Update the public contact, then update the gcontact from that
+
                $gcontact = ["url" => $contact["url"], "network" => Protocol::DIASPORA, "generation" => 2,
                                        "photo" => $image_url, "name" => $name, "location" => $location,
                                        "about" => $about, "birthday" => $birthday, "gender" => $gender,
@@ -3543,7 +3603,7 @@ class Diaspora
                $public = ($item["private"] ? "false" : "true");
 
                $created = DateTimeFormat::utc($item["created"], DateTimeFormat::ATOM);
-               $edited = DateTimeFormat::utc($item["edited"], DateTimeFormat::ATOM);
+               $edited = DateTimeFormat::utc($item["edited"] ?? $item["created"], DateTimeFormat::ATOM);
 
                // Detect a share element and do a reshare
                if (!$item['private'] && ($ret = self::isReshare($item["body"]))) {