account_view.ex 3.7 KB
Newer Older
1
# Pleroma: A lightweight social networking server
kaniini's avatar
kaniini committed
2
# Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>
3
4
# SPDX-License-Identifier: AGPL-3.0-only

lain's avatar
lain committed
5
6
defmodule Pleroma.Web.MastodonAPI.AccountView do
  use Pleroma.Web, :view
Haelwenn's avatar
Haelwenn committed
7

Haelwenn's avatar
Haelwenn committed
8
9
  alias Pleroma.HTML
  alias Pleroma.User
10
  alias Pleroma.Web.CommonAPI.Utils
Haelwenn's avatar
Haelwenn committed
11
  alias Pleroma.Web.MastodonAPI.AccountView
href's avatar
href committed
12
  alias Pleroma.Web.MediaProxy
lain's avatar
lain committed
13

14
  def render("accounts.json", %{users: users} = opts) do
15
16
17
    users
    |> render_many(AccountView, "account.json", opts)
    |> Enum.filter(&Enum.any?/1)
18
19
  end

20
  def render("account.json", %{user: user} = opts) do
21
    if User.visible_for?(user, opts[:for]),
22
23
      do: do_render("account.json", opts),
      else: %{}
24
25
  end

26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
  def render("mention.json", %{user: user}) do
    %{
      id: to_string(user.id),
      acct: user.nickname,
      username: username_from_nickname(user.nickname),
      url: user.ap_id
    }
  end

  def render("relationship.json", %{user: user, target: target}) do
    follow_activity = Pleroma.Web.ActivityPub.Utils.fetch_latest_follow(user, target)

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

    %{
      id: to_string(target.id),
      following: User.following?(user, target),
      followed_by: User.following?(target, user),
      blocking: User.blocks?(user, target),
      muting: false,
      muting_notifications: false,
      requested: requested,
      domain_blocking: false,
      showing_reblogs: false,
      endorsed: false
    }
57
58
  end

59
60
61
62
63
  def render("relationships.json", %{user: user, targets: targets}) do
    render_many(targets, AccountView, "relationship.json", user: user, as: :target)
  end

  defp do_render("account.json", %{user: user} = opts) do
href's avatar
href committed
64
    image = User.avatar_url(user) |> MediaProxy.url()
65
    header = User.banner_url(user) |> MediaProxy.url()
lain's avatar
lain committed
66
    user_info = User.user_info(user)
lain's avatar
lain committed
67
    bot = (user.info.source_data["type"] || "Person") in ["Application", "Service"]
lain's avatar
lain committed
68

69
    emojis =
lain's avatar
lain committed
70
      (user.info.source_data["tag"] || [])
71
72
73
74
75
76
77
78
79
80
      |> 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)

81
    fields =
lain's avatar
lain committed
82
      (user.info.source_data["attachment"] || [])
83
84
85
      |> Enum.filter(fn %{"type" => t} -> t == "PropertyValue" end)
      |> Enum.map(fn fields -> Map.take(fields, ["name", "value"]) end)

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

lain's avatar
lain committed
88
    %{
lain's avatar
lain committed
89
      id: to_string(user.id),
90
      username: username_from_nickname(user.nickname),
lain's avatar
lain committed
91
      acct: user.nickname,
92
      display_name: user.name || user.nickname,
93
      locked: user_info.locked,
94
      created_at: Utils.to_masto_date(user.inserted_at),
lain's avatar
lain committed
95
96
97
      followers_count: user_info.follower_count,
      following_count: user_info.following_count,
      statuses_count: user_info.note_count,
98
      note: bio || "",
lain's avatar
lain committed
99
100
101
      url: user.ap_id,
      avatar: image,
      avatar_static: image,
lain's avatar
lain committed
102
      header: header,
103
      header_static: header,
104
      emojis: emojis,
105
      fields: fields,
106
      bot: bot,
107
108
      source: %{
        note: "",
109
        privacy: user_info.default_scope,
110
        sensitive: false
111
      },
112

Ivan Tashkinov's avatar
Ivan Tashkinov committed
113
      # Pleroma extension
114
      pleroma: %{
115
        confirmation_pending: user_info.confirmation_pending,
116
117
118
        tags: user.tags,
        is_moderator: user.info.is_moderator,
        is_admin: user.info.is_admin
119
      }
lain's avatar
lain committed
120
121
    }
  end
lain's avatar
lain committed
122

123
124
125
126
127
  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
128
end