]> git.mxchange.org Git - friendica.git/commitdiff
Store the server transport protocol
authorMichael <heluecht@pirati.ca>
Sat, 9 Jan 2021 19:18:22 +0000 (19:18 +0000)
committerHypolite Petovan <hypolite@mrpetovan.com>
Mon, 11 Jan 2021 00:08:23 +0000 (19:08 -0500)
database.sql
src/Model/GServer.php
src/Model/PushSubscriber.php
src/Worker/APDelivery.php
src/Worker/Delivery.php
static/dbstructure.config.php

index 87ea52aa5952e7960ad0abe060092514ca903487..1d4f335e4a17b343932e54cd0986620d194bf601 100644 (file)
@@ -1,6 +1,6 @@
 -- ------------------------------------------
--- Friendica 2020.12-rc (Red Hot Poker)
--- DB_UPDATE_VERSION 1384
+-- Friendica 2021.03-dev (Red Hot Poker)
+-- DB_UPDATE_VERSION 1385
 -- ------------------------------------------
 
 
@@ -20,6 +20,7 @@ CREATE TABLE IF NOT EXISTS `gserver` (
        `poco` varchar(255) NOT NULL DEFAULT '' COMMENT '',
        `noscrape` varchar(255) NOT NULL DEFAULT '' COMMENT '',
        `network` char(4) NOT NULL DEFAULT '' COMMENT '',
+       `protocol` tinyint unsigned COMMENT 'The protocol of the server',
        `platform` varchar(255) NOT NULL DEFAULT '' COMMENT '',
        `relay-subscribe` boolean NOT NULL DEFAULT '0' COMMENT 'Has the server subscribed to the relay system',
        `relay-scope` varchar(10) NOT NULL DEFAULT '' COMMENT 'The scope of messages that the server wants to get',
index e91a946e0bf3e0362b03e0dd2da7db2385ac7097..86893529177b1e078fb113889684e4c9d270059b 100644 (file)
@@ -23,6 +23,7 @@ namespace Friendica\Model;
 
 use DOMDocument;
 use DOMXPath;
+use Exception;
 use Friendica\Core\Logger;
 use Friendica\Core\Protocol;
 use Friendica\Core\System;
@@ -1734,4 +1735,65 @@ class GServer
 
                DI::config()->set('poco', 'last_federation_discovery', time());
        }
+
+       /**
+        * Set the protocol for the given server
+        *
+        * @param int $gsid     Server id
+        * @param int $protocol Protocol id
+        * @return void 
+        * @throws Exception 
+        */
+       static function setProtocol(int $gsid, int $protocol)
+       {
+               if (empty($gsid)) {
+                       return;
+               }
+
+               $gserver = DBA::selectFirst('gserver', ['protocol', 'url'], ['id' => $gsid]);
+               if (!DBA::isResult($gserver)) {
+                       return;
+               }
+
+               $old = $gserver['protocol'];
+
+               if (!is_null($old)) {
+                       /*
+                       The priority for the protocols is:
+                               1. ActivityPub
+                               2. DFRN via Diaspora
+                               3. Legacy DFRN
+                               4. Diaspora
+                               5. OStatus
+                       */
+
+                       // We don't need to change it when nothing is to be changed
+                       if ($old == $protocol) {
+                               return;
+                       }
+
+                       // We don't want to mark a server as OStatus when it had been marked with any other protocol before
+                       if ($protocol == Post\DeliveryData::OSTATUS) {
+                               return;
+                       }
+
+                       // If the server is marked as ActivityPub then we won't change it to anything different
+                       if ($old == Post\DeliveryData::ACTIVITYPUB) {
+                               return;
+                       }
+
+                       // Don't change it to anything lower than DFRN if the new one wasn't ActivityPub
+                       if (($old == Post\DeliveryData::DFRN) && ($protocol != Post\DeliveryData::ACTIVITYPUB)) {
+                               return;
+                       }
+
+                       // Don't change it to Diaspora when it is a legacy DFRN server
+                       if (($old == Post\DeliveryData::LEGACY_DFRN) && ($protocol == Post\DeliveryData::DIASPORA)) {
+                               return;
+                       }
+               }
+
+               Logger::info('Protocol for server', ['protocol' => $protocol, 'old' => $old, 'id' => $gsid, 'url' => $gserver['url']]);
+               DBA::update('gserver', ['protocol' => $protocol], ['id' => $gsid]);
+       }
 }
index 2a7be3c35a1c6eb86f8264d6015f81e0f3aea4d4..84d45c4c4dd35d450e92b2423315dbf5c4ea7437 100644 (file)
@@ -25,6 +25,7 @@ use Friendica\Core\Logger;
 use Friendica\Core\Worker;
 use Friendica\Database\DBA;
 use Friendica\Util\DateTimeFormat;
+use Friendica\Util\Network;
 
 class PushSubscriber
 {
@@ -170,5 +171,13 @@ class PushSubscriber
                $fields = ['push' => 0, 'next_try' => DBA::NULL_DATETIME, 'last_update' => $last_update];
                DBA::update('push_subscriber', $fields, ['id' => $id]);
                Logger::log('Subscriber ' . $subscriber['callback_url'] . ' for ' . $subscriber['nickname'] . ' is marked as vital', Logger::DEBUG);
+
+               $parts = parse_url($subscriber['callback_url']);
+               unset($parts['path']);
+               $server_url = Network::unparseURL($parts);
+               $gsid = GServer::getID($server_url, true);
+               if (!empty($gsid)) {
+                       GServer::setProtocol($gsid, Post\DeliveryData::OSTATUS);
+               }
        }
 }
index 0c2c0ca9c487c59aa775418c0f102344adc58dd7..2bf869b4dec1fe0bc3048cd9817243239eea6ee0 100644 (file)
@@ -24,6 +24,7 @@ namespace Friendica\Worker;
 use Friendica\Core\Logger;
 use Friendica\Core\Worker;
 use Friendica\Model\Contact;
+use Friendica\Model\GServer;
 use Friendica\Model\Item;
 use Friendica\Model\Post;
 use Friendica\Protocol\ActivityPub;
@@ -82,6 +83,7 @@ class APDelivery
                // This should never fail and is temporariy (until the move to the "post" structure)
                $item = Item::selectFirst(['uri-id'], ['id' => $item_id]);
                $uriid = $item['uri-id'] ?? 0;
+               $gsid = null;
 
                foreach ($receivers as $receiver) {
                        $contact = Contact::getById($receiver);
@@ -89,6 +91,8 @@ class APDelivery
                                continue;
                        }
 
+                       $gsid = $gsid ?: $contact['gsid'];
+
                        if ($success) {
                                Contact::unmarkForArchival($contact);
                        } else {
@@ -96,6 +100,10 @@ class APDelivery
                        }
                }
 
+               if (!empty($gsid)) {
+                       GServer::setProtocol($gsid, Post\DeliveryData::ACTIVITYPUB);
+               }
+
                if (!$success && !Worker::defer() && in_array($cmd, [Delivery::POST])) {
                        Post\DeliveryData::incrementQueueFailed($uriid);
                } elseif ($success && in_array($cmd, [Delivery::POST])) {
index 600243d50b9aab774d0f95fecbe9bf8af7beade8..73d4d160d6e411aba668787df8d61bb16177ddd5 100644 (file)
@@ -360,6 +360,8 @@ class Delivery
                                if (in_array($cmd, [Delivery::POST, Delivery::POKE])) {
                                        if (($deliver_status >= 200) && ($deliver_status <= 299)) {
                                                Model\Post\DeliveryData::incrementQueueDone($target_item['uri-id'], $protocol);
+
+                                               Model\GServer::setProtocol($contact['gsid'], $protocol);
                                        } else {
                                                Model\Post\DeliveryData::incrementQueueFailed($target_item['uri-id']);
                                        }
@@ -391,6 +393,8 @@ class Delivery
                        // We successfully delivered a message, the contact is alive
                        Model\Contact::unmarkForArchival($contact);
 
+                       Model\GServer::setProtocol($contact['gsid'], $protocol);
+
                        if (in_array($cmd, [Delivery::POST, Delivery::POKE])) {
                                Model\Post\DeliveryData::incrementQueueDone($target_item['uri-id'], $protocol);
                        }
@@ -476,6 +480,8 @@ class Delivery
                        // We successfully delivered a message, the contact is alive
                        Model\Contact::unmarkForArchival($contact);
 
+                       Model\GServer::setProtocol($contact['gsid'], Model\Post\DeliveryData::DIASPORA);
+
                        if (in_array($cmd, [Delivery::POST, Delivery::POKE])) {
                                Model\Post\DeliveryData::incrementQueueDone($target_item['uri-id'], Model\Post\DeliveryData::DIASPORA);
                        }
index f03f90bcb0a0f6a68ce31e824539a1c7e26cde2d..b12ac56b28f38457064a5dbc3c9ff0fd6674380a 100644 (file)
@@ -55,7 +55,7 @@
 use Friendica\Database\DBA;
 
 if (!defined('DB_UPDATE_VERSION')) {
-       define('DB_UPDATE_VERSION', 1384);
+       define('DB_UPDATE_VERSION', 1385);
 }
 
 return [
@@ -75,6 +75,7 @@ return [
                        "poco" => ["type" => "varchar(255)", "not null" => "1", "default" => "", "comment" => ""],
                        "noscrape" => ["type" => "varchar(255)", "not null" => "1", "default" => "", "comment" => ""],
                        "network" => ["type" => "char(4)", "not null" => "1", "default" => "", "comment" => ""],
+                       "protocol" => ["type" => "tinyint unsigned", "comment" => "The protocol of the server"],
                        "platform" => ["type" => "varchar(255)", "not null" => "1", "default" => "", "comment" => ""],
                        "relay-subscribe" => ["type" => "boolean", "not null" => "1", "default" => "0", "comment" => "Has the server subscribed to the relay system"],
                        "relay-scope" => ["type" => "varchar(10)", "not null" => "1", "default" => "", "comment" => "The scope of messages that the server wants to get"],