TwApi ActivityView: Add Like rendering.
authorlain <lain@soykaf.club>
Fri, 30 Mar 2018 11:49:09 +0000 (13:49 +0200)
committerlain <lain@soykaf.club>
Fri, 30 Mar 2018 11:49:09 +0000 (13:49 +0200)
lib/pleroma/web/twitter_api/views/activity_view.ex
test/web/twitter_api/views/activity_view_test.exs

index ae5be60ce9e32eeea048d81bbb752ea9538757ad..520f0aa369d99042bb07aeb43222a48d4c9c42dd 100644 (file)
@@ -5,8 +5,32 @@ defmodule Pleroma.Web.TwitterAPI.ActivityView do
   alias Pleroma.Web.TwitterAPI.UserView
   alias Pleroma.Web.TwitterAPI.TwitterAPI
   alias Pleroma.Web.TwitterAPI.Representers.ObjectRepresenter
+  alias Pleroma.Activity
   alias Pleroma.Formatter
 
+  def render("activity.json", %{activity: %{data: %{"type" => "Like"}} = activity} = opts) do
+    user = User.get_cached_by_ap_id(activity.data["actor"])
+    liked_activity = Activity.get_create_activity_by_object_ap_id(activity.data["object"])
+    created_at = activity.data["published"]
+    |> Utils.date_to_asctime
+
+    text = "#{user.nickname} favorited a status."
+
+    %{
+      "id" => activity.id,
+      "user" => UserView.render("show.json", %{user: user, for: opts[:for]}),
+      "statusnet_html" => text,
+      "text" => text,
+      "is_local" => activity.local,
+      "is_post_verb" => false,
+      "uri" => "tag:#{activity.data["id"]}:objectType=Favourite",
+      "created_at" => created_at,
+      "in_reply_to_status_id" => liked_activity.id,
+      "external_url" => activity.data["id"],
+      "activity_type" => "like"
+    }
+  end
+
   def render("activity.json", %{activity: %{data: %{"type" => "Create", "object" => object}} = activity} = opts) do
     actor = get_in(activity.data, ["actor"])
     user = User.get_cached_by_ap_id(actor)
index 76ce10724c8223f591ce64b37efdcff11d80f904..244ffd5c4b2c9db1f5d23266c09d9685eea43140 100644 (file)
@@ -24,7 +24,7 @@ defmodule Pleroma.Web.TwitterAPI.ActivityViewTest do
       "attentions" => [
         UserView.render("show.json", %{user: other_user})
       ],
-      "created_at" => activity.data["object"]["published"] |> Utils.date_to_asctime,
+      "created_at" => activity.data["object"]["published"] |> Utils.date_to_asctime(),
       "external_url" => activity.data["object"]["id"],
       "fave_num" => 0,
       "favorited" => false,
@@ -37,7 +37,7 @@ defmodule Pleroma.Web.TwitterAPI.ActivityViewTest do
       "repeated" => false,
       "statusnet_conversation_id" => convo_id,
       "statusnet_html" =>
-        "Hey <span><a href=\"http://localhost:4001/users/nick1\">@<span>shp</span></a></span>!",
+        "Hey <span><a href=\"#{other_user.ap_id}\">@<span>shp</span></a></span>!",
       "tags" => [],
       "text" => "Hey @shp!",
       "uri" => activity.data["object"]["id"],
@@ -46,4 +46,44 @@ defmodule Pleroma.Web.TwitterAPI.ActivityViewTest do
 
     assert result == expected
   end
+
+  test "an activity that is a reply" do
+    user = insert(:user)
+    other_user = insert(:user, %{nickname: "shp"})
+
+    {:ok, activity} = CommonAPI.post(user, %{"status" => "Hey @shp!"})
+
+    {:ok, answer} =
+      CommonAPI.post(other_user, %{"status" => "Hi!", "in_reply_to_status_id" => activity.id})
+
+    result = ActivityView.render("activity.json", %{activity: answer})
+
+    assert result["in_reply_to_status_id"] == activity.id
+  end
+
+  test "a like activity" 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)
+
+    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" => activity.id,
+      "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
 end