Return visilility in twitter api, too.
[akkoma] / lib / pleroma / web / twitter_api / views / activity_view.ex
1 defmodule Pleroma.Web.TwitterAPI.ActivityView do
2 use Pleroma.Web, :view
3 alias Pleroma.Web.CommonAPI.Utils
4 alias Pleroma.User
5 alias Pleroma.Web.TwitterAPI.UserView
6 alias Pleroma.Web.TwitterAPI.ActivityView
7 alias Pleroma.Web.TwitterAPI.TwitterAPI
8 alias Pleroma.Web.TwitterAPI.Representers.ObjectRepresenter
9 alias Pleroma.Activity
10 alias Pleroma.Object
11 alias Pleroma.User
12 alias Pleroma.Repo
13 alias Pleroma.Formatter
14
15 import Ecto.Query
16
17 defp query_context_ids([]), do: []
18
19 defp query_context_ids(contexts) do
20 query = from(o in Object, where: fragment("(?)->>'id' = ANY(?)", o.data, ^contexts))
21
22 Repo.all(query)
23 end
24
25 defp query_users([]), do: []
26
27 defp query_users(user_ids) do
28 query = from(user in User, where: user.ap_id in ^user_ids)
29
30 Repo.all(query)
31 end
32
33 defp collect_context_ids(activities) do
34 _contexts =
35 activities
36 |> Enum.reject(& &1.data["context_id"])
37 |> Enum.map(fn %{data: data} ->
38 data["context"]
39 end)
40 |> Enum.filter(& &1)
41 |> query_context_ids()
42 |> Enum.reduce(%{}, fn %{data: %{"id" => ap_id}, id: id}, acc ->
43 Map.put(acc, ap_id, id)
44 end)
45 end
46
47 defp collect_users(activities) do
48 activities
49 |> Enum.map(fn activity ->
50 case activity.data do
51 data = %{"type" => "Follow"} ->
52 [data["actor"], data["object"]]
53
54 data ->
55 [data["actor"]]
56 end ++ activity.recipients
57 end)
58 |> List.flatten()
59 |> Enum.uniq()
60 |> query_users()
61 |> Enum.reduce(%{}, fn user, acc ->
62 Map.put(acc, user.ap_id, user)
63 end)
64 end
65
66 defp get_context_id(%{data: %{"context_id" => context_id}}, _) when not is_nil(context_id),
67 do: context_id
68
69 defp get_context_id(%{data: %{"context" => nil}}, _), do: nil
70
71 defp get_context_id(%{data: %{"context" => context}}, options) do
72 cond do
73 id = options[:context_ids][context] -> id
74 true -> TwitterAPI.context_to_conversation_id(context)
75 end
76 end
77
78 defp get_context_id(_, _), do: nil
79
80 defp get_user(ap_id, opts) do
81 cond do
82 user = opts[:users][ap_id] ->
83 user
84
85 String.ends_with?(ap_id, "/followers") ->
86 nil
87
88 ap_id == "https://www.w3.org/ns/activitystreams#Public" ->
89 nil
90
91 true ->
92 User.get_cached_by_ap_id(ap_id)
93 end
94 end
95
96 def render("index.json", opts) do
97 context_ids = collect_context_ids(opts.activities)
98 users = collect_users(opts.activities)
99
100 opts =
101 opts
102 |> Map.put(:context_ids, context_ids)
103 |> Map.put(:users, users)
104
105 render_many(
106 opts.activities,
107 ActivityView,
108 "activity.json",
109 opts
110 )
111 end
112
113 def render("activity.json", %{activity: %{data: %{"type" => "Delete"}} = activity} = opts) do
114 user = get_user(activity.data["actor"], opts)
115 created_at = activity.data["published"] |> Utils.date_to_asctime()
116
117 %{
118 "id" => activity.id,
119 "uri" => activity.data["object"],
120 "user" => UserView.render("show.json", %{user: user, for: opts[:for]}),
121 "attentions" => [],
122 "statusnet_html" => "deleted notice {{tag",
123 "text" => "deleted notice {{tag",
124 "is_local" => activity.local,
125 "is_post_verb" => false,
126 "created_at" => created_at,
127 "in_reply_to_status_id" => nil,
128 "external_url" => activity.data["id"],
129 "activity_type" => "delete"
130 }
131 end
132
133 def render("activity.json", %{activity: %{data: %{"type" => "Follow"}} = activity} = opts) do
134 user = get_user(activity.data["actor"], opts)
135 created_at = activity.data["published"] || DateTime.to_iso8601(activity.inserted_at)
136 created_at = created_at |> Utils.date_to_asctime()
137
138 followed = get_user(activity.data["object"], opts)
139 text = "#{user.nickname} started following #{followed.nickname}"
140
141 %{
142 "id" => activity.id,
143 "user" => UserView.render("show.json", %{user: user, for: opts[:for]}),
144 "attentions" => [],
145 "statusnet_html" => text,
146 "text" => text,
147 "is_local" => activity.local,
148 "is_post_verb" => false,
149 "created_at" => created_at,
150 "in_reply_to_status_id" => nil,
151 "external_url" => activity.data["id"],
152 "activity_type" => "follow"
153 }
154 end
155
156 def render("activity.json", %{activity: %{data: %{"type" => "Announce"}} = activity} = opts) do
157 user = get_user(activity.data["actor"], opts)
158 created_at = activity.data["published"] |> Utils.date_to_asctime()
159 announced_activity = Activity.get_create_activity_by_object_ap_id(activity.data["object"])
160
161 text = "#{user.nickname} retweeted a status."
162
163 retweeted_status = render("activity.json", Map.merge(opts, %{activity: announced_activity}))
164
165 %{
166 "id" => activity.id,
167 "user" => UserView.render("show.json", %{user: user, for: opts[:for]}),
168 "statusnet_html" => text,
169 "text" => text,
170 "is_local" => activity.local,
171 "is_post_verb" => false,
172 "uri" => "tag:#{activity.data["id"]}:objectType=note",
173 "created_at" => created_at,
174 "retweeted_status" => retweeted_status,
175 "statusnet_conversation_id" => get_context_id(announced_activity, opts),
176 "external_url" => activity.data["id"],
177 "activity_type" => "repeat"
178 }
179 end
180
181 def render("activity.json", %{activity: %{data: %{"type" => "Like"}} = activity} = opts) do
182 user = get_user(activity.data["actor"], opts)
183 liked_activity = Activity.get_create_activity_by_object_ap_id(activity.data["object"])
184
185 created_at =
186 activity.data["published"]
187 |> Utils.date_to_asctime()
188
189 text = "#{user.nickname} favorited a status."
190
191 %{
192 "id" => activity.id,
193 "user" => UserView.render("show.json", %{user: user, for: opts[:for]}),
194 "statusnet_html" => text,
195 "text" => text,
196 "is_local" => activity.local,
197 "is_post_verb" => false,
198 "uri" => "tag:#{activity.data["id"]}:objectType=Favourite",
199 "created_at" => created_at,
200 "in_reply_to_status_id" => liked_activity.id,
201 "external_url" => activity.data["id"],
202 "activity_type" => "like"
203 }
204 end
205
206 def render(
207 "activity.json",
208 %{activity: %{data: %{"type" => "Create", "object" => object}} = activity} = opts
209 ) do
210 user = get_user(activity.data["actor"], opts)
211
212 created_at = object["published"] |> Utils.date_to_asctime()
213 like_count = object["like_count"] || 0
214 announcement_count = object["announcement_count"] || 0
215 favorited = opts[:for] && opts[:for].ap_id in (object["likes"] || [])
216 repeated = opts[:for] && opts[:for].ap_id in (object["announcements"] || [])
217
218 attentions =
219 activity.recipients
220 |> Enum.map(fn ap_id -> get_user(ap_id, opts) end)
221 |> Enum.filter(& &1)
222 |> Enum.map(fn user -> UserView.render("show.json", %{user: user, for: opts[:for]}) end)
223
224 conversation_id = get_context_id(activity, opts)
225
226 tags = activity.data["object"]["tag"] || []
227 possibly_sensitive = activity.data["object"]["sensitive"] || Enum.member?(tags, "nsfw")
228
229 tags = if possibly_sensitive, do: Enum.uniq(["nsfw" | tags]), else: tags
230
231 summary = activity.data["object"]["summary"]
232 content = object["content"]
233
234 content =
235 if !!summary and summary != "" do
236 "<span>#{activity.data["object"]["summary"]}</span><br />#{content}</span>"
237 else
238 content
239 end
240
241 html =
242 HtmlSanitizeEx.basic_html(content)
243 |> Formatter.emojify(object["emoji"])
244
245 %{
246 "id" => activity.id,
247 "uri" => activity.data["object"]["id"],
248 "user" => UserView.render("show.json", %{user: user, for: opts[:for]}),
249 "statusnet_html" => html,
250 "text" => HtmlSanitizeEx.strip_tags(content),
251 "is_local" => activity.local,
252 "is_post_verb" => true,
253 "created_at" => created_at,
254 "in_reply_to_status_id" => object["inReplyToStatusId"],
255 "statusnet_conversation_id" => conversation_id,
256 "attachments" => (object["attachment"] || []) |> ObjectRepresenter.enum_to_list(opts),
257 "attentions" => attentions,
258 "fave_num" => like_count,
259 "repeat_num" => announcement_count,
260 "favorited" => !!favorited,
261 "repeated" => !!repeated,
262 "external_url" => object["external_url"] || object["id"],
263 "tags" => tags,
264 "activity_type" => "post",
265 "possibly_sensitive" => possibly_sensitive,
266 "visibility" => Pleroma.Web.MastodonAPI.StatusView.get_visibility(object)
267 }
268 end
269 end