822a6800cd59b6b4b94d1f119dac0d86ace217f2
[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.recipients
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.recipients
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 = if object["inReplyTo"] && object["inReplyTo"] != "" do
77 Activity.get_create_activity_by_object_ap_id(object["inReplyTo"])
78 else
79 nil
80 end
81 reply_to_user = reply_to && User.get_cached_by_ap_id(reply_to.data["actor"])
82
83 emojis = (activity.data["object"]["emoji"] || [])
84 |> Enum.map(fn {name, url} ->
85 name = HtmlSanitizeEx.strip_tags(name)
86 url = HtmlSanitizeEx.strip_tags(url)
87 %{ shortcode: name, url: url, static_url: url }
88 end)
89
90 %{
91 id: to_string(activity.id),
92 uri: object["id"],
93 url: object["external_url"] || object["id"],
94 account: AccountView.render("account.json", %{user: user}),
95 in_reply_to_id: reply_to && reply_to.id,
96 in_reply_to_account_id: reply_to_user && reply_to_user.id,
97 reblog: nil,
98 content: HtmlSanitizeEx.basic_html(object["content"]),
99 created_at: created_at,
100 reblogs_count: announcement_count,
101 favourites_count: like_count,
102 reblogged: !!repeated,
103 favourited: !!favorited,
104 muted: false,
105 sensitive: sensitive,
106 spoiler_text: object["summary"] || "",
107 visibility: get_visibility(object),
108 media_attachments: attachments |> Enum.take(4),
109 mentions: mentions,
110 tags: [], # fix,
111 application: %{
112 name: "Web",
113 website: nil
114 },
115 language: nil,
116 emojis: emojis
117 }
118 end
119
120 def get_visibility(object) do
121 public = "https://www.w3.org/ns/activitystreams#Public"
122 to = object["to"] || []
123 cc = object["cc"] || []
124 cond do
125 public in to -> "public"
126 public in cc -> "unlisted"
127 Enum.any?(to, &(String.contains?(&1, "/followers"))) -> "private"
128 true -> "direct"
129 end
130 end
131
132 def render("attachment.json", %{attachment: attachment}) do
133 [%{"mediaType" => media_type, "href" => href} | _] = attachment["url"]
134
135 type = cond do
136 String.contains?(media_type, "image") -> "image"
137 String.contains?(media_type, "video") -> "video"
138 String.contains?(media_type, "audio") -> "audio"
139 true -> "unknown"
140 end
141
142 << hash_id::signed-32, _rest::binary >> = :crypto.hash(:md5, href)
143
144 %{
145 id: to_string(attachment["id"] || hash_id),
146 url: MediaProxy.url(href),
147 remote_url: href,
148 preview_url: MediaProxy.url(href),
149 text_url: href,
150 type: type
151 }
152 end
153 end