Merge branch 'develop' of ssh.gitgud.io:lambadalambda/pleroma into develop
[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(%{assigns: %{user: user}} = conn, status_data) 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 end
21
22 defp extract_media_ids(status_data) do
23 with media_ids when not is_nil(media_ids) <- status_data["media_ids"],
24 split_ids <- String.split(media_ids, ","),
25 clean_ids <- Enum.reject(split_ids, fn (id) -> String.length(id) == 0 end)
26 do
27 clean_ids
28 else _e -> []
29 end
30 end
31
32 def public_timeline(%{assigns: %{user: user}} = conn, params) do
33 statuses = TwitterAPI.fetch_public_statuses(user, params)
34 {:ok, json} = Poison.encode(statuses)
35
36 conn
37 |> json_reply(200, json)
38 end
39
40 def friends_timeline(%{assigns: %{user: user}} = conn, params) do
41 statuses = TwitterAPI.fetch_friend_statuses(user, params)
42 {:ok, json} = Poison.encode(statuses)
43
44 conn
45 |> json_reply(200, json)
46 end
47
48 def user_timeline(%{assigns: %{user: user}} = conn, params) do
49 case TwitterAPI.get_user(user, params) do
50 {:ok, target_user} ->
51 params = Map.merge(params, %{"actor_id" => target_user.ap_id})
52 statuses = TwitterAPI.fetch_user_statuses(user, params)
53 conn
54 |> json_reply(200, statuses |> Poison.encode!)
55 {:error, msg} ->
56 bad_request_reply(conn, msg)
57 end
58 end
59
60 def mentions_timeline(%{assigns: %{user: user}} = conn, params) do
61 statuses = TwitterAPI.fetch_mentions(user, params)
62 {:ok, json} = Poison.encode(statuses)
63
64 conn
65 |> json_reply(200, json)
66 end
67
68 def follow(%{assigns: %{user: user}} = conn, %{ "user_id" => followed_id }) do
69 case TwitterAPI.follow(user, followed_id) do
70 { :ok, user, followed, _activity } ->
71 response = followed |> UserRepresenter.to_json(%{for: user})
72 conn
73 |> json_reply(200, response)
74 { :error, msg } -> forbidden_json_reply(conn, msg)
75 end
76 end
77
78 def unfollow(%{assigns: %{user: user}} = conn, %{ "user_id" => followed_id }) do
79 case TwitterAPI.unfollow(user, followed_id) do
80 { :ok, user, followed } ->
81 response = followed |> UserRepresenter.to_json(%{for: user})
82
83 conn
84 |> json_reply(200, response)
85 { :error, msg } -> forbidden_json_reply(conn, msg)
86 end
87 end
88
89 def fetch_status(%{assigns: %{user: user}} = conn, %{ "id" => id }) do
90 response = TwitterAPI.fetch_status(user, id) |> Poison.encode!
91
92 conn
93 |> json_reply(200, response)
94 end
95
96 def fetch_conversation(%{assigns: %{user: user}} = conn, %{ "id" => id }) do
97 id = String.to_integer(id)
98 response = TwitterAPI.fetch_conversation(user, id) |> Poison.encode!
99
100 conn
101 |> json_reply(200, response)
102 end
103
104 def upload(conn, %{"media" => media}) do
105 response = TwitterAPI.upload(media)
106 conn
107 |> put_resp_content_type("application/atom+xml")
108 |> send_resp(200, response)
109 end
110
111 def help_test(conn, _params) do
112 conn |> json_reply(200, Poison.encode!("ok"))
113 end
114
115 def upload_json(conn, %{"media" => media}) do
116 response = TwitterAPI.upload(media, "json")
117 conn
118 |> json_reply(200, response)
119 end
120
121 def config(conn, _params) do
122 response = %{
123 site: %{
124 name: Pleroma.Web.base_url,
125 server: Pleroma.Web.base_url,
126 textlimit: -1
127 }
128 }
129 |> Poison.encode!
130
131 conn
132 |> json_reply(200, response)
133 end
134
135 def favorite(%{assigns: %{user: user}} = conn, %{"id" => id}) do
136 activity = Repo.get(Activity, id)
137 {:ok, status} = TwitterAPI.favorite(user, activity)
138 response = Poison.encode!(status)
139
140 conn
141 |> json_reply(200, response)
142 end
143
144 def unfavorite(%{assigns: %{user: user}} = conn, %{"id" => id}) do
145 activity = Repo.get(Activity, id)
146 {:ok, status} = TwitterAPI.unfavorite(user, activity)
147 response = Poison.encode!(status)
148
149 conn
150 |> json_reply(200, response)
151 end
152
153 def retweet(%{assigns: %{user: user}} = conn, %{"id" => id}) do
154 activity = Repo.get(Activity, id)
155 {:ok, status} = TwitterAPI.retweet(user, activity)
156 response = Poison.encode!(status)
157
158 conn
159 |> json_reply(200, response)
160 end
161
162 def register(conn, params) do
163 with {:ok, user} <- TwitterAPI.register_user(params) do
164 conn
165 |> json_reply(200, Poison.encode!(user))
166 else
167 {:error, errors} ->
168 conn
169 |> json_reply(400, Poison.encode!(errors))
170 end
171 end
172
173 def update_avatar(%{assigns: %{user: user}} = conn, params) do
174 {:ok, object} = ActivityPub.upload(params)
175 change = Ecto.Changeset.change(user, %{avatar: object.data})
176 {:ok, user} = Repo.update(change)
177
178 response = UserRepresenter.to_map(user, %{for: user})
179 |> Poison.encode!
180
181 conn
182 |> json_reply(200, response)
183 end
184
185 defp bad_request_reply(conn, error_message) do
186 json = Poison.encode!(%{"error" => error_message})
187 json_reply(conn, 400, json)
188 end
189
190 defp json_reply(conn, status, json) do
191 conn
192 |> put_resp_content_type("application/json")
193 |> send_resp(status, json)
194 end
195
196 defp forbidden_json_reply(conn, error_message) do
197 json = %{"error" => error_message, "request" => conn.request_path}
198 |> Poison.encode!
199
200 json_reply(conn, 403, json)
201 end
202 end