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
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})
16 def status_update(%{assigns: %{user: user}} = conn, %{"status" => _} = status_data) do
17 with media_ids <- extract_media_ids(status_data),
19 TwitterAPI.create_status(user, Map.put(status_data, "media_ids", media_ids)) do
21 |> json(ActivityView.render("activity.json", activity: activity, for: user))
23 _ -> empty_status_reply(conn)
27 def status_update(conn, _status_data) do
28 empty_status_reply(conn)
31 defp empty_status_reply(conn) do
32 bad_request_reply(conn, "Client must provide a 'status' parameter with a value.")
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
45 def public_and_external_timeline(%{assigns: %{user: user}} = conn, params) do
48 |> Map.put("type", ["Create", "Announce"])
49 |> Map.put("blocking_user", user)
51 activities = ActivityPub.fetch_public_activities(params)
54 |> render(ActivityView, "index.json", %{activities: activities, for: user})
57 def public_timeline(%{assigns: %{user: user}} = conn, params) do
60 |> Map.put("type", ["Create", "Announce"])
61 |> Map.put("local_only", true)
62 |> Map.put("blocking_user", user)
64 activities = ActivityPub.fetch_public_activities(params)
67 |> render(ActivityView, "index.json", %{activities: activities, for: user})
70 def friends_timeline(%{assigns: %{user: user}} = conn, params) do
73 |> Map.put("type", ["Create", "Announce", "Follow", "Like"])
74 |> Map.put("blocking_user", user)
75 |> Map.put("user", user)
77 activities = ActivityPub.fetch_activities([user.ap_id | user.following], params)
80 |> render(ActivityView, "index.json", %{activities: activities, for: user})
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})
88 render(conn, UserView, "show.json", %{user: shown})
92 bad_request_reply(conn, msg)
96 def user_timeline(%{assigns: %{user: user}} = conn, params) do
97 case TwitterAPI.get_user(user, params) do
101 |> Map.put("type", ["Create", "Announce"])
102 |> Map.put("actor_id", target_user.ap_id)
103 |> Map.put("whole_db", true)
105 activities = ActivityPub.fetch_public_activities(params)
108 |> render(ActivityView, "index.json", %{activities: activities, for: user})
111 bad_request_reply(conn, msg)
115 def mentions_timeline(%{assigns: %{user: user}} = conn, params) do
116 activities = ActivityPub.fetch_activities([user.ap_id], params)
119 |> render(ActivityView, "index.json", %{activities: activities, for: user})
122 def notifications(%{assigns: %{user: user}} = conn, params) do
123 notifications = Notification.for_user(user, params)
126 |> render(NotificationView, "notification.json", %{notifications: notifications, for: user})
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})
135 forbidden_json_reply(conn, msg)
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})
145 forbidden_json_reply(conn, msg)
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})
155 forbidden_json_reply(conn, msg)
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})
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})
171 forbidden_json_reply(conn, msg)
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})
182 def fetch_conversation(%{assigns: %{user: user}} = conn, %{"id" => id}) do
183 id = String.to_integer(id)
185 with context when is_binary(context) <- TwitterAPI.conversation_id_to_context(id),
187 ActivityPub.fetch_activities_for_context(context, %{
188 "blocking_user" => user,
192 |> render(ActivityView, "index.json", %{activities: activities, for: user})
196 def upload(conn, %{"media" => media}) do
197 response = TwitterAPI.upload(media)
200 |> put_resp_content_type("application/atom+xml")
201 |> send_resp(200, response)
204 def upload_json(conn, %{"media" => media}) do
205 response = TwitterAPI.upload(media, "json")
208 |> json_reply(200, response)
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)
214 if activity.data["type"] == "Create" do
217 Activity.get_create_activity_by_object_ap_id(activity.data["object"])
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})
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})
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})
239 def register(conn, params) do
240 with {:ok, user} <- TwitterAPI.register_user(params) do
241 render(conn, UserView, "show.json", %{user: user})
245 |> json_reply(400, Jason.encode!(errors))
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)
255 render(conn, UserView, "show.json", %{user: user, for: user})
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!()
268 |> json_reply(200, response)
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!()
281 |> json_reply(200, response)
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
289 |> json_reply(200, response)
294 |> json(%{error: "Can't find user"})
298 def update_most_recent_notification(%{assigns: %{user: user}} = conn, %{"id" => id}) do
299 with id when is_number(id) <- String.to_integer(id),
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
306 |> json_reply(200, Jason.encode!(mrn))
308 _e -> bad_request_reply(conn, "Can't update.")
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})
317 _e -> bad_request_reply(conn, "Can't get followers")
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})
326 _e -> bad_request_reply(conn, "Can't get friends")
330 def friends_ids(%{assigns: %{user: user}} = conn, _params) do
331 with {:ok, friends} <- User.get_friends(user) do
334 |> Enum.map(fn x -> x.id end)
339 _e -> bad_request_reply(conn, "Can't get friends")
343 def empty_array(conn, _params) do
344 json(conn, Jason.encode!([]))
347 def update_profile(%{assigns: %{user: user}} = conn, params) do
349 if bio = params["description"] do
350 Map.put(params, "bio", bio)
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})
361 Logger.debug("Can't update user: #{inspect(error)}")
362 bad_request_reply(conn, "Can't update user")
366 def search(%{assigns: %{user: user}} = conn, %{"q" => _query} = params) do
367 activities = TwitterAPI.search(user, params)
370 |> render(ActivityView, "index.json", %{activities: activities, for: user})
373 defp bad_request_reply(conn, error_message) do
374 json = error_json(conn, error_message)
375 json_reply(conn, 400, json)
378 defp json_reply(conn, status, json) do
380 |> put_resp_content_type("application/json")
381 |> send_resp(status, json)
384 defp forbidden_json_reply(conn, error_message) do
385 json = error_json(conn, error_message)
386 json_reply(conn, 403, json)
389 defp error_json(conn, error_message) do
390 %{"error" => error_message, "request" => conn.request_path} |> Jason.encode!()