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