account_view.ex 3.19 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)
lain's avatar
lain committed
17
    bot = (user.info.source_data["type"] || "Person") in ["Application", "Service"]
lain's avatar
lain committed
18

19
    emojis =
lain's avatar
lain committed
20
      (user.info.source_data["tag"] || [])
21 22 23 24 25 26 27 28 29 30
      |> 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
    fields =
lain's avatar
lain committed
32
      (user.info.source_data["attachment"] || [])
33 34 35
      |> 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 62 63
      },
      # Note: Mastodon does not return this field:
      tags: user.tags
lain's avatar
lain committed
64 65
    }
  end
lain's avatar
lain committed
66 67 68

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

  def render("relationship.json", %{user: user, target: target}) do
77
    follow_activity = Pleroma.Web.ActivityPub.Utils.fetch_latest_follow(user, target)
78 79 80 81 82 83 84

    requested =
      if follow_activity do
        follow_activity.data["state"] == "pending"
      else
        false
      end
85

Roger Braun's avatar
Roger Braun committed
86
    %{
lain's avatar
lain committed
87
      id: to_string(target.id),
Roger Braun's avatar
Roger Braun committed
88 89
      following: User.following?(user, target),
      followed_by: User.following?(target, user),
lain's avatar
lain committed
90
      blocking: User.blocks?(user, target),
Roger Braun's avatar
Roger Braun committed
91
      muting: false,
92
      muting_notifications: false,
93
      requested: requested,
94
      domain_blocking: false,
95 96
      showing_reblogs: false,
      endorsed: false
Roger Braun's avatar
Roger Braun committed
97 98 99 100 101 102
    }
  end

  def render("relationships.json", %{user: user, targets: targets}) do
    render_many(targets, AccountView, "relationship.json", user: user, as: :target)
  end
103 104 105 106 107 108

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