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