9ea0773c9554e14ebec10c994dd58a33702da486
[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, UserView}
4 alias Pleroma.Web.TwitterAPI.Representers.ActivityRepresenter
5 alias Pleroma.Web.CommonAPI
6 alias Pleroma.{Repo, Activity, User, Object}
7 alias Pleroma.Web.ActivityPub.ActivityPub
8 alias Ecto.Changeset
9
10 require Logger
11
12 def verify_credentials(%{assigns: %{user: user}} = conn, _params) do
13 render(conn, UserView, "show.json", %{user: user})
14 end
15
16 def status_update(%{assigns: %{user: user}} = conn, %{"status" => status_text} = status_data) do
17 with media_ids <- extract_media_ids(status_data),
18 {:ok, activity} <- TwitterAPI.create_status(user, Map.put(status_data, "media_ids", media_ids)) do
19 conn
20 |> json(ActivityRepresenter.to_map(activity, %{user: user}))
21 else
22 _ -> empty_status_reply(conn)
23 end
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_and_external_timeline(%{assigns: %{user: user}} = conn, params) do
45 statuses = TwitterAPI.fetch_public_and_external_statuses(user, params)
46 {:ok, json} = Poison.encode(statuses)
47
48 conn
49 |> json_reply(200, json)
50 end
51
52 def public_timeline(%{assigns: %{user: user}} = conn, params) do
53 statuses = TwitterAPI.fetch_public_statuses(user, params)
54 {:ok, json} = Poison.encode(statuses)
55
56 conn
57 |> json_reply(200, json)
58 end
59
60 def friends_timeline(%{assigns: %{user: user}} = conn, params) do
61 statuses = TwitterAPI.fetch_friend_statuses(user, params)
62 {:ok, json} = Poison.encode(statuses)
63
64 conn
65 |> json_reply(200, json)
66 end
67
68 def user_timeline(%{assigns: %{user: user}} = conn, params) do
69 case TwitterAPI.get_user(user, params) do
70 {:ok, target_user} ->
71 params = Map.merge(params, %{"actor_id" => target_user.ap_id, "whole_db" => true})
72 statuses = TwitterAPI.fetch_user_statuses(user, params)
73 conn
74 |> json_reply(200, statuses |> Poison.encode!)
75 {:error, msg} ->
76 bad_request_reply(conn, msg)
77 end
78 end
79
80 def mentions_timeline(%{assigns: %{user: user}} = conn, params) do
81 statuses = TwitterAPI.fetch_mentions(user, params)
82 {:ok, json} = Poison.encode(statuses)
83
84 conn
85 |> json_reply(200, json)
86 end
87
88 def follow(%{assigns: %{user: user}} = conn, params) do
89 case TwitterAPI.follow(user, params) do
90 {:ok, user, followed, _activity} ->
91 render(conn, UserView, "show.json", %{user: followed, for: user})
92 {:error, msg} -> forbidden_json_reply(conn, msg)
93 end
94 end
95
96 def block(%{assigns: %{user: user}} = conn, params) do
97 case TwitterAPI.block(user, params) do
98 {:ok, user, blocked} ->
99 render conn, UserView, "show.json", %{user: blocked, for: user}
100 {:error, msg} -> forbidden_json_reply(conn, msg)
101 end
102 end
103
104 def unblock(%{assigns: %{user: user}} = conn, params) do
105 case TwitterAPI.unblock(user, params) do
106 {:ok, user, blocked} ->
107 render conn, UserView, "show.json", %{user: blocked, for: user}
108 {:error, msg} -> forbidden_json_reply(conn, msg)
109 end
110 end
111
112 def delete_post(%{assigns: %{user: user}} = conn, %{"id" => id}) do
113 with {:ok, delete} <- CommonAPI.delete(id, user) do
114 json = ActivityRepresenter.to_json(delete, %{user: user, for: user})
115 conn
116 |> json_reply(200, json)
117 end
118 end
119
120 def unfollow(%{assigns: %{user: user}} = conn, params) do
121 case TwitterAPI.unfollow(user, params) do
122 {:ok, user, unfollowed} ->
123 render(conn, UserView, "show.json", %{user: unfollowed, for: user})
124 {:error, msg} -> forbidden_json_reply(conn, msg)
125 end
126 end
127
128 def fetch_status(%{assigns: %{user: user}} = conn, %{"id" => id}) do
129 response = Poison.encode!(TwitterAPI.fetch_status(user, id))
130
131 conn
132 |> json_reply(200, response)
133 end
134
135 def fetch_conversation(%{assigns: %{user: user}} = conn, %{"id" => id}) do
136 id = String.to_integer(id)
137 response = Poison.encode!(TwitterAPI.fetch_conversation(user, id))
138
139 conn
140 |> json_reply(200, response)
141 end
142
143 def upload(conn, %{"media" => media}) do
144 response = TwitterAPI.upload(media)
145 conn
146 |> put_resp_content_type("application/atom+xml")
147 |> send_resp(200, response)
148 end
149
150 def upload_json(conn, %{"media" => media}) do
151 response = TwitterAPI.upload(media, "json")
152 conn
153 |> json_reply(200, response)
154 end
155
156 def get_by_id_or_ap_id(id) do
157 activity = Repo.get(Activity, id) || Activity.get_create_activity_by_object_ap_id(id)
158 if activity.data["type"] == "Create" do
159 activity
160 else
161 Activity.get_create_activity_by_object_ap_id(activity.data["object"])
162 end
163 end
164
165 def favorite(%{assigns: %{user: user}} = conn, %{"id" => id}) do
166 with {:ok, status} <- TwitterAPI.fav(user, id) do
167 json(conn, status)
168 end
169 end
170
171 def unfavorite(%{assigns: %{user: user}} = conn, %{"id" => id}) do
172 with {:ok, status} <- TwitterAPI.unfav(user, id) do
173 json(conn, status)
174 end
175 end
176
177 def retweet(%{assigns: %{user: user}} = conn, %{"id" => id}) do
178 with {:ok, status} <- TwitterAPI.repeat(user, id) do
179 json(conn, status)
180 end
181 end
182
183 def register(conn, params) do
184 with {:ok, user} <- TwitterAPI.register_user(params) do
185 render(conn, UserView, "show.json", %{user: user})
186 else
187 {:error, errors} ->
188 conn
189 |> json_reply(400, Poison.encode!(errors))
190 end
191 end
192
193 def update_avatar(%{assigns: %{user: user}} = conn, params) do
194 {:ok, object} = ActivityPub.upload(params)
195 change = Changeset.change(user, %{avatar: object.data})
196 {:ok, user} = Repo.update(change)
197
198 render(conn, UserView, "show.json", %{user: user, for: user})
199 end
200
201 def update_banner(%{assigns: %{user: user}} = conn, params) do
202 with {:ok, object} <- ActivityPub.upload(%{"img" => params["banner"]}),
203 new_info <- Map.put(user.info, "banner", object.data),
204 change <- User.info_changeset(user, %{info: new_info}),
205 {:ok, user} <- Repo.update(change) do
206 %{"url" => [ %{ "href" => href } | t ]} = object.data
207 response = %{ url: href } |> Poison.encode!
208 conn
209 |> json_reply(200, response)
210 end
211 end
212
213 def update_background(%{assigns: %{user: user}} = conn, params) do
214 with {:ok, object} <- ActivityPub.upload(params),
215 new_info <- Map.put(user.info, "background", object.data),
216 change <- User.info_changeset(user, %{info: new_info}),
217 {:ok, user} <- Repo.update(change) do
218 %{"url" => [ %{ "href" => href } | t ]} = object.data
219 response = %{ url: href } |> Poison.encode!
220 conn
221 |> json_reply(200, response)
222 end
223 end
224
225 def external_profile(%{assigns: %{user: current_user}} = conn, %{"profileurl" => uri}) do
226 with {:ok, user_map} <- TwitterAPI.get_external_profile(current_user, uri),
227 response <- Poison.encode!(user_map) do
228 conn
229 |> json_reply(200, response)
230 else
231 _e ->
232 conn
233 |> put_status(404)
234 |> json(%{error: "Can't find user"})
235 end
236 end
237
238 def update_most_recent_notification(%{assigns: %{user: user}} = conn, %{"id" => id}) do
239 with id when is_number(id) <- String.to_integer(id),
240 info <- user.info,
241 mrn <- max(id, user.info["most_recent_notification"] || 0),
242 updated_info <- Map.put(info, "most_recent_notification", mrn),
243 changeset <- User.info_changeset(user, %{info: updated_info}),
244 {:ok, user} <- Repo.update(changeset) do
245 conn
246 |> json_reply(200, Poison.encode!(mrn))
247 else
248 _e -> bad_request_reply(conn, "Can't update.")
249 end
250 end
251
252 def followers(%{assigns: %{user: user}} = conn, _params) do
253 with {:ok, followers} <- User.get_followers(user) do
254 render(conn, UserView, "index.json", %{users: followers, for: user})
255 else
256 _e -> bad_request_reply(conn, "Can't get followers")
257 end
258 end
259
260 def friends(%{assigns: %{user: user}} = conn, _params) do
261 with {:ok, friends} <- User.get_friends(user) do
262 render(conn, UserView, "index.json", %{users: friends, for: user})
263 else
264 _e -> bad_request_reply(conn, "Can't get friends")
265 end
266 end
267
268 def friends_ids(%{assigns: %{user: user}} = conn, _params) do
269 with {:ok, friends} <- User.get_friends(user) do
270 ids = friends
271 |> Enum.map(fn x -> x.id end)
272 |> Poison.encode!
273
274 json(conn, ids)
275 else
276 _e -> bad_request_reply(conn, "Can't get friends")
277 end
278 end
279
280 def empty_array(conn, _params) do
281 json(conn, Poison.encode!([]))
282 end
283
284 def update_profile(%{assigns: %{user: user}} = conn, params) do
285 params = if bio = params["description"] do
286 Map.put(params, "bio", bio)
287 else
288 params
289 end
290
291 with changeset <- User.update_changeset(user, params),
292 {:ok, user} <- Repo.update(changeset) do
293 render(conn, UserView, "user.json", %{user: user, for: user})
294 else
295 error ->
296 Logger.debug("Can't update user: #{inspect(error)}")
297 bad_request_reply(conn, "Can't update user")
298 end
299 end
300
301 def search(%{assigns: %{user: user}} = conn, %{"q" => query} = params) do
302 conn
303 |> json(TwitterAPI.search(user, params))
304 end
305
306 defp bad_request_reply(conn, error_message) do
307 json = error_json(conn, error_message)
308 json_reply(conn, 400, json)
309 end
310
311 defp json_reply(conn, status, json) do
312 conn
313 |> put_resp_content_type("application/json")
314 |> send_resp(status, json)
315 end
316
317 defp forbidden_json_reply(conn, error_message) do
318 json = error_json(conn, error_message)
319 json_reply(conn, 403, json)
320 end
321
322 defp error_json(conn, error_message) do
323 %{"error" => error_message, "request" => conn.request_path} |> Poison.encode!
324 end
325 end