Add favoriting to twitter api.
authorRoger Braun <roger@rogerbraun.net>
Thu, 13 Apr 2017 14:19:07 +0000 (16:19 +0200)
committerRoger Braun <roger@rogerbraun.net>
Thu, 13 Apr 2017 14:19:07 +0000 (16:19 +0200)
lib/pleroma/web/twitter_api/representers/activity_representer.ex
lib/pleroma/web/twitter_api/twitter_api.ex
test/web/twitter_api/representers/activity_representer_test.exs
test/web/twitter_api/twitter_api_test.exs

index 9e4ffaefe618511dde0fd38c59a7d395d2144e9b..6a5304049e46186bc27a18e6686e9b8c94ce99dc 100644 (file)
@@ -25,6 +25,7 @@ defmodule Pleroma.Web.TwitterAPI.Representers.ActivityRepresenter do
     content = get_in(activity.data, ["object", "content"])
     created_at = get_in(activity.data, ["object", "published"])
     |> date_to_asctime
+    like_count = get_in(activity.data, ["object", "like_count"]) || 0
 
     mentions = opts[:mentioned] || []
 
@@ -45,14 +46,15 @@ defmodule Pleroma.Web.TwitterAPI.Representers.ActivityRepresenter do
       "in_reply_to_status_id" => activity.data["object"]["inReplyToStatusId"],
       "statusnet_conversation_id" => activity.data["object"]["statusnetConversationId"],
       "attachments" => (activity.data["object"]["attachment"] || []) |> ObjectRepresenter.enum_to_list(opts),
-      "attentions" => attentions
+      "attentions" => attentions,
+      "fave_num" => like_count
     }
   end
 
   defp date_to_asctime(date) do
     with {:ok, date, _offset} <- date |> DateTime.from_iso8601 do
       Calendar.Strftime.strftime!(date, "%a %b %d %H:%M:%S %z %Y")
-    else e ->
+    else _e ->
       ""
     end
   end
index a195301ee27df99521097312d52beadb4bca1eec..2679397d974761b821b5c5507cb34821e1e33d0e 100644 (file)
@@ -124,6 +124,19 @@ defmodule Pleroma.Web.TwitterAPI.TwitterAPI do
     end
   end
 
+  def favorite(%User{} = user, %Activity{data: %{"object" => object}} = activity) do
+    object = Object.get_by_ap_id(object["id"])
+
+    {:ok, _like_activity, object} = ActivityPub.like(user, object)
+    new_data = activity.data
+    |> Map.put("object", object.data)
+
+    status = %{activity | data: new_data}
+    |> activity_to_status(%{for: user})
+
+    {:ok, status}
+  end
+
   def upload(%Plug.Upload{} = file) do
     {:ok, object} = ActivityPub.upload(file)
 
index f1f2b4c9ce6b41df752c7f8635117c5a12a12d30..70df79a77ce431995dd3fafb73170b27c5a4dd0d 100644 (file)
@@ -45,7 +45,8 @@ defmodule Pleroma.Web.TwitterAPI.Representers.ActivityRepresenterTest do
           "statusnetConversationId" => 4711,
           "attachment" => [
             object
-          ]
+          ],
+          "like_count" => 5
         },
         "published" => date
       }
@@ -68,7 +69,8 @@ defmodule Pleroma.Web.TwitterAPI.Representers.ActivityRepresenterTest do
       ],
       "attentions" => [
         UserRepresenter.to_map(mentioned_user, %{for: follower})
-      ]
+      ],
+      "fave_num" => 5
     }
 
     assert ActivityRepresenter.to_map(activity, %{user: user, for: follower, mentioned: [mentioned_user]}) == expected_status
index a4c9bd7e72b580ec472985ef3bdc258ee5226e60..341622758f53af3b046ec0015f8b9c6706cd1088 100644 (file)
@@ -5,6 +5,8 @@ defmodule Pleroma.Web.TwitterAPI.TwitterAPITest do
   alias Pleroma.{Activity, User, Object, Repo}
   alias Pleroma.Web.TwitterAPI.Representers.ActivityRepresenter
 
+  import Pleroma.Factory
+
   test "create a status" do
     user = UserBuilder.build(%{ap_id: "142344"})
     _mentioned_user = UserBuilder.insert(%{nickname: "shp", ap_id: "shp"})
@@ -177,4 +179,15 @@ defmodule Pleroma.Web.TwitterAPI.TwitterAPITest do
 
     assert TwitterAPI.add_user_links(text, mentions) == expected_text
   end
+
+  test "it favorites a status, returns the updated status" do
+    user = insert(:user)
+    note_activity = insert(:note_activity)
+    activity_user = Repo.get_by!(User, ap_id: note_activity.data["actor"])
+
+    {:ok, status} = TwitterAPI.favorite(user, note_activity)
+    updated_activity = Activity.get_by_ap_id(note_activity.data["id"])
+
+    assert status == ActivityRepresenter.to_map(updated_activity, %{user: activity_user, for: user})
+  end
 end