From a348c2e4dd0551a603509501d718d9e0b995be90 Mon Sep 17 00:00:00 2001 From: tusooa Date: Sat, 1 Apr 2023 01:29:11 -0400 Subject: [PATCH] Use pleroma: instead of pleroma. for ws events --- .../web/mastodon_api/websocket_handler.ex | 8 ++-- lib/pleroma/web/views/streamer_view.ex | 2 +- .../integration/mastodon_websocket_test.exs | 46 ++++++++++--------- test/pleroma/web/streamer_test.exs | 14 +++++- 4 files changed, 42 insertions(+), 28 deletions(-) diff --git a/lib/pleroma/web/mastodon_api/websocket_handler.ex b/lib/pleroma/web/mastodon_api/websocket_handler.ex index 2707673ba..07c2b62e3 100644 --- a/lib/pleroma/web/mastodon_api/websocket_handler.ex +++ b/lib/pleroma/web/mastodon_api/websocket_handler.ex @@ -215,7 +215,7 @@ defp handle_client_event(%{"type" => "unsubscribe", "stream" => _topic} = params end defp handle_client_event( - %{"type" => "pleroma.authenticate", "token" => access_token} = _params, + %{"type" => "pleroma:authenticate", "token" => access_token} = _params, state ) do with {:auth, nil, nil} <- {:auth, state.user, state.oauth_token}, @@ -223,7 +223,7 @@ defp handle_client_event( {[ {:text, StreamerView.render("pleroma_respond.json", %{ - type: "pleroma.authenticate", + type: "pleroma:authenticate", result: "success" })} ], %{state | user: user, oauth_token: oauth_token}} @@ -232,7 +232,7 @@ defp handle_client_event( {[ {:text, StreamerView.render("pleroma_respond.json", %{ - type: "pleroma.authenticate", + type: "pleroma:authenticate", result: "error", error: :already_authenticated })} @@ -242,7 +242,7 @@ defp handle_client_event( {[ {:text, StreamerView.render("pleroma_respond.json", %{ - type: "pleroma.authenticate", + type: "pleroma:authenticate", result: "error", error: :unauthorized })} diff --git a/lib/pleroma/web/views/streamer_view.ex b/lib/pleroma/web/views/streamer_view.ex index f591da9a6..f97570b0a 100644 --- a/lib/pleroma/web/views/streamer_view.ex +++ b/lib/pleroma/web/views/streamer_view.ex @@ -148,7 +148,7 @@ def render("conversation.json", %Participation{} = participation, topic) do def render("pleroma_respond.json", %{type: type, result: result} = params) do %{ - event: "pleroma.respond", + event: "pleroma:respond", payload: %{ result: result, diff --git a/test/pleroma/integration/mastodon_websocket_test.exs b/test/pleroma/integration/mastodon_websocket_test.exs index d2e98df3b..21e7ca2b0 100644 --- a/test/pleroma/integration/mastodon_websocket_test.exs +++ b/test/pleroma/integration/mastodon_websocket_test.exs @@ -95,7 +95,7 @@ test "can subscribe" do assert {:ok, %{ - "event" => "pleroma.respond", + "event" => "pleroma:respond", "payload" => %{"type" => "subscribe", "result" => "success"} }} = decode_json(raw_json) @@ -124,16 +124,20 @@ test "can subscribe to multiple streams" do assert {:ok, %{ - "event" => "pleroma.respond", + "event" => "pleroma:respond", "payload" => %{"type" => "subscribe", "result" => "success"} }} = decode_json(raw_json) - WebsocketClient.send_text(pid, %{type: "subscribe", stream: "hashtag", tag: "mew"} |> Jason.encode!()) + WebsocketClient.send_text( + pid, + %{type: "subscribe", stream: "hashtag", tag: "mew"} |> Jason.encode!() + ) + assert_receive {:text, raw_json}, 1_000 assert {:ok, %{ - "event" => "pleroma.respond", + "event" => "pleroma:respond", "payload" => %{"type" => "subscribe", "result" => "success"} }} = decode_json(raw_json) @@ -157,7 +161,7 @@ test "won't double subscribe" do assert {:ok, %{ - "event" => "pleroma.respond", + "event" => "pleroma:respond", "payload" => %{"type" => "subscribe", "result" => "success"} }} = decode_json(raw_json) @@ -166,7 +170,7 @@ test "won't double subscribe" do assert {:ok, %{ - "event" => "pleroma.respond", + "event" => "pleroma:respond", "payload" => %{"type" => "subscribe", "result" => "ignored"} }} = decode_json(raw_json) @@ -184,7 +188,7 @@ test "can unsubscribe" do assert {:ok, %{ - "event" => "pleroma.respond", + "event" => "pleroma:respond", "payload" => %{"type" => "subscribe", "result" => "success"} }} = decode_json(raw_json) @@ -193,7 +197,7 @@ test "can unsubscribe" do assert {:ok, %{ - "event" => "pleroma.respond", + "event" => "pleroma:respond", "payload" => %{"type" => "unsubscribe", "result" => "success"} }} = decode_json(raw_json) @@ -262,15 +266,15 @@ test "accepts valid token on client-sent event", %{token: token} do WebsocketClient.send_text( pid, - %{type: "pleroma.authenticate", token: token.token} |> Jason.encode!() + %{type: "pleroma:authenticate", token: token.token} |> Jason.encode!() ) assert_receive {:text, raw_json}, 1_000 assert {:ok, %{ - "event" => "pleroma.respond", - "payload" => %{"type" => "pleroma.authenticate", "result" => "success"} + "event" => "pleroma:respond", + "payload" => %{"type" => "pleroma:authenticate", "result" => "success"} }} = decode_json(raw_json) WebsocketClient.send_text(pid, %{type: "subscribe", stream: "user"} |> Jason.encode!()) @@ -278,7 +282,7 @@ test "accepts valid token on client-sent event", %{token: token} do assert {:ok, %{ - "event" => "pleroma.respond", + "event" => "pleroma:respond", "payload" => %{"type" => "subscribe", "result" => "success"} }} = decode_json(raw_json) end @@ -288,16 +292,16 @@ test "rejects invalid token on client-sent event" do WebsocketClient.send_text( pid, - %{type: "pleroma.authenticate", token: "Something else"} |> Jason.encode!() + %{type: "pleroma:authenticate", token: "Something else"} |> Jason.encode!() ) assert_receive {:text, raw_json}, 1_000 assert {:ok, %{ - "event" => "pleroma.respond", + "event" => "pleroma:respond", "payload" => %{ - "type" => "pleroma.authenticate", + "type" => "pleroma:authenticate", "result" => "error", "error" => "unauthorized" } @@ -309,29 +313,29 @@ test "rejects new authenticate request if already logged-in", %{token: token} do WebsocketClient.send_text( pid, - %{type: "pleroma.authenticate", token: token.token} |> Jason.encode!() + %{type: "pleroma:authenticate", token: token.token} |> Jason.encode!() ) assert_receive {:text, raw_json}, 1_000 assert {:ok, %{ - "event" => "pleroma.respond", - "payload" => %{"type" => "pleroma.authenticate", "result" => "success"} + "event" => "pleroma:respond", + "payload" => %{"type" => "pleroma:authenticate", "result" => "success"} }} = decode_json(raw_json) WebsocketClient.send_text( pid, - %{type: "pleroma.authenticate", token: "Something else"} |> Jason.encode!() + %{type: "pleroma:authenticate", token: "Something else"} |> Jason.encode!() ) assert_receive {:text, raw_json}, 1_000 assert {:ok, %{ - "event" => "pleroma.respond", + "event" => "pleroma:respond", "payload" => %{ - "type" => "pleroma.authenticate", + "type" => "pleroma:authenticate", "result" => "error", "error" => "already_authenticated" } diff --git a/test/pleroma/web/streamer_test.exs b/test/pleroma/web/streamer_test.exs index cc57a2989..d85358fd4 100644 --- a/test/pleroma/web/streamer_test.exs +++ b/test/pleroma/web/streamer_test.exs @@ -355,7 +355,12 @@ test "it sends chat messages to the 'user:pleroma_chat' stream", %{ Streamer.get_topic_and_add_socket("user:pleroma_chat", user, oauth_token) Streamer.stream("user:pleroma_chat", {user, cm_ref}) - text = StreamerView.render("chat_update.json", %{chat_message_reference: cm_ref}, "user:pleroma_chat:#{user.id}") + text = + StreamerView.render( + "chat_update.json", + %{chat_message_reference: cm_ref}, + "user:pleroma_chat:#{user.id}" + ) assert text =~ "hey cirno" assert_receive {:text, ^text} @@ -373,7 +378,12 @@ test "it sends chat messages to the 'user' stream", %{user: user, token: oauth_t Streamer.get_topic_and_add_socket("user", user, oauth_token) Streamer.stream("user", {user, cm_ref}) - text = StreamerView.render("chat_update.json", %{chat_message_reference: cm_ref}, "user:#{user.id}") + text = + StreamerView.render( + "chat_update.json", + %{chat_message_reference: cm_ref}, + "user:#{user.id}" + ) assert text =~ "hey cirno" assert_receive {:text, ^text}