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 activity = get_by_id_or_ap_id(id)
159 {:ok, status} = TwitterAPI.unfavorite(user, activity)
160 response = Poison.encode!(status)
163 |> json_reply(200, response)
166 def retweet(%{assigns: %{user: user}} = conn, %{"id" => id}) do
167 with {:ok, status} <- TwitterAPI.repeat(user, id) do
172 def register(conn, params) do
173 with {:ok, user} <- TwitterAPI.register_user(params) do
174 render(conn, UserView, "show.json", %{user: user})
178 |> json_reply(400, Poison.encode!(errors))
182 def update_avatar(%{assigns: %{user: user}} = conn, params) do
183 {:ok, object} = ActivityPub.upload(params)
184 change = Changeset.change(user, %{avatar: object.data})
185 {:ok, user} = Repo.update(change)
187 render(conn, UserView, "show.json", %{user: user, for: user})
190 def update_banner(%{assigns: %{user: user}} = conn, params) do
191 with {:ok, object} <- ActivityPub.upload(%{"img" => params["banner"]}),
192 new_info <- Map.put(user.info, "banner", object.data),
193 change <- User.info_changeset(user, %{info: new_info}),
194 {:ok, user} <- Repo.update(change) do
195 %{"url" => [ %{ "href" => href } | t ]} = object.data
196 response = %{ url: href } |> Poison.encode!
198 |> json_reply(200, response)
202 def update_background(%{assigns: %{user: user}} = conn, params) do
203 with {:ok, object} <- ActivityPub.upload(params),
204 new_info <- Map.put(user.info, "background", object.data),
205 change <- User.info_changeset(user, %{info: new_info}),
206 {:ok, user} <- Repo.update(change) do
207 %{"url" => [ %{ "href" => href } | t ]} = object.data
208 response = %{ url: href } |> Poison.encode!
210 |> json_reply(200, response)
214 def external_profile(%{assigns: %{user: current_user}} = conn, %{"profileurl" => uri}) do
215 with {:ok, user_map} <- TwitterAPI.get_external_profile(current_user, uri),
216 response <- Poison.encode!(user_map) do
218 |> json_reply(200, response)
222 def update_most_recent_notification(%{assigns: %{user: user}} = conn, %{"id" => id}) do
223 with id when is_number(id) <- String.to_integer(id),
225 mrn <- max(id, user.info["most_recent_notification"] || 0),
226 updated_info <- Map.put(info, "most_recent_notification", mrn),
227 changeset <- User.info_changeset(user, %{info: updated_info}),
228 {:ok, user} <- Repo.update(changeset) do
230 |> json_reply(200, Poison.encode!(mrn))
232 _e -> bad_request_reply(conn, "Can't update.")
236 def followers(%{assigns: %{user: user}} = conn, _params) do
237 with {:ok, followers} <- User.get_followers(user) do
238 render(conn, UserView, "index.json", %{users: followers, for: user})
240 _e -> bad_request_reply(conn, "Can't get followers")
244 def friends(%{assigns: %{user: user}} = conn, _params) do
245 with {:ok, friends} <- User.get_friends(user) do
246 render(conn, UserView, "index.json", %{users: friends, for: user})
248 _e -> bad_request_reply(conn, "Can't get friends")
252 def update_profile(%{assigns: %{user: user}} = conn, params) do
253 params = if bio = params["description"] do
254 Map.put(params, "bio", bio)
259 with changeset <- User.update_changeset(user, params),
260 {:ok, user} <- Repo.update(changeset) do
261 render(conn, UserView, "user.json", %{user: user, for: user})
264 Logger.debug("Can't update user: #{inspect(error)}")
265 bad_request_reply(conn, "Can't update user")
269 defp bad_request_reply(conn, error_message) do
270 json = error_json(conn, error_message)
271 json_reply(conn, 400, json)
274 defp json_reply(conn, status, json) do
276 |> put_resp_content_type("application/json")
277 |> send_resp(status, json)
280 defp forbidden_json_reply(conn, error_message) do
281 json = error_json(conn, error_message)
282 json_reply(conn, 403, json)
285 defp error_json(conn, error_message) do
286 %{"error" => error_message, "request" => conn.request_path} |> Poison.encode!