]> git.mxchange.org Git - friendica.git/blobdiff - include/ostatus.php
Only optical stuff
[friendica.git] / include / ostatus.php
index 4fb07df7114fbf8a9c5237e50b09eef7cd868cdf..e0e6c2186e308bd0325ff141f0e336cf571b0fde 100644 (file)
@@ -9,6 +9,8 @@ require_once("include/socgraph.php");
 require_once("include/Photo.php");
 require_once("include/Scrape.php");
 require_once("include/follow.php");
+require_once("include/api.php");
+require_once("mod/proxy.php");
 
 define('OSTATUS_DEFAULT_POLL_INTERVAL', 30); // given in minutes
 define('OSTATUS_DEFAULT_POLL_TIMEFRAME', 1440); // given in minutes
@@ -1077,6 +1079,35 @@ function ostatus_store_conversation($itemid, $conversation_url) {
        }
 }
 
+function get_reshared_guid($item) {
+       $body = trim($item["body"]);
+
+       // Skip if it isn't a pure repeated messages
+       // Does it start with a share?
+       if (strpos($body, "[share") > 0)
+               return("");
+
+       // Does it end with a share?
+       if (strlen($body) > (strrpos($body, "[/share]") + 8))
+               return("");
+
+       $attributes = preg_replace("/\[share(.*?)\]\s?(.*?)\s?\[\/share\]\s?/ism","$1",$body);
+       // Skip if there is no shared message in there
+       if ($body == $attributes)
+               return(false);
+
+       $guid = "";
+       preg_match("/guid='(.*?)'/ism", $attributes, $matches);
+       if ($matches[1] != "")
+               $guid = $matches[1];
+
+       preg_match('/guid="(.*?)"/ism', $attributes, $matches);
+       if ($matches[1] != "")
+               $guid = $matches[1];
+
+       return $guid;
+}
+
 function xml_add_element($doc, $parent, $element, $value = "", $attributes = array()) {
        $element = $doc->createElement($element, xmlify($value));
 
@@ -1089,6 +1120,33 @@ function xml_add_element($doc, $parent, $element, $value = "", $attributes = arr
        $parent->appendChild($element);
 }
 
+function ostatus_format_picture_post($body) {
+       $siteinfo = get_attached_data($body);
+
+       if (($siteinfo["type"] == "photo")) {
+               if (isset($siteinfo["preview"]))
+                       $preview = $siteinfo["preview"];
+               else
+                       $preview = $siteinfo["image"];
+
+               // Is it a remote picture? Then make a smaller preview here
+               $preview = proxy_url($preview, false, PROXY_SIZE_SMALL);
+
+               // Is it a local picture? Then make it smaller here
+               $preview = str_replace(array("-0.jpg", "-0.png"), array("-2.jpg", "-2.png"), $preview);
+               $preview = str_replace(array("-1.jpg", "-1.png"), array("-2.jpg", "-2.png"), $preview);
+
+               if (isset($siteinfo["url"]))
+                       $url = $siteinfo["url"];
+               else
+                       $url = $siteinfo["image"];
+
+               $body = trim($siteinfo["text"])." [url]".$url."[/url]\n[img]".$preview."[/img]";
+       }
+
+       return $body;
+}
+
 function ostatus_add_header($doc, $owner) {
        $a = get_app();
 
@@ -1201,6 +1259,14 @@ function ostatus_get_attachment($doc, $root, $item) {
                        break;
        }
 
+       if (($siteinfo["type"] != "photo") AND isset($siteinfo["image"])) {
+               $photodata = get_photo_info($siteinfo["image"]);
+
+               $attributes = array("rel" => "preview", "href" => $siteinfo["image"], "media:width" => $photodata[0], "media:height" => $photodata[1]);
+               xml_add_element($doc, $root, "link", "", $attributes);
+       }
+
+
        $arr = explode('[/attach],',$item['attach']);
        if(count($arr)) {
                foreach($arr as $r) {
@@ -1223,13 +1289,13 @@ function ostatus_get_attachment($doc, $root, $item) {
        }
 }
 
-function ostatus_add_author($doc, $owner, $profile) {
+function ostatus_add_author($doc, $owner, $profile = array()) {
        $a = get_app();
 
        $author = $doc->createElement("author");
        xml_add_element($doc, $author, "activity:object-type", ACTIVITY_OBJ_PERSON);
        xml_add_element($doc, $author, "uri", $owner["url"]);
-       xml_add_element($doc, $author, "name", $owner["nick"]);
+       xml_add_element($doc, $author, "name", $owner["name"]);
 
        $attributes = array("rel" => "alternate", "type" => "text/html", "href" => $owner["url"]);
        xml_add_element($doc, $author, "link", "", $attributes);
@@ -1239,20 +1305,22 @@ function ostatus_add_author($doc, $owner, $profile) {
                        "type" => "image/jpeg", // To-Do?
                        "media:width" => 175,
                        "media:height" => 175,
-                       "href" => $profile["photo"]);
+                       "href" => $owner["photo"]);
        xml_add_element($doc, $author, "link", "", $attributes);
 
-       $attributes = array(
-                       "rel" => "avatar",
-                       "type" => "image/jpeg", // To-Do?
-                       "media:width" => 80,
-                       "media:height" => 80,
-                       "href" => $profile["thumb"]);
-       xml_add_element($doc, $author, "link", "", $attributes);
+       if (isset($owner["thumb"])) {
+               $attributes = array(
+                               "rel" => "avatar",
+                               "type" => "image/jpeg", // To-Do?
+                               "media:width" => 80,
+                               "media:height" => 80,
+                               "href" => $owner["thumb"]);
+               xml_add_element($doc, $author, "link", "", $attributes);
+       }
 
        xml_add_element($doc, $author, "poco:preferredUsername", $owner["nick"]);
-       xml_add_element($doc, $author, "poco:displayName", $profile["name"]);
-       xml_add_element($doc, $author, "poco:note", $profile["about"]);
+       xml_add_element($doc, $author, "poco:displayName", $owner["name"]);
+       xml_add_element($doc, $author, "poco:note", $owner["about"]);
 
        if (trim($owner["location"]) != "") {
                $element = $doc->createElement("poco:address");
@@ -1268,18 +1336,46 @@ function ostatus_add_author($doc, $owner, $profile) {
                $author->appendChild($urls);
        }
 
-       xml_add_element($doc, $author, "followers", "", array("url" => $a->get_baseurl()."/viewcontacts/".$owner["nick"]));
-       xml_add_element($doc, $author, "statusnet:profile_info", "", array("local_id" => $owner["uid"]));
+       if (count($profile)) {
+               xml_add_element($doc, $author, "followers", "", array("url" => $a->get_baseurl()."/viewcontacts/".$owner["nick"]));
+               xml_add_element($doc, $author, "statusnet:profile_info", "", array("local_id" => $owner["uid"]));
+       }
 
        return $author;
 }
 
-function ostatus_entry($doc, $item, $owner, $toplevel = false) {
+/*
+To-Do: Picture attachments should look like this:
+
+<a href="https://status.pirati.ca/attachment/572819" title="https://status.pirati.ca/file/heluecht-20151202T222602-rd3u49p.gif"
+class="attachment thumbnail" id="attachment-572819" rel="nofollow external">https://status.pirati.ca/attachment/572819</a>
+
+*/
+
+function ostatus_entry($doc, $item, $owner, $toplevel = false, $repeat = false) {
        $a = get_app();
 
-       if (!$toplevel) {
+       $is_repeat = false;
+
+/*     if (!$repeat) {
+               $repeated_guid = get_reshared_guid($item);
+
+               if ($repeated_guid != "") {
+                       $r = q("SELECT * FROM `item` WHERE `uid` = %d AND `guid` = '%s' LIMIT 1",
+                               intval($owner["uid"]), dbesc($repeated_guid));
+                       if ($r) {
+                               $repeated_item = $r[0];
+                               $is_repeat = true;
+                       }
+               }
+       }
+*/
+       if (!$toplevel AND !$repeat) {
                $entry = $doc->createElement("entry");
                $title = sprintf("New note by %s", $owner["nick"]);
+       } elseif (!$toplevel AND $repeat) {
+               $entry = $doc->createElement("activity:object");
+               $title = sprintf("New note by %s", $owner["nick"]);
        } else {
                $entry = $doc->createElementNS(NS_ATOM, "entry");
 
@@ -1291,12 +1387,15 @@ function ostatus_entry($doc, $item, $owner, $toplevel = false) {
                $entry->setAttribute("xmlns:ostatus", NS_OSTATUS);
                $entry->setAttribute("xmlns:statusnet", NS_STATUSNET);
 
-               $r = q("SELECT * FROM `profile` WHERE `uid` = %d AND `is-default`",
-                       intval($owner["uid"]));
-               if (!$r)
-                       return;
+               if (!$repeat) {
+                       $r = q("SELECT * FROM `profile` WHERE `uid` = %d AND `is-default`",
+                               intval($owner["uid"]));
+                       if (!$r)
+                               return;
 
-               $profile = $r[0];
+                       $profile = $r[0];
+               }
+                       $profile = array();
 
                $author = ostatus_add_author($doc, $owner, $profile);
                $entry->appendChild($author);
@@ -1315,7 +1414,11 @@ function ostatus_entry($doc, $item, $owner, $toplevel = false) {
        // But: it seems as if it doesn't federate well between the GS servers
        // So we just set it to "note" to be sure that it reaches their target systems
 
-       xml_add_element($doc, $entry, "activity:object-type", ACTIVITY_OBJ_NOTE);
+       if (!$repeat)
+               xml_add_element($doc, $entry, "activity:object-type", ACTIVITY_OBJ_NOTE);
+       else
+               xml_add_element($doc, $entry, "activity:object-type", NAMESPACE_ACTIVITY_SCHEMA.'activity');
+
        xml_add_element($doc, $entry, "id", $item["uri"]);
        xml_add_element($doc, $entry, "title", $title);
 
@@ -1324,9 +1427,12 @@ function ostatus_entry($doc, $item, $owner, $toplevel = false) {
        else
                $body = $item['body'];
 
+       $body = ostatus_format_picture_post($body);
+
        if ($item['title'] != "")
                $body = "[b]".$item['title']."[/b]\n\n".$body;
 
+       //$body = bb_remove_share_information($body);
        $body = bbcode($body, false, false, 7);
 
        xml_add_element($doc, $entry, "content", $body, array("type" => "html"));
@@ -1335,12 +1441,43 @@ function ostatus_entry($doc, $item, $owner, $toplevel = false) {
                                                        "href" => $a->get_baseurl()."/display/".$item["guid"]));
 
        xml_add_element($doc, $entry, "status_net", "", array("notice_id" => $item["id"]));
-       xml_add_element($doc, $entry, "activity:verb", construct_verb($item));
+
+       if (!$is_repeat)
+               xml_add_element($doc, $entry, "activity:verb", construct_verb($item));
+       else
+               xml_add_element($doc, $entry, "activity:verb", ACTIVITY_SHARE);
+
        xml_add_element($doc, $entry, "published", datetime_convert("UTC","UTC",$item["created"]."+00:00",ATOM_TIME));
        xml_add_element($doc, $entry, "updated", datetime_convert("UTC","UTC",$item["edited"]."+00:00",ATOM_TIME));
 
+       if ($is_repeat) {
+               $repeated_owner = array();
+               $repeated_owner["name"] = $repeated_item["author-name"];
+               $repeated_owner["url"] = $repeated_item["author-link"];
+               $repeated_owner["photo"] = $repeated_item["author-avatar"];
+               $repeated_owner["nick"] = $repeated_owner["name"];
+               $repeated_owner["location"] = "";
+               $repeated_owner["about"] = "";
+               $repeated_owner["uid"] = 0;
+
+               $r =q("SELECT * FROM `unique_contacts` WHERE `url` = '%s'", normalise_link($repeated_item["author-link"]));
+               if ($r) {
+                       $repeated_owner["nick"] = $r[0]["nick"];
+                       $repeated_owner["location"] = $r[0]["location"];
+                       $repeated_owner["about"] = $r[0]["about"];
+               }
+
+               $entry_repeat = ostatus_entry($doc, $repeated_item, $repeated_owner, false, true);
+               $entry->appendChild($entry_repeat);
+       } elseif ($repeat) {
+               $author = ostatus_add_author($doc, $owner);
+               $entry->appendChild($author);
+       }
+
+       $mentioned = array();
+
        if (($item['parent'] != $item['id']) || ($item['parent-uri'] !== $item['uri']) || (($item['thr-parent'] !== '') && ($item['thr-parent'] !== $item['uri']))) {
-               $parent = q("SELECT `guid` FROM `item` WHERE `id` = %d", intval($item["parent"]));
+               $parent = q("SELECT `guid`, `author-link`, `owner-link` FROM `item` WHERE `id` = %d", intval($item["parent"]));
                $parent_item = (($item['thr-parent']) ? $item['thr-parent'] : $item['parent-uri']);
 
                $attributes = array(
@@ -1353,7 +1490,18 @@ function ostatus_entry($doc, $item, $owner, $toplevel = false) {
                                "rel" => "related",
                                "href" => $a->get_baseurl()."/display/".$parent[0]["guid"]);
                xml_add_element($doc, $entry, "link", "", $attributes);
-        }
+
+               $mentioned[$parent[0]["author-link"]] = $parent[0]["author-link"];
+               $mentioned[$parent[0]["owner-link"]] = $parent[0]["owner-link"];
+
+               $thrparent = q("SELECT `guid`, `author-link`, `owner-link` FROM `item` WHERE `uid` = %d AND `uri` = '%s'",
+                               intval($owner["uid"]),
+                               dbesc($parent_item));
+               if ($thrparent) {
+                       $mentioned[$thrparent[0]["author-link"]] = $thrparent[0]["author-link"];
+                       $mentioned[$thrparent[0]["owner-link"]] = $thrparent[0]["owner-link"];
+               }
+       }
 
        xml_add_element($doc, $entry, "link", "", array("rel" => "ostatus:conversation",
                                                        "href" => $a->get_baseurl()."/display/".$owner["nick"]."/".$item["parent"]));
@@ -1364,9 +1512,29 @@ function ostatus_entry($doc, $item, $owner, $toplevel = false) {
        if(count($tags))
                foreach($tags as $t)
                        if ($t[0] == "@")
-                               xml_add_element($doc, $entry, "link", "", array("rel" => "mentioned",
+                               $mentioned[$t[1]] = $t[1];
+
+       // Make sure that mentions are accepted (GNU Social has problems with mixing HTTP and HTTPS)
+       $newmentions = array();
+       foreach ($mentioned AS $mention) {
+               $newmentions[str_replace("http://", "https://", $mention)] = str_replace("http://", "https://", $mention);
+               $newmentions[str_replace("https://", "http://", $mention)] = str_replace("https://", "http://", $mention);
+       }
+       $mentioned = $newmentions;
+
+       foreach ($mentioned AS $mention) {
+               $r = q("SELECT `forum`, `prv` FROM `contact` WHERE `uid` = %d AND `nurl` = '%s'",
+                       intval($owner["uid"]),
+                       dbesc(normalise_link($mention)));
+               if ($r[0]["forum"] OR $r[0]["prv"])
+                       xml_add_element($doc, $entry, "link", "", array("rel" => "mentioned",
+                                                                               "ostatus:object-type" => ACTIVITY_OBJ_GROUP,
+                                                                               "href" => $mention));
+               else
+                       xml_add_element($doc, $entry, "link", "", array("rel" => "mentioned",
                                                                                "ostatus:object-type" => ACTIVITY_OBJ_PERSON,
-                                                                               "href" => $t[1]));
+                                                                               "href" => $mention));
+       }
 
        if (!$item["private"])
                xml_add_element($doc, $entry, "link", "", array("rel" => "mentioned",
@@ -1382,19 +1550,24 @@ function ostatus_entry($doc, $item, $owner, $toplevel = false) {
 
        // To-Do:
        // The API call has yet to be implemented
-       $attributes = array("href" => $a->get_baseurl()."/api/statuses/show/".$item["id"].".atom",
-                       "rel" => "self", "type" => "application/atom+xml");
-       xml_add_element($doc, $entry, "link", "", $attributes);
+       //$attributes = array("href" => $a->get_baseurl()."/api/statuses/show/".$item["id"].".atom",
+       //              "rel" => "self", "type" => "application/atom+xml");
+       //xml_add_element($doc, $entry, "link", "", $attributes);
 
-       $attributes = array("href" => $a->get_baseurl()."/api/statuses/show/".$item["id"].".atom",
-                       "rel" => "edit", "type" => "application/atom+xml");
-       xml_add_element($doc, $entry, "link", "", $attributes);
+       //$attributes = array("href" => $a->get_baseurl()."/api/statuses/show/".$item["id"].".atom",
+       //              "rel" => "edit", "type" => "application/atom+xml");
+       //xml_add_element($doc, $entry, "link", "", $attributes);
 
        $app = $item["app"];
        if ($app == "")
                $app = "web";
 
-       xml_add_element($doc, $entry, "statusnet:notice_info", "", array("local_id" => $item["id"], "source" => $app));
+
+       $attributes = array("local_id" => $item["id"], "source" => $app);
+       if ($is_repeat)
+               $attributes["repeat_of"] = $repeated_item["id"];
+
+       xml_add_element($doc, $entry, "statusnet:notice_info", "", $attributes);
 
        return $entry;
 }
@@ -1421,12 +1594,17 @@ function ostatus_feed(&$a, $owner_nick, $last_update) {
                        WHERE `item`.`uid` = %d AND `item`.`received` > '%s' AND NOT `item`.`private` AND NOT `item`.`deleted`
                                AND `item`.`allow_cid` = '' AND `item`.`allow_gid` = '' AND `item`.`deny_cid`  = '' AND `item`.`deny_gid`  = ''
                                AND ((`item`.`wall` AND (`item`.`parent` = `item`.`id`))
-                                       OR (`item`.`network` = '%s' AND ((`thread`.`network`='%s') OR (`thritem`.`network` = '%s'))) AND `thread`.`mention`)
-                               AND (`item`.`owner-link` IN ('%s', '%s'))
+                                       OR (`item`.`network` = '%s' AND ((`thread`.`network` IN ('%s', '%s')) OR (`thritem`.`network` IN ('%s', '%s')))) AND `thread`.`mention`)
+                               AND ((`item`.`owner-link` IN ('%s', '%s') AND (`item`.`parent` = `item`.`id`))
+                                       OR (`item`.`author-link` IN ('%s', '%s')))
                        ORDER BY `item`.`received` DESC
                        LIMIT 0, 300",
-                       intval($owner["uid"]), dbesc($check_date),
-                       dbesc(NETWORK_DFRN), dbesc(NETWORK_OSTATUS), dbesc(NETWORK_OSTATUS),
+                       intval($owner["uid"]), dbesc($check_date), dbesc(NETWORK_DFRN),
+                       //dbesc(NETWORK_OSTATUS), dbesc(NETWORK_OSTATUS),
+                       //dbesc(NETWORK_OSTATUS), dbesc(NETWORK_OSTATUS),
+                       dbesc(NETWORK_OSTATUS), dbesc(NETWORK_DFRN),
+                       dbesc(NETWORK_OSTATUS), dbesc(NETWORK_DFRN),
+                       dbesc($owner["nurl"]), dbesc(str_replace("http://", "https://", $owner["nurl"])),
                        dbesc($owner["nurl"]), dbesc(str_replace("http://", "https://", $owner["nurl"]))
                );
 
@@ -1440,7 +1618,7 @@ function ostatus_feed(&$a, $owner_nick, $last_update) {
                $root->appendChild($entry);
        }
 
-       return($doc->saveXML());
+       return(trim($doc->saveXML()));
 }
 
 function ostatus_salmon($item,$owner) {
@@ -1452,6 +1630,6 @@ function ostatus_salmon($item,$owner) {
 
        $doc->appendChild($entry);
 
-       return($doc->saveXML());
+       return(trim($doc->saveXML()));
 }
 ?>