Email-like field in /api/v1/accounts/verify_credentials response (for OAuth plugins...
authorIvan Tashkinov <ivantashkinov@gmail.com>
Thu, 28 Jan 2021 16:49:43 +0000 (19:49 +0300)
committerIvan Tashkinov <ivantashkinov@gmail.com>
Thu, 28 Jan 2021 16:49:43 +0000 (19:49 +0300)
Addresses https://git.pleroma.social/pleroma/pleroma-support/-/issues/56.

lib/pleroma/user.ex
lib/pleroma/web/mastodon_api/views/account_view.ex
lib/pleroma/web/router.ex
lib/pleroma/web/templates/embed/show.html.eex
lib/pleroma/web/views/embed_view.ex
test/pleroma/user_test.exs
test/pleroma/web/mastodon_api/views/account_view_test.exs

index e422b59f18c8d0d63148f0d4007b46d6c721d453..7cb36454ac99bb65224c680fd8e35a87d7db35c6 100644 (file)
@@ -2030,6 +2030,15 @@ defmodule Pleroma.User do
     |> hd()
   end
 
+  def full_nickname(%User{} = user) do
+    if String.contains?(user.nickname, "@") do
+      user.nickname
+    else
+      %{host: host} = URI.parse(user.ap_id)
+      user.nickname <> "@" <> host
+    end
+  end
+
   def full_nickname(nickname_or_mention),
     do: String.trim_leading(nickname_or_mention, "@")
 
index 63417142f79d6c256e907d7deb47dff8509e1c07..ac25aefdd6653850c4843e585aada9898d2b87e5 100644 (file)
@@ -262,7 +262,9 @@ defmodule Pleroma.Web.MastodonAPI.AccountView do
         }
       },
 
-      # Pleroma extension
+      # Pleroma extensions
+      # Note: it's insecure to output :email but fully-qualified nickname may serve as safe stub
+      fqn: User.full_nickname(user),
       pleroma: %{
         ap_id: user.ap_id,
         also_known_as: user.also_known_as,
index a9e332fa1c4ce3e298d3d2337385b7ad497df268..f70d327d2d2c7cdc229ba99245450dc42a06b978 100644 (file)
@@ -319,6 +319,8 @@ defmodule Pleroma.Web.Router do
   end
 
   scope "/oauth", Pleroma.Web.OAuth do
+    # Note: use /api/v1/accounts/verify_credentials for userinfo of signed-in user
+
     get("/registration_details", OAuthController, :registration_details)
 
     post("/mfa/verify", MFAController, :verify, as: :mfa_verify)
index 05a3f0ee305acf26c0cc469ace6d0f8b7b82fee4..092b52b705350807f86089402a85ff772698c4d4 100644 (file)
@@ -6,7 +6,7 @@
       </div>
       <span class="display-name" style="padding-left: 0.5em;">
         <bdi><%= raw (@author.name |> Formatter.emojify(@author.emoji)) %></bdi>
-        <span class="nickname"><%= full_nickname(@author) %></span>
+        <span class="nickname">@<%= full_nickname(@author) %></span>
       </span>
     </a>
   </div>
index cb7600adbaf3f1952f05ed3ba01d22286dfe16a7..81e19673038d46e052378512ffcc71895dd5dd77 100644 (file)
@@ -17,6 +17,8 @@ defmodule Pleroma.Web.EmbedView do
 
   use Phoenix.HTML
 
+  defdelegate full_nickname(user), to: User
+
   @media_types ["image", "audio", "video"]
 
   defp fetch_media_type(%{"mediaType" => mediaType}) do
@@ -30,11 +32,6 @@ defmodule Pleroma.Web.EmbedView do
     )
   end
 
-  defp full_nickname(user) do
-    %{host: host} = URI.parse(user.ap_id)
-    "@" <> user.nickname <> "@" <> host
-  end
-
   defp status_title(%Activity{object: %Object{data: %{"name" => name}}}) when is_binary(name),
     do: name
 
index b4df22c2c849c535c3bb69f707daed3ab8621995..baa71ca66e858878386ca5e25f3fe1344b5f48aa 100644 (file)
@@ -2232,6 +2232,36 @@ defmodule Pleroma.UserTest do
     end
   end
 
+  describe "local_nickname/1" do
+    test "returns nickname without host" do
+      assert User.local_nickname("@mentioned") == "mentioned"
+      assert User.local_nickname("a_local_nickname") == "a_local_nickname"
+      assert User.local_nickname("nickname@host.com") == "nickname"
+    end
+  end
+
+  describe "full_nickname/1" do
+    test "returns fully qualified nickname for local and remote users" do
+      local_user =
+        insert(:user, nickname: "local_user", ap_id: "https://somehost.com/users/local_user")
+
+      remote_user = insert(:user, nickname: "remote@host.com", local: false)
+
+      assert User.full_nickname(local_user) == "local_user@somehost.com"
+      assert User.full_nickname(remote_user) == "remote@host.com"
+    end
+
+    test "strips leading @ from mentions" do
+      assert User.full_nickname("@mentioned") == "mentioned"
+      assert User.full_nickname("@nickname@host.com") == "nickname@host.com"
+    end
+
+    test "does not modify nicknames" do
+      assert User.full_nickname("nickname") == "nickname"
+      assert User.full_nickname("nickname@host.com") == "nickname@host.com"
+    end
+  end
+
   test "avatar fallback" do
     user = insert(:user)
     assert User.avatar_url(user) =~ "/images/avi.png"
index 999bde474fd82df177cadc99632e58fde2e9f3a1..5373a17c38f84a0db34a20ef213e0812daadd7b3 100644 (file)
@@ -73,6 +73,7 @@ defmodule Pleroma.Web.MastodonAPI.AccountViewTest do
         },
         fields: []
       },
+      fqn: "shp@shitposter.club",
       pleroma: %{
         ap_id: user.ap_id,
         also_known_as: ["https://shitposter.zone/users/shp"],
@@ -172,6 +173,7 @@ defmodule Pleroma.Web.MastodonAPI.AccountViewTest do
         },
         fields: []
       },
+      fqn: "shp@shitposter.club",
       pleroma: %{
         ap_id: user.ap_id,
         also_known_as: [],