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