6cf8682b882033147c34d844e4c3d69c3f23b2a4
[akkoma] / lib / pleroma / web / twitter_api / twitter_api_controller.ex
1 defmodule Pleroma.Web.TwitterAPI.Controller do
2 use Pleroma.Web, :controller
3 alias Pleroma.Web.TwitterAPI.{TwitterAPI, UserView, ActivityView, NotificationView}
4 alias Pleroma.Web.CommonAPI
5 alias Pleroma.{Repo, Activity, User, Notification}
6 alias Pleroma.Web.ActivityPub.ActivityPub
7 alias Ecto.Changeset
8
9 require Logger
10
11 def verify_credentials(%{assigns: %{user: user}} = conn, _params) do
12 token = Phoenix.Token.sign(conn, "user socket", user.id)
13 render(conn, UserView, "show.json", %{user: user, token: token})
14 end
15
16 def status_update(%{assigns: %{user: user}} = conn, %{"status" => _} = status_data) do
17 with media_ids <- extract_media_ids(status_data),
18 {:ok, activity} <-
19 TwitterAPI.create_status(user, Map.put(status_data, "media_ids", media_ids)) do
20 conn
21 |> json(ActivityView.render("activity.json", activity: activity, for: user))
22 else
23 _ -> empty_status_reply(conn)
24 end
25 end
26
27 def status_update(conn, _status_data) do
28 empty_status_reply(conn)
29 end
30
31 defp empty_status_reply(conn) do
32 bad_request_reply(conn, "Client must provide a 'status' parameter with a value.")
33 end
34
35 defp extract_media_ids(status_data) do
36 with media_ids when not is_nil(media_ids) <- status_data["media_ids"],
37 split_ids <- String.split(media_ids, ","),
38 clean_ids <- Enum.reject(split_ids, fn id -> String.length(id) == 0 end) do
39 clean_ids
40 else
41 _e -> []
42 end
43 end
44
45 def public_and_external_timeline(%{assigns: %{user: user}} = conn, params) do
46 params =
47 params
48 |> Map.put("type", ["Create", "Announce"])
49 |> Map.put("blocking_user", user)
50
51 activities = ActivityPub.fetch_public_activities(params)
52
53 conn
54 |> render(ActivityView, "index.json", %{activities: activities, for: user})
55 end
56
57 def public_timeline(%{assigns: %{user: user}} = conn, params) do
58 params =
59 params
60 |> Map.put("type", ["Create", "Announce"])
61 |> Map.put("local_only", true)
62 |> Map.put("blocking_user", user)
63
64 activities = ActivityPub.fetch_public_activities(params)
65
66 conn
67 |> render(ActivityView, "index.json", %{activities: activities, for: user})
68 end
69
70 def friends_timeline(%{assigns: %{user: user}} = conn, params) do
71 params =
72 params
73 |> Map.put("type", ["Create", "Announce", "Follow", "Like"])
74 |> Map.put("blocking_user", user)
75 |> Map.put("user", user)
76
77 activities = ActivityPub.fetch_activities([user.ap_id | user.following], params)
78
79 conn
80 |> render(ActivityView, "index.json", %{activities: activities, for: user})
81 end
82
83 def show_user(conn, params) do
84 with {:ok, shown} <- TwitterAPI.get_user(params) do
85 if user = conn.assigns.user do
86 render(conn, UserView, "show.json", %{user: shown, for: user})
87 else
88 render(conn, UserView, "show.json", %{user: shown})
89 end
90 else
91 {:error, msg} ->
92 bad_request_reply(conn, msg)
93 end
94 end
95
96 def user_timeline(%{assigns: %{user: user}} = conn, params) do
97 case TwitterAPI.get_user(user, params) do
98 {:ok, target_user} ->
99 params =
100 params
101 |> Map.put("type", ["Create", "Announce"])
102 |> Map.put("actor_id", target_user.ap_id)
103 |> Map.put("whole_db", true)
104
105 activities = ActivityPub.fetch_public_activities(params)
106
107 conn
108 |> render(ActivityView, "index.json", %{activities: activities, for: user})
109
110 {:error, msg} ->
111 bad_request_reply(conn, msg)
112 end
113 end
114
115 def mentions_timeline(%{assigns: %{user: user}} = conn, params) do
116 activities = ActivityPub.fetch_activities([user.ap_id], params)
117
118 conn
119 |> render(ActivityView, "index.json", %{activities: activities, for: user})
120 end
121
122 def notifications(%{assigns: %{user: user}} = conn, params) do
123 notifications = Notification.for_user(user, params)
124
125 conn
126 |> render(NotificationView, "notification.json", %{notifications: notifications, for: user})
127 end
128
129 def follow(%{assigns: %{user: user}} = conn, params) do
130 case TwitterAPI.follow(user, params) do
131 {:ok, user, followed, _activity} ->
132 render(conn, UserView, "show.json", %{user: followed, for: user})
133
134 {:error, msg} ->
135 forbidden_json_reply(conn, msg)
136 end
137 end
138
139 def block(%{assigns: %{user: user}} = conn, params) do
140 case TwitterAPI.block(user, params) do
141 {:ok, user, blocked} ->
142 render(conn, UserView, "show.json", %{user: blocked, for: user})
143
144 {:error, msg} ->
145 forbidden_json_reply(conn, msg)
146 end
147 end
148
149 def unblock(%{assigns: %{user: user}} = conn, params) do
150 case TwitterAPI.unblock(user, params) do
151 {:ok, user, blocked} ->
152 render(conn, UserView, "show.json", %{user: blocked, for: user})
153
154 {:error, msg} ->
155 forbidden_json_reply(conn, msg)
156 end
157 end
158
159 def delete_post(%{assigns: %{user: user}} = conn, %{"id" => id}) do
160 with {:ok, delete} <- CommonAPI.delete(id, user) do
161 render(conn, ActivityView, "activity.json", %{activity: delete, for: user})
162 end
163 end
164
165 def unfollow(%{assigns: %{user: user}} = conn, params) do
166 case TwitterAPI.unfollow(user, params) do
167 {:ok, user, unfollowed} ->
168 render(conn, UserView, "show.json", %{user: unfollowed, for: user})
169
170 {:error, msg} ->
171 forbidden_json_reply(conn, msg)
172 end
173 end
174
175 def fetch_status(%{assigns: %{user: user}} = conn, %{"id" => id}) do
176 with %Activity{} = activity <- Repo.get(Activity, id),
177 true <- ActivityPub.visible_for_user?(activity, user) do
178 render(conn, ActivityView, "activity.json", %{activity: activity, for: user})
179 end
180 end
181
182 def fetch_conversation(%{assigns: %{user: user}} = conn, %{"id" => id}) do
183 id = String.to_integer(id)
184
185 with context when is_binary(context) <- TwitterAPI.conversation_id_to_context(id),
186 activities <-
187 ActivityPub.fetch_activities_for_context(context, %{
188 "blocking_user" => user,
189 "user" => user
190 }) do
191 conn
192 |> render(ActivityView, "index.json", %{activities: activities, for: user})
193 end
194 end
195
196 def upload(conn, %{"media" => media}) do
197 response = TwitterAPI.upload(media)
198
199 conn
200 |> put_resp_content_type("application/atom+xml")
201 |> send_resp(200, response)
202 end
203
204 def upload_json(conn, %{"media" => media}) do
205 response = TwitterAPI.upload(media, "json")
206
207 conn
208 |> json_reply(200, response)
209 end
210
211 def get_by_id_or_ap_id(id) do
212 activity = Repo.get(Activity, id) || Activity.get_create_activity_by_object_ap_id(id)
213
214 if activity.data["type"] == "Create" do
215 activity
216 else
217 Activity.get_create_activity_by_object_ap_id(activity.data["object"])
218 end
219 end
220
221 def favorite(%{assigns: %{user: user}} = conn, %{"id" => id}) do
222 with {:ok, activity} <- TwitterAPI.fav(user, id) do
223 render(conn, ActivityView, "activity.json", %{activity: activity, for: user})
224 end
225 end
226
227 def unfavorite(%{assigns: %{user: user}} = conn, %{"id" => id}) do
228 with {:ok, activity} <- TwitterAPI.unfav(user, id) do
229 render(conn, ActivityView, "activity.json", %{activity: activity, for: user})
230 end
231 end
232
233 def retweet(%{assigns: %{user: user}} = conn, %{"id" => id}) do
234 with {:ok, activity} <- TwitterAPI.repeat(user, id) do
235 render(conn, ActivityView, "activity.json", %{activity: activity, for: user})
236 end
237 end
238
239 def register(conn, params) do
240 with {:ok, user} <- TwitterAPI.register_user(params) do
241 render(conn, UserView, "show.json", %{user: user})
242 else
243 {:error, errors} ->
244 conn
245 |> json_reply(400, Jason.encode!(errors))
246 end
247 end
248
249 def update_avatar(%{assigns: %{user: user}} = conn, params) do
250 {:ok, object} = ActivityPub.upload(params)
251 change = Changeset.change(user, %{avatar: object.data})
252 {:ok, user} = User.update_and_set_cache(change)
253 CommonAPI.update(user)
254
255 render(conn, UserView, "show.json", %{user: user, for: user})
256 end
257
258 def update_banner(%{assigns: %{user: user}} = conn, params) do
259 with {:ok, object} <- ActivityPub.upload(%{"img" => params["banner"]}),
260 new_info <- Map.put(user.info, "banner", object.data),
261 change <- User.info_changeset(user, %{info: new_info}),
262 {:ok, user} <- User.update_and_set_cache(change) do
263 CommonAPI.update(user)
264 %{"url" => [%{"href" => href} | _]} = object.data
265 response = %{url: href} |> Jason.encode!()
266
267 conn
268 |> json_reply(200, response)
269 end
270 end
271
272 def update_background(%{assigns: %{user: user}} = conn, params) do
273 with {:ok, object} <- ActivityPub.upload(params),
274 new_info <- Map.put(user.info, "background", object.data),
275 change <- User.info_changeset(user, %{info: new_info}),
276 {:ok, _user} <- User.update_and_set_cache(change) do
277 %{"url" => [%{"href" => href} | _]} = object.data
278 response = %{url: href} |> Jason.encode!()
279
280 conn
281 |> json_reply(200, response)
282 end
283 end
284
285 def external_profile(%{assigns: %{user: current_user}} = conn, %{"profileurl" => uri}) do
286 with {:ok, user_map} <- TwitterAPI.get_external_profile(current_user, uri),
287 response <- Jason.encode!(user_map) do
288 conn
289 |> json_reply(200, response)
290 else
291 _e ->
292 conn
293 |> put_status(404)
294 |> json(%{error: "Can't find user"})
295 end
296 end
297
298 def update_most_recent_notification(%{assigns: %{user: user}} = conn, %{"id" => id}) do
299 with id when is_number(id) <- String.to_integer(id),
300 info <- user.info,
301 mrn <- max(id, user.info["most_recent_notification"] || 0),
302 updated_info <- Map.put(info, "most_recent_notification", mrn),
303 changeset <- User.info_changeset(user, %{info: updated_info}),
304 {:ok, _user} <- User.update_and_set_cache(changeset) do
305 conn
306 |> json_reply(200, Jason.encode!(mrn))
307 else
308 _e -> bad_request_reply(conn, "Can't update.")
309 end
310 end
311
312 def followers(conn, params) do
313 with {:ok, user} <- TwitterAPI.get_user(conn.assigns.user, params),
314 {:ok, followers} <- User.get_followers(user) do
315 render(conn, UserView, "index.json", %{users: followers, for: user})
316 else
317 _e -> bad_request_reply(conn, "Can't get followers")
318 end
319 end
320
321 def friends(conn, params) do
322 with {:ok, user} <- TwitterAPI.get_user(conn.assigns.user, params),
323 {:ok, friends} <- User.get_friends(user) do
324 render(conn, UserView, "index.json", %{users: friends, for: user})
325 else
326 _e -> bad_request_reply(conn, "Can't get friends")
327 end
328 end
329
330 def friends_ids(%{assigns: %{user: user}} = conn, _params) do
331 with {:ok, friends} <- User.get_friends(user) do
332 ids =
333 friends
334 |> Enum.map(fn x -> x.id end)
335 |> Jason.encode!()
336
337 json(conn, ids)
338 else
339 _e -> bad_request_reply(conn, "Can't get friends")
340 end
341 end
342
343 def empty_array(conn, _params) do
344 json(conn, Jason.encode!([]))
345 end
346
347 def update_profile(%{assigns: %{user: user}} = conn, params) do
348 params =
349 if bio = params["description"] do
350 Map.put(params, "bio", bio)
351 else
352 params
353 end
354
355 with changeset <- User.update_changeset(user, params),
356 {:ok, user} <- User.update_and_set_cache(changeset) do
357 CommonAPI.update(user)
358 render(conn, UserView, "user.json", %{user: user, for: user})
359 else
360 error ->
361 Logger.debug("Can't update user: #{inspect(error)}")
362 bad_request_reply(conn, "Can't update user")
363 end
364 end
365
366 def search(%{assigns: %{user: user}} = conn, %{"q" => _query} = params) do
367 activities = TwitterAPI.search(user, params)
368
369 conn
370 |> render(ActivityView, "index.json", %{activities: activities, for: user})
371 end
372
373 defp bad_request_reply(conn, error_message) do
374 json = error_json(conn, error_message)
375 json_reply(conn, 400, json)
376 end
377
378 defp json_reply(conn, status, json) do
379 conn
380 |> put_resp_content_type("application/json")
381 |> send_resp(status, json)
382 end
383
384 defp forbidden_json_reply(conn, error_message) do
385 json = error_json(conn, error_message)
386 json_reply(conn, 403, json)
387 end
388
389 defp error_json(conn, error_message) do
390 %{"error" => error_message, "request" => conn.request_path} |> Jason.encode!()
391 end
392 end