Merge branch 'mastodon-notification-endpoints' into 'develop'
[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
7 def render("index.json", opts) do
8 render_many(opts.activities, StatusView, "status.json", opts)
9 end
10
11 def render("status.json", %{activity: %{data: %{"type" => "Announce", "object" => object}} = activity} = opts) do
12 user = User.get_cached_by_ap_id(activity.data["actor"])
13 created_at = Utils.to_masto_date(activity.data["published"])
14
15 reblogged = Activity.get_create_activity_by_object_ap_id(object)
16 reblogged = render("status.json", Map.put(opts, :activity, reblogged))
17
18 mentions = activity.data["to"]
19 |> Enum.map(fn (ap_id) -> User.get_cached_by_ap_id(ap_id) end)
20 |> Enum.filter(&(&1))
21 |> Enum.map(fn (user) -> AccountView.render("mention.json", %{user: user}) end)
22
23 %{
24 id: to_string(activity.id),
25 uri: object,
26 url: nil, # TODO: This might be wrong, check with mastodon.
27 account: AccountView.render("account.json", %{user: user}),
28 in_reply_to_id: nil,
29 in_reply_to_account_id: nil,
30 reblog: reblogged,
31 content: reblogged[:content],
32 created_at: created_at,
33 reblogs_count: 0,
34 favourites_count: 0,
35 reblogged: false,
36 favourited: false,
37 muted: false,
38 sensitive: false,
39 spoiler_text: "",
40 visibility: "public",
41 media_attachments: [],
42 mentions: mentions,
43 tags: [],
44 application: %{
45 name: "Web",
46 website: nil
47 },
48 language: nil,
49 emojis: []
50 }
51 end
52
53 def render("status.json", %{activity: %{data: %{"object" => object}} = activity} = opts) do
54 user = User.get_cached_by_ap_id(activity.data["actor"])
55
56 like_count = object["like_count"] || 0
57 announcement_count = object["announcement_count"] || 0
58
59 tags = object["tag"] || []
60 sensitive = Enum.member?(tags, "nsfw")
61
62 mentions = activity.data["to"]
63 |> Enum.map(fn (ap_id) -> User.get_cached_by_ap_id(ap_id) end)
64 |> Enum.filter(&(&1))
65 |> Enum.map(fn (user) -> AccountView.render("mention.json", %{user: user}) end)
66
67 repeated = opts[:for] && opts[:for].ap_id in (object["announcements"] || [])
68 favorited = opts[:for] && opts[:for].ap_id in (object["likes"] || [])
69
70 attachments = render_many(object["attachment"] || [], StatusView, "attachment.json", as: :attachment)
71
72 created_at = Utils.to_masto_date(object["published"])
73
74 # TODO: Add cached version.
75 reply_to = Activity.get_create_activity_by_object_ap_id(object["inReplyTo"])
76 reply_to_user = reply_to && User.get_cached_by_ap_id(reply_to.data["actor"])
77
78 emojis = (activity.data["object"]["emoji"] || [])
79 |> Enum.map(fn {name, url} -> %{ shortcode: name, url: url, static_url: url } end)
80
81 %{
82 id: to_string(activity.id),
83 uri: object["id"],
84 url: object["external_url"] || object["id"],
85 account: AccountView.render("account.json", %{user: user}),
86 in_reply_to_id: reply_to && reply_to.id,
87 in_reply_to_account_id: reply_to_user && reply_to_user.id,
88 reblog: nil,
89 content: HtmlSanitizeEx.basic_html(object["content"]),
90 created_at: created_at,
91 reblogs_count: announcement_count,
92 favourites_count: like_count,
93 reblogged: !!repeated,
94 favourited: !!favorited,
95 muted: false,
96 sensitive: sensitive,
97 spoiler_text: object["summary"] || "",
98 visibility: "public",
99 media_attachments: attachments,
100 mentions: mentions,
101 tags: [], # fix,
102 application: %{
103 name: "Web",
104 website: nil
105 },
106 language: nil,
107 emojis: emojis
108 }
109 end
110
111 def render("attachment.json", %{attachment: attachment}) do
112 [%{"mediaType" => media_type, "href" => href} | _] = attachment["url"]
113
114 type = cond do
115 String.contains?(media_type, "image") -> "image"
116 String.contains?(media_type, "video") -> "video"
117 true -> "unknown"
118 end
119
120 << hash_id::signed-32, _rest::binary >> = :crypto.hash(:md5, href)
121
122 %{
123 id: attachment["id"] || hash_id,
124 url: href,
125 remote_url: href,
126 preview_url: href,
127 text_url: href,
128 type: type
129 }
130 end
131 end