cbc2dd77072341a01657e4cd0831ea1847ae8b40
[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 alias Pleroma.Repo
8
9 # TODO: Add cached version.
10 defp get_replied_to_activities(activities) do
11 activities
12 |> Enum.map(fn
13 %{data: %{"type" => "Create", "object" => %{"inReplyTo" => inReplyTo}}} ->
14 inReplyTo != "" && inReplyTo
15
16 _ ->
17 nil
18 end)
19 |> Enum.filter(& &1)
20 |> Activity.create_activity_by_object_id_query()
21 |> Repo.all()
22 |> Enum.reduce(%{}, fn activity, acc ->
23 Map.put(acc, activity.data["object"]["id"], activity)
24 end)
25 end
26
27 def render("index.json", opts) do
28 replied_to_activities = get_replied_to_activities(opts.activities)
29
30 render_many(
31 opts.activities,
32 StatusView,
33 "status.json",
34 Map.put(opts, :replied_to_activities, replied_to_activities)
35 )
36 end
37
38 def render(
39 "status.json",
40 %{activity: %{data: %{"type" => "Announce", "object" => object}} = activity} = opts
41 ) do
42 user = User.get_cached_by_ap_id(activity.data["actor"])
43 created_at = Utils.to_masto_date(activity.data["published"])
44
45 reblogged = Activity.get_create_activity_by_object_ap_id(object)
46 reblogged = render("status.json", Map.put(opts, :activity, reblogged))
47
48 mentions =
49 activity.recipients
50 |> Enum.map(fn ap_id -> User.get_cached_by_ap_id(ap_id) end)
51 |> Enum.filter(& &1)
52 |> Enum.map(fn user -> AccountView.render("mention.json", %{user: user}) end)
53
54 %{
55 id: to_string(activity.id),
56 uri: object,
57 # TODO: This might be wrong, check with mastodon.
58 url: nil,
59 account: AccountView.render("account.json", %{user: user}),
60 in_reply_to_id: nil,
61 in_reply_to_account_id: nil,
62 reblog: reblogged,
63 content: reblogged[:content],
64 created_at: created_at,
65 reblogs_count: 0,
66 favourites_count: 0,
67 reblogged: false,
68 favourited: false,
69 muted: false,
70 sensitive: false,
71 spoiler_text: "",
72 visibility: "public",
73 media_attachments: [],
74 mentions: mentions,
75 tags: [],
76 application: %{
77 name: "Web",
78 website: nil
79 },
80 language: nil,
81 emojis: []
82 }
83 end
84
85 def get_reply_to(activity, %{replied_to_activities: replied_to_activities}) do
86 _id = activity.data["object"]["inReplyTo"]
87 replied_to_activities[activity.data["object"]["inReplyTo"]]
88 end
89
90 def get_reply_to(%{data: %{"object" => object}}, _) do
91 if object["inReplyTo"] && object["inReplyTo"] != "" do
92 Activity.get_create_activity_by_object_ap_id(object["inReplyTo"])
93 else
94 nil
95 end
96 end
97
98 def render("status.json", %{activity: %{data: %{"object" => object}} = activity} = opts) do
99 user = User.get_cached_by_ap_id(activity.data["actor"])
100
101 like_count = object["like_count"] || 0
102 announcement_count = object["announcement_count"] || 0
103
104 tags = object["tag"] || []
105 sensitive = object["sensitive"] || Enum.member?(tags, "nsfw")
106
107 mentions =
108 activity.recipients
109 |> Enum.map(fn ap_id -> User.get_cached_by_ap_id(ap_id) end)
110 |> Enum.filter(& &1)
111 |> Enum.map(fn user -> AccountView.render("mention.json", %{user: user}) end)
112
113 repeated = opts[:for] && opts[:for].ap_id in (object["announcements"] || [])
114 favorited = opts[:for] && opts[:for].ap_id in (object["likes"] || [])
115
116 attachments =
117 render_many(object["attachment"] || [], StatusView, "attachment.json", as: :attachment)
118
119 created_at = Utils.to_masto_date(object["published"])
120
121 reply_to = get_reply_to(activity, opts)
122 reply_to_user = reply_to && User.get_cached_by_ap_id(reply_to.data["actor"])
123
124 emojis =
125 (activity.data["object"]["emoji"] || [])
126 |> Enum.map(fn {name, url} ->
127 name = HtmlSanitizeEx.strip_tags(name)
128
129 url =
130 HtmlSanitizeEx.strip_tags(url)
131 |> MediaProxy.url()
132
133 %{shortcode: name, url: url, static_url: url}
134 end)
135
136 %{
137 id: to_string(activity.id),
138 uri: object["id"],
139 url: object["external_url"] || object["id"],
140 account: AccountView.render("account.json", %{user: user}),
141 in_reply_to_id: reply_to && reply_to.id,
142 in_reply_to_account_id: reply_to_user && reply_to_user.id,
143 reblog: nil,
144 content: HtmlSanitizeEx.basic_html(object["content"]),
145 created_at: created_at,
146 reblogs_count: announcement_count,
147 favourites_count: like_count,
148 reblogged: !!repeated,
149 favourited: !!favorited,
150 muted: false,
151 sensitive: sensitive,
152 spoiler_text: object["summary"] || "",
153 visibility: get_visibility(object),
154 media_attachments: attachments |> Enum.take(4),
155 mentions: mentions,
156 # fix,
157 tags: [],
158 application: %{
159 name: "Web",
160 website: nil
161 },
162 language: nil,
163 emojis: emojis
164 }
165 end
166
167 def get_visibility(object) do
168 public = "https://www.w3.org/ns/activitystreams#Public"
169 to = object["to"] || []
170 cc = object["cc"] || []
171
172 cond do
173 public in to -> "public"
174 public in cc -> "unlisted"
175 Enum.any?(to, &String.contains?(&1, "/followers")) -> "private"
176 true -> "direct"
177 end
178 end
179
180 def render("attachment.json", %{attachment: attachment}) do
181 [%{"mediaType" => media_type, "href" => href} | _] = attachment["url"]
182
183 type =
184 cond do
185 String.contains?(media_type, "image") -> "image"
186 String.contains?(media_type, "video") -> "video"
187 String.contains?(media_type, "audio") -> "audio"
188 true -> "unknown"
189 end
190
191 <<hash_id::signed-32, _rest::binary>> = :crypto.hash(:md5, href)
192
193 %{
194 id: to_string(attachment["id"] || hash_id),
195 url: MediaProxy.url(href),
196 remote_url: href,
197 preview_url: MediaProxy.url(href),
198 text_url: href,
199 type: type
200 }
201 end
202 end