Merge branch 'develop' into oauth2
[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
5
6 def render("index.json", opts) do
7 render_many(opts.activities, StatusView, "status.json", opts)
8 end
9
10 def render("status.json", %{activity: %{data: %{"object" => object}} = activity} = opts) do
11 user = User.get_cached_by_ap_id(activity.data["actor"])
12
13 like_count = object["like_count"] || 0
14 announcement_count = object["announcement_count"] || 0
15
16 tags = object["tag"] || []
17 sensitive = Enum.member?(tags, "nsfw")
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 repeated = opts[:for] && opts[:for].ap_id in (object["announcements"] || [])
25 favorited = opts[:for] && opts[:for].ap_id in (object["likes"] || [])
26
27 attachments = render_many(object["attachment"] || [], StatusView, "attachment.json", as: :attachment)
28
29 %{
30 id: activity.id,
31 uri: object["id"],
32 url: object["external_url"],
33 account: AccountView.render("account.json", %{user: user}),
34 in_reply_to_id: object["inReplyToStatusId"],
35 in_reply_to_account_id: nil,
36 reblog: nil,
37 content: HtmlSanitizeEx.basic_html(object["content"]),
38 created_at: object["published"],
39 reblogs_count: announcement_count,
40 favourites_count: like_count,
41 reblogged: !!repeated,
42 favourited: !!favorited,
43 muted: false,
44 sensitive: sensitive,
45 spoiler_text: "",
46 visibility: "public",
47 media_attachments: attachments,
48 mentions: mentions,
49 tags: [], # fix,
50 application: nil,
51 language: nil
52 }
53 end
54
55 def render("attachment.json", %{attachment: attachment}) do
56 [%{"mediaType" => media_type, "href" => href} | _] = attachment["url"]
57
58 type = cond do
59 String.contains?(media_type, "image") -> "image"
60 String.contains?(media_type, "video") -> "video"
61 true -> "unknown"
62 end
63
64 %{
65 id: attachment["uuid"],
66 url: href,
67 remote_url: href,
68 preview_url: href,
69 type: type
70 }
71 end
72 end