Credo fixes: alias grouping/ordering
[akkoma] / lib / pleroma / web / twitter_api / views / user_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.TwitterAPI.UserView do
6 use Pleroma.Web, :view
7 alias Pleroma.{Formatter, HTML, User}
8 alias Pleroma.Web.CommonAPI.Utils
9 alias Pleroma.Web.MediaProxy
10
11 def render("show.json", %{user: user = %User{}} = assigns) do
12 render_one(user, Pleroma.Web.TwitterAPI.UserView, "user.json", assigns)
13 end
14
15 def render("index.json", %{users: users, for: user}) do
16 users
17 |> render_many(Pleroma.Web.TwitterAPI.UserView, "user.json", for: user)
18 |> Enum.filter(&Enum.any?/1)
19 end
20
21 def render("user.json", %{user: user = %User{}} = assigns) do
22 if User.visible_for?(user, assigns[:for]),
23 do: do_render("user.json", assigns),
24 else: %{}
25 end
26
27 def render("short.json", %{
28 user: %User{
29 nickname: nickname,
30 id: id,
31 ap_id: ap_id,
32 name: name
33 }
34 }) do
35 %{
36 "fullname" => name,
37 "id" => id,
38 "ostatus_uri" => ap_id,
39 "profile_url" => ap_id,
40 "screen_name" => nickname
41 }
42 end
43
44 defp do_render("user.json", %{user: user = %User{}} = assigns) do
45 for_user = assigns[:for]
46 image = User.avatar_url(user) |> MediaProxy.url()
47
48 {following, follows_you, statusnet_blocking} =
49 if for_user do
50 {
51 User.following?(for_user, user),
52 User.following?(user, for_user),
53 User.blocks?(for_user, user)
54 }
55 else
56 {false, false, false}
57 end
58
59 user_info = User.get_cached_user_info(user)
60
61 emoji =
62 (user.info.source_data["tag"] || [])
63 |> Enum.filter(fn %{"type" => t} -> t == "Emoji" end)
64 |> Enum.map(fn %{"icon" => %{"url" => url}, "name" => name} ->
65 {String.trim(name, ":"), url}
66 end)
67
68 # ``fields`` is an array of mastodon profile field, containing ``{"name": "…", "value": "…"}``.
69 # For example: [{"name": "Pronoun", "value": "she/her"}, …]
70 fields =
71 (user.info.source_data["attachment"] || [])
72 |> Enum.filter(fn %{"type" => t} -> t == "PropertyValue" end)
73 |> Enum.map(fn fields -> Map.take(fields, ["name", "value"]) end)
74
75 data = %{
76 "created_at" => user.inserted_at |> Utils.format_naive_asctime(),
77 "description" => HTML.strip_tags((user.bio || "") |> String.replace("<br>", "\n")),
78 "description_html" => HTML.filter_tags(user.bio, User.html_filter_policy(for_user)),
79 "favourites_count" => 0,
80 "followers_count" => user_info[:follower_count],
81 "following" => following,
82 "follows_you" => follows_you,
83 "statusnet_blocking" => statusnet_blocking,
84 "friends_count" => user_info[:following_count],
85 "id" => user.id,
86 "name" => user.name || user.nickname,
87 "name_html" =>
88 if(user.name,
89 do: HTML.strip_tags(user.name) |> Formatter.emojify(emoji),
90 else: user.nickname
91 ),
92 "profile_image_url" => image,
93 "profile_image_url_https" => image,
94 "profile_image_url_profile_size" => image,
95 "profile_image_url_original" => image,
96 "rights" => %{
97 "delete_others_notice" => !!user.info.is_moderator,
98 "admin" => !!user.info.is_admin
99 },
100 "screen_name" => user.nickname,
101 "statuses_count" => user_info[:note_count],
102 "statusnet_profile_url" => user.ap_id,
103 "cover_photo" => User.banner_url(user) |> MediaProxy.url(),
104 "background_image" => image_url(user.info.background) |> MediaProxy.url(),
105 "is_local" => user.local,
106 "locked" => user.info.locked,
107 "default_scope" => user.info.default_scope,
108 "no_rich_text" => user.info.no_rich_text,
109 "hide_followers" => user.info.hide_followers,
110 "hide_follows" => user.info.hide_follows,
111 "fields" => fields,
112
113 # Pleroma extension
114 "pleroma" => %{
115 "confirmation_pending" => user_info.confirmation_pending,
116 "tags" => user.tags
117 }
118 }
119
120 data =
121 if(user.info.is_admin || user.info.is_moderator,
122 do: maybe_with_role(data, user, for_user),
123 else: data
124 )
125
126 if assigns[:token] do
127 Map.put(data, "token", token_string(assigns[:token]))
128 else
129 data
130 end
131 end
132
133 defp maybe_with_role(data, %User{id: id} = user, %User{id: id}) do
134 Map.merge(data, %{"role" => role(user), "show_role" => user.info.show_role})
135 end
136
137 defp maybe_with_role(data, %User{info: %{show_role: true}} = user, _user) do
138 Map.merge(data, %{"role" => role(user)})
139 end
140
141 defp maybe_with_role(data, _, _), do: data
142
143 defp role(%User{info: %{:is_admin => true}}), do: "admin"
144 defp role(%User{info: %{:is_moderator => true}}), do: "moderator"
145 defp role(_), do: "member"
146
147 defp image_url(%{"url" => [%{"href" => href} | _]}), do: href
148 defp image_url(_), do: nil
149
150 defp token_string(%Pleroma.Web.OAuth.Token{token: token_str}), do: token_str
151 defp token_string(token), do: token
152 end