account_view.ex 2.83 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
7
  alias Pleroma.HTML
lain's avatar
lain committed
8

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

lain's avatar
lain committed
13
  def render("account.json", %{user: user}) do
href's avatar
href committed
14
    image = User.avatar_url(user) |> MediaProxy.url()
15
    header = User.banner_url(user) |> MediaProxy.url()
lain's avatar
lain committed
16
    user_info = User.user_info(user)
17
    bot = (user.info["source_data"]["type"] || "Person") in ["Application", "Service"]
lain's avatar
lain committed
18

19
20
21
22
23
24
25
26
27
28
29
30
    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)

31
32
33
34
35
    fields =
      (user.info["source_data"]["attachment"] || [])
      |> Enum.filter(fn %{"type" => t} -> t == "PropertyValue" end)
      |> Enum.map(fn fields -> Map.take(fields, ["name", "value"]) end)

lain's avatar
lain committed
36
    %{
lain's avatar
lain committed
37
      id: to_string(user.id),
38
      username: username_from_nickname(user.nickname),
lain's avatar
lain committed
39
      acct: user.nickname,
40
      display_name: user.name || user.nickname,
41
      locked: user_info.locked,
42
      created_at: Utils.to_masto_date(user.inserted_at),
lain's avatar
lain committed
43
44
45
      followers_count: user_info.follower_count,
      following_count: user_info.following_count,
      statuses_count: user_info.note_count,
46
      note: HTML.filter_tags(user.bio) || "",
lain's avatar
lain committed
47
48
49
      url: user.ap_id,
      avatar: image,
      avatar_static: image,
lain's avatar
lain committed
50
      header: header,
51
      header_static: header,
52
      emojis: emojis,
53
      fields: fields,
54
      bot: bot,
55
56
      source: %{
        note: "",
57
        privacy: user_info.default_scope,
58
        sensitive: false
59
      }
lain's avatar
lain committed
60
61
    }
  end
lain's avatar
lain committed
62
63
64

  def render("mention.json", %{user: user}) do
    %{
lain's avatar
lain committed
65
      id: to_string(user.id),
lain's avatar
lain committed
66
      acct: user.nickname,
67
      username: username_from_nickname(user.nickname),
lain's avatar
lain committed
68
69
70
      url: user.ap_id
    }
  end
Roger Braun's avatar
Roger Braun committed
71
72
73

  def render("relationship.json", %{user: user, target: target}) do
    %{
lain's avatar
lain committed
74
      id: to_string(target.id),
Roger Braun's avatar
Roger Braun committed
75
76
      following: User.following?(user, target),
      followed_by: User.following?(target, user),
lain's avatar
lain committed
77
      blocking: User.blocks?(user, target),
Roger Braun's avatar
Roger Braun committed
78
      muting: false,
79
      muting_notifications: false,
Roger Braun's avatar
Roger Braun committed
80
      requested: false,
81
82
      domain_blocking: false,
      showing_reblogs: false
Roger Braun's avatar
Roger Braun committed
83
84
85
86
87
88
    }
  end

  def render("relationships.json", %{user: user, targets: targets}) do
    render_many(targets, AccountView, "relationship.json", user: user, as: :target)
  end
89
90
91
92
93
94

  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
95
end