From: Michael Vogel <icarus@dabo.de>
Date: Tue, 19 Jan 2016 14:12:18 +0000 (+0100)
Subject: Auto discovery of contacts from GNU Social
X-Git-Url: https://git.mxchange.org/?a=commitdiff_plain;h=5a6950bace2b8ccb099f3c30192761adb36e3598;p=friendica.git

Auto discovery of contacts from GNU Social
---

diff --git a/include/discover_poco.php b/include/discover_poco.php
index 6293317d3f..5411e757ad 100644
--- a/include/discover_poco.php
+++ b/include/discover_poco.php
@@ -78,9 +78,15 @@ function discover_poco_run(&$argv, &$argc){
 		discover_users();
 	elseif (($mode == 1) AND ($search != "") and get_config('system','poco_local_search'))
 		discover_directory($search);
-	elseif (($mode == 0) AND ($search == "") and (get_config('system','poco_discovery') > 0))
+	elseif (($mode == 0) AND ($search == "") and (get_config('system','poco_discovery') > 0)) {
+		// Query Friendica and Hubzilla servers for their users
 		poco_discover();
 
+		// Query GNU Social servers for their users ("statistics" addon has to be enabled on the GS server)
+		if (!get_config('system','ostatus_disabled'))
+			gs_discover();
+	}
+
 	logger('end '.$search);
 
 	return;
@@ -128,7 +134,7 @@ function discover_users() {
 		else
 			$server_url = poco_detect_server($user["url"]);
 
-		if (poco_check_server($server_url, $gcontacts[0]["network"])) {
+		if (($server_url == "") OR poco_check_server($server_url, $gcontacts[0]["network"])) {
 			logger('Check user '.$user["url"]);
 			poco_last_updated($user["url"], true);
 
diff --git a/include/socgraph.php b/include/socgraph.php
index a01c9c5cb0..4b4b358787 100644
--- a/include/socgraph.php
+++ b/include/socgraph.php
@@ -721,6 +721,10 @@ function poco_to_boolean($val) {
 
 function poco_check_server($server_url, $network = "", $force = false) {
 
+	// Unify the server address
+	$server_url = trim($server_url, "/");
+	$server_url = str_replace("/index.php", "", $server_url);
+
 	if ($server_url == "")
 		return false;
 
@@ -1315,18 +1319,30 @@ function poco_discover_federation() {
 			return;
 	}
 
+	// Discover Friendica, Hubzilla and Diaspora servers
 	$serverdata = fetch_url("http://the-federation.info/pods.json");
 
-	if (!$serverdata)
-		return;
+	if ($serverdata) {
+		$servers = json_decode($serverdata);
+
+		foreach($servers->pods AS $server)
+			poco_check_server("https://".$server->host);
+	}
 
-	$servers = json_decode($serverdata);
+	// Discover GNU Social Servers
+	if (!get_config('system','ostatus_disabled')) {
+		$serverdata = "http://gstools.org/api/get_open_instances/";
 
-	foreach($servers->pods AS $server)
-		poco_check_server("https://".$server->host);
+		$result = z_fetch_url($serverdata);
+		if ($result["success"]) {
+			$servers = json_decode($result["body"]);
 
-	set_config('poco','last_federation_discovery', time());
+			foreach($servers->data AS $server)
+				poco_check_server($server->instance_address);
+		}
+	}
 
+	set_config('poco','last_federation_discovery', time());
 }
 
 function poco_discover($complete = false) {
@@ -1656,4 +1672,81 @@ function update_gcontact_from_probe($url) {
 	if ($data["network"] != NETWORK_PHANTOM)
 		update_gcontact($data);
 }
+
+/**
+ * @brief Fetches users of given GNU Social server
+ *
+ * If the "Statistics" plugin is enabled (See http://gstools.org/ for details) we query user data with this.
+ *
+ * @param str $server Server address
+ */
+function gs_fetch_users($server) {
+
+	logger("Fetching users from GNU Social server ".$server, LOGGER_DEBUG);
+
+	$a = get_app();
+
+	$url = $server."/main/statistics";
+
+	$result = z_fetch_url($url);
+	if (!$result["success"])
+		return false;
+
+	$statistics = json_decode($result["body"]);
+
+	if (is_object($statistics->config)) {
+		if ($statistics->config->instance_with_ssl)
+			$server = "https://";
+		else
+			$server = "http://";
+
+		$server .= $statistics->config->instance_address;
+
+		$hostname = $statistics->config->instance_address;
+	} else {
+		if ($statistics->instance_with_ssl)
+			$server = "https://";
+		else
+			$server = "http://";
+
+		$server .= $statistics->instance_address;
+
+		$hostname = $statistics->instance_address;
+	}
+
+	foreach ($statistics->users AS $nick => $user) {
+		$profile_url = $server."/".$user->nickname;
+
+		$contact = array("url" => $profile_url,
+				"name" => $user->fullname,
+				"addr" => $user->nickname."@".$hostname,
+				"nick" => $user->nickname,
+				"about" => $user->bio,
+				"network" => NETWORK_OSTATUS,
+				"photo" => $a->get_baseurl()."/images/person-175.jpg");
+		get_gcontact_id($contact);
+	}
+}
+
+/**
+ * @brief Asking GNU Social server on a regular base for their user data
+ *
+ */
+function gs_discover() {
+
+	$requery_days = intval(get_config("system", "poco_requery_days"));
+
+	$last_update = date("c", time() - (60 * 60 * 24 * $requery_days));
+
+	$r = q("SELECT `nurl`, `url` FROM `gserver` WHERE `last_contact` >= `last_failure` AND `network` = '%s' AND `last_poco_query` < '%s' ORDER BY RAND() LIMIT 5",
+		dbesc(NETWORK_OSTATUS), dbesc($last_update));
+
+	if (!$r)
+		return;
+
+	foreach ($r AS $server) {
+		gs_fetch_users($server["url"]);
+		q("UPDATE `gserver` SET `last_poco_query` = '%s' WHERE `nurl` = '%s'", dbesc(datetime_convert()), dbesc($server["nurl"]));
+	}
+}
 ?>