Format the code.
[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 url = HtmlSanitizeEx.strip_tags(url)
129 %{shortcode: name, url: url, static_url: url}
130 end)
131
132 %{
133 id: to_string(activity.id),
134 uri: object["id"],
135 url: object["external_url"] || object["id"],
136 account: AccountView.render("account.json", %{user: user}),
137 in_reply_to_id: reply_to && reply_to.id,
138 in_reply_to_account_id: reply_to_user && reply_to_user.id,
139 reblog: nil,
140 content: HtmlSanitizeEx.basic_html(object["content"]),
141 created_at: created_at,
142 reblogs_count: announcement_count,
143 favourites_count: like_count,
144 reblogged: !!repeated,
145 favourited: !!favorited,
146 muted: false,
147 sensitive: sensitive,
148 spoiler_text: object["summary"] || "",
149 visibility: get_visibility(object),
150 media_attachments: attachments |> Enum.take(4),
151 mentions: mentions,
152 # fix,
153 tags: [],
154 application: %{
155 name: "Web",
156 website: nil
157 },
158 language: nil,
159 emojis: emojis
160 }
161 end
162
163 def get_visibility(object) do
164 public = "https://www.w3.org/ns/activitystreams#Public"
165 to = object["to"] || []
166 cc = object["cc"] || []
167
168 cond do
169 public in to -> "public"
170 public in cc -> "unlisted"
171 Enum.any?(to, &String.contains?(&1, "/followers")) -> "private"
172 true -> "direct"
173 end
174 end
175
176 def render("attachment.json", %{attachment: attachment}) do
177 [%{"mediaType" => media_type, "href" => href} | _] = attachment["url"]
178
179 type =
180 cond do
181 String.contains?(media_type, "image") -> "image"
182 String.contains?(media_type, "video") -> "video"
183 String.contains?(media_type, "audio") -> "audio"
184 true -> "unknown"
185 end
186
187 <<hash_id::signed-32, _rest::binary>> = :crypto.hash(:md5, href)
188
189 %{
190 id: to_string(attachment["id"] || hash_id),
191 url: MediaProxy.url(href),
192 remote_url: href,
193 preview_url: MediaProxy.url(href),
194 text_url: href,
195 type: type
196 }
197 end
198 end