1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Pleroma.Web.MastodonAPI.AccountView do
10 alias Pleroma.Web.CommonAPI.Utils
11 alias Pleroma.Web.MastodonAPI.AccountView
12 alias Pleroma.Web.MediaProxy
14 def render("accounts.json", %{users: users} = opts) do
16 |> render_many(AccountView, "account.json", opts)
17 |> Enum.filter(&Enum.any?/1)
20 def render("account.json", %{user: user} = opts) do
21 if User.visible_for?(user, opts[:for]),
22 do: do_render("account.json", opts),
26 def render("mention.json", %{user: user}) do
28 id: to_string(user.id),
30 username: username_from_nickname(user.nickname),
35 def render("relationship.json", %{user: user, target: target}) do
36 follow_activity = Pleroma.Web.ActivityPub.Utils.fetch_latest_follow(user, target)
40 follow_activity.data["state"] == "pending"
46 id: to_string(target.id),
47 following: User.following?(user, target),
48 followed_by: User.following?(target, user),
49 blocking: User.blocks?(user, target),
50 muting: User.mutes?(user, target),
52 muting_notifications: false,
54 domain_blocking: false,
55 showing_reblogs: false,
60 def render("relationships.json", %{user: user, targets: targets}) do
61 render_many(targets, AccountView, "relationship.json", user: user, as: :target)
64 defp do_render("account.json", %{user: user} = opts) do
65 image = User.avatar_url(user) |> MediaProxy.url()
66 header = User.banner_url(user) |> MediaProxy.url()
67 user_info = User.user_info(user)
68 bot = (user.info.source_data["type"] || "Person") in ["Application", "Service"]
71 (user.info.source_data["tag"] || [])
72 |> Enum.filter(fn %{"type" => t} -> t == "Emoji" end)
73 |> Enum.map(fn %{"icon" => %{"url" => url}, "name" => name} ->
75 "shortcode" => String.trim(name, ":"),
76 "url" => MediaProxy.url(url),
77 "static_url" => MediaProxy.url(url),
78 "visible_in_picker" => false
83 (user.info.source_data["attachment"] || [])
84 |> Enum.filter(fn %{"type" => t} -> t == "PropertyValue" end)
85 |> Enum.map(fn fields -> Map.take(fields, ["name", "value"]) end)
87 bio = HTML.filter_tags(user.bio, User.html_filter_policy(opts[:for]))
90 id: to_string(user.id),
91 username: username_from_nickname(user.nickname),
93 display_name: user.name || user.nickname,
94 locked: user_info.locked,
95 created_at: Utils.to_masto_date(user.inserted_at),
96 followers_count: user_info.follower_count,
97 following_count: user_info.following_count,
98 statuses_count: user_info.note_count,
102 avatar_static: image,
104 header_static: header,
110 privacy: user_info.default_scope,
116 confirmation_pending: user_info.confirmation_pending,
118 is_moderator: user.info.is_moderator,
119 is_admin: user.info.is_admin
124 defp username_from_nickname(string) when is_binary(string) do
125 hd(String.split(string, "@"))
128 defp username_from_nickname(_), do: nil