account_view.ex 4.02 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
      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()
lain's avatar
lain committed
71
    user_info = User.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: "",
116
        privacy: user_info.default_scope,
117
        sensitive: false
118
      },
119

Ivan Tashkinov's avatar
Ivan Tashkinov committed
120
      # Pleroma extension
121
      pleroma: %{
122
        confirmation_pending: user_info.confirmation_pending,
123
124
        tags: user.tags,
        is_moderator: user.info.is_moderator,
125
126
        is_admin: user.info.is_admin,
        relationship: relationship
127
      }
lain's avatar
lain committed
128
129
    }
  end
lain's avatar
lain committed
130

131
132
133
134
135
  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
136
end