From 6e670df80f353ed31ed55f5f077c5d363963f2e9 Mon Sep 17 00:00:00 2001
From: Michael <heluecht@pirati.ca>
Date: Thu, 7 Sep 2017 15:34:50 +0000
Subject: [PATCH 1/2] OStatus: Better attachment handling for pictures

---
 include/ostatus.php | 45 +++++++++++++++++++++++++--------------------
 include/text.php    | 11 ++---------
 2 files changed, 27 insertions(+), 29 deletions(-)

diff --git a/include/ostatus.php b/include/ostatus.php
index 809a48cd02..5fcf2003c3 100644
--- a/include/ostatus.php
+++ b/include/ostatus.php
@@ -351,17 +351,11 @@ class ostatus {
 				continue;
 			}
 
-			$item["body"] = add_page_info_to_body(html2bbcode($xpath->query('atom:content/text()', $entry)->item(0)->nodeValue));
+			$item["body"] = html2bbcode($xpath->query('atom:content/text()', $entry)->item(0)->nodeValue);
+			//$item["body"] = add_page_info_to_body(html2bbcode($xpath->query('atom:content/text()', $entry)->item(0)->nodeValue));
 			$item["object-type"] = $xpath->query('activity:object-type/text()', $entry)->item(0)->nodeValue;
 			$item["verb"] = $xpath->query('activity:verb/text()', $entry)->item(0)->nodeValue;
 
-			// Mastodon Content Warning
-			if (($item["verb"] == ACTIVITY_POST) && $xpath->evaluate('boolean(atom:summary)', $entry)) {
-				$clear_text = $xpath->query('atom:summary/text()', $entry)->item(0)->nodeValue;
-
-				$item["body"] = html2bbcode($clear_text) . '[spoiler]' . $item["body"] . '[/spoiler]';
-			}
-
 			if (($item["object-type"] == ACTIVITY_OBJ_BOOKMARK) || ($item["object-type"] == ACTIVITY_OBJ_EVENT)) {
 				$item["title"] = $xpath->query('atom:title/text()', $entry)->item(0)->nodeValue;
 				$item["body"] = $xpath->query('atom:summary/text()', $entry)->item(0)->nodeValue;
@@ -464,7 +458,6 @@ class ostatus {
 			}
 
 			$self = "";
-			$enclosure = "";
 
 			$links = $xpath->query('atom:link', $entry);
 			if ($links) {
@@ -488,14 +481,18 @@ class ostatus {
 								}
 								break;
 							case "enclosure":
-								$enclosure = $attribute['href'];
-								if (strlen($item["attach"])) {
-									$item["attach"] .= ',';
+								$filetype = strtolower(substr($attribute['type'], 0, strpos($attribute['type'],'/')));
+								if ($filetype == 'image') {
+									$item['body'] .= "\n[img]".$attribute['href'].'[/img]';
+								} else {
+									if (strlen($item["attach"])) {
+										$item["attach"] .= ',';
+									}
+									if (!isset($attribute['length'])) {
+										$attribute['length'] = "0";
+									}
+									$item["attach"] .= '[attach]href="'.$attribute['href'].'" length="'.$attribute['length'].'" type="'.$attribute['type'].'" title="'.$attribute['title'].'"[/attach]';
 								}
-								if (!isset($attribute['length'])) {
-									$attribute['length'] = "0";
-								}
-								$item["attach"] .= '[attach]href="'.$attribute['href'].'" length="'.$attribute['length'].'" type="'.$attribute['type'].'" title="'.$attribute['title'].'"[/attach]';
 								break;
 							case "related":
 								if ($item["object-type"] != ACTIVITY_OBJ_BOOKMARK) {
@@ -523,6 +520,18 @@ class ostatus {
 				}
 			}
 
+			// Only add additional data when there is no picture in the post
+			if (!strstr($item["body"],'[/img]')) {
+				$item["body"] = add_page_info_to_body($item["body"]);
+			}
+
+			// Mastodon Content Warning
+			if (($item["verb"] == ACTIVITY_POST) && $xpath->evaluate('boolean(atom:summary)', $entry)) {
+				$clear_text = $xpath->query('atom:summary/text()', $entry)->item(0)->nodeValue;
+
+				$item["body"] = html2bbcode($clear_text) . '[spoiler]' . $item["body"] . '[/spoiler]';
+			}
+
 			$local_id = "";
 			$repeat_of = "";
 
@@ -601,7 +610,6 @@ class ostatus {
 						foreach ($enclosures AS $link) {
 							$attribute = self::read_attributes($link);
 							if ($href != "") {
-								$enclosure = $attribute['href'];
 								if (strlen($item["attach"])) {
 									$item["attach"] .= ',';
 								}
@@ -615,9 +623,6 @@ class ostatus {
 				}
 			}
 
-			//if ($enclosure != "")
-			//	$item["body"] .= add_page_info($enclosure);
-
 			if (isset($item["parent-uri"])) {
 				$r = q("SELECT `id` FROM `item` WHERE `uid` = %d AND `uri` = '%s'",
 					intval($importer["uid"]), dbesc($item["parent-uri"]));
diff --git a/include/text.php b/include/text.php
index c2c6dcdfc9..66d4f17ea6 100644
--- a/include/text.php
+++ b/include/text.php
@@ -1424,15 +1424,8 @@ function prepare_body(&$item, $attach = false, $preview = false) {
 					$title = ((strlen(trim($mtch[4]))) ? escape_tags(trim($mtch[4])) : escape_tags($mtch[1]));
 					$title .= ' ' . $mtch[2] . ' ' . t('bytes');
 
-					if (($filetype == 'image') AND ($item['network'] == NETWORK_OSTATUS)) {
-						/// @todo Respect the spoiler for mastodon
-						$icon = '<img class="attached" src="'.$the_url.'" alt="" title="'.$title.'">';
-						$s .= '<br><a href="' . strip_tags($the_url) . '" title="' . $title . '" class="attached" target="_blank" >' . $icon . '</a>';
-					} else {
-						$icon = '<div class="attachtype icon s22 type-' . $filetype . ' subtype-' . $filesubtype . '"></div>';
-						$as .= '<a href="' . strip_tags($the_url) . '" title="' . $title . '" class="attachlink" target="_blank" >' . $icon . '</a>';
-					}
-
+					$icon = '<div class="attachtype icon s22 type-' . $filetype . ' subtype-' . $filesubtype . '"></div>';
+					$as .= '<a href="' . strip_tags($the_url) . '" title="' . $title . '" class="attachlink" target="_blank" >' . $icon . '</a>';
 				}
 			}
 		}

From c3eaf336dc3d0923438b127c404099091c6084ed Mon Sep 17 00:00:00 2001
From: Michael <heluecht@pirati.ca>
Date: Thu, 7 Sep 2017 15:48:25 +0000
Subject: [PATCH 2/2] Removed code

---
 include/ostatus.php | 1 -
 1 file changed, 1 deletion(-)

diff --git a/include/ostatus.php b/include/ostatus.php
index 5fcf2003c3..7fd920d635 100644
--- a/include/ostatus.php
+++ b/include/ostatus.php
@@ -352,7 +352,6 @@ class ostatus {
 			}
 
 			$item["body"] = html2bbcode($xpath->query('atom:content/text()', $entry)->item(0)->nodeValue);
-			//$item["body"] = add_page_info_to_body(html2bbcode($xpath->query('atom:content/text()', $entry)->item(0)->nodeValue));
 			$item["object-type"] = $xpath->query('activity:object-type/text()', $entry)->item(0)->nodeValue;
 			$item["verb"] = $xpath->query('activity:verb/text()', $entry)->item(0)->nodeValue;