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
7
defmodule Pleroma.Web.MastodonAPI.AccountView do
  use Pleroma.Web, :view
  alias Pleroma.User
Roger Braun's avatar
Roger Braun committed
8
  alias Pleroma.Web.MastodonAPI.AccountView
9
  alias Pleroma.Web.CommonAPI.Utils
href's avatar
href committed
10
  alias Pleroma.Web.MediaProxy
11
  alias Pleroma.HTML
lain's avatar
lain committed
12

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

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

25
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
  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
    }
56
57
  end

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

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

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

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

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

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

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