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