From 0b1188b7fe15c6a228b68caaabb0a01e87b0d336 Mon Sep 17 00:00:00 2001
From: Michael Vogel <icarus@dabo.de>
Date: Sat, 25 Jun 2016 13:56:55 +0200
Subject: [PATCH] Imrpoved avatar handling when storing them/partly use of
 "micro"

---
 include/Contact.php   | 11 +++++++----
 include/bbcode.php    |  4 ++--
 include/diaspora.php  |  2 ++
 include/items.php     | 12 ++----------
 include/notifier.php  | 19 ++++++++++++++++---
 mod/dfrn_request.php  | 13 ++++++++++---
 mod/notifications.php |  2 ++
 mod/ping.php          |  4 ++--
 8 files changed, 43 insertions(+), 24 deletions(-)

diff --git a/include/Contact.php b/include/Contact.php
index 3ee491e505..10005d3e3c 100644
--- a/include/Contact.php
+++ b/include/Contact.php
@@ -209,21 +209,21 @@ function get_contact_details_by_url($url, $uid = -1, $default = array()) {
 
 	// Fetch contact data from the contact table for the given user
 	$r = q("SELECT `id`, `id` AS `cid`, 0 AS `gid`, 0 AS `zid`, `uid`, `url`, `nurl`, `alias`, `network`, `name`, `nick`, `addr`, `location`, `about`,
-			`keywords`, `gender`, `photo`, `thumb`, `forum`, `prv`, (`forum` | `prv`) AS `community`, `bd` AS `birthday`, `self`
+			`keywords`, `gender`, `photo`, `thumb`, `micro`, `forum`, `prv`, (`forum` | `prv`) AS `community`, `bd` AS `birthday`, `self`
 		FROM `contact` WHERE `nurl` = '%s' AND `uid` = %d",
 			dbesc(normalise_link($url)), intval($uid));
 
 	// Fetch the data from the contact table with "uid=0" (which is filled automatically)
 	if (!$r)
 		$r = q("SELECT `id`, 0 AS `cid`, `id` AS `zid`, 0 AS `gid`, `uid`, `url`, `nurl`, `alias`, `network`, `name`, `nick`, `addr`, `location`, `about`,
-				`keywords`, `gender`, `photo`, `thumb`, `forum`, `prv`, (`forum` | `prv`) AS `community`, `bd` AS `birthday`, 0 AS `self`
+				`keywords`, `gender`, `photo`, `thumb`, `micro`, `forum`, `prv`, (`forum` | `prv`) AS `community`, `bd` AS `birthday`, 0 AS `self`
 			FROM `contact` WHERE `nurl` = '%s' AND `uid` = 0",
 				dbesc(normalise_link($url)));
 
 	// Fetch the data from the gcontact table
 	if (!$r)
 		$r = q("SELECT 0 AS `id`, 0 AS `cid`, `id` AS `gid`, 0 AS `zid`, 0 AS `uid`, `url`, `nurl`, `alias`, `network`, `name`, `nick`, `addr`, `location`, `about`,
-				`keywords`, `gender`, `photo`, `photo` AS `thumb`, `community` AS `forum`, 0 AS `prv`, `community`, `birthday`, 0 AS `self`
+				`keywords`, `gender`, `photo`, `photo` AS `thumb`, `photo` AS `micro`, `community` AS `forum`, 0 AS `prv`, `community`, `birthday`, 0 AS `self`
 			FROM `gcontact` WHERE `nurl` = '%s'",
 				dbesc(normalise_link($url)));
 
@@ -267,9 +267,12 @@ function get_contact_details_by_url($url, $uid = -1, $default = array()) {
 	if (($profile["network"] == "") AND isset($default["network"]))
 		$profile["network"] = $default["network"];
 
-	if (!isset($profile["thumb"]) AND isset($profile["photo"]))
+	if (($profile["thumb"] == "") AND isset($profile["photo"]))
 		$profile["thumb"] = $profile["photo"];
 
+	if (($profile["micro"] == "") AND isset($profile["thumb"]))
+		$profile["micro"] = $profile["thumb"];
+
 	if ((($profile["addr"] == "") OR ($profile["name"] == "")) AND ($profile["gid"] != 0) AND
 		in_array($profile["network"], array(NETWORK_DFRN, NETWORK_DIASPORA, NETWORK_OSTATUS)))
 		proc_run('php',"include/update_gcontact.php", $profile["gid"]);
diff --git a/include/bbcode.php b/include/bbcode.php
index 359a7ba2f0..46e34a1fd5 100644
--- a/include/bbcode.php
+++ b/include/bbcode.php
@@ -428,8 +428,8 @@ function bb_ShareAttributes($share, $simplehtml) {
 	if (isset($data["name"]))
 		$author = $data["name"];
 
-	if (isset($data["thumb"]))
-		$avatar = $data["thumb"];
+	if (isset($data["micro"]))
+		$avatar = $data["micro"];
 
 	$preshare = trim($share[1]);
 
diff --git a/include/diaspora.php b/include/diaspora.php
index 84aed81471..a82346db67 100644
--- a/include/diaspora.php
+++ b/include/diaspora.php
@@ -1809,6 +1809,8 @@ class diaspora {
 		if(intval($def_gid))
 			group_add_member($importer["uid"], "", $contact_record["id"], $def_gid);
 
+		update_contact_avatar($ret["photo"], $importer['uid'], $contact_record["id"], true);
+
 		if($importer["page-flags"] == PAGE_NORMAL) {
 
 			$hash = random_string().(string)time();   // Generate a confirm_key
diff --git a/include/items.php b/include/items.php
index f5e568306e..1d2a775c49 100644
--- a/include/items.php
+++ b/include/items.php
@@ -1404,16 +1404,8 @@ function new_follower($importer,$contact,$datarray,$item,$sharing = false) {
 				dbesc($url)
 		);
 		if(count($r)) {
-				$contact_record = $r[0];
-
-				$photos = import_profile_photo($photo,$importer["uid"],$contact_record["id"]);
-
-				q("UPDATE `contact` SET `photo` = '%s', `thumb` = '%s', `micro` = '%s' WHERE `id` = %d",
-					dbesc($photos[0]),
-					dbesc($photos[1]),
-					dbesc($photos[2]),
-					intval($contact_record["id"])
-				);
+			$contact_record = $r[0];
+			update_contact_avatar($photo, $importer["uid"], $contact_record["id"], true);
 		}
 
 
diff --git a/include/notifier.php b/include/notifier.php
index 6c90629bd8..7019ffccef 100644
--- a/include/notifier.php
+++ b/include/notifier.php
@@ -388,17 +388,30 @@ function notifier_run(&$argv, &$argc){
 		// We have not only to look at the parent, since it could be a Friendica thread.
 		if (($thr_parent AND ($thr_parent[0]['network'] == NETWORK_OSTATUS)) OR ($parent['network'] == NETWORK_OSTATUS)) {
 
-			logger('Some parent is OStatus for '.$target_item["guid"], LOGGER_DEBUG);
+			logger('Some parent is OStatus for '.$target_item["guid"]." - Author: ".$thr_parent[0]['author-link']." - Owner: ".$thr_parent[0]['owner-link'], LOGGER_DEBUG);
 
 			// Send a salmon to the parent author
-			$probed_contact = probe_url($thr_parent[0]['author-link']);
+			$r = q("SELECT `notify` FROM `contact` WHERE `nurl`='%s' AND `uid` IN (0, %d) AND `notify` != ''",
+				dbesc(normalise_link($thr_parent[0]['author-link'])),
+				intval($uid));
+			if ($r)
+				$probed_contact = $r[0];
+			else
+				$probed_contact = probe_url($thr_parent[0]['author-link']);
+
 			if ($probed_contact["notify"] != "") {
 				logger('Notify parent author '.$probed_contact["url"].': '.$probed_contact["notify"]);
 				$url_recipients[$probed_contact["notify"]] = $probed_contact["notify"];
 			}
 
 			// Send a salmon to the parent owner
-			$probed_contact = probe_url($thr_parent[0]['owner-link']);
+			$r = q("SELECT `notify` FROM `contact` WHERE `nurl`='%s' AND `uid` IN (0, %d) AND `notify` != ''",
+				dbesc(normalise_link($thr_parent[0]['owner-link'])),
+				intval($uid));
+			if ($r)
+				$probed_contact = $r[0];
+			else
+				$probed_contact = probe_url($thr_parent[0]['owner-link']);
 			if ($probed_contact["notify"] != "") {
 				logger('Notify parent owner '.$probed_contact["url"].': '.$probed_contact["notify"]);
 				$url_recipients[$probed_contact["notify"]] = $probed_contact["notify"];
diff --git a/mod/dfrn_request.php b/mod/dfrn_request.php
index 32e835b715..2a9f68eabd 100644
--- a/mod/dfrn_request.php
+++ b/mod/dfrn_request.php
@@ -138,6 +138,8 @@ function dfrn_request_post(&$a) {
 
 					$dfrn_request = $parms['dfrn-request'];
 
+					$photo = $parms["photo"];
+
 					/********* Escape the entire array ********/
 
 					dbesc_array($parms);
@@ -185,6 +187,9 @@ function dfrn_request_post(&$a) {
 					if(intval($def_gid))
 						group_add_member(local_user(), '', $r[0]['id'], $def_gid);
 
+					if (isset($photo))
+						update_contact_avatar($photo, local_user(), $r[0]["id"], true);
+
 					$forwardurl = $a->get_baseurl()."/contacts/".$r[0]['id'];
 				} else
 					$forwardurl = $a->get_baseurl()."/contacts";
@@ -530,7 +535,7 @@ function dfrn_request_post(&$a) {
 
 				$parms['url'] = $url;
 				$parms['issued-id'] = $issued_id;
-
+				$photo = $parms["photo"];
 
 				dbesc_array($parms);
 				$r = q("INSERT INTO `contact` ( `uid`, `created`, `url`, `nurl`, `addr`, `name`, `nick`, `issued-id`, `photo`, `site-pubkey`,
@@ -539,7 +544,7 @@ function dfrn_request_post(&$a) {
 					intval($uid),
 					dbesc(datetime_convert()),
 					$parms['url'],
-					dbesc(normalise_link($parms['url'])),
+					dbesc(normalise_link($url)),
 					$parms['addr'],
 					$parms['fn'],
 					$parms['nick'],
@@ -562,8 +567,10 @@ function dfrn_request_post(&$a) {
 						$parms['url'],
 						$parms['issued-id']
 					);
-					if(count($r))
+					if(count($r)) {
 						$contact_record = $r[0];
+						update_contact_avatar($photo, $uid, $contact_record["id"], true);
+					}
 				}
 
 			}
diff --git a/mod/notifications.php b/mod/notifications.php
index f6c4e8f51f..e0f07118f7 100644
--- a/mod/notifications.php
+++ b/mod/notifications.php
@@ -138,6 +138,8 @@ function notifications_content(&$a) {
 			$a->set_pager_itemspage(20);
 		}
 
+		/// @todo Fetch contact details by "get_contact_details_by_url" instead of queries to contact, fcontact and gcontact
+
 		$r = q("SELECT `intro`.`id` AS `intro_id`, `intro`.*, `contact`.*, `fcontact`.`name` AS `fname`,`fcontact`.`url` AS `furl`,`fcontact`.`photo` AS `fphoto`,`fcontact`.`request` AS `frequest`,
 				`gcontact`.`location` AS `glocation`, `gcontact`.`about` AS `gabout`,
 				`gcontact`.`keywords` AS `gkeywords`, `gcontact`.`gender` AS `ggender`,
diff --git a/mod/ping.php b/mod/ping.php
index c12da56131..0ed2c31c35 100644
--- a/mod/ping.php
+++ b/mod/ping.php
@@ -191,8 +191,8 @@ function ping_init(&$a) {
 		function xmlize($n){
 
 			$contact = get_contact_details_by_url($n['url']);
-			if (isset($contact["thumb"]))
-				$n['photo'] = proxy_url($contact["thumb"], false, PROXY_SIZE_MICRO);
+			if (isset($contact["micro"]))
+				$n['photo'] = proxy_url($contact["micro"], false, PROXY_SIZE_MICRO);
 			else
 				$n['photo'] = proxy_url($n['photo'], false, PROXY_SIZE_MICRO);
 
-- 
2.39.5