account_view.ex 3.74 KB
Newer Older
1
# Pleroma: A lightweight social networking server
kaniini's avatar
kaniini committed
2
# Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>
3 4
# SPDX-License-Identifier: AGPL-3.0-only

lain's avatar
lain committed
5 6
defmodule Pleroma.Web.MastodonAPI.AccountView do
  use Pleroma.Web, :view
Haelwenn's avatar
Haelwenn committed
7

Haelwenn's avatar
Haelwenn committed
8 9
  alias Pleroma.HTML
  alias Pleroma.User
10
  alias Pleroma.Web.CommonAPI.Utils
Haelwenn's avatar
Haelwenn committed
11
  alias Pleroma.Web.MastodonAPI.AccountView
href's avatar
href committed
12
  alias Pleroma.Web.MediaProxy
lain's avatar
lain committed
13

14
  def render("accounts.json", %{users: users} = opts) do
15 16 17
    users
    |> render_many(AccountView, "account.json", opts)
    |> Enum.filter(&Enum.any?/1)
18 19
  end

20
  def render("account.json", %{user: user} = opts) do
21
    if User.visible_for?(user, opts[:for]),
22 23
      do: do_render("account.json", opts),
      else: %{}
24 25
  end

26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
  def render("mention.json", %{user: user}) do
    %{
      id: to_string(user.id),
      acct: user.nickname,
      username: username_from_nickname(user.nickname),
      url: user.ap_id
    }
  end

  def render("relationship.json", %{user: user, target: target}) do
    follow_activity = Pleroma.Web.ActivityPub.Utils.fetch_latest_follow(user, target)

    requested =
      if follow_activity do
        follow_activity.data["state"] == "pending"
      else
        false
      end

    %{
      id: to_string(target.id),
      following: User.following?(user, target),
      followed_by: User.following?(target, user),
      blocking: User.blocks?(user, target),
50
      muting: User.mutes?(user, target),
51 52 53 54 55 56 57
      muting: false,
      muting_notifications: false,
      requested: requested,
      domain_blocking: false,
      showing_reblogs: false,
      endorsed: false
    }
58 59
  end

60 61 62 63 64
  def render("relationships.json", %{user: user, targets: targets}) do
    render_many(targets, AccountView, "relationship.json", user: user, as: :target)
  end

  defp do_render("account.json", %{user: user} = opts) do
href's avatar
href committed
65
    image = User.avatar_url(user) |> MediaProxy.url()
66
    header = User.banner_url(user) |> MediaProxy.url()
lain's avatar
lain committed
67
    user_info = User.user_info(user)
lain's avatar
lain committed
68
    bot = (user.info.source_data["type"] || "Person") in ["Application", "Service"]
lain's avatar
lain committed
69

70
    emojis =
lain's avatar
lain committed
71
      (user.info.source_data["tag"] || [])
72 73 74 75 76 77 78 79 80 81
      |> Enum.filter(fn %{"type" => t} -> t == "Emoji" end)
      |> Enum.map(fn %{"icon" => %{"url" => url}, "name" => name} ->
        %{
          "shortcode" => String.trim(name, ":"),
          "url" => MediaProxy.url(url),
          "static_url" => MediaProxy.url(url),
          "visible_in_picker" => false
        }
      end)

82
    fields =
lain's avatar
lain committed
83
      (user.info.source_data["attachment"] || [])
84 85 86
      |> Enum.filter(fn %{"type" => t} -> t == "PropertyValue" end)
      |> Enum.map(fn fields -> Map.take(fields, ["name", "value"]) end)

87 88
    bio = HTML.filter_tags(user.bio, User.html_filter_policy(opts[:for]))

lain's avatar
lain committed
89
    %{
lain's avatar
lain committed
90
      id: to_string(user.id),
91
      username: username_from_nickname(user.nickname),
lain's avatar
lain committed
92
      acct: user.nickname,
93
      display_name: user.name || user.nickname,
94
      locked: user_info.locked,
95
      created_at: Utils.to_masto_date(user.inserted_at),
lain's avatar
lain committed
96 97 98
      followers_count: user_info.follower_count,
      following_count: user_info.following_count,
      statuses_count: user_info.note_count,
99
      note: bio || "",
lain's avatar
lain committed
100 101 102
      url: user.ap_id,
      avatar: image,
      avatar_static: image,
lain's avatar
lain committed
103
      header: header,
104
      header_static: header,
105
      emojis: emojis,
106
      fields: fields,
107
      bot: bot,
108 109
      source: %{
        note: "",
110
        privacy: user_info.default_scope,
111
        sensitive: false
112
      },
113

Ivan Tashkinov's avatar
Ivan Tashkinov committed
114
      # Pleroma extension
115
      pleroma: %{
116
        confirmation_pending: user_info.confirmation_pending,
117 118 119
        tags: user.tags,
        is_moderator: user.info.is_moderator,
        is_admin: user.info.is_admin
120
      }
lain's avatar
lain committed
121 122
    }
  end
lain's avatar
lain committed
123

124 125 126 127 128
  defp username_from_nickname(string) when is_binary(string) do
    hd(String.split(string, "@"))
  end

  defp username_from_nickname(_), do: nil
lain's avatar
lain committed
129
end