[#471] Factored out User.visible_for?/2.
[akkoma] / lib / pleroma / web / mastodon_api / views / account_view.ex
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2018 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
4
5 defmodule Pleroma.Web.MastodonAPI.AccountView do
6 use Pleroma.Web, :view
7 alias Pleroma.User
8 alias Pleroma.Web.MastodonAPI.AccountView
9 alias Pleroma.Web.CommonAPI.Utils
10 alias Pleroma.Web.MediaProxy
11 alias Pleroma.HTML
12
13 def render("accounts.json", %{users: users} = opts) do
14 users
15 |> render_many(AccountView, "account.json", opts)
16 |> Enum.filter(&Enum.any?/1)
17 end
18
19 def render("account.json", %{user: user} = opts) do
20 if User.visible_for?(user, opts[:for]),
21 do: render("valid_account.json", opts),
22 else: render("invalid_account.json", opts)
23 end
24
25 def render("invalid_account.json", _opts) do
26 %{}
27 end
28
29 def render("valid_account.json", %{user: user} = opts) do
30 image = User.avatar_url(user) |> MediaProxy.url()
31 header = User.banner_url(user) |> MediaProxy.url()
32 user_info = User.user_info(user)
33 bot = (user.info.source_data["type"] || "Person") in ["Application", "Service"]
34
35 emojis =
36 (user.info.source_data["tag"] || [])
37 |> Enum.filter(fn %{"type" => t} -> t == "Emoji" end)
38 |> Enum.map(fn %{"icon" => %{"url" => url}, "name" => name} ->
39 %{
40 "shortcode" => String.trim(name, ":"),
41 "url" => MediaProxy.url(url),
42 "static_url" => MediaProxy.url(url),
43 "visible_in_picker" => false
44 }
45 end)
46
47 fields =
48 (user.info.source_data["attachment"] || [])
49 |> Enum.filter(fn %{"type" => t} -> t == "PropertyValue" end)
50 |> Enum.map(fn fields -> Map.take(fields, ["name", "value"]) end)
51
52 bio = HTML.filter_tags(user.bio, User.html_filter_policy(opts[:for]))
53
54 %{
55 id: to_string(user.id),
56 username: username_from_nickname(user.nickname),
57 acct: user.nickname,
58 display_name: user.name || user.nickname,
59 locked: user_info.locked,
60 created_at: Utils.to_masto_date(user.inserted_at),
61 followers_count: user_info.follower_count,
62 following_count: user_info.following_count,
63 statuses_count: user_info.note_count,
64 note: bio || "",
65 url: user.ap_id,
66 avatar: image,
67 avatar_static: image,
68 header: header,
69 header_static: header,
70 emojis: emojis,
71 fields: fields,
72 bot: bot,
73 source: %{
74 note: "",
75 privacy: user_info.default_scope,
76 sensitive: false
77 },
78
79 # Pleroma extension
80 pleroma: %{
81 confirmation_pending: user_info.confirmation_pending,
82 tags: user.tags
83 }
84 }
85 end
86
87 def render("mention.json", %{user: user}) do
88 %{
89 id: to_string(user.id),
90 acct: user.nickname,
91 username: username_from_nickname(user.nickname),
92 url: user.ap_id
93 }
94 end
95
96 def render("relationship.json", %{user: user, target: target}) do
97 follow_activity = Pleroma.Web.ActivityPub.Utils.fetch_latest_follow(user, target)
98
99 requested =
100 if follow_activity do
101 follow_activity.data["state"] == "pending"
102 else
103 false
104 end
105
106 %{
107 id: to_string(target.id),
108 following: User.following?(user, target),
109 followed_by: User.following?(target, user),
110 blocking: User.blocks?(user, target),
111 muting: false,
112 muting_notifications: false,
113 requested: requested,
114 domain_blocking: false,
115 showing_reblogs: false,
116 endorsed: false
117 }
118 end
119
120 def render("relationships.json", %{user: user, targets: targets}) do
121 render_many(targets, AccountView, "relationship.json", user: user, as: :target)
122 end
123
124 defp username_from_nickname(string) when is_binary(string) do
125 hd(String.split(string, "@"))
126 end
127
128 defp username_from_nickname(_), do: nil
129 end