Implement muting, add it to the mastodon API
[akkoma] / lib / pleroma / web / mastodon_api / views / account_view.ex
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
4
5 defmodule Pleroma.Web.MastodonAPI.AccountView do
6 use Pleroma.Web, :view
7
8 alias Pleroma.HTML
9 alias Pleroma.User
10 alias Pleroma.Web.CommonAPI.Utils
11 alias Pleroma.Web.MastodonAPI.AccountView
12 alias Pleroma.Web.MediaProxy
13
14 def render("accounts.json", %{users: users} = opts) do
15 users
16 |> render_many(AccountView, "account.json", opts)
17 |> Enum.filter(&Enum.any?/1)
18 end
19
20 def render("account.json", %{user: user} = opts) do
21 if User.visible_for?(user, opts[:for]),
22 do: do_render("account.json", opts),
23 else: %{}
24 end
25
26 def render("mention.json", %{user: user}) do
27 %{
28 id: to_string(user.id),
29 acct: user.nickname,
30 username: username_from_nickname(user.nickname),
31 url: user.ap_id
32 }
33 end
34
35 def render("relationship.json", %{user: user, target: target}) do
36 follow_activity = Pleroma.Web.ActivityPub.Utils.fetch_latest_follow(user, target)
37
38 requested =
39 if follow_activity do
40 follow_activity.data["state"] == "pending"
41 else
42 false
43 end
44
45 %{
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),
51 muting: false,
52 muting_notifications: false,
53 requested: requested,
54 domain_blocking: false,
55 showing_reblogs: false,
56 endorsed: false
57 }
58 end
59
60 def render("relationships.json", %{user: user, targets: targets}) do
61 render_many(targets, AccountView, "relationship.json", user: user, as: :target)
62 end
63
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"]
69
70 emojis =
71 (user.info.source_data["tag"] || [])
72 |> Enum.filter(fn %{"type" => t} -> t == "Emoji" end)
73 |> Enum.map(fn %{"icon" => %{"url" => url}, "name" => name} ->
74 %{
75 "shortcode" => String.trim(name, ":"),
76 "url" => MediaProxy.url(url),
77 "static_url" => MediaProxy.url(url),
78 "visible_in_picker" => false
79 }
80 end)
81
82 fields =
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)
86
87 bio = HTML.filter_tags(user.bio, User.html_filter_policy(opts[:for]))
88
89 %{
90 id: to_string(user.id),
91 username: username_from_nickname(user.nickname),
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),
96 followers_count: user_info.follower_count,
97 following_count: user_info.following_count,
98 statuses_count: user_info.note_count,
99 note: bio || "",
100 url: user.ap_id,
101 avatar: image,
102 avatar_static: image,
103 header: header,
104 header_static: header,
105 emojis: emojis,
106 fields: fields,
107 bot: bot,
108 source: %{
109 note: "",
110 privacy: user_info.default_scope,
111 sensitive: false
112 },
113
114 # Pleroma extension
115 pleroma: %{
116 confirmation_pending: user_info.confirmation_pending,
117 tags: user.tags,
118 is_moderator: user.info.is_moderator,
119 is_admin: user.info.is_admin
120 }
121 }
122 end
123
124 defp username_from_nickname(string) when is_binary(string) do
125 hd(String.split(string, "@"))
126 end
127
128 defp username_from_nickname(_), do: nil
129 end