user_view.ex 3.57 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.Formatter
5
  alias Pleroma.Web.CommonAPI.Utils
href's avatar
href committed
6
  alias Pleroma.Web.MediaProxy
7
  alias Pleroma.HTML
8

dtluna's avatar
dtluna committed
9
  def render("show.json", %{user: user = %User{}} = assigns) do
lain's avatar
lain committed
10 11 12 13 14 15 16 17
    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
18
    image = User.avatar_url(user) |> MediaProxy.url()
lain's avatar
lain committed
19 20 21 22 23 24 25 26 27 28 29

    {following, follows_you, statusnet_blocking} =
      if assigns[:for] do
        {
          User.following?(assigns[:for], user),
          User.following?(user, assigns[:for]),
          User.blocks?(assigns[:for], user)
        }
      else
        {false, false, false}
      end
30

lain's avatar
lain committed
31
    user_info = User.get_cached_user_info(user)
lain's avatar
lain committed
32

33
    emoji =
lain's avatar
lain committed
34
      (user.info.source_data["tag"] || [])
35 36 37 38 39
      |> Enum.filter(fn %{"type" => t} -> t == "Emoji" end)
      |> Enum.map(fn %{"icon" => %{"url" => url}, "name" => name} ->
        {String.trim(name, ":"), url}
      end)

40 41 42
    # ``fields`` is an array of mastodon profile field, containing ``{"name": "…", "value": "…"}``.
    # For example: [{"name": "Pronoun", "value": "she/her"}, …]
    fields =
lain's avatar
lain committed
43
      (user.info.source_data["attachment"] || [])
44 45 46
      |> Enum.filter(fn %{"type" => t} -> t == "PropertyValue" end)
      |> Enum.map(fn fields -> Map.take(fields, ["name", "value"]) end)

lain's avatar
lain committed
47
    data = %{
lain's avatar
lain committed
48
      "created_at" => user.inserted_at |> Utils.format_naive_asctime(),
kaniini's avatar
kaniini committed
49
      "description" => HTML.strip_tags((user.bio || "") |> String.replace("<br>", "\n")),
50
      "description_html" => HTML.filter_tags(user.bio, User.html_filter_policy(assigns[:for])),
lain's avatar
lain committed
51
      "favourites_count" => 0,
lain's avatar
lain committed
52
      "followers_count" => user_info[:follower_count],
dtluna's avatar
dtluna committed
53
      "following" => following,
eal's avatar
eal committed
54
      "follows_you" => follows_you,
eal's avatar
eal committed
55
      "statusnet_blocking" => statusnet_blocking,
dtluna's avatar
dtluna committed
56 57
      "friends_count" => user_info[:following_count],
      "id" => user.id,
58 59 60 61 62 63
      "name" => user.name || user.nickname,
      "name_html" =>
        if(user.name,
          do: HTML.strip_tags(user.name) |> Formatter.emojify(emoji),
          else: user.nickname
        ),
lain's avatar
lain committed
64 65 66 67
      "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
68
      "rights" => %{
lain's avatar
lain committed
69
        "delete_others_notice" => !!user.info.is_moderator
lain's avatar
lain committed
70
      },
dtluna's avatar
dtluna committed
71 72
      "screen_name" => user.nickname,
      "statuses_count" => user_info[:note_count],
lain's avatar
lain committed
73
      "statusnet_profile_url" => user.ap_id,
74
      "cover_photo" => User.banner_url(user) |> MediaProxy.url(),
lain's avatar
lain committed
75
      "background_image" => image_url(user.info.background) |> MediaProxy.url(),
76
      "is_local" => user.local,
lain's avatar
lain committed
77 78 79
      "locked" => user.info.locked,
      "default_scope" => user.info.default_scope,
      "no_rich_text" => user.info.no_rich_text,
80 81 82
      "fields" => fields,
      # Note: twitter.com does not return this field:
      "tags" => user.tags
lain's avatar
lain committed
83
    }
lain's avatar
lain committed
84 85 86 87 88 89

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

lain's avatar
lain committed
92 93 94 95 96 97 98 99
  def render("short.json", %{
        user: %User{
          nickname: nickname,
          id: id,
          ap_id: ap_id,
          name: name
        }
      }) do
dtluna's avatar
dtluna committed
100 101 102 103 104 105 106
    %{
      "fullname" => name,
      "id" => id,
      "ostatus_uri" => ap_id,
      "profile_url" => ap_id,
      "screen_name" => nickname
    }
lain's avatar
lain committed
107
  end
Thog's avatar
Thog committed
108

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