TwApi ActivityView: Add announces.
authorlain <lain@soykaf.club>
Fri, 30 Mar 2018 12:46:36 +0000 (14:46 +0200)
committerlain <lain@soykaf.club>
Fri, 30 Mar 2018 12:46:36 +0000 (14:46 +0200)
lib/pleroma/web/twitter_api/views/activity_view.ex
test/web/twitter_api/views/activity_view_test.exs

index 520f0aa369d99042bb07aeb43222a48d4c9c42dd..50d536d0ea2d56a75ca70457175b73d3934424e3 100644 (file)
@@ -8,6 +8,32 @@ defmodule Pleroma.Web.TwitterAPI.ActivityView do
   alias Pleroma.Activity
   alias Pleroma.Formatter
 
+  def render("activity.json", %{activity: %{data: %{"type" => "Announce"}} = activity} = opts) do
+    user = User.get_by_ap_id(activity.data["actor"])
+    created_at = activity.data["published"] |> Utils.date_to_asctime
+    announced_activity = Activity.get_create_activity_by_object_ap_id(activity.data["object"])
+
+    text = "#{user.nickname} retweeted a status."
+
+    # retweeted_status = to_map(announced_activity, Map.merge(%{user: announced_user}, opts))
+    retweeted_status = render("activity.json", Map.merge(opts, %{activity: announced_activity}))
+
+    %{
+      "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=note",
+      "created_at" => created_at,
+      "retweeted_status" => retweeted_status,
+      "statusnet_conversation_id" => conversation_id(announced_activity),
+      "external_url" => activity.data["id"],
+      "activity_type" => "repeat"
+    }
+  end
+
   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"])
index 244ffd5c4b2c9db1f5d23266c09d9685eea43140..7615454daf4c5ff60a96b0f33e8bbd0ff62a9576 100644 (file)
@@ -6,6 +6,8 @@ defmodule Pleroma.Web.TwitterAPI.ActivityViewTest do
   alias Pleroma.Web.TwitterAPI.ActivityView
   alias Pleroma.Web.TwitterAPI.UserView
   alias Pleroma.Web.TwitterAPI.TwitterAPI
+  alias Pleroma.Repo
+  alias Pleroma.Activity
   import Pleroma.Factory
 
   test "a create activity with a note" do
@@ -86,4 +88,35 @@ defmodule Pleroma.Web.TwitterAPI.ActivityViewTest do
 
     assert result == expected
   end
+
+  test "an announce activity" do
+    user = insert(:user)
+    other_user = insert(:user, %{nickname: "shp"})
+
+    {:ok, activity} = CommonAPI.post(user, %{"status" => "Hey @shp!"})
+    {:ok, announce, _object} = CommonAPI.repeat(activity.id, other_user)
+
+    convo_id = TwitterAPI.context_to_conversation_id(activity.data["object"]["context"])
+
+    activity = Repo.get(Activity, activity.id)
+
+    result = ActivityView.render("activity.json", activity: announce)
+
+    expected = %{
+      "activity_type" => "repeat",
+      "created_at" => announce.data["published"] |> Utils.date_to_asctime(),
+      "external_url" => announce.data["id"],
+      "id" => announce.id,
+      "is_local" => true,
+      "is_post_verb" => false,
+      "statusnet_html" => "shp retweeted a status.",
+      "text" => "shp retweeted a status.",
+      "uri" => "tag:#{announce.data["id"]}:objectType=note",
+      "user" => UserView.render("show.json", user: other_user),
+      "retweeted_status" => ActivityView.render("activity.json", activity: activity),
+      "statusnet_conversation_id" => convo_id
+    }
+
+    assert result == expected
+  end
 end