user_view.ex 2.21 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
5

dtluna's avatar
dtluna committed
6
  def render("show.json", %{user: user = %User{}} = assigns) do
lain's avatar
lain committed
7
8
9
10
11
12
13
14
    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
lain's avatar
lain committed
15
    image = User.avatar_url(user)
eal's avatar
eal committed
16
17
18
19
20
21
    {following, follows_you, statusnet_blocking} = if assigns[:for] do
      {
        User.following?(assigns[:for], user),
        User.following?(user, assigns[:for]),
        User.blocks?(assigns[:for], user)
      }
22
    else
eal's avatar
eal committed
23
      {false, false, false}
eal's avatar
eal committed
24
    end
25

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

lain's avatar
lain committed
28
    data = %{
dtluna's avatar
dtluna committed
29
      "created_at" => user.inserted_at |> Utils.format_naive_asctime,
lain's avatar
lain committed
30
      "description" => HtmlSanitizeEx.strip_tags(user.bio),
lain's avatar
lain committed
31
      "favourites_count" => 0,
lain's avatar
lain committed
32
      "followers_count" => user_info[:follower_count],
dtluna's avatar
dtluna committed
33
      "following" => following,
eal's avatar
eal committed
34
      "follows_you" => follows_you,
eal's avatar
eal committed
35
      "statusnet_blocking" => statusnet_blocking,
dtluna's avatar
dtluna committed
36
37
38
      "friends_count" => user_info[:following_count],
      "id" => user.id,
      "name" => user.name,
lain's avatar
lain committed
39
40
41
42
      "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
43
      "rights" => %{},
dtluna's avatar
dtluna committed
44
45
      "screen_name" => user.nickname,
      "statuses_count" => user_info[:note_count],
lain's avatar
lain committed
46
47
48
      "statusnet_profile_url" => user.ap_id,
      "cover_photo" => image_url(user.info["banner"]),
      "background_image" => image_url(user.info["background"])
lain's avatar
lain committed
49
    }
lain's avatar
lain committed
50
51
52
53
54
55

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

dtluna's avatar
dtluna committed
58
59
60
61
62
63
64
65
66
67
  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
68
  end
Thog's avatar
Thog committed
69
70
71

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