From bd77556b494f167adb675630f44b6e74aa451927 Mon Sep 17 00:00:00 2001
From: Michael <heluecht@pirati.ca>
Date: Fri, 6 Mar 2020 08:08:49 +0000
Subject: [PATCH] New table for relations between global contacts - will
 replace glink in the future

---
 database.sql                  | 12 +++++++++++-
 src/Model/GContact.php        | 17 ++++++++++++++++-
 static/dbstructure.config.php | 13 ++++++++++++-
 3 files changed, 39 insertions(+), 3 deletions(-)

diff --git a/database.sql b/database.sql
index 3cb87fc90a..a5e94f3f54 100644
--- a/database.sql
+++ b/database.sql
@@ -1,6 +1,6 @@
 -- ------------------------------------------
 -- Friendica 2020.03-dev (Dalmatian Bellflower)
--- DB_UPDATE_VERSION 1335
+-- DB_UPDATE_VERSION 1336
 -- ------------------------------------------
 
 
@@ -419,6 +419,16 @@ CREATE TABLE IF NOT EXISTS `gcontact` (
 	 INDEX `updated` (`updated`)
 ) DEFAULT COLLATE utf8mb4_general_ci COMMENT='global contacts';
 
+--
+-- TABLE gfollower
+--
+CREATE TABLE IF NOT EXISTS `gfollower` (
+	`gcid` int unsigned NOT NULL DEFAULT 0 COMMENT 'global contact',
+	`follower-gcid` int unsigned NOT NULL DEFAULT 0 COMMENT 'global contact of the follower',
+	 PRIMARY KEY(`gcid`,`follower-gcid`),
+	 INDEX `follower-gcid` (`follower-gcid`)
+) DEFAULT COLLATE utf8mb4_general_ci COMMENT='Followers of global contacts';
+
 --
 -- TABLE glink
 --
diff --git a/src/Model/GContact.php b/src/Model/GContact.php
index 6f41fd7325..86b6bb94cd 100644
--- a/src/Model/GContact.php
+++ b/src/Model/GContact.php
@@ -1299,10 +1299,25 @@ class GContact
 		}
 
 		if (!empty($followers) || !empty($followings)) {
+			$gcontact = DBA::selectFirst('gcontact', ['id'], ['nurl' => Strings::normaliseLink(($url))]);
+			$gcid = $gcontact['id'];
+			if (!empty($followers)) {
+				// Clear the follower list, since it will be recreated in the next step
+				DBA::delete('gfollower', ['gcid' => $gcid]);
+			}
+
 			$contacts = array_unique(array_merge($followers, $followings));
 			Logger::info('Discover AP contacts', ['url' => $url, 'contacts' => count($contacts)]);
 			foreach ($contacts as $contact) {
-				if (DBA::exists('gcontact', ['nurl' => Strings::normaliseLink(($contact))])) {
+				$gcontact = DBA::selectFirst('gcontact', ['id'], ['nurl' => Strings::normaliseLink(($contact))]);
+				if (DBA::isResult($gcontact)) {
+					if (in_array($contact, $followers)) {
+						$fields = ['gcid' => $gcid, 'follower-gcid' => $gcontact['id']];
+					} elseif (in_array($contact, $followings)) {
+						$fields = ['gcid' => $gcontact['id'], 'follower-gcid' => $gcid];
+					}
+					Logger::info('Set relation between contacts', $fields);
+					DBA::update('gfollower', $fields, $fields, true);
 					continue;
 				}
 				Logger::info('Discover new AP contact', ['url' => $contact]);
diff --git a/static/dbstructure.config.php b/static/dbstructure.config.php
index 0c0b289414..11695b5924 100755
--- a/static/dbstructure.config.php
+++ b/static/dbstructure.config.php
@@ -51,7 +51,7 @@
 use Friendica\Database\DBA;
 
 if (!defined('DB_UPDATE_VERSION')) {
-	define('DB_UPDATE_VERSION', 1335);
+	define('DB_UPDATE_VERSION', 1336);
 }
 
 return [
@@ -490,6 +490,17 @@ return [
 			"updated" => ["updated"],
 		]
 	],
+	"gfollower" => [
+		"comment" => "Followers of global contacts",
+		"fields" => [
+			"gcid" => ["type" => "int unsigned", "not null" => "1", "default" => "0", "primary" => "1", "relation" => ["gcontact" => "id"], "comment" => "global contact"],
+			"follower-gcid" => ["type" => "int unsigned", "not null" => "1", "default" => "0", "primary" => "1", "relation" => ["gcontact" => "id"], "comment" => "global contact of the follower"],
+		],
+		"indexes" => [
+			"PRIMARY" => ["gcid", "follower-gcid"],
+			"follower-gcid" => ["follower-gcid"],
+		]
+	],
 	"glink" => [
 		"comment" => "'friends of friends' linkages derived from poco",
 		"fields" => [
-- 
2.39.5