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, Repo, Activity}
6 alias Pleroma.Web.ActivityPub.ActivityPub
9 def verify_credentials(%{assigns: %{user: user}} = conn, _params) do
10 render(conn, UserView, "show.json", %{user: user})
13 def status_update(%{assigns: %{user: user}} = conn, %{"status" => status_text} = status_data) do
14 l = status_text |> String.trim |> String.length
15 if l > 0 && l < 5000 do
16 media_ids = extract_media_ids(status_data)
17 {:ok, activity} = TwitterAPI.create_status(user, Map.put(status_data, "media_ids", media_ids))
19 |> json_reply(200, ActivityRepresenter.to_json(activity, %{user: user}))
21 empty_status_reply(conn)
25 def status_update(conn, _status_data) do
26 empty_status_reply(conn)
29 defp empty_status_reply(conn) do
30 bad_request_reply(conn, "Client must provide a 'status' parameter with a value.")
33 defp extract_media_ids(status_data) do
34 with media_ids when not is_nil(media_ids) <- status_data["media_ids"],
35 split_ids <- String.split(media_ids, ","),
36 clean_ids <- Enum.reject(split_ids, fn (id) -> String.length(id) == 0 end)
43 def public_and_external_timeline(%{assigns: %{user: user}} = conn, params) do
44 statuses = TwitterAPI.fetch_public_and_external_statuses(user, params)
45 {:ok, json} = Poison.encode(statuses)
48 |> json_reply(200, json)
51 def public_timeline(%{assigns: %{user: user}} = conn, params) do
52 statuses = TwitterAPI.fetch_public_statuses(user, params)
53 {:ok, json} = Poison.encode(statuses)
56 |> json_reply(200, json)
59 def friends_timeline(%{assigns: %{user: user}} = conn, params) do
60 statuses = TwitterAPI.fetch_friend_statuses(user, params)
61 {:ok, json} = Poison.encode(statuses)
64 |> json_reply(200, json)
67 def user_timeline(%{assigns: %{user: user}} = conn, params) do
68 case TwitterAPI.get_user(user, params) do
70 params = Map.merge(params, %{"actor_id" => target_user.ap_id})
71 statuses = TwitterAPI.fetch_user_statuses(user, params)
73 |> json_reply(200, statuses |> Poison.encode!)
75 bad_request_reply(conn, msg)
79 def mentions_timeline(%{assigns: %{user: user}} = conn, params) do
80 statuses = TwitterAPI.fetch_mentions(user, params)
81 {:ok, json} = Poison.encode(statuses)
84 |> json_reply(200, json)
87 def follow(%{assigns: %{user: user}} = conn, params) do
88 case TwitterAPI.follow(user, params) do
89 {:ok, user, followed, _activity} ->
90 render(conn, UserView, "show.json", %{user: followed, for: user})
91 {:error, msg} -> forbidden_json_reply(conn, msg)
95 def unfollow(%{assigns: %{user: user}} = conn, params) do
96 case TwitterAPI.unfollow(user, params) do
97 {:ok, user, unfollowed} ->
98 render(conn, UserView, "show.json", %{user: unfollowed, for: user})
99 {:error, msg} -> forbidden_json_reply(conn, msg)
103 def fetch_status(%{assigns: %{user: user}} = conn, %{"id" => id}) do
104 response = Poison.encode!(TwitterAPI.fetch_status(user, id))
107 |> json_reply(200, response)
110 def fetch_conversation(%{assigns: %{user: user}} = conn, %{"id" => id}) do
111 id = String.to_integer(id)
112 response = Poison.encode!(TwitterAPI.fetch_conversation(user, id))
115 |> json_reply(200, response)
118 def upload(conn, %{"media" => media}) do
119 response = TwitterAPI.upload(media)
121 |> put_resp_content_type("application/atom+xml")
122 |> send_resp(200, response)
125 def help_test(conn, _params) do
126 conn |> json_reply(200, Poison.encode!("ok"))
129 def upload_json(conn, %{"media" => media}) do
130 response = TwitterAPI.upload(media, "json")
132 |> json_reply(200, response)
135 def config(conn, _params) do
139 server: Web.base_url,
146 |> json_reply(200, response)
149 def favorite(%{assigns: %{user: user}} = conn, %{"id" => id}) do
150 activity = Repo.get(Activity, id)
151 {:ok, status} = TwitterAPI.favorite(user, activity)
152 response = Poison.encode!(status)
155 |> json_reply(200, response)
158 def unfavorite(%{assigns: %{user: user}} = conn, %{"id" => id}) do
159 activity = Repo.get(Activity, id)
160 {:ok, status} = TwitterAPI.unfavorite(user, activity)
161 response = Poison.encode!(status)
164 |> json_reply(200, response)
167 def retweet(%{assigns: %{user: user}} = conn, %{"id" => id}) do
168 activity = Repo.get(Activity, id)
169 if activity.data["actor"] == user.ap_id do
170 bad_request_reply(conn, "You cannot repeat your own notice.")
172 {:ok, status} = TwitterAPI.retweet(user, activity)
173 response = Poison.encode!(status)
177 |> json_reply(200, response)
181 def register(conn, params) do
182 with {:ok, user} <- TwitterAPI.register_user(params) do
184 render(conn, UserView, "show.json", %{user: user})
188 |> json_reply(400, Poison.encode!(errors))
192 def update_avatar(%{assigns: %{user: user}} = conn, params) do
193 {:ok, object} = ActivityPub.upload(params)
194 change = Changeset.change(user, %{avatar: object.data})
195 {:ok, user} = Repo.update(change)
197 render(conn, UserView, "show.json", %{user: user, for: user})
200 def external_profile(%{assigns: %{user: current_user}} = conn, %{"profileurl" => uri}) do
201 with {:ok, user_map} <- TwitterAPI.get_external_profile(current_user, uri),
202 response <- Poison.encode!(user_map) do
204 |> json_reply(200, response)
208 defp bad_request_reply(conn, error_message) do
209 json = error_json(conn, error_message)
210 json_reply(conn, 400, json)
213 defp json_reply(conn, status, json) do
215 |> put_resp_content_type("application/json")
216 |> send_resp(status, json)
219 defp forbidden_json_reply(conn, error_message) do
220 json = error_json(conn, error_message)
221 json_reply(conn, 403, json)
224 defp error_json(conn, error_message) do
225 %{"error" => error_message, "request" => conn.request_path} |> Poison.encode!