1 defmodule Pleroma.Web.TwitterAPI.Controller do
2 use Pleroma.Web, :controller
3 alias Pleroma.Web.TwitterAPI.TwitterAPI
4 alias Pleroma.Web.TwitterAPI.Representers.{UserRepresenter, ActivityRepresenter}
5 alias Pleroma.{Repo, Activity}
6 alias Pleroma.Web.ActivityPub.ActivityPub
8 def verify_credentials(%{assigns: %{user: user}} = conn, _params) do
9 response = user |> UserRepresenter.to_json(%{for: user})
12 |> json_reply(200, response)
15 def status_update(conn, %{"status" => ""} = _status_data) do
16 empty_status_reply(conn)
19 def status_update(%{assigns: %{user: user}} = conn, %{"status" => status_text} = status_data) do
20 if status_text |> String.trim |> String.length != 0 do
21 media_ids = extract_media_ids(status_data)
22 {:ok, activity} = TwitterAPI.create_status(user, Map.put(status_data, "media_ids", media_ids ))
24 |> json_reply(200, ActivityRepresenter.to_json(activity, %{user: user}))
26 empty_status_reply(conn)
30 def status_update(conn, _status_data) do
31 empty_status_reply(conn)
34 defp empty_status_reply(conn) do
35 bad_request_reply(conn, "Client must provide a 'status' parameter with a value.")
38 defp extract_media_ids(status_data) do
39 with media_ids when not is_nil(media_ids) <- status_data["media_ids"],
40 split_ids <- String.split(media_ids, ","),
41 clean_ids <- Enum.reject(split_ids, fn (id) -> String.length(id) == 0 end)
48 def public_timeline(%{assigns: %{user: user}} = conn, params) do
49 statuses = TwitterAPI.fetch_public_statuses(user, params)
50 {:ok, json} = Poison.encode(statuses)
53 |> json_reply(200, json)
56 def friends_timeline(%{assigns: %{user: user}} = conn, params) do
57 statuses = TwitterAPI.fetch_friend_statuses(user, params)
58 {:ok, json} = Poison.encode(statuses)
61 |> json_reply(200, json)
64 def user_timeline(%{assigns: %{user: user}} = conn, params) do
65 case TwitterAPI.get_user(user, params) do
67 params = Map.merge(params, %{"actor_id" => target_user.ap_id})
68 statuses = TwitterAPI.fetch_user_statuses(user, params)
70 |> json_reply(200, statuses |> Poison.encode!)
72 bad_request_reply(conn, msg)
76 def mentions_timeline(%{assigns: %{user: user}} = conn, params) do
77 statuses = TwitterAPI.fetch_mentions(user, params)
78 {:ok, json} = Poison.encode(statuses)
81 |> json_reply(200, json)
84 def follow(%{assigns: %{user: user}} = conn, %{ "user_id" => followed_id }) do
85 case TwitterAPI.follow(user, followed_id) do
86 { :ok, user, followed, _activity } ->
87 response = followed |> UserRepresenter.to_json(%{for: user})
89 |> json_reply(200, response)
90 { :error, msg } -> forbidden_json_reply(conn, msg)
94 def unfollow(%{assigns: %{user: user}} = conn, %{ "user_id" => followed_id }) do
95 case TwitterAPI.unfollow(user, followed_id) do
96 { :ok, user, followed } ->
97 response = followed |> UserRepresenter.to_json(%{for: user})
100 |> json_reply(200, response)
101 { :error, msg } -> forbidden_json_reply(conn, msg)
105 def fetch_status(%{assigns: %{user: user}} = conn, %{ "id" => id }) do
106 response = TwitterAPI.fetch_status(user, id) |> Poison.encode!
109 |> json_reply(200, response)
112 def fetch_conversation(%{assigns: %{user: user}} = conn, %{ "id" => id }) do
113 id = String.to_integer(id)
114 response = TwitterAPI.fetch_conversation(user, id) |> Poison.encode!
117 |> json_reply(200, response)
120 def upload(conn, %{"media" => media}) do
121 response = TwitterAPI.upload(media)
123 |> put_resp_content_type("application/atom+xml")
124 |> send_resp(200, response)
127 def help_test(conn, _params) do
128 conn |> json_reply(200, Poison.encode!("ok"))
131 def upload_json(conn, %{"media" => media}) do
132 response = TwitterAPI.upload(media, "json")
134 |> json_reply(200, response)
137 def config(conn, _params) do
140 name: Pleroma.Web.base_url,
141 server: Pleroma.Web.base_url,
148 |> json_reply(200, response)
151 def favorite(%{assigns: %{user: user}} = conn, %{"id" => id}) do
152 activity = Repo.get(Activity, id)
153 {:ok, status} = TwitterAPI.favorite(user, activity)
154 response = Poison.encode!(status)
157 |> json_reply(200, response)
160 def unfavorite(%{assigns: %{user: user}} = conn, %{"id" => id}) do
161 activity = Repo.get(Activity, id)
162 {:ok, status} = TwitterAPI.unfavorite(user, activity)
163 response = Poison.encode!(status)
166 |> json_reply(200, response)
169 def retweet(%{assigns: %{user: user}} = conn, %{"id" => id}) do
170 activity = Repo.get(Activity, id)
171 {:ok, status} = TwitterAPI.retweet(user, activity)
172 response = Poison.encode!(status)
175 |> json_reply(200, response)
178 def register(conn, params) do
179 with {:ok, user} <- TwitterAPI.register_user(params) do
181 |> json_reply(200, Poison.encode!(user))
185 |> json_reply(400, Poison.encode!(errors))
189 def update_avatar(%{assigns: %{user: user}} = conn, params) do
190 {:ok, object} = ActivityPub.upload(params)
191 change = Ecto.Changeset.change(user, %{avatar: object.data})
192 {:ok, user} = Repo.update(change)
194 response = UserRepresenter.to_map(user, %{for: user})
198 |> json_reply(200, response)
201 defp bad_request_reply(conn, error_message) do
202 json = error_json(conn, error_message)
203 json_reply(conn, 400, json)
206 defp json_reply(conn, status, json) do
208 |> put_resp_content_type("application/json")
209 |> send_resp(status, json)
212 defp forbidden_json_reply(conn, error_message) do
213 json = error_json(conn, error_message)
214 json_reply(conn, 403, json)
217 defp error_json(conn, error_message) do
218 %{"error" => error_message, "request" => conn.request_path} |> Poison.encode!