Deny whitespace statuses
[akkoma] / lib / pleroma / web / twitter_api / twitter_api_controller.ex
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
7
8 def verify_credentials(%{assigns: %{user: user}} = conn, _params) do
9 response = user |> UserRepresenter.to_json(%{for: user})
10
11 conn
12 |> json_reply(200, response)
13 end
14
15 def status_update(conn, %{"status" => ""} = _status_data) do
16 empty_status_reply(conn)
17 end
18
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 ))
23 conn
24 |> json_reply(200, ActivityRepresenter.to_json(activity, %{user: user}))
25 else
26 empty_status_reply(conn)
27 end
28 end
29
30 def status_update(conn, _status_data) do
31 empty_status_reply(conn)
32 end
33
34 defp empty_status_reply(conn) do
35 bad_request_reply(conn, "Client must provide a 'status' parameter with a value.")
36 end
37
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)
42 do
43 clean_ids
44 else _e -> []
45 end
46 end
47
48 def public_timeline(%{assigns: %{user: user}} = conn, params) do
49 statuses = TwitterAPI.fetch_public_statuses(user, params)
50 {:ok, json} = Poison.encode(statuses)
51
52 conn
53 |> json_reply(200, json)
54 end
55
56 def friends_timeline(%{assigns: %{user: user}} = conn, params) do
57 statuses = TwitterAPI.fetch_friend_statuses(user, params)
58 {:ok, json} = Poison.encode(statuses)
59
60 conn
61 |> json_reply(200, json)
62 end
63
64 def user_timeline(%{assigns: %{user: user}} = conn, params) do
65 case TwitterAPI.get_user(user, params) do
66 {:ok, target_user} ->
67 params = Map.merge(params, %{"actor_id" => target_user.ap_id})
68 statuses = TwitterAPI.fetch_user_statuses(user, params)
69 conn
70 |> json_reply(200, statuses |> Poison.encode!)
71 {:error, msg} ->
72 bad_request_reply(conn, msg)
73 end
74 end
75
76 def mentions_timeline(%{assigns: %{user: user}} = conn, params) do
77 statuses = TwitterAPI.fetch_mentions(user, params)
78 {:ok, json} = Poison.encode(statuses)
79
80 conn
81 |> json_reply(200, json)
82 end
83
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})
88 conn
89 |> json_reply(200, response)
90 { :error, msg } -> forbidden_json_reply(conn, msg)
91 end
92 end
93
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})
98
99 conn
100 |> json_reply(200, response)
101 { :error, msg } -> forbidden_json_reply(conn, msg)
102 end
103 end
104
105 def fetch_status(%{assigns: %{user: user}} = conn, %{ "id" => id }) do
106 response = TwitterAPI.fetch_status(user, id) |> Poison.encode!
107
108 conn
109 |> json_reply(200, response)
110 end
111
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!
115
116 conn
117 |> json_reply(200, response)
118 end
119
120 def upload(conn, %{"media" => media}) do
121 response = TwitterAPI.upload(media)
122 conn
123 |> put_resp_content_type("application/atom+xml")
124 |> send_resp(200, response)
125 end
126
127 def help_test(conn, _params) do
128 conn |> json_reply(200, Poison.encode!("ok"))
129 end
130
131 def upload_json(conn, %{"media" => media}) do
132 response = TwitterAPI.upload(media, "json")
133 conn
134 |> json_reply(200, response)
135 end
136
137 def config(conn, _params) do
138 response = %{
139 site: %{
140 name: Pleroma.Web.base_url,
141 server: Pleroma.Web.base_url,
142 textlimit: -1
143 }
144 }
145 |> Poison.encode!
146
147 conn
148 |> json_reply(200, response)
149 end
150
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)
155
156 conn
157 |> json_reply(200, response)
158 end
159
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)
164
165 conn
166 |> json_reply(200, response)
167 end
168
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)
173
174 conn
175 |> json_reply(200, response)
176 end
177
178 def register(conn, params) do
179 with {:ok, user} <- TwitterAPI.register_user(params) do
180 conn
181 |> json_reply(200, Poison.encode!(user))
182 else
183 {:error, errors} ->
184 conn
185 |> json_reply(400, Poison.encode!(errors))
186 end
187 end
188
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)
193
194 response = UserRepresenter.to_map(user, %{for: user})
195 |> Poison.encode!
196
197 conn
198 |> json_reply(200, response)
199 end
200
201 defp bad_request_reply(conn, error_message) do
202 json = error_json(conn, error_message)
203 json_reply(conn, 400, json)
204 end
205
206 defp json_reply(conn, status, json) do
207 conn
208 |> put_resp_content_type("application/json")
209 |> send_resp(status, json)
210 end
211
212 defp forbidden_json_reply(conn, error_message) do
213 json = error_json(conn, error_message)
214 json_reply(conn, 403, json)
215 end
216
217 defp error_json(conn, error_message) do
218 %{"error" => error_message, "request" => conn.request_path} |> Poison.encode!
219 end
220 end