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 l = status_text |> String.trim |> String.length
18 if l > 0 && l < 5000 do
19 media_ids = extract_media_ids(status_data)
20 {:ok, activity} = TwitterAPI.create_status(user, Map.put(status_data, "media_ids", media_ids))
22 |> json_reply(200, ActivityRepresenter.to_json(activity, %{user: user}))
24 empty_status_reply(conn)
28 def status_update(conn, _status_data) do
29 empty_status_reply(conn)
32 defp empty_status_reply(conn) do
33 bad_request_reply(conn, "Client must provide a 'status' parameter with a value.")
36 defp extract_media_ids(status_data) do
37 with media_ids when not is_nil(media_ids) <- status_data["media_ids"],
38 split_ids <- String.split(media_ids, ","),
39 clean_ids <- Enum.reject(split_ids, fn (id) -> String.length(id) == 0 end)
46 def public_and_external_timeline(%{assigns: %{user: user}} = conn, params) do
47 statuses = TwitterAPI.fetch_public_and_external_statuses(user, params)
48 {:ok, json} = Poison.encode(statuses)
51 |> json_reply(200, json)
54 def public_timeline(%{assigns: %{user: user}} = conn, params) do
55 statuses = TwitterAPI.fetch_public_statuses(user, params)
56 {:ok, json} = Poison.encode(statuses)
59 |> json_reply(200, json)
62 def friends_timeline(%{assigns: %{user: user}} = conn, params) do
63 statuses = TwitterAPI.fetch_friend_statuses(user, params)
64 {:ok, json} = Poison.encode(statuses)
67 |> json_reply(200, json)
70 def user_timeline(%{assigns: %{user: user}} = conn, params) do
71 case TwitterAPI.get_user(user, params) do
73 params = Map.merge(params, %{"actor_id" => target_user.ap_id})
74 statuses = TwitterAPI.fetch_user_statuses(user, params)
76 |> json_reply(200, statuses |> Poison.encode!)
78 bad_request_reply(conn, msg)
82 def mentions_timeline(%{assigns: %{user: user}} = conn, params) do
83 statuses = TwitterAPI.fetch_mentions(user, params)
84 {:ok, json} = Poison.encode(statuses)
87 |> json_reply(200, json)
90 def follow(%{assigns: %{user: user}} = conn, params) do
91 case TwitterAPI.follow(user, params) do
92 {:ok, user, followed, _activity} ->
93 render(conn, UserView, "show.json", %{user: followed, for: user})
94 {:error, msg} -> forbidden_json_reply(conn, msg)
98 def delete_post(%{assigns: %{user: user}} = conn, %{"id" => id}) do
99 with {:ok, delete} <- CommonAPI.delete(id, user) do
100 json = ActivityRepresenter.to_json(delete, %{user: user, for: user})
102 |> json_reply(200, json)
106 def unfollow(%{assigns: %{user: user}} = conn, params) do
107 case TwitterAPI.unfollow(user, params) do
108 {:ok, user, unfollowed} ->
109 render(conn, UserView, "show.json", %{user: unfollowed, for: user})
110 {:error, msg} -> forbidden_json_reply(conn, msg)
114 def fetch_status(%{assigns: %{user: user}} = conn, %{"id" => id}) do
115 response = Poison.encode!(TwitterAPI.fetch_status(user, id))
118 |> json_reply(200, response)
121 def fetch_conversation(%{assigns: %{user: user}} = conn, %{"id" => id}) do
122 id = String.to_integer(id)
123 response = Poison.encode!(TwitterAPI.fetch_conversation(user, id))
126 |> json_reply(200, response)
129 def upload(conn, %{"media" => media}) do
130 response = TwitterAPI.upload(media)
132 |> put_resp_content_type("application/atom+xml")
133 |> send_resp(200, response)
136 def upload_json(conn, %{"media" => media}) do
137 response = TwitterAPI.upload(media, "json")
139 |> json_reply(200, response)
142 def get_by_id_or_ap_id(id) do
143 activity = Repo.get(Activity, id) || Activity.get_create_activity_by_object_ap_id(id)
144 if activity.data["type"] == "Create" do
147 Activity.get_create_activity_by_object_ap_id(activity.data["object"])
151 def favorite(%{assigns: %{user: user}} = conn, %{"id" => id}) do
152 with {:ok, status} <- TwitterAPI.fav(user, id) do
157 def unfavorite(%{assigns: %{user: user}} = conn, %{"id" => id}) do
158 with {:ok, status} <- TwitterAPI.unfav(user, id) do
163 def retweet(%{assigns: %{user: user}} = conn, %{"id" => id}) do
164 with {:ok, status} <- TwitterAPI.repeat(user, id) do
169 def register(conn, params) do
170 with {:ok, user} <- TwitterAPI.register_user(params) do
171 render(conn, UserView, "show.json", %{user: user})
175 |> json_reply(400, Poison.encode!(errors))
179 def update_avatar(%{assigns: %{user: user}} = conn, params) do
180 {:ok, object} = ActivityPub.upload(params)
181 change = Changeset.change(user, %{avatar: object.data})
182 {:ok, user} = Repo.update(change)
184 render(conn, UserView, "show.json", %{user: user, for: user})
187 def update_banner(%{assigns: %{user: user}} = conn, params) do
188 with {:ok, object} <- ActivityPub.upload(%{"img" => params["banner"]}),
189 new_info <- Map.put(user.info, "banner", object.data),
190 change <- User.info_changeset(user, %{info: new_info}),
191 {:ok, user} <- Repo.update(change) do
192 %{"url" => [ %{ "href" => href } | t ]} = object.data
193 response = %{ url: href } |> Poison.encode!
195 |> json_reply(200, response)
199 def update_background(%{assigns: %{user: user}} = conn, params) do
200 with {:ok, object} <- ActivityPub.upload(params),
201 new_info <- Map.put(user.info, "background", object.data),
202 change <- User.info_changeset(user, %{info: new_info}),
203 {:ok, user} <- Repo.update(change) do
204 %{"url" => [ %{ "href" => href } | t ]} = object.data
205 response = %{ url: href } |> Poison.encode!
207 |> json_reply(200, response)
211 def external_profile(%{assigns: %{user: current_user}} = conn, %{"profileurl" => uri}) do
212 with {:ok, user_map} <- TwitterAPI.get_external_profile(current_user, uri),
213 response <- Poison.encode!(user_map) do
215 |> json_reply(200, response)
219 def update_most_recent_notification(%{assigns: %{user: user}} = conn, %{"id" => id}) do
220 with id when is_number(id) <- String.to_integer(id),
222 mrn <- max(id, user.info["most_recent_notification"] || 0),
223 updated_info <- Map.put(info, "most_recent_notification", mrn),
224 changeset <- User.info_changeset(user, %{info: updated_info}),
225 {:ok, user} <- Repo.update(changeset) do
227 |> json_reply(200, Poison.encode!(mrn))
229 _e -> bad_request_reply(conn, "Can't update.")
233 def followers(%{assigns: %{user: user}} = conn, _params) do
234 with {:ok, followers} <- User.get_followers(user) do
235 render(conn, UserView, "index.json", %{users: followers, for: user})
237 _e -> bad_request_reply(conn, "Can't get followers")
241 def friends(%{assigns: %{user: user}} = conn, _params) do
242 with {:ok, friends} <- User.get_friends(user) do
243 render(conn, UserView, "index.json", %{users: friends, for: user})
245 _e -> bad_request_reply(conn, "Can't get friends")
249 def update_profile(%{assigns: %{user: user}} = conn, params) do
250 params = if bio = params["description"] do
251 Map.put(params, "bio", bio)
256 with changeset <- User.update_changeset(user, params),
257 {:ok, user} <- Repo.update(changeset) do
258 render(conn, UserView, "user.json", %{user: user, for: user})
261 Logger.debug("Can't update user: #{inspect(error)}")
262 bad_request_reply(conn, "Can't update user")
266 defp bad_request_reply(conn, error_message) do
267 json = error_json(conn, error_message)
268 json_reply(conn, 400, json)
271 defp json_reply(conn, status, json) do
273 |> put_resp_content_type("application/json")
274 |> send_resp(status, json)
277 defp forbidden_json_reply(conn, error_message) do
278 json = error_json(conn, error_message)
279 json_reply(conn, 403, json)
282 defp error_json(conn, error_message) do
283 %{"error" => error_message, "request" => conn.request_path} |> Poison.encode!