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