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.{Repo, Activity, User, Object}
6 alias Pleroma.Web.ActivityPub.ActivityPub
11 def verify_credentials(%{assigns: %{user: user}} = conn, _params) do
12 render(conn, UserView, "show.json", %{user: user})
15 def status_update(%{assigns: %{user: user}} = conn, %{"status" => status_text} = status_data) do
16 l = status_text |> String.trim |> String.length
17 if l > 0 && l < 5000 do
18 media_ids = extract_media_ids(status_data)
19 {:ok, activity} = TwitterAPI.create_status(user, Map.put(status_data, "media_ids", media_ids))
21 |> json_reply(200, ActivityRepresenter.to_json(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 user_timeline(%{assigns: %{user: user}} = conn, params) do
70 case TwitterAPI.get_user(user, params) do
72 params = Map.merge(params, %{"actor_id" => target_user.ap_id})
73 statuses = TwitterAPI.fetch_user_statuses(user, params)
75 |> json_reply(200, statuses |> Poison.encode!)
77 bad_request_reply(conn, msg)
81 def mentions_timeline(%{assigns: %{user: user}} = conn, params) do
82 statuses = TwitterAPI.fetch_mentions(user, params)
83 {:ok, json} = Poison.encode(statuses)
86 |> json_reply(200, json)
89 def follow(%{assigns: %{user: user}} = conn, params) do
90 case TwitterAPI.follow(user, params) do
91 {:ok, user, followed, _activity} ->
92 render(conn, UserView, "show.json", %{user: followed, for: user})
93 {:error, msg} -> forbidden_json_reply(conn, msg)
97 def delete_post(%{assigns: %{user: user}} = conn, %{"id" => id}) do
98 with %Activity{data: %{"object" => %{"id" => object_id}}} <- Repo.get(Activity, id),
99 %Object{} = object <- Object.get_by_ap_id(object_id),
100 true <- user.ap_id == object.data["actor"],
101 {:ok, delete} <- ActivityPub.delete(object) |> IO.inspect do
102 json = ActivityRepresenter.to_json(delete, %{user: user, for: user})
104 |> json_reply(200, json)
108 def unfollow(%{assigns: %{user: user}} = conn, params) do
109 case TwitterAPI.unfollow(user, params) do
110 {:ok, user, unfollowed} ->
111 render(conn, UserView, "show.json", %{user: unfollowed, for: user})
112 {:error, msg} -> forbidden_json_reply(conn, msg)
116 def fetch_status(%{assigns: %{user: user}} = conn, %{"id" => id}) do
117 response = Poison.encode!(TwitterAPI.fetch_status(user, id))
120 |> json_reply(200, response)
123 def fetch_conversation(%{assigns: %{user: user}} = conn, %{"id" => id}) do
124 id = String.to_integer(id)
125 response = Poison.encode!(TwitterAPI.fetch_conversation(user, id))
128 |> json_reply(200, response)
131 def upload(conn, %{"media" => media}) do
132 response = TwitterAPI.upload(media)
134 |> put_resp_content_type("application/atom+xml")
135 |> send_resp(200, response)
138 def upload_json(conn, %{"media" => media}) do
139 response = TwitterAPI.upload(media, "json")
141 |> json_reply(200, response)
144 def get_by_id_or_ap_id(id) do
145 activity = Repo.get(Activity, id) || Activity.get_create_activity_by_object_ap_id(id)
146 if activity.data["type"] == "Create" do
149 Activity.get_create_activity_by_object_ap_id(activity.data["object"])
153 def favorite(%{assigns: %{user: user}} = conn, %{"id" => id}) do
154 activity = get_by_id_or_ap_id(id)
155 {:ok, status} = TwitterAPI.favorite(user, activity)
156 response = Poison.encode!(status)
159 |> json_reply(200, response)
162 def unfavorite(%{assigns: %{user: user}} = conn, %{"id" => id}) do
163 activity = get_by_id_or_ap_id(id)
164 {:ok, status} = TwitterAPI.unfavorite(user, activity)
165 response = Poison.encode!(status)
168 |> json_reply(200, response)
171 def retweet(%{assigns: %{user: user}} = conn, %{"id" => id}) do
172 activity = get_by_id_or_ap_id(id)
173 if activity.data["actor"] == user.ap_id do
174 bad_request_reply(conn, "You cannot repeat your own notice.")
176 {:ok, status} = TwitterAPI.retweet(user, activity)
177 response = Poison.encode!(status)
181 |> json_reply(200, response)
185 def register(conn, params) do
186 with {:ok, user} <- TwitterAPI.register_user(params) do
188 render(conn, UserView, "show.json", %{user: user})
192 |> json_reply(400, Poison.encode!(errors))
196 def update_avatar(%{assigns: %{user: user}} = conn, params) do
197 {:ok, object} = ActivityPub.upload(params)
198 change = Changeset.change(user, %{avatar: object.data})
199 {:ok, user} = Repo.update(change)
201 render(conn, UserView, "show.json", %{user: user, for: user})
204 def update_banner(%{assigns: %{user: user}} = conn, params) do
205 with {:ok, object} <- ActivityPub.upload(%{"img" => params["banner"]}),
206 new_info <- Map.put(user.info, "banner", object.data),
207 change <- User.info_changeset(user, %{info: new_info}),
208 {:ok, user} <- Repo.update(change) do
209 %{"url" => [ %{ "href" => href } | t ]} = object.data
210 response = %{ url: href } |> Poison.encode!
212 |> json_reply(200, response)
216 def update_background(%{assigns: %{user: user}} = conn, params) do
217 with {:ok, object} <- ActivityPub.upload(params),
218 new_info <- Map.put(user.info, "background", object.data),
219 change <- User.info_changeset(user, %{info: new_info}),
220 {:ok, user} <- Repo.update(change) do
221 %{"url" => [ %{ "href" => href } | t ]} = object.data
222 response = %{ url: href } |> Poison.encode!
224 |> json_reply(200, response)
228 def external_profile(%{assigns: %{user: current_user}} = conn, %{"profileurl" => uri}) do
229 with {:ok, user_map} <- TwitterAPI.get_external_profile(current_user, uri),
230 response <- Poison.encode!(user_map) do
232 |> json_reply(200, response)
236 def update_most_recent_notification(%{assigns: %{user: user}} = conn, %{"id" => id}) do
237 with id when is_number(id) <- String.to_integer(id),
239 mrn <- max(id, user.info["most_recent_notification"] || 0),
240 updated_info <- Map.put(info, "most_recent_notification", mrn),
241 changeset <- User.info_changeset(user, %{info: updated_info}),
242 {:ok, user} <- Repo.update(changeset) do
244 |> json_reply(200, Poison.encode!(mrn))
246 _e -> bad_request_reply(conn, "Can't update.")
250 def followers(%{assigns: %{user: user}} = conn, _params) do
251 with {:ok, followers} <- User.get_followers(user) do
252 render(conn, UserView, "index.json", %{users: followers, for: user})
254 _e -> bad_request_reply(conn, "Can't get followers")
258 def friends(%{assigns: %{user: user}} = conn, _params) do
259 with {:ok, friends} <- User.get_friends(user) do
260 render(conn, UserView, "index.json", %{users: friends, for: user})
262 _e -> bad_request_reply(conn, "Can't get friends")
266 def update_profile(%{assigns: %{user: user}} = conn, params) do
267 params = if bio = params["description"] do
268 Map.put(params, "bio", bio)
273 with changeset <- User.update_changeset(user, params),
274 {:ok, user} <- Repo.update(changeset) do
275 render(conn, UserView, "user.json", %{user: user, for: user})
278 Logger.debug("Can't update user: #{inspect(error)}")
279 bad_request_reply(conn, "Can't update user")
283 defp bad_request_reply(conn, error_message) do
284 json = error_json(conn, error_message)
285 json_reply(conn, 400, json)
288 defp json_reply(conn, status, json) do
290 |> put_resp_content_type("application/json")
291 |> send_resp(status, json)
294 defp forbidden_json_reply(conn, error_message) do
295 json = error_json(conn, error_message)
296 json_reply(conn, 403, json)
299 defp error_json(conn, error_message) do
300 %{"error" => error_message, "request" => conn.request_path} |> Poison.encode!