From 73f596cc46b40b6d8acc6e3d69f3bf5720fd8fa0 Mon Sep 17 00:00:00 2001
From: Michael <heluecht@pirati.ca>
Date: Sat, 27 Oct 2018 11:09:23 +0000
Subject: [PATCH] Fixes duplicated transmissions and not working undo of
 like/dislike

---
 mod/item.php                             |  3 ---
 mod/photos.php                           |  7 -------
 mod/poke.php                             |  3 ---
 src/Model/Item.php                       | 24 ++++++++++++++++--------
 src/Protocol/ActivityPub/Transmitter.php | 12 ++++++++----
 src/Protocol/Diaspora.php                | 22 +++++-----------------
 src/Worker/Notifier.php                  |  2 +-
 7 files changed, 30 insertions(+), 43 deletions(-)

diff --git a/mod/item.php b/mod/item.php
index dcccf5d75c..a4f8d7c667 100644
--- a/mod/item.php
+++ b/mod/item.php
@@ -830,9 +830,6 @@ function item_post(App $a) {
 	// We don't fork a new process since this is done anyway with the following command
 	Worker::add(['priority' => PRIORITY_HIGH, 'dont_fork' => true], "CreateShadowEntry", $post_id);
 
-	// Call the background process that is delivering the item to the receivers
-	Worker::add(PRIORITY_HIGH, "Notifier", $notify_type, $post_id);
-
 	logger('post_complete');
 
 	if ($api_source) {
diff --git a/mod/photos.php b/mod/photos.php
index b80350221b..f5b1bf9190 100644
--- a/mod/photos.php
+++ b/mod/photos.php
@@ -691,9 +691,6 @@ function photos_post(App $a)
 					$arr['target'] .= '<link>' . xmlify('<link rel="alternate" type="text/html" href="' . System::baseUrl() . '/photos/' . $owner_record['nickname'] . '/image/' . $p[0]['resource-id'] . '" />' . "\n" . '<link rel="preview" type="'.$p[0]['type'].'" href="' . System::baseUrl() . "/photo/" . $p[0]['resource-id'] . '-' . $best . '.' . $ext . '" />') . '</link></target>';
 
 					$item_id = Item::insert($arr);
-					if ($item_id) {
-						Worker::add(PRIORITY_HIGH, "Notifier", "tag", $item_id);
-					}
 				}
 			}
 		}
@@ -919,10 +916,6 @@ function photos_post(App $a)
 	// Update the photo albums cache
 	Photo::clearAlbumCache($page_owner_uid);
 
-	if ($visible) {
-		Worker::add(PRIORITY_HIGH, "Notifier", 'wall-new', $item_id);
-	}
-
 	Addon::callHooks('photo_post_end', $item_id);
 
 	// addon uploaders should call "killme()" [e.g. exit] within the photo_post_end hook
diff --git a/mod/poke.php b/mod/poke.php
index 80d476005c..3318b0fcde 100644
--- a/mod/poke.php
+++ b/mod/poke.php
@@ -128,9 +128,6 @@ function poke_init(App $a)
 	$arr['object'] .= '</link></object>' . "\n";
 
 	$item_id = Item::insert($arr);
-	if ($item_id) {
-		Worker::add(PRIORITY_HIGH, "Notifier", "tag", $item_id);
-	}
 
 	Addon::callHooks('post_local_end', $arr);
 
diff --git a/src/Model/Item.php b/src/Model/Item.php
index e7358a776e..298a8be001 100644
--- a/src/Model/Item.php
+++ b/src/Model/Item.php
@@ -1827,9 +1827,17 @@ class Item extends BaseObject
 		check_user_notification($current_post);
 
 		if ($notify) {
-			Worker::add(['priority' => $priority, 'dont_fork' => true], "Notifier", $notify_type, $current_post);
-		} elseif (!empty($parent) && $parent['origin']) {
-			Worker::add(['priority' => PRIORITY_HIGH, 'dont_fork' => true], "Notifier", "comment-import", $current_post);
+			Worker::add(['priority' => $priority, 'dont_fork' => true], 'Notifier', $notify_type, $current_post);
+		} elseif ($item['visible'] && ((!empty($parent) && $parent['origin']) || $item['origin'])) {
+			if ($item['gravity'] == GRAVITY_ACTIVITY) {
+				$cmd = $item['origin'] ? 'activity-new' : 'activity-import';
+			} elseif ($item['gravity'] == GRAVITY_COMMENT) {
+				$cmd = $item['origin'] ? 'comment-new' : 'comment-import';
+			} else {
+				$cmd = 'wall-new';
+			}
+
+			Worker::add(['priority' => PRIORITY_HIGH, 'dont_fork' => true], 'Notifier', $cmd, $current_post);
 		}
 
 		return $current_post;
@@ -3103,6 +3111,11 @@ class Item extends BaseObject
 			'unseen'        => 1,
 		];
 
+		$signed = Diaspora::createLikeSignature($item_contact, $new_item);
+		if (!empty($signed)) {
+			$new_item['diaspora_signed_text'] = $signed;
+		}
+
 		$new_item_id = self::insert($new_item);
 
 		// If the parent item isn't visible then set it to visible
@@ -3110,15 +3123,10 @@ class Item extends BaseObject
 			self::update(['visible' => true], ['id' => $item['id']]);
 		}
 
-		// Save the author information for the like in case we need to relay to Diaspora
-		Diaspora::storeLikeSignature($item_contact, $new_item_id);
-
 		$new_item['id'] = $new_item_id;
 
 		Addon::callHooks('post_local_end', $new_item);
 
-		Worker::add(PRIORITY_HIGH, "Notifier", "like", $new_item_id);
-
 		return true;
 	}
 
diff --git a/src/Protocol/ActivityPub/Transmitter.php b/src/Protocol/ActivityPub/Transmitter.php
index bfc26cd9f1..8f01a53611 100644
--- a/src/Protocol/ActivityPub/Transmitter.php
+++ b/src/Protocol/ActivityPub/Transmitter.php
@@ -549,15 +549,19 @@ class Transmitter
 	 * Creates the activity or fetches it from the cache
 	 *
 	 * @param integer $item_id
+	 * @param boolean $force   Force new cache entry
 	 *
 	 * @return array with the activity
 	 */
-	public static function createCachedActivityFromItem($item_id)
+	public static function createCachedActivityFromItem($item_id, $force = false)
 	{
 		$cachekey = 'APDelivery:createActivity:' . $item_id;
-		$data = Cache::get($cachekey);
-		if (!is_null($data)) {
-			return $data;
+
+		if (!$force) {
+			$data = Cache::get($cachekey);
+			if (!is_null($data)) {
+				return $data;
+			}
 		}
 
 		$data = ActivityPub\Transmitter::createActivityFromItem($item_id);
diff --git a/src/Protocol/Diaspora.php b/src/Protocol/Diaspora.php
index 1a8054aad5..f04eaa2a40 100644
--- a/src/Protocol/Diaspora.php
+++ b/src/Protocol/Diaspora.php
@@ -4113,14 +4113,14 @@ class Diaspora
 	}
 
 	/**
-	 * @brief Stores the signature for likes that are created on our system
+	 * @brief Creates the signature for likes that are created on our system
 	 *
 	 * @param array $contact The contact array of the "like"
-	 * @param int   $post_id The post id of the "like"
+	 * @param array $item Item array
 	 *
-	 * @return bool Success
+	 * @return array Signed content
 	 */
-	public static function storeLikeSignature(array $contact, $post_id)
+	public static function createLikeSignature(array $contact, array $item)
 	{
 		// Is the contact the owner? Then fetch the private key
 		if (!$contact['self'] || ($contact['uid'] == 0)) {
@@ -4135,11 +4135,6 @@ class Diaspora
 
 		$contact["uprvkey"] = $user['prvkey'];
 
-		$item = Item::selectFirst([], ['id' => $post_id]);
-		if (!DBA::isResult($item)) {
-			return false;
-		}
-
 		if (!in_array($item["verb"], [ACTIVITY_LIKE, ACTIVITY_DISLIKE])) {
 			return false;
 		}
@@ -4151,14 +4146,7 @@ class Diaspora
 
 		$message["author_signature"] = self::signature($contact, $message);
 
-		/*
-		 * Now store the signature more flexible to dynamically support new fields.
-		 * This will break Diaspora compatibility with Friendica versions prior to 3.5.
-		 */
-		DBA::insert('sign', ['iid' => $post_id, 'signed_text' => json_encode($message)]);
-
-		logger('Stored diaspora like signature');
-		return true;
+		return $message;
 	}
 
 	/**
diff --git a/src/Worker/Notifier.php b/src/Worker/Notifier.php
index 31000a26f2..480938ec53 100644
--- a/src/Worker/Notifier.php
+++ b/src/Worker/Notifier.php
@@ -539,7 +539,7 @@ class Notifier
 		}
 
 		// Fill the item cache
-		ActivityPub\Transmitter::createCachedActivityFromItem($item_id);
+		ActivityPub\Transmitter::createCachedActivityFromItem($item_id, true);
 
 		foreach ($inboxes as $inbox) {
 			logger('Deliver ' . $item_id .' to ' . $inbox .' via ActivityPub', LOGGER_DEBUG);
-- 
2.39.5