Add error response to self-repeats
[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 media_ids = extract_media_ids(status_data)
21 {:ok, activity} = TwitterAPI.create_status(user, Map.put(status_data, "media_ids", media_ids ))
22 conn
23 |> json_reply(200, ActivityRepresenter.to_json(activity, %{user: user}))
24 end
25
26 def status_update(conn, _status_data) do
27 empty_status_reply(conn)
28 end
29
30 defp empty_status_reply(conn) do
31 bad_request_reply(conn, "Client must provide a 'status' parameter with a value.")
32 end
33
34 defp extract_media_ids(status_data) do
35 with media_ids when not is_nil(media_ids) <- status_data["media_ids"],
36 split_ids <- String.split(media_ids, ","),
37 clean_ids <- Enum.reject(split_ids, fn (id) -> String.length(id) == 0 end)
38 do
39 clean_ids
40 else _e -> []
41 end
42 end
43
44 def public_timeline(%{assigns: %{user: user}} = conn, params) do
45 statuses = TwitterAPI.fetch_public_statuses(user, params)
46 {:ok, json} = Poison.encode(statuses)
47
48 conn
49 |> json_reply(200, json)
50 end
51
52 def friends_timeline(%{assigns: %{user: user}} = conn, params) do
53 statuses = TwitterAPI.fetch_friend_statuses(user, params)
54 {:ok, json} = Poison.encode(statuses)
55
56 conn
57 |> json_reply(200, json)
58 end
59
60 def user_timeline(%{assigns: %{user: user}} = conn, params) do
61 case TwitterAPI.get_user(user, params) do
62 {:ok, target_user} ->
63 params = Map.merge(params, %{"actor_id" => target_user.ap_id})
64 statuses = TwitterAPI.fetch_user_statuses(user, params)
65 conn
66 |> json_reply(200, statuses |> Poison.encode!)
67 {:error, msg} ->
68 bad_request_reply(conn, msg)
69 end
70 end
71
72 def mentions_timeline(%{assigns: %{user: user}} = conn, params) do
73 statuses = TwitterAPI.fetch_mentions(user, params)
74 {:ok, json} = Poison.encode(statuses)
75
76 conn
77 |> json_reply(200, json)
78 end
79
80 def follow(%{assigns: %{user: user}} = conn, params) do
81 case TwitterAPI.follow(user, params) do
82 { :ok, user, followed, _activity } ->
83 response = followed |> UserRepresenter.to_json(%{for: user})
84 conn
85 |> json_reply(200, response)
86 { :error, msg } -> forbidden_json_reply(conn, msg)
87 end
88 end
89
90 def unfollow(%{assigns: %{user: user}} = conn, params) do
91 case TwitterAPI.unfollow(user, params) do
92 { :ok, user, unfollowed, } ->
93 response = unfollowed |> UserRepresenter.to_json(%{for: user})
94 conn
95 |> json_reply(200, response)
96 { :error, msg } -> forbidden_json_reply(conn, msg)
97 end
98 end
99
100 def fetch_status(%{assigns: %{user: user}} = conn, %{ "id" => id }) do
101 response = TwitterAPI.fetch_status(user, id) |> Poison.encode!
102
103 conn
104 |> json_reply(200, response)
105 end
106
107 def fetch_conversation(%{assigns: %{user: user}} = conn, %{ "id" => id }) do
108 id = String.to_integer(id)
109 response = TwitterAPI.fetch_conversation(user, id) |> Poison.encode!
110
111 conn
112 |> json_reply(200, response)
113 end
114
115 def upload(conn, %{"media" => media}) do
116 response = TwitterAPI.upload(media)
117 conn
118 |> put_resp_content_type("application/atom+xml")
119 |> send_resp(200, response)
120 end
121
122 def help_test(conn, _params) do
123 conn |> json_reply(200, Poison.encode!("ok"))
124 end
125
126 def upload_json(conn, %{"media" => media}) do
127 response = TwitterAPI.upload(media, "json")
128 conn
129 |> json_reply(200, response)
130 end
131
132 def config(conn, _params) do
133 response = %{
134 site: %{
135 name: Pleroma.Web.base_url,
136 server: Pleroma.Web.base_url,
137 textlimit: -1
138 }
139 }
140 |> Poison.encode!
141
142 conn
143 |> json_reply(200, response)
144 end
145
146 def favorite(%{assigns: %{user: user}} = conn, %{"id" => id}) do
147 activity = Repo.get(Activity, id)
148 {:ok, status} = TwitterAPI.favorite(user, activity)
149 response = Poison.encode!(status)
150
151 conn
152 |> json_reply(200, response)
153 end
154
155 def unfavorite(%{assigns: %{user: user}} = conn, %{"id" => id}) do
156 activity = Repo.get(Activity, id)
157 {:ok, status} = TwitterAPI.unfavorite(user, activity)
158 response = Poison.encode!(status)
159
160 conn
161 |> json_reply(200, response)
162 end
163
164 def retweet(%{assigns: %{user: user}} = conn, %{"id" => id}) do
165 activity = Repo.get(Activity, id)
166 if activity.data["actor"] == user.ap_id do
167 bad_request_reply(conn, "You cannot repeat your own notice.")
168 else
169 {:ok, status} = TwitterAPI.retweet(user, activity)
170 response = Poison.encode!(status)
171
172 conn
173
174 |> json_reply(200, response)
175 end
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