c3ab5ae8c10b46fbb0ce156bb871e239fe3c5f7a
[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, Repo, Activity}
6 alias Pleroma.Web.ActivityPub.ActivityPub
7 alias Ecto.Changeset
8
9 def verify_credentials(%{assigns: %{user: user}} = conn, _params) do
10 render(conn, UserView, "show.json", %{user: user})
11 end
12
13 def status_update(%{assigns: %{user: user}} = conn, %{"status" => status_text} = status_data) do
14 l = status_text |> String.trim |> String.length
15 if l > 0 && l < 5000 do
16 media_ids = extract_media_ids(status_data)
17 {:ok, activity} = TwitterAPI.create_status(user, Map.put(status_data, "media_ids", media_ids))
18 conn
19 |> json_reply(200, ActivityRepresenter.to_json(activity, %{user: user}))
20 else
21 empty_status_reply(conn)
22 end
23 end
24
25 def status_update(conn, _status_data) do
26 empty_status_reply(conn)
27 end
28
29 defp empty_status_reply(conn) do
30 bad_request_reply(conn, "Client must provide a 'status' parameter with a value.")
31 end
32
33 defp extract_media_ids(status_data) do
34 with media_ids when not is_nil(media_ids) <- status_data["media_ids"],
35 split_ids <- String.split(media_ids, ","),
36 clean_ids <- Enum.reject(split_ids, fn (id) -> String.length(id) == 0 end)
37 do
38 clean_ids
39 else _e -> []
40 end
41 end
42
43 def public_and_external_timeline(%{assigns: %{user: user}} = conn, params) do
44 statuses = TwitterAPI.fetch_public_and_external_statuses(user, params)
45 {:ok, json} = Poison.encode(statuses)
46
47 conn
48 |> json_reply(200, json)
49 end
50
51 def public_timeline(%{assigns: %{user: user}} = conn, params) do
52 statuses = TwitterAPI.fetch_public_statuses(user, params)
53 {:ok, json} = Poison.encode(statuses)
54
55 conn
56 |> json_reply(200, json)
57 end
58
59 def friends_timeline(%{assigns: %{user: user}} = conn, params) do
60 statuses = TwitterAPI.fetch_friend_statuses(user, params)
61 {:ok, json} = Poison.encode(statuses)
62
63 conn
64 |> json_reply(200, json)
65 end
66
67 def user_timeline(%{assigns: %{user: user}} = conn, params) do
68 case TwitterAPI.get_user(user, params) do
69 {:ok, target_user} ->
70 params = Map.merge(params, %{"actor_id" => target_user.ap_id})
71 statuses = TwitterAPI.fetch_user_statuses(user, params)
72 conn
73 |> json_reply(200, statuses |> Poison.encode!)
74 {:error, msg} ->
75 bad_request_reply(conn, msg)
76 end
77 end
78
79 def mentions_timeline(%{assigns: %{user: user}} = conn, params) do
80 statuses = TwitterAPI.fetch_mentions(user, params)
81 {:ok, json} = Poison.encode(statuses)
82
83 conn
84 |> json_reply(200, json)
85 end
86
87 def follow(%{assigns: %{user: user}} = conn, params) do
88 case TwitterAPI.follow(user, params) do
89 {:ok, user, followed, _activity} ->
90 render(conn, UserView, "show.json", %{user: followed, for: user})
91 {:error, msg} -> forbidden_json_reply(conn, msg)
92 end
93 end
94
95 def unfollow(%{assigns: %{user: user}} = conn, params) do
96 case TwitterAPI.unfollow(user, params) do
97 {:ok, user, unfollowed} ->
98 render(conn, UserView, "show.json", %{user: unfollowed, for: user})
99 {:error, msg} -> forbidden_json_reply(conn, msg)
100 end
101 end
102
103 def fetch_status(%{assigns: %{user: user}} = conn, %{"id" => id}) do
104 response = Poison.encode!(TwitterAPI.fetch_status(user, id))
105
106 conn
107 |> json_reply(200, response)
108 end
109
110 def fetch_conversation(%{assigns: %{user: user}} = conn, %{"id" => id}) do
111 id = String.to_integer(id)
112 response = Poison.encode!(TwitterAPI.fetch_conversation(user, id))
113
114 conn
115 |> json_reply(200, response)
116 end
117
118 def upload(conn, %{"media" => media}) do
119 response = TwitterAPI.upload(media)
120 conn
121 |> put_resp_content_type("application/atom+xml")
122 |> send_resp(200, response)
123 end
124
125 def help_test(conn, _params) do
126 conn |> json_reply(200, Poison.encode!("ok"))
127 end
128
129 def upload_json(conn, %{"media" => media}) do
130 response = TwitterAPI.upload(media, "json")
131 conn
132 |> json_reply(200, response)
133 end
134
135 def config(conn, _params) do
136 response = %{
137 site: %{
138 name: Web.base_url,
139 server: Web.base_url,
140 textlimit: -1
141 }
142 }
143 |> Poison.encode!
144
145 conn
146 |> json_reply(200, response)
147 end
148
149 def favorite(%{assigns: %{user: user}} = conn, %{"id" => id}) do
150 activity = Repo.get(Activity, id)
151 {:ok, status} = TwitterAPI.favorite(user, activity)
152 response = Poison.encode!(status)
153
154 conn
155 |> json_reply(200, response)
156 end
157
158 def unfavorite(%{assigns: %{user: user}} = conn, %{"id" => id}) do
159 activity = Repo.get(Activity, id)
160 {:ok, status} = TwitterAPI.unfavorite(user, activity)
161 response = Poison.encode!(status)
162
163 conn
164 |> json_reply(200, response)
165 end
166
167 def retweet(%{assigns: %{user: user}} = conn, %{"id" => id}) do
168 activity = Repo.get(Activity, id)
169 if activity.data["actor"] == user.ap_id do
170 bad_request_reply(conn, "You cannot repeat your own notice.")
171 else
172 {:ok, status} = TwitterAPI.retweet(user, activity)
173 response = Poison.encode!(status)
174
175 conn
176
177 |> json_reply(200, response)
178 end
179 end
180
181 def register(conn, params) do
182 with {:ok, user} <- TwitterAPI.register_user(params) do
183
184 render(conn, UserView, "show.json", %{user: user})
185 else
186 {:error, errors} ->
187 conn
188 |> json_reply(400, Poison.encode!(errors))
189 end
190 end
191
192 def update_avatar(%{assigns: %{user: user}} = conn, params) do
193 {:ok, object} = ActivityPub.upload(params)
194 change = Changeset.change(user, %{avatar: object.data})
195 {:ok, user} = Repo.update(change)
196
197 render(conn, UserView, "show.json", %{user: user, for: user})
198 end
199
200 def external_profile(%{assigns: %{user: current_user}} = conn, %{"profileurl" => uri}) do
201 with {:ok, user_map} <- TwitterAPI.get_external_profile(current_user, uri),
202 response <- Poison.encode!(user_map) do
203 conn
204 |> json_reply(200, response)
205 end
206 end
207
208 defp bad_request_reply(conn, error_message) do
209 json = error_json(conn, error_message)
210 json_reply(conn, 400, json)
211 end
212
213 defp json_reply(conn, status, json) do
214 conn
215 |> put_resp_content_type("application/json")
216 |> send_resp(status, json)
217 end
218
219 defp forbidden_json_reply(conn, error_message) do
220 json = error_json(conn, error_message)
221 json_reply(conn, 403, json)
222 end
223
224 defp error_json(conn, error_message) do
225 %{"error" => error_message, "request" => conn.request_path} |> Poison.encode!
226 end
227 end