Prefer FollowBot naming convention vs Followbot
[akkoma] / lib / pleroma / web / mastodon_api / controllers / status_controller.ex
index 47a5bbd60d51e387e632932136376c5dd5293e33..b051fca741f560f512a12f455bb6563d1b7924fa 100644 (file)
@@ -21,6 +21,7 @@ defmodule Pleroma.Web.MastodonAPI.StatusController do
   alias Pleroma.Web.CommonAPI
   alias Pleroma.Web.MastodonAPI.AccountView
   alias Pleroma.Web.MastodonAPI.ScheduledActivityView
+  alias Pleroma.Web.OAuth.Token
   alias Pleroma.Web.Plugs.OAuthScopesPlug
   alias Pleroma.Web.Plugs.RateLimiter
 
@@ -132,7 +133,7 @@ defmodule Pleroma.Web.MastodonAPI.StatusController do
   # Creates a scheduled status when `scheduled_at` param is present and it's far enough
   def create(
         %{
-          assigns: %{user: user, token: %{app_id: app_id}},
+          assigns: %{user: user},
           body_params: %{status: _, scheduled_at: scheduled_at} = params
         } = conn,
         _
@@ -140,7 +141,7 @@ defmodule Pleroma.Web.MastodonAPI.StatusController do
       when not is_nil(scheduled_at) do
     params =
       Map.put(params, :in_reply_to_status_id, params[:in_reply_to_id])
-      |> add_application(app_id)
+      |> put_application(conn)
 
     attrs = %{
       params: Map.new(params, fn {key, value} -> {to_string(key), value} end),
@@ -163,14 +164,10 @@ defmodule Pleroma.Web.MastodonAPI.StatusController do
   end
 
   # Creates a regular status
-  def create(
-        %{assigns: %{user: user, token: %{app_id: app_id}}, body_params: %{status: _} = params} =
-          conn,
-        _
-      ) do
+  def create(%{assigns: %{user: user}, body_params: %{status: _} = params} = conn, _) do
     params =
       Map.put(params, :in_reply_to_status_id, params[:in_reply_to_id])
-      |> add_application(app_id)
+      |> put_application(conn)
 
     with {:ok, activity} <- CommonAPI.post(user, params) do
       try_render(conn, "show.json",
@@ -423,7 +420,14 @@ defmodule Pleroma.Web.MastodonAPI.StatusController do
     )
   end
 
-  defp add_application(params, app_id) do
-    params |> Map.put(:application, Pleroma.Web.OAuth.App.get_app_by_id(app_id))
+  defp put_application(params, %{assigns: %{token: %Token{user: %User{} = user} = token}} = _conn) do
+    if user.disclose_client do
+      %{client_name: client_name, website: website} = Repo.preload(token, :app).app
+      Map.put(params, :generator, %{type: "Application", name: client_name, url: website})
+    else
+      Map.put(params, :generator, nil)
+    end
   end
+
+  defp put_application(params, _), do: Map.put(params, :generator, nil)
 end