1 defmodule Pleroma.Web.TwitterAPI.Controller do
2 use Pleroma.Web, :controller
3 alias Pleroma.Web.TwitterAPI.{TwitterAPI, UserView}
4 alias Pleroma.Web.TwitterAPI.Representers.ActivityRepresenter
5 alias Pleroma.Web.CommonAPI
6 alias Pleroma.{Repo, Activity, User}
7 alias Pleroma.Web.ActivityPub.ActivityPub
12 def verify_credentials(%{assigns: %{user: user}} = conn, _params) do
13 token = Phoenix.Token.sign(conn, "user socket", user.id)
14 render(conn, UserView, "show.json", %{user: user, token: token})
17 def status_update(%{assigns: %{user: user}} = conn, %{"status" => _} = status_data) do
18 with media_ids <- extract_media_ids(status_data),
19 {:ok, activity} <- TwitterAPI.create_status(user, Map.put(status_data, "media_ids", media_ids)) do
21 |> json(ActivityRepresenter.to_map(activity, %{user: 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)
45 def public_and_external_timeline(%{assigns: %{user: user}} = conn, params) do
46 statuses = TwitterAPI.fetch_public_and_external_statuses(user, params)
47 {:ok, json} = Poison.encode(statuses)
50 |> json_reply(200, json)
53 def public_timeline(%{assigns: %{user: user}} = conn, params) do
54 statuses = TwitterAPI.fetch_public_statuses(user, params)
55 {:ok, json} = Poison.encode(statuses)
58 |> json_reply(200, json)
61 def friends_timeline(%{assigns: %{user: user}} = conn, params) do
62 statuses = TwitterAPI.fetch_friend_statuses(user, params)
63 {:ok, json} = Poison.encode(statuses)
66 |> json_reply(200, json)
69 def show_user(conn, params) do
70 with {:ok, shown} <- TwitterAPI.get_user(params) do
71 if user = conn.assigns.user do
72 render conn, UserView, "show.json", %{user: shown, for: user}
74 render conn, UserView, "show.json", %{user: shown}
78 bad_request_reply(conn, msg)
82 def user_timeline(%{assigns: %{user: user}} = conn, params) do
83 case TwitterAPI.get_user(user, params) do
85 params = Map.merge(params, %{"actor_id" => target_user.ap_id, "whole_db" => true})
86 statuses = TwitterAPI.fetch_user_statuses(user, params)
88 |> json_reply(200, statuses |> Poison.encode!)
90 bad_request_reply(conn, msg)
94 def mentions_timeline(%{assigns: %{user: user}} = conn, params) do
95 statuses = TwitterAPI.fetch_mentions(user, params)
96 {:ok, json} = Poison.encode(statuses)
99 |> json_reply(200, json)
102 def follow(%{assigns: %{user: user}} = conn, params) do
103 case TwitterAPI.follow(user, params) do
104 {:ok, user, followed, _activity} ->
105 render(conn, UserView, "show.json", %{user: followed, for: user})
106 {:error, msg} -> forbidden_json_reply(conn, msg)
110 def block(%{assigns: %{user: user}} = conn, params) do
111 case TwitterAPI.block(user, params) do
112 {:ok, user, blocked} ->
113 render conn, UserView, "show.json", %{user: blocked, for: user}
114 {:error, msg} -> forbidden_json_reply(conn, msg)
118 def unblock(%{assigns: %{user: user}} = conn, params) do
119 case TwitterAPI.unblock(user, params) do
120 {:ok, user, blocked} ->
121 render conn, UserView, "show.json", %{user: blocked, for: user}
122 {:error, msg} -> forbidden_json_reply(conn, msg)
126 def delete_post(%{assigns: %{user: user}} = conn, %{"id" => id}) do
127 with {:ok, delete} <- CommonAPI.delete(id, user) do
128 json = ActivityRepresenter.to_json(delete, %{user: user, for: user})
130 |> json_reply(200, json)
134 def unfollow(%{assigns: %{user: user}} = conn, params) do
135 case TwitterAPI.unfollow(user, params) do
136 {:ok, user, unfollowed} ->
137 render(conn, UserView, "show.json", %{user: unfollowed, for: user})
138 {:error, msg} -> forbidden_json_reply(conn, msg)
142 def fetch_status(%{assigns: %{user: user}} = conn, %{"id" => id}) do
143 response = Poison.encode!(TwitterAPI.fetch_status(user, id))
146 |> json_reply(200, response)
149 def fetch_conversation(%{assigns: %{user: user}} = conn, %{"id" => id}) do
150 id = String.to_integer(id)
151 response = Poison.encode!(TwitterAPI.fetch_conversation(user, id))
154 |> json_reply(200, response)
157 def upload(conn, %{"media" => media}) do
158 response = TwitterAPI.upload(media)
160 |> put_resp_content_type("application/atom+xml")
161 |> send_resp(200, response)
164 def upload_json(conn, %{"media" => media}) do
165 response = TwitterAPI.upload(media, "json")
167 |> json_reply(200, response)
170 def get_by_id_or_ap_id(id) do
171 activity = Repo.get(Activity, id) || Activity.get_create_activity_by_object_ap_id(id)
172 if activity.data["type"] == "Create" do
175 Activity.get_create_activity_by_object_ap_id(activity.data["object"])
179 def favorite(%{assigns: %{user: user}} = conn, %{"id" => id}) do
180 with {:ok, status} <- TwitterAPI.fav(user, id) do
185 def unfavorite(%{assigns: %{user: user}} = conn, %{"id" => id}) do
186 with {:ok, status} <- TwitterAPI.unfav(user, id) do
191 def retweet(%{assigns: %{user: user}} = conn, %{"id" => id}) do
192 with {:ok, status} <- TwitterAPI.repeat(user, id) do
197 def register(conn, params) do
198 with {:ok, user} <- TwitterAPI.register_user(params) do
199 render(conn, UserView, "show.json", %{user: user})
203 |> json_reply(400, Poison.encode!(errors))
207 def update_avatar(%{assigns: %{user: user}} = conn, params) do
208 {:ok, object} = ActivityPub.upload(params)
209 change = Changeset.change(user, %{avatar: object.data})
210 {:ok, user} = User.update_and_set_cache(change)
212 render(conn, UserView, "show.json", %{user: user, for: user})
215 def update_banner(%{assigns: %{user: user}} = conn, params) do
216 with {:ok, object} <- ActivityPub.upload(%{"img" => params["banner"]}),
217 new_info <- Map.put(user.info, "banner", object.data),
218 change <- User.info_changeset(user, %{info: new_info}),
219 {:ok, _user} <- User.update_and_set_cache(change) do
220 %{"url" => [ %{ "href" => href } | _ ]} = object.data
221 response = %{ url: href } |> Poison.encode!
223 |> json_reply(200, response)
227 def update_background(%{assigns: %{user: user}} = conn, params) do
228 with {:ok, object} <- ActivityPub.upload(params),
229 new_info <- Map.put(user.info, "background", object.data),
230 change <- User.info_changeset(user, %{info: new_info}),
231 {:ok, _user} <- User.update_and_set_cache(change) do
232 %{"url" => [ %{ "href" => href } | _ ]} = object.data
233 response = %{ url: href } |> Poison.encode!
235 |> json_reply(200, response)
239 def external_profile(%{assigns: %{user: current_user}} = conn, %{"profileurl" => uri}) do
240 with {:ok, user_map} <- TwitterAPI.get_external_profile(current_user, uri),
241 response <- Poison.encode!(user_map) do
243 |> json_reply(200, response)
248 |> json(%{error: "Can't find user"})
252 def update_most_recent_notification(%{assigns: %{user: user}} = conn, %{"id" => id}) do
253 with id when is_number(id) <- String.to_integer(id),
255 mrn <- max(id, user.info["most_recent_notification"] || 0),
256 updated_info <- Map.put(info, "most_recent_notification", mrn),
257 changeset <- User.info_changeset(user, %{info: updated_info}),
258 {:ok, _user} <- User.update_and_set_cache(changeset) do
260 |> json_reply(200, Poison.encode!(mrn))
262 _e -> bad_request_reply(conn, "Can't update.")
266 def followers(conn, params) do
267 with {:ok, user} <- TwitterAPI.get_user(conn.assigns.user, params),
268 {:ok, followers} <- User.get_followers(user) do
269 render(conn, UserView, "index.json", %{users: followers, for: user})
271 _e -> bad_request_reply(conn, "Can't get followers")
275 def friends(conn, params) do
276 with {:ok, user} <- TwitterAPI.get_user(conn.assigns.user, params),
277 {:ok, friends} <- User.get_friends(user) do
278 render(conn, UserView, "index.json", %{users: friends, for: user})
280 _e -> bad_request_reply(conn, "Can't get friends")
284 def friends_ids(%{assigns: %{user: user}} = conn, _params) do
285 with {:ok, friends} <- User.get_friends(user) do
287 |> Enum.map(fn x -> x.id end)
292 _e -> bad_request_reply(conn, "Can't get friends")
296 def empty_array(conn, _params) do
297 json(conn, Poison.encode!([]))
300 def update_profile(%{assigns: %{user: user}} = conn, params) do
301 params = if bio = params["description"] do
302 Map.put(params, "bio", bio)
307 with changeset <- User.update_changeset(user, params),
308 {:ok, user} <- User.update_and_set_cache(changeset) do
309 render(conn, UserView, "user.json", %{user: user, for: user})
312 Logger.debug("Can't update user: #{inspect(error)}")
313 bad_request_reply(conn, "Can't update user")
317 def search(%{assigns: %{user: user}} = conn, %{"q" => _query} = params) do
319 |> json(TwitterAPI.search(user, params))
322 defp bad_request_reply(conn, error_message) do
323 json = error_json(conn, error_message)
324 json_reply(conn, 400, json)
327 defp json_reply(conn, status, json) do
329 |> put_resp_content_type("application/json")
330 |> send_resp(status, json)
333 defp forbidden_json_reply(conn, error_message) do
334 json = error_json(conn, error_message)
335 json_reply(conn, 403, json)
338 defp error_json(conn, error_message) do
339 %{"error" => error_message, "request" => conn.request_path} |> Poison.encode!