account_view.ex 3.06 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

13
  def render("account.json", %{user: user} = opts) 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)

36
37
    bio = HTML.filter_tags(user.bio, User.html_filter_policy(opts[:for]))

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

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

  def render("relationship.json", %{user: user, target: target}) do
75
76
77
    follow_activity = Pleroma.Web.ActivityPub.Utils.fetch_latest_follow(user, target)
    requested = follow_activity.data["state"] == "pending"

Roger Braun's avatar
Roger Braun committed
78
    %{
lain's avatar
lain committed
79
      id: to_string(target.id),
Roger Braun's avatar
Roger Braun committed
80
81
      following: User.following?(user, target),
      followed_by: User.following?(target, user),
lain's avatar
lain committed
82
      blocking: User.blocks?(user, target),
Roger Braun's avatar
Roger Braun committed
83
      muting: false,
84
      muting_notifications: false,
85
      requested: requested,
86
      domain_blocking: false,
87
88
      showing_reblogs: false,
      endorsed: false
Roger Braun's avatar
Roger Braun committed
89
90
91
92
93
94
    }
  end

  def render("relationships.json", %{user: user, targets: targets}) do
    render_many(targets, AccountView, "relationship.json", user: user, as: :target)
  end
95
96
97
98
99
100

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