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

35
36
37
38
39
  def render("relationship.json", %{user: nil, target: _target}) do
    %{}
  end

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

    requested =
lain's avatar
lain committed
43
      if follow_activity && !User.following?(target, user) do
44
45
46
47
48
49
50
51
52
53
        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),
54
      muting: User.mutes?(user, target),
55
      muting_notifications: false,
56
      subscribing: User.subscribed_to?(user, target),
57
58
      requested: requested,
      domain_blocking: false,
59
      showing_reblogs: User.showing_reblogs?(user, target),
60
61
      endorsed: false
    }
62
63
  end

64
65
66
67
68
  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
69
    image = User.avatar_url(user) |> MediaProxy.url()
70
    header = User.banner_url(user) |> MediaProxy.url()
minibikini's avatar
minibikini committed
71
    user_info = User.get_cached_user_info(user)
lain's avatar
lain committed
72
    bot = (user.info.source_data["type"] || "Person") in ["Application", "Service"]
lain's avatar
lain committed
73

74
    emojis =
lain's avatar
lain committed
75
      (user.info.source_data["tag"] || [])
76
77
78
79
80
81
82
83
84
85
      |> 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)

86
    fields =
lain's avatar
lain committed
87
      (user.info.source_data["attachment"] || [])
88
89
90
      |> Enum.filter(fn %{"type" => t} -> t == "PropertyValue" end)
      |> Enum.map(fn fields -> Map.take(fields, ["name", "value"]) end)

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

93
94
    relationship = render("relationship.json", %{user: opts[:for], target: user})

lain's avatar
lain committed
95
    %{
lain's avatar
lain committed
96
      id: to_string(user.id),
97
      username: username_from_nickname(user.nickname),
lain's avatar
lain committed
98
      acct: user.nickname,
99
      display_name: user.name || user.nickname,
100
      locked: user_info.locked,
101
      created_at: Utils.to_masto_date(user.inserted_at),
lain's avatar
lain committed
102
103
104
      followers_count: user_info.follower_count,
      following_count: user_info.following_count,
      statuses_count: user_info.note_count,
105
      note: bio || "",
lain's avatar
lain committed
106
107
108
      url: user.ap_id,
      avatar: image,
      avatar_static: image,
lain's avatar
lain committed
109
      header: header,
110
      header_static: header,
111
      emojis: emojis,
112
      fields: fields,
113
      bot: bot,
114
115
      source: %{
        note: "",
rinpatch's avatar
rinpatch committed
116
117
        sensitive: false,
        pleroma: %{}
118
      },
119

Ivan Tashkinov's avatar
Ivan Tashkinov committed
120
      # Pleroma extension
121
122
123
124
125
126
127
128
      pleroma: %{
        confirmation_pending: user_info.confirmation_pending,
        tags: user.tags,
        hide_followers: user.info.hide_followers,
        hide_follows: user.info.hide_follows,
        hide_favorites: user.info.hide_favorites,
        relationship: relationship
      }
lain's avatar
lain committed
129
    }
130
131
132
    |> maybe_put_role(user, opts[:for])
    |> maybe_put_settings(user, opts[:for], user_info)
    |> maybe_put_notification_settings(user, opts[:for])
lain's avatar
lain committed
133
  end
lain's avatar
lain committed
134

135
136
137
138
139
  defp username_from_nickname(string) when is_binary(string) do
    hd(String.split(string, "@"))
  end

  defp username_from_nickname(_), do: nil
140

141
142
143
144
145
146
147
148
  defp maybe_put_settings(
         data,
         %User{id: user_id} = user,
         %User{id: user_id},
         user_info
       ) do
    data
    |> Kernel.put_in([:source, :privacy], user_info.default_scope)
rinpatch's avatar
rinpatch committed
149
150
    |> Kernel.put_in([:source, :pleroma, :show_role], user.info.show_role)
    |> Kernel.put_in([:source, :pleroma, :no_rich_text], user.info.no_rich_text)
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
  end

  defp maybe_put_settings(data, _, _, _), do: data

  defp maybe_put_role(data, %User{info: %{show_role: true}} = user, _) do
    data
    |> Kernel.put_in([:pleroma, :is_admin], user.info.is_admin)
    |> Kernel.put_in([:pleroma, :is_moderator], user.info.is_moderator)
  end

  defp maybe_put_role(data, %User{id: user_id} = user, %User{id: user_id}) do
    data
    |> Kernel.put_in([:pleroma, :is_admin], user.info.is_admin)
    |> Kernel.put_in([:pleroma, :is_moderator], user.info.is_moderator)
  end

  defp maybe_put_role(data, _, _), do: data

  defp maybe_put_notification_settings(data, %User{id: user_id} = user, %User{id: user_id}) do
    Kernel.put_in(data, [:pleroma, :notification_settings], user.info.notification_settings)
171
172
  end

173
  defp maybe_put_notification_settings(data, _, _), do: data
lain's avatar
lain committed
174
end