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, Object}
7 alias Pleroma.Web.ActivityPub.ActivityPub
12 def verify_credentials(%{assigns: %{user: user}} = conn, _params) do
13 render(conn, UserView, "show.json", %{user: user})
16 def status_update(%{assigns: %{user: user}} = conn, %{"status" => status_text} = status_data) do
17 with media_ids <- extract_media_ids(status_data),
18 {:ok, activity} <- TwitterAPI.create_status(user, Map.put(status_data, "media_ids", media_ids)) do
20 |> json(ActivityRepresenter.to_map(activity, %{user: user}))
22 _ -> empty_status_reply(conn)
26 def status_update(conn, _status_data) do
27 empty_status_reply(conn)
30 defp empty_status_reply(conn) do
31 bad_request_reply(conn, "Client must provide a 'status' parameter with a value.")
34 defp extract_media_ids(status_data) do
35 with media_ids when not is_nil(media_ids) <- status_data["media_ids"],
36 split_ids <- String.split(media_ids, ","),
37 clean_ids <- Enum.reject(split_ids, fn (id) -> String.length(id) == 0 end)
44 def public_and_external_timeline(%{assigns: %{user: user}} = conn, params) do
45 statuses = TwitterAPI.fetch_public_and_external_statuses(user, params)
46 {:ok, json} = Poison.encode(statuses)
49 |> json_reply(200, json)
52 def public_timeline(%{assigns: %{user: user}} = conn, params) do
53 statuses = TwitterAPI.fetch_public_statuses(user, params)
54 {:ok, json} = Poison.encode(statuses)
57 |> json_reply(200, json)
60 def friends_timeline(%{assigns: %{user: user}} = conn, params) do
61 statuses = TwitterAPI.fetch_friend_statuses(user, params)
62 {:ok, json} = Poison.encode(statuses)
65 |> json_reply(200, json)
68 def user_timeline(%{assigns: %{user: user}} = conn, params) do
69 case TwitterAPI.get_user(user, params) do
71 params = Map.merge(params, %{"actor_id" => target_user.ap_id, "whole_db" => true})
72 statuses = TwitterAPI.fetch_user_statuses(user, params)
74 |> json_reply(200, statuses |> Poison.encode!)
76 bad_request_reply(conn, msg)
80 def mentions_timeline(%{assigns: %{user: user}} = conn, params) do
81 statuses = TwitterAPI.fetch_mentions(user, params)
82 {:ok, json} = Poison.encode(statuses)
85 |> json_reply(200, json)
88 def follow(%{assigns: %{user: user}} = conn, params) do
89 case TwitterAPI.follow(user, params) do
90 {:ok, user, followed, _activity} ->
91 render(conn, UserView, "show.json", %{user: followed, for: user})
92 {:error, msg} -> forbidden_json_reply(conn, msg)
96 def block(%{assigns: %{user: user}} = conn, params) do
97 case TwitterAPI.block(user, params) do
98 {:ok, user, blocked} ->
99 render conn, UserView, "show.json", %{user: blocked, for: user}
100 {:error, msg} -> forbidden_json_reply(conn, msg)
104 def unblock(%{assigns: %{user: user}} = conn, params) do
105 case TwitterAPI.unblock(user, params) do
106 {:ok, user, blocked} ->
107 render conn, UserView, "show.json", %{user: blocked, for: user}
108 {:error, msg} -> forbidden_json_reply(conn, msg)
112 def delete_post(%{assigns: %{user: user}} = conn, %{"id" => id}) do
113 with {:ok, delete} <- CommonAPI.delete(id, user) do
114 json = ActivityRepresenter.to_json(delete, %{user: user, for: user})
116 |> json_reply(200, json)
120 def unfollow(%{assigns: %{user: user}} = conn, params) do
121 case TwitterAPI.unfollow(user, params) do
122 {:ok, user, unfollowed} ->
123 render(conn, UserView, "show.json", %{user: unfollowed, for: user})
124 {:error, msg} -> forbidden_json_reply(conn, msg)
128 def fetch_status(%{assigns: %{user: user}} = conn, %{"id" => id}) do
129 response = Poison.encode!(TwitterAPI.fetch_status(user, id))
132 |> json_reply(200, response)
135 def fetch_conversation(%{assigns: %{user: user}} = conn, %{"id" => id}) do
136 id = String.to_integer(id)
137 response = Poison.encode!(TwitterAPI.fetch_conversation(user, id))
140 |> json_reply(200, response)
143 def upload(conn, %{"media" => media}) do
144 response = TwitterAPI.upload(media)
146 |> put_resp_content_type("application/atom+xml")
147 |> send_resp(200, response)
150 def upload_json(conn, %{"media" => media}) do
151 response = TwitterAPI.upload(media, "json")
153 |> json_reply(200, response)
156 def get_by_id_or_ap_id(id) do
157 activity = Repo.get(Activity, id) || Activity.get_create_activity_by_object_ap_id(id)
158 if activity.data["type"] == "Create" do
161 Activity.get_create_activity_by_object_ap_id(activity.data["object"])
165 def favorite(%{assigns: %{user: user}} = conn, %{"id" => id}) do
166 with {:ok, status} <- TwitterAPI.fav(user, id) do
171 def unfavorite(%{assigns: %{user: user}} = conn, %{"id" => id}) do
172 with {:ok, status} <- TwitterAPI.unfav(user, id) do
177 def retweet(%{assigns: %{user: user}} = conn, %{"id" => id}) do
178 with {:ok, status} <- TwitterAPI.repeat(user, id) do
183 def register(conn, params) do
184 with {:ok, user} <- TwitterAPI.register_user(params) do
185 render(conn, UserView, "show.json", %{user: user})
189 |> json_reply(400, Poison.encode!(errors))
193 def update_avatar(%{assigns: %{user: user}} = conn, params) do
194 {:ok, object} = ActivityPub.upload(params)
195 change = Changeset.change(user, %{avatar: object.data})
196 {:ok, user} = Repo.update(change)
198 render(conn, UserView, "show.json", %{user: user, for: user})
201 def update_banner(%{assigns: %{user: user}} = conn, params) do
202 with {:ok, object} <- ActivityPub.upload(%{"img" => params["banner"]}),
203 new_info <- Map.put(user.info, "banner", object.data),
204 change <- User.info_changeset(user, %{info: new_info}),
205 {:ok, user} <- Repo.update(change) do
206 %{"url" => [ %{ "href" => href } | t ]} = object.data
207 response = %{ url: href } |> Poison.encode!
209 |> json_reply(200, response)
213 def update_background(%{assigns: %{user: user}} = conn, params) do
214 with {:ok, object} <- ActivityPub.upload(params),
215 new_info <- Map.put(user.info, "background", object.data),
216 change <- User.info_changeset(user, %{info: new_info}),
217 {:ok, user} <- Repo.update(change) do
218 %{"url" => [ %{ "href" => href } | t ]} = object.data
219 response = %{ url: href } |> Poison.encode!
221 |> json_reply(200, response)
225 def external_profile(%{assigns: %{user: current_user}} = conn, %{"profileurl" => uri}) do
226 with {:ok, user_map} <- TwitterAPI.get_external_profile(current_user, uri),
227 response <- Poison.encode!(user_map) do
229 |> json_reply(200, response)
234 |> json(%{error: "Can't find user"})
238 def update_most_recent_notification(%{assigns: %{user: user}} = conn, %{"id" => id}) do
239 with id when is_number(id) <- String.to_integer(id),
241 mrn <- max(id, user.info["most_recent_notification"] || 0),
242 updated_info <- Map.put(info, "most_recent_notification", mrn),
243 changeset <- User.info_changeset(user, %{info: updated_info}),
244 {:ok, user} <- Repo.update(changeset) do
246 |> json_reply(200, Poison.encode!(mrn))
248 _e -> bad_request_reply(conn, "Can't update.")
252 def followers(%{assigns: %{user: user}} = conn, _params) do
253 with {:ok, followers} <- User.get_followers(user) do
254 render(conn, UserView, "index.json", %{users: followers, for: user})
256 _e -> bad_request_reply(conn, "Can't get followers")
260 def friends(%{assigns: %{user: user}} = conn, _params) do
261 with {:ok, friends} <- User.get_friends(user) do
262 render(conn, UserView, "index.json", %{users: friends, for: user})
264 _e -> bad_request_reply(conn, "Can't get friends")
268 def friends_ids(%{assigns: %{user: user}} = conn, _params) do
269 with {:ok, friends} <- User.get_friends(user) do
271 |> Enum.map(fn x -> x.id end)
276 _e -> bad_request_reply(conn, "Can't get friends")
280 def empty_array(conn, _params) do
281 json(conn, Poison.encode!([]))
284 def update_profile(%{assigns: %{user: user}} = conn, params) do
285 params = if bio = params["description"] do
286 Map.put(params, "bio", bio)
291 with changeset <- User.update_changeset(user, params),
292 {:ok, user} <- Repo.update(changeset) do
293 render(conn, UserView, "user.json", %{user: user, for: user})
296 Logger.debug("Can't update user: #{inspect(error)}")
297 bad_request_reply(conn, "Can't update user")
301 def search(%{assigns: %{user: user}} = conn, %{"q" => query} = params) do
303 |> json(TwitterAPI.search(user, params))
306 defp bad_request_reply(conn, error_message) do
307 json = error_json(conn, error_message)
308 json_reply(conn, 400, json)
311 defp json_reply(conn, status, json) do
313 |> put_resp_content_type("application/json")
314 |> send_resp(status, json)
317 defp forbidden_json_reply(conn, error_message) do
318 json = error_json(conn, error_message)
319 json_reply(conn, 403, json)
322 defp error_json(conn, error_message) do
323 %{"error" => error_message, "request" => conn.request_path} |> Poison.encode!