account_view.ex 4.26 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
43
44
45
46
47
48
49
50
51
52
53
    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),
54
      muting: User.mutes?(user, target),
55
56
57
      muting_notifications: false,
      requested: requested,
      domain_blocking: false,
58
      showing_reblogs: User.showing_reblogs?(user, target),
59
60
      endorsed: false
    }
61
62
  end

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

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

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

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

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

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

Ivan Tashkinov's avatar
Ivan Tashkinov committed
119
      # Pleroma extension
120
121
122
123
124
125
126
127
128
      pleroma:
        %{
          confirmation_pending: user_info.confirmation_pending,
          tags: user.tags,
          is_moderator: user.info.is_moderator,
          is_admin: user.info.is_admin,
          relationship: relationship
        }
        |> with_notification_settings(user, opts[:for])
lain's avatar
lain committed
129
130
    }
  end
lain's avatar
lain committed
131

132
133
134
135
136
  defp username_from_nickname(string) when is_binary(string) do
    hd(String.split(string, "@"))
  end

  defp username_from_nickname(_), do: nil
137
138
139
140
141
142

  defp with_notification_settings(data, %User{id: user_id} = user, %User{id: user_id}) do
    Map.put(data, :notification_settings, user.info.notification_settings)
  end

  defp with_notification_settings(data, _, _), do: data
lain's avatar
lain committed
143
end