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