EmojiReactions: Add Mastodon-aligned reaction endpoints, change response
authorLain Soykaf <lain@soykaf.club>
Fri, 7 Feb 2020 13:52:13 +0000 (14:52 +0100)
committerLain Soykaf <lain@soykaf.club>
Fri, 7 Feb 2020 13:52:13 +0000 (14:52 +0100)
lib/pleroma/web/mastodon_api/views/status_view.ex
lib/pleroma/web/pleroma_api/controllers/pleroma_api_controller.ex
lib/pleroma/web/router.ex
test/web/mastodon_api/views/status_view_test.exs
test/web/pleroma_api/controllers/pleroma_api_controller_test.exs

index 6cb158bbfedaec737e1ede58975a053dd4b116fb..e1e92034f04b00f985c8784a095a66012aae4826 100644 (file)
@@ -242,9 +242,9 @@ defmodule Pleroma.Web.MastodonAPI.StatusView do
       with %{data: %{"reactions" => emoji_reactions}} <- object do
         Enum.map(emoji_reactions, fn [emoji, users] ->
           %{
-            emoji: emoji,
+            name: emoji,
             count: length(users),
-            reacted: !!(opts[:for] && opts[:for].ap_id in users)
+            me: !!(opts[:for] && opts[:for].ap_id in users)
           }
         end)
       else
index d76e39795a84d706b066dd8bac2f2924e05970c6..108e48438ca139ac62ac543bc6b0674ce87af029 100644 (file)
@@ -53,10 +53,10 @@ defmodule Pleroma.Web.PleromaAPI.PleromaAPIController do
             |> Enum.filter(& &1)
 
           %{
-            emoji: emoji,
+            name: emoji,
             count: length(users),
             accounts: AccountView.render("index.json", %{users: users, for: user, as: :user}),
-            reacted: !!(user && user.ap_id in user_ap_ids)
+            me: !!(user && user.ap_id in user_ap_ids)
           }
         end)
 
index e86bc3cc3c82882132ff4f0dc47399033493672c..c1680092787332137cf9909ba4f6750d090d9bb7 100644 (file)
@@ -272,6 +272,7 @@ defmodule Pleroma.Web.Router do
     pipe_through(:api)
 
     get("/statuses/:id/emoji_reactions_by", PleromaAPIController, :emoji_reactions_by)
+    get("/statuses/:id/reactions", PleromaAPIController, :emoji_reactions_by)
   end
 
   scope "/api/v1/pleroma", Pleroma.Web.PleromaAPI do
@@ -289,6 +290,8 @@ defmodule Pleroma.Web.Router do
       patch("/conversations/:id", PleromaAPIController, :update_conversation)
       post("/statuses/:id/react_with_emoji", PleromaAPIController, :react_with_emoji)
       post("/statuses/:id/unreact_with_emoji", PleromaAPIController, :unreact_with_emoji)
+      put("/statuses/:id/reactions/:emoji", PleromaAPIController, :react_with_emoji)
+      delete("/statuses/:id/reactions/:emoji", PleromaAPIController, :unreact_with_emoji)
       post("/notifications/read", PleromaAPIController, :read_notification)
 
       patch("/accounts/update_avatar", AccountController, :update_avatar)
index fc110417cd35d6d52e15caa75df1c1ef2a9f7b60..ba58e48e88d37a13ecb46a6524050792f166b2d3 100644 (file)
@@ -37,15 +37,15 @@ defmodule Pleroma.Web.MastodonAPI.StatusViewTest do
     status = StatusView.render("show.json", activity: activity)
 
     assert status[:pleroma][:emoji_reactions] == [
-             %{emoji: "☕", count: 2, reacted: false},
-             %{emoji: "🍵", count: 1, reacted: false}
+             %{name: "☕", count: 2, me: false},
+             %{name: "🍵", count: 1, me: false}
            ]
 
     status = StatusView.render("show.json", activity: activity, for: user)
 
     assert status[:pleroma][:emoji_reactions] == [
-             %{emoji: "☕", count: 2, reacted: true},
-             %{emoji: "🍵", count: 1, reacted: false}
+             %{name: "☕", count: 2, me: true},
+             %{name: "🍵", count: 1, me: false}
            ]
   end
 
index be5007de52b00acbaa8a256b9a84693ec207a2fc..c9da9037796e6831fd2b42dd73c17a07e1564c8d 100644 (file)
@@ -31,7 +31,29 @@ defmodule Pleroma.Web.PleromaAPI.PleromaAPIControllerTest do
     assert to_string(activity.id) == id
 
     assert result["pleroma"]["emoji_reactions"] == [
-             %{"emoji" => "☕", "count" => 1, "reacted" => true}
+             %{"name" => "☕", "count" => 1, "me" => true}
+           ]
+  end
+
+  test "PUT /api/v1/pleroma/statuses/:id/reactions/:emoji", %{conn: conn} do
+    user = insert(:user)
+    other_user = insert(:user)
+
+    {:ok, activity} = CommonAPI.post(user, %{"status" => "#cofe"})
+
+    result =
+      conn
+      |> assign(:user, other_user)
+      |> assign(:token, insert(:oauth_token, user: other_user, scopes: ["write:statuses"]))
+      |> put("/api/v1/pleroma/statuses/#{activity.id}/reactions/☕")
+      |> json_response(200)
+
+    # We return the status, but this our implementation detail.
+    assert %{"id" => id} = result
+    assert to_string(activity.id) == id
+
+    assert result["pleroma"]["emoji_reactions"] == [
+             %{"name" => "☕", "count" => 1, "me" => true}
            ]
   end
 
@@ -56,6 +78,27 @@ defmodule Pleroma.Web.PleromaAPI.PleromaAPIControllerTest do
     assert object.data["reaction_count"] == 0
   end
 
+  test "DELETE /api/v1/pleroma/statuses/:id/reactions/:emoji", %{conn: conn} do
+    user = insert(:user)
+    other_user = insert(:user)
+
+    {:ok, activity} = CommonAPI.post(user, %{"status" => "#cofe"})
+    {:ok, activity, _object} = CommonAPI.react_with_emoji(activity.id, other_user, "☕")
+
+    result =
+      conn
+      |> assign(:user, other_user)
+      |> assign(:token, insert(:oauth_token, user: other_user, scopes: ["write:statuses"]))
+      |> delete("/api/v1/pleroma/statuses/#{activity.id}/reactions/☕")
+
+    assert %{"id" => id} = json_response(result, 200)
+    assert to_string(activity.id) == id
+
+    object = Object.normalize(activity)
+
+    assert object.data["reaction_count"] == 0
+  end
+
   test "GET /api/v1/pleroma/statuses/:id/emoji_reactions_by", %{conn: conn} do
     user = insert(:user)
     other_user = insert(:user)
@@ -80,8 +123,7 @@ defmodule Pleroma.Web.PleromaAPI.PleromaAPIControllerTest do
       |> get("/api/v1/pleroma/statuses/#{activity.id}/emoji_reactions_by")
       |> json_response(200)
 
-    [%{"emoji" => "🎅", "count" => 1, "accounts" => [represented_user], "reacted" => false}] =
-      result
+    [%{"name" => "🎅", "count" => 1, "accounts" => [represented_user], "me" => false}] = result
 
     assert represented_user["id"] == other_user.id
 
@@ -92,7 +134,46 @@ defmodule Pleroma.Web.PleromaAPI.PleromaAPIControllerTest do
       |> get("/api/v1/pleroma/statuses/#{activity.id}/emoji_reactions_by")
       |> json_response(200)
 
-    assert [%{"emoji" => "🎅", "count" => 1, "accounts" => [_represented_user], "reacted" => true}] =
+    assert [%{"name" => "🎅", "count" => 1, "accounts" => [_represented_user], "me" => true}] =
+             result
+  end
+
+  test "GET /api/v1/pleroma/statuses/:id/reactions", %{conn: conn} do
+    user = insert(:user)
+    other_user = insert(:user)
+    doomed_user = insert(:user)
+
+    {:ok, activity} = CommonAPI.post(user, %{"status" => "#cofe"})
+
+    result =
+      conn
+      |> get("/api/v1/pleroma/statuses/#{activity.id}/reactions")
+      |> json_response(200)
+
+    assert result == []
+
+    {:ok, _, _} = CommonAPI.react_with_emoji(activity.id, other_user, "🎅")
+    {:ok, _, _} = CommonAPI.react_with_emoji(activity.id, doomed_user, "🎅")
+
+    User.perform(:delete, doomed_user)
+
+    result =
+      conn
+      |> get("/api/v1/pleroma/statuses/#{activity.id}/reactions")
+      |> json_response(200)
+
+    [%{"name" => "🎅", "count" => 1, "accounts" => [represented_user], "me" => false}] = result
+
+    assert represented_user["id"] == other_user.id
+
+    result =
+      conn
+      |> assign(:user, other_user)
+      |> assign(:token, insert(:oauth_token, user: other_user, scopes: ["read:statuses"]))
+      |> get("/api/v1/pleroma/statuses/#{activity.id}/reactions")
+      |> json_response(200)
+
+    assert [%{"name" => "🎅", "count" => 1, "accounts" => [_represented_user], "me" => true}] =
              result
   end