Clarify that this error is about a problem decoding the expected JSON
[akkoma] / lib / pleroma / web / mastodon_api / views / status_view.ex
1 defmodule Pleroma.Web.MastodonAPI.StatusView do
2 use Pleroma.Web, :view
3 alias Pleroma.Web.MastodonAPI.{AccountView, StatusView}
4 alias Pleroma.{User, Activity}
5 alias Pleroma.Web.CommonAPI.Utils
6 alias Pleroma.Web.MediaProxy
7
8 def render("index.json", opts) do
9 render_many(opts.activities, StatusView, "status.json", opts)
10 end
11
12 def render("status.json", %{activity: %{data: %{"type" => "Announce", "object" => object}} = activity} = opts) do
13 user = User.get_cached_by_ap_id(activity.data["actor"])
14 created_at = Utils.to_masto_date(activity.data["published"])
15
16 reblogged = Activity.get_create_activity_by_object_ap_id(object)
17 reblogged = render("status.json", Map.put(opts, :activity, reblogged))
18
19 mentions = activity.data["to"]
20 |> Enum.map(fn (ap_id) -> User.get_cached_by_ap_id(ap_id) end)
21 |> Enum.filter(&(&1))
22 |> Enum.map(fn (user) -> AccountView.render("mention.json", %{user: user}) end)
23
24 %{
25 id: to_string(activity.id),
26 uri: object,
27 url: nil, # TODO: This might be wrong, check with mastodon.
28 account: AccountView.render("account.json", %{user: user}),
29 in_reply_to_id: nil,
30 in_reply_to_account_id: nil,
31 reblog: reblogged,
32 content: reblogged[:content],
33 created_at: created_at,
34 reblogs_count: 0,
35 favourites_count: 0,
36 reblogged: false,
37 favourited: false,
38 muted: false,
39 sensitive: false,
40 spoiler_text: "",
41 visibility: "public",
42 media_attachments: [],
43 mentions: mentions,
44 tags: [],
45 application: %{
46 name: "Web",
47 website: nil
48 },
49 language: nil,
50 emojis: []
51 }
52 end
53
54 def render("status.json", %{activity: %{data: %{"object" => object}} = activity} = opts) do
55 user = User.get_cached_by_ap_id(activity.data["actor"])
56
57 like_count = object["like_count"] || 0
58 announcement_count = object["announcement_count"] || 0
59
60 tags = object["tag"] || []
61 sensitive = object["sensitive"] || Enum.member?(tags, "nsfw")
62
63 mentions = activity.data["to"]
64 |> Enum.map(fn (ap_id) -> User.get_cached_by_ap_id(ap_id) end)
65 |> Enum.filter(&(&1))
66 |> Enum.map(fn (user) -> AccountView.render("mention.json", %{user: user}) end)
67
68 repeated = opts[:for] && opts[:for].ap_id in (object["announcements"] || [])
69 favorited = opts[:for] && opts[:for].ap_id in (object["likes"] || [])
70
71 attachments = render_many(object["attachment"] || [], StatusView, "attachment.json", as: :attachment)
72
73 created_at = Utils.to_masto_date(object["published"])
74
75 # TODO: Add cached version.
76 reply_to = Activity.get_create_activity_by_object_ap_id(object["inReplyTo"])
77 reply_to_user = reply_to && User.get_cached_by_ap_id(reply_to.data["actor"])
78
79 emojis = (activity.data["object"]["emoji"] || [])
80 |> Enum.map(fn {name, url} -> %{ shortcode: name, url: url, static_url: url } end)
81
82 %{
83 id: to_string(activity.id),
84 uri: object["id"],
85 url: object["external_url"] || object["id"],
86 account: AccountView.render("account.json", %{user: user}),
87 in_reply_to_id: reply_to && reply_to.id,
88 in_reply_to_account_id: reply_to_user && reply_to_user.id,
89 reblog: nil,
90 content: HtmlSanitizeEx.basic_html(object["content"]),
91 created_at: created_at,
92 reblogs_count: announcement_count,
93 favourites_count: like_count,
94 reblogged: !!repeated,
95 favourited: !!favorited,
96 muted: false,
97 sensitive: sensitive,
98 spoiler_text: object["summary"] || "",
99 visibility: get_visibility(object),
100 media_attachments: attachments |> Enum.take(4),
101 mentions: mentions,
102 tags: [], # fix,
103 application: %{
104 name: "Web",
105 website: nil
106 },
107 language: nil,
108 emojis: emojis
109 }
110 end
111
112 def get_visibility(object) do
113 public = "https://www.w3.org/ns/activitystreams#Public"
114 to = object["to"] || []
115 cc = object["cc"] || []
116 cond do
117 public in to -> "public"
118 public in cc -> "unlisted"
119 Enum.any?(to, &(String.contains?(&1, "/followers"))) -> "private"
120 true -> "direct"
121 end
122 end
123
124 def render("attachment.json", %{attachment: attachment}) do
125 [%{"mediaType" => media_type, "href" => href} | _] = attachment["url"]
126
127 type = cond do
128 String.contains?(media_type, "image") -> "image"
129 String.contains?(media_type, "video") -> "video"
130 true -> "unknown"
131 end
132
133 << hash_id::signed-32, _rest::binary >> = :crypto.hash(:md5, href)
134
135 %{
136 id: to_string(attachment["id"] || hash_id),
137 url: MediaProxy.url(href),
138 remote_url: href,
139 preview_url: MediaProxy.url(href),
140 text_url: href,
141 type: type
142 }
143 end
144 end