]> git.mxchange.org Git - friendica.git/commitdiff
Auto discovery of contacts from GNU Social
authorMichael Vogel <icarus@dabo.de>
Tue, 19 Jan 2016 14:12:18 +0000 (15:12 +0100)
committerMichael Vogel <icarus@dabo.de>
Tue, 19 Jan 2016 14:12:18 +0000 (15:12 +0100)
include/discover_poco.php
include/socgraph.php

index 6293317d3f4401e9ec71b0fe213f14ad669193e9..5411e757ad286624858dcafea4f10fba164e8d2a 100644 (file)
@@ -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);
 
index a01c9c5cb025ece0a1c4c7768bf32ebdd65cdc64..4b4b3587879cc6e2bd11fc4ba09710d5d98fa75b 100644 (file)
@@ -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"]));
+       }
+}
 ?>