Conversations: Make tests run.
authorlain <lain@soykaf.club>
Wed, 10 Apr 2019 15:48:31 +0000 (17:48 +0200)
committerlain <lain@soykaf.club>
Wed, 10 Apr 2019 15:48:31 +0000 (17:48 +0200)
lib/pleroma/web/mastodon_api/mastodon_api_controller.ex
lib/pleroma/web/router.ex
test/web/mastodon_api/mastodon_api_controller_test.exs

index 5462ce8bebd19a78d9be63dc855abdcc2998b17b..57f73dacd4078a3c1ece36185a8f3c6a4858b9e5 100644 (file)
@@ -8,6 +8,7 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIController do
   alias Ecto.Changeset
   alias Pleroma.Activity
   alias Pleroma.Config
+  alias Pleroma.Conversation.Participation
   alias Pleroma.Filter
   alias Pleroma.Notification
   alias Pleroma.Object
@@ -1584,6 +1585,41 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIController do
     end
   end
 
+  def conversations(%{assigns: %{user: user}} = conn, params) do
+    participations = Participation.for_user_with_last_activity_id(user, params)
+
+    conversations =
+      Enum.map(participations, fn participation ->
+        %{
+          id: participation.id,
+          # TODO: Add this.
+          accounts: [],
+          unread: !participation.read,
+          last_status: participation.last_activity_id
+        }
+      end)
+
+    conn
+    |> add_link_headers(:conversations, participations)
+    |> json(conversations)
+  end
+
+  def conversation_read(%{assigns: %{user: user}} = conn, %{"id" => participation_id}) do
+    with %Participation{} = participation <-
+           Repo.get_by(Participation, id: participation_id, user_id: user.id),
+         {:ok, participation} <- Participation.mark_as_read(participation) do
+      conn
+      |> json(%{
+        id: participation.id,
+        # TODO: Add this.
+        accounts: [],
+        unread: !participation.read,
+        # TODO: Add this.
+        last_status: nil
+      })
+    end
+  end
+
   def try_render(conn, target, params)
       when is_binary(target) do
     res = render(conn, target, params)
index 0af743b805dd6f42082946d9b37bbebe8d956945..dc5119c50a7b263bab7d1510061ad9f191d1f48f 100644 (file)
@@ -273,7 +273,7 @@ defmodule Pleroma.Web.Router do
       get("/suggestions", MastodonAPIController, :suggestions)
 
       get("/conversations", MastodonAPIController, :conversations)
-      get("/conversations/:id/read", MastodonAPIController, :get_conversation)
+      post("/conversations/:id/read", MastodonAPIController, :conversation_read)
 
       get("/endorsements", MastodonAPIController, :empty_array)
 
index 519ad8f4dce1f295d64b3468431746b339469548..d1d22edde5e47f3a9f5a24acf841d958e5ed42f6 100644 (file)
@@ -325,14 +325,17 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIControllerTest do
 
     assert response = json_response(res_conn, 200)
 
-    assert %{
-             "id" => res_id,
-             "accounts" => res_accounts,
-             "last_status" => res_last_status,
-             "unread" => unread
-           } = response
+    assert [
+             %{
+               "id" => res_id,
+               "accounts" => res_accounts,
+               "last_status" => res_last_status,
+               "unread" => unread
+             }
+           ] = response
 
-    assert unread == false
+    assert unread == true
+    assert res_last_status == direct.id
 
     # Apparently undocumented API endpoint
     res_conn =
@@ -340,15 +343,16 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIControllerTest do
       |> assign(:user, user_one)
       |> post("/api/v1/conversations/#{res_id}/read")
 
-    assert response == json_response(res_conn, 200)
+    assert response = json_response(res_conn, 200)
+    assert response["unread"] == false
 
     # (vanilla) Mastodon frontend behaviour
     res_conn =
       conn
       |> assign(:user, user_one)
-      |> get("/api/v1/statuses/#{res_last_status.id}/context")
+      |> get("/api/v1/statuses/#{res_last_status}/context")
 
-    assert %{ancestors: [], descendants: []} == json_response(res_conn, 200)
+    assert %{"ancestors" => [], "descendants" => []} == json_response(res_conn, 200)
   end
 
   test "doesn't include DMs from blocked users", %{conn: conn} do