account_view.ex 2.45 KB
Newer Older
lain's avatar
lain committed
1
2
3
defmodule Pleroma.Web.MastodonAPI.AccountView do
  use Pleroma.Web, :view
  alias Pleroma.User
Roger Braun's avatar
Roger Braun committed
4
  alias Pleroma.Web.MastodonAPI.AccountView
5
  alias Pleroma.Web.CommonAPI.Utils
href's avatar
href committed
6
  alias Pleroma.Web.MediaProxy
lain's avatar
lain committed
7

8
9
10
11
  def render("accounts.json", %{users: users} = opts) do
    render_many(users, AccountView, "account.json", opts)
  end

lain's avatar
lain committed
12
  def render("account.json", %{user: user}) do
href's avatar
href committed
13
    image = User.avatar_url(user) |> MediaProxy.url()
14
    header = User.banner_url(user) |> MediaProxy.url()
lain's avatar
lain committed
15
16
    user_info = User.user_info(user)

17
18
19
20
21
22
23
24
25
26
27
28
    emojis =
      (user.info["source_data"]["tag"] || [])
      |> Enum.filter(fn %{"type" => t} -> t == "Emoji" end)
      |> Enum.map(fn %{"icon" => %{"url" => url}, "name" => name} ->
        %{
          "shortcode" => String.trim(name, ":"),
          "url" => MediaProxy.url(url),
          "static_url" => MediaProxy.url(url),
          "visible_in_picker" => false
        }
      end)

lain's avatar
lain committed
29
    %{
lain's avatar
lain committed
30
      id: to_string(user.id),
31
      username: username_from_nickname(user.nickname),
lain's avatar
lain committed
32
      acct: user.nickname,
33
      display_name: user.name || user.nickname,
34
      locked: user_info.locked,
35
      created_at: Utils.to_masto_date(user.inserted_at),
lain's avatar
lain committed
36
37
38
      followers_count: user_info.follower_count,
      following_count: user_info.following_count,
      statuses_count: user_info.note_count,
ensra's avatar
ensra committed
39
      note: HtmlSanitizeEx.basic_html(user.bio) || "",
lain's avatar
lain committed
40
41
42
      url: user.ap_id,
      avatar: image,
      avatar_static: image,
lain's avatar
lain committed
43
      header: header,
44
      header_static: header,
45
      emojis: emojis,
Haelwenn's avatar
Haelwenn committed
46
      fields: [],
47
48
      source: %{
        note: "",
49
        privacy: user_info.default_scope,
50
51
        sensitive: "false"
      }
lain's avatar
lain committed
52
53
    }
  end
lain's avatar
lain committed
54
55
56

  def render("mention.json", %{user: user}) do
    %{
lain's avatar
lain committed
57
      id: to_string(user.id),
lain's avatar
lain committed
58
      acct: user.nickname,
59
      username: username_from_nickname(user.nickname),
lain's avatar
lain committed
60
61
62
      url: user.ap_id
    }
  end
Roger Braun's avatar
Roger Braun committed
63
64
65

  def render("relationship.json", %{user: user, target: target}) do
    %{
lain's avatar
lain committed
66
      id: to_string(target.id),
Roger Braun's avatar
Roger Braun committed
67
68
      following: User.following?(user, target),
      followed_by: User.following?(target, user),
lain's avatar
lain committed
69
      blocking: User.blocks?(user, target),
Roger Braun's avatar
Roger Braun committed
70
71
72
73
74
75
76
77
78
      muting: false,
      requested: false,
      domain_blocking: false
    }
  end

  def render("relationships.json", %{user: user, targets: targets}) do
    render_many(targets, AccountView, "relationship.json", user: user, as: :target)
  end
79
80
81
82
83
84

  defp username_from_nickname(string) when is_binary(string) do
    hd(String.split(string, "@"))
  end

  defp username_from_nickname(_), do: nil
lain's avatar
lain committed
85
end