Add ability to set a default post expiry (#321)
[akkoma] / lib / pleroma / web / mastodon_api / views / account_view.ex
index 4290d11aeccd721c432880fb48aade501e36978c..653a50e20aecc3a4b76eb70a16a56d8814399ccd 100644 (file)
@@ -7,6 +7,7 @@ defmodule Pleroma.Web.MastodonAPI.AccountView do
 
   alias Pleroma.FollowingRelationship
   alias Pleroma.User
+  alias Pleroma.UserNote
   alias Pleroma.UserRelationship
   alias Pleroma.Web.CommonAPI.Utils
   alias Pleroma.Web.MastodonAPI.AccountView
@@ -93,12 +94,12 @@ defmodule Pleroma.Web.MastodonAPI.AccountView do
 
     followed_by =
       if following_relationships do
-        case FollowingRelationship.find(following_relationships, target, reading_user) do
-          %{state: :follow_accept} -> true
-          _ -> false
-        end
+        target_to_user_following_relation =
+          FollowingRelationship.find(following_relationships, target, reading_user)
+
+        User.get_follow_state(target, reading_user, target_to_user_following_relation)
       else
-        User.following?(target, reading_user)
+        User.get_follow_state(target, reading_user)
       end
 
     subscribing =
@@ -114,7 +115,7 @@ defmodule Pleroma.Web.MastodonAPI.AccountView do
     %{
       id: to_string(target.id),
       following: follow_state == :follow_accept,
-      followed_by: followed_by,
+      followed_by: followed_by == :follow_accept,
       blocking:
         UserRelationship.exists?(
           user_relationships,
@@ -150,6 +151,7 @@ defmodule Pleroma.Web.MastodonAPI.AccountView do
       subscribing: subscribing,
       notifying: subscribing,
       requested: follow_state == :follow_pending,
+      requested_by: followed_by == :follow_pending,
       domain_blocking: User.blocks_domain?(reading_user, target),
       showing_reblogs:
         not UserRelationship.exists?(
@@ -159,7 +161,12 @@ defmodule Pleroma.Web.MastodonAPI.AccountView do
           target,
           &User.muting_reblogs?(&1, &2)
         ),
-      endorsed: false
+      endorsed: false,
+      note:
+        UserNote.show(
+          reading_user,
+          target
+        )
     }
   end
 
@@ -180,6 +187,16 @@ defmodule Pleroma.Web.MastodonAPI.AccountView do
     render_many(targets, AccountView, "relationship.json", render_opts)
   end
 
+  def render("instance.json", %{instance: %Pleroma.Instances.Instance{} = instance}) do
+    %{
+      name: instance.host,
+      favicon: instance.favicon |> MediaProxy.url(),
+      nodeinfo: instance.nodeinfo
+    }
+  end
+
+  def render("instance.json", _), do: nil
+
   defp do_render("show.json", %{user: user} = opts) do
     user = User.sanitize_html(user, User.html_filter_policy(opts[:for]))
     display_name = user.name || user.nickname
@@ -224,16 +241,20 @@ defmodule Pleroma.Web.MastodonAPI.AccountView do
         %{}
       end
 
-    favicon =
-      if Pleroma.Config.get([:instances_favicons, :enabled]) do
-        user
-        |> Map.get(:ap_id, "")
-        |> URI.parse()
-        |> URI.merge("/")
-        |> Pleroma.Instances.Instance.get_or_update_favicon()
-        |> MediaProxy.url()
+    instance =
+      with {:ok, instance} <- Pleroma.Instances.Instance.get_cached_by_url(user.ap_id) do
+        instance
       else
+        _ ->
+          nil
+      end
+
+    favicon =
+      if is_nil(instance) do
         nil
+      else
+        instance.favicon
+        |> MediaProxy.url()
       end
 
     %{
@@ -264,7 +285,11 @@ defmodule Pleroma.Web.MastodonAPI.AccountView do
           actor_type: user.actor_type
         }
       },
-
+      last_status_at: user.last_status_at,
+      akkoma: %{
+        instance: render("instance.json", %{instance: instance}),
+        status_ttl_days: user.status_ttl_days
+      },
       # Pleroma extensions
       # Note: it's insecure to output :email but fully-qualified nickname may serve as safe stub
       fqn: User.full_nickname(user),
@@ -282,7 +307,6 @@ defmodule Pleroma.Web.MastodonAPI.AccountView do
         relationship: relationship,
         skip_thread_containment: user.skip_thread_containment,
         background_image: image_url(user.background) |> MediaProxy.url(),
-        accepts_chat_messages: user.accepts_chat_messages,
         favicon: favicon
       }
     }
@@ -290,7 +314,6 @@ defmodule Pleroma.Web.MastodonAPI.AccountView do
     |> maybe_put_settings(user, opts[:for], opts)
     |> maybe_put_notification_settings(user, opts[:for])
     |> maybe_put_settings_store(user, opts[:for], opts)
-    |> maybe_put_chat_token(user, opts[:for], opts)
     |> maybe_put_activation_status(user, opts[:for])
     |> maybe_put_follow_requests_count(user, opts[:for])
     |> maybe_put_allow_following_move(user, opts[:for])
@@ -343,15 +366,6 @@ defmodule Pleroma.Web.MastodonAPI.AccountView do
 
   defp maybe_put_settings_store(data, _, _, _), do: data
 
-  defp maybe_put_chat_token(data, %User{id: id}, %User{id: id}, %{
-         with_chat_token: token
-       }) do
-    data
-    |> Kernel.put_in([:pleroma, :chat_token], token)
-  end
-
-  defp maybe_put_chat_token(data, _, _, _), do: data
-
   defp maybe_put_role(data, %User{show_role: true} = user, _) do
     data
     |> Kernel.put_in([:pleroma, :is_admin], user.is_admin)