user_view.ex 2.32 KB
Newer Older
dtluna's avatar
dtluna committed
1
2
defmodule Pleroma.Web.TwitterAPI.UserView do
  use Pleroma.Web, :view
3
  alias Pleroma.User
4
  alias Pleroma.Web.CommonAPI.Utils
href's avatar
href committed
5
  alias Pleroma.Web.MediaProxy
6

dtluna's avatar
dtluna committed
7
  def render("show.json", %{user: user = %User{}} = assigns) do
lain's avatar
lain committed
8
9
10
11
12
13
14
15
    render_one(user, Pleroma.Web.TwitterAPI.UserView, "user.json", assigns)
  end

  def render("index.json", %{users: users, for: user}) do
    render_many(users, Pleroma.Web.TwitterAPI.UserView, "user.json", for: user)
  end

  def render("user.json", %{user: user = %User{}} = assigns) do
href's avatar
href committed
16
    image = User.avatar_url(user) |> MediaProxy.url()
eal's avatar
eal committed
17
18
19
20
21
22
    {following, follows_you, statusnet_blocking} = if assigns[:for] do
      {
        User.following?(assigns[:for], user),
        User.following?(user, assigns[:for]),
        User.blocks?(assigns[:for], user)
      }
23
    else
eal's avatar
eal committed
24
      {false, false, false}
eal's avatar
eal committed
25
    end
26

lain's avatar
lain committed
27
    user_info = User.get_cached_user_info(user)
lain's avatar
lain committed
28

lain's avatar
lain committed
29
    data = %{
dtluna's avatar
dtluna committed
30
      "created_at" => user.inserted_at |> Utils.format_naive_asctime,
lain's avatar
lain committed
31
      "description" => HtmlSanitizeEx.strip_tags(user.bio),
lain's avatar
lain committed
32
      "favourites_count" => 0,
lain's avatar
lain committed
33
      "followers_count" => user_info[:follower_count],
dtluna's avatar
dtluna committed
34
      "following" => following,
eal's avatar
eal committed
35
      "follows_you" => follows_you,
eal's avatar
eal committed
36
      "statusnet_blocking" => statusnet_blocking,
dtluna's avatar
dtluna committed
37
38
39
      "friends_count" => user_info[:following_count],
      "id" => user.id,
      "name" => user.name,
lain's avatar
lain committed
40
41
42
43
      "profile_image_url" => image,
      "profile_image_url_https" => image,
      "profile_image_url_profile_size" => image,
      "profile_image_url_original" => image,
lain's avatar
lain committed
44
      "rights" => %{},
dtluna's avatar
dtluna committed
45
46
      "screen_name" => user.nickname,
      "statuses_count" => user_info[:note_count],
lain's avatar
lain committed
47
      "statusnet_profile_url" => user.ap_id,
48
      "cover_photo" => User.banner_url(user) |> MediaProxy.url(),
href's avatar
href committed
49
      "background_image" => image_url(user.info["background"]) |> MediaProxy.url(),
eal's avatar
eal committed
50
      "is_local" => user.local
lain's avatar
lain committed
51
    }
lain's avatar
lain committed
52
53
54
55
56
57

    if assigns[:token] do
      Map.put(data, "token", assigns[:token])
    else
      data
    end
dtluna's avatar
dtluna committed
58
  end
lain's avatar
lain committed
59

dtluna's avatar
dtluna committed
60
61
62
63
64
65
66
67
68
69
  def render("short.json", %{user: %User{
                               nickname: nickname, id: id, ap_id: ap_id, name: name
                           }}) do
    %{
      "fullname" => name,
      "id" => id,
      "ostatus_uri" => ap_id,
      "profile_url" => ap_id,
      "screen_name" => nickname
    }
lain's avatar
lain committed
70
  end
Thog's avatar
Thog committed
71
72
73

  defp image_url(%{"url" => [ %{ "href" => href } | _ ]}), do: href
  defp image_url(_), do: nil
lain's avatar
lain committed
74
end