From 9b046d2a8481c50e244071be1192830e0bbd08bc Mon Sep 17 00:00:00 2001
From: Henry Jameson <me@hjkos.com>
Date: Wed, 22 Aug 2018 16:10:59 +0300
Subject: [PATCH 1/2] fixed notifications API completely breaking if there's a
 like for missing (deleted) post.

---
 lib/pleroma/web/twitter_api/views/activity_view.ex | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/lib/pleroma/web/twitter_api/views/activity_view.ex b/lib/pleroma/web/twitter_api/views/activity_view.ex
index 55b5287f5..0efc0df2e 100644
--- a/lib/pleroma/web/twitter_api/views/activity_view.ex
+++ b/lib/pleroma/web/twitter_api/views/activity_view.ex
@@ -188,7 +188,7 @@ def render("activity.json", %{activity: %{data: %{"type" => "Like"}} = activity}
 
     text = "#{user.nickname} favorited a status."
 
-    %{
+    if liked_activity, do: %{
       "id" => activity.id,
       "user" => UserView.render("show.json", %{user: user, for: opts[:for]}),
       "statusnet_html" => text,
@@ -200,7 +200,7 @@ def render("activity.json", %{activity: %{data: %{"type" => "Like"}} = activity}
       "in_reply_to_status_id" => liked_activity.id,
       "external_url" => activity.data["id"],
       "activity_type" => "like"
-    }
+    }, else: %{}
   end
 
   def render(

From 0f1c629d657f569058c36fb0f0c7855a261d5257 Mon Sep 17 00:00:00 2001
From: Henry Jameson <me@hjkos.com>
Date: Mon, 27 Aug 2018 17:07:26 +0300
Subject: [PATCH 2/2] better solution, added test.

---
 .../web/twitter_api/views/activity_view.ex    |  7 ++---
 .../twitter_api/views/activity_view_test.exs  | 27 +++++++++++++++++++
 2 files changed, 31 insertions(+), 3 deletions(-)

diff --git a/lib/pleroma/web/twitter_api/views/activity_view.ex b/lib/pleroma/web/twitter_api/views/activity_view.ex
index 0efc0df2e..909eefdd8 100644
--- a/lib/pleroma/web/twitter_api/views/activity_view.ex
+++ b/lib/pleroma/web/twitter_api/views/activity_view.ex
@@ -181,6 +181,7 @@ def render("activity.json", %{activity: %{data: %{"type" => "Announce"}} = activ
   def render("activity.json", %{activity: %{data: %{"type" => "Like"}} = activity} = opts) do
     user = get_user(activity.data["actor"], opts)
     liked_activity = Activity.get_create_activity_by_object_ap_id(activity.data["object"])
+    liked_activity_id = if liked_activity, do: liked_activity.id, else: nil
 
     created_at =
       activity.data["published"]
@@ -188,7 +189,7 @@ def render("activity.json", %{activity: %{data: %{"type" => "Like"}} = activity}
 
     text = "#{user.nickname} favorited a status."
 
-    if liked_activity, do: %{
+    %{
       "id" => activity.id,
       "user" => UserView.render("show.json", %{user: user, for: opts[:for]}),
       "statusnet_html" => text,
@@ -197,10 +198,10 @@ def render("activity.json", %{activity: %{data: %{"type" => "Like"}} = activity}
       "is_post_verb" => false,
       "uri" => "tag:#{activity.data["id"]}:objectType=Favourite",
       "created_at" => created_at,
-      "in_reply_to_status_id" => liked_activity.id,
+      "in_reply_to_status_id" => liked_activity_id,
       "external_url" => activity.data["id"],
       "activity_type" => "like"
-    }, else: %{}
+    }
   end
 
   def render(
diff --git a/test/web/twitter_api/views/activity_view_test.exs b/test/web/twitter_api/views/activity_view_test.exs
index a101e4ae8..b9a8efdad 100644
--- a/test/web/twitter_api/views/activity_view_test.exs
+++ b/test/web/twitter_api/views/activity_view_test.exs
@@ -126,6 +126,33 @@ test "a like activity" do
     assert result == expected
   end
 
+  test "a like activity for deleted post" do
+    user = insert(:user)
+    other_user = insert(:user, %{nickname: "shp"})
+
+    {:ok, activity} = CommonAPI.post(user, %{"status" => "Hey @shp!"})
+    {:ok, like, _object} = CommonAPI.favorite(activity.id, other_user)
+    CommonAPI.delete(activity.id, user)
+
+    result = ActivityView.render("activity.json", activity: like)
+
+    expected = %{
+      "activity_type" => "like",
+      "created_at" => like.data["published"] |> Utils.date_to_asctime(),
+      "external_url" => like.data["id"],
+      "id" => like.id,
+      "in_reply_to_status_id" => nil,
+      "is_local" => true,
+      "is_post_verb" => false,
+      "statusnet_html" => "shp favorited a status.",
+      "text" => "shp favorited a status.",
+      "uri" => "tag:#{like.data["id"]}:objectType=Favourite",
+      "user" => UserView.render("show.json", user: other_user)
+    }
+
+    assert result == expected
+  end
+
   test "an announce activity" do
     user = insert(:user)
     other_user = insert(:user, %{nickname: "shp"})