Merge branch 'feature/user-timeline' 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 follow(%{assigns: %{user: user}} = conn, %{ "user_id" => followed_id }) do
61 case TwitterAPI.follow(user, followed_id) do
62 { :ok, user, followed, _activity } ->
63 response = followed |> UserRepresenter.to_json(%{for: user})
64 conn
65 |> json_reply(200, response)
66 { :error, msg } -> forbidden_json_reply(conn, msg)
67 end
68 end
69
70 def unfollow(%{assigns: %{user: user}} = conn, %{ "user_id" => followed_id }) do
71 case TwitterAPI.unfollow(user, followed_id) do
72 { :ok, user, followed } ->
73 response = followed |> UserRepresenter.to_json(%{for: user})
74
75 conn
76 |> json_reply(200, response)
77 { :error, msg } -> forbidden_json_reply(conn, msg)
78 end
79 end
80
81 def fetch_status(%{assigns: %{user: user}} = conn, %{ "id" => id }) do
82 response = TwitterAPI.fetch_status(user, id) |> Poison.encode!
83
84 conn
85 |> json_reply(200, response)
86 end
87
88 def fetch_conversation(%{assigns: %{user: user}} = conn, %{ "id" => id }) do
89 id = String.to_integer(id)
90 response = TwitterAPI.fetch_conversation(user, id) |> Poison.encode!
91
92 conn
93 |> json_reply(200, response)
94 end
95
96 def upload(conn, %{"media" => media}) do
97 response = TwitterAPI.upload(media)
98 conn
99 |> put_resp_content_type("application/atom+xml")
100 |> send_resp(200, response)
101 end
102
103 def help_test(conn, _params) do
104 conn |> json_reply(200, Poison.encode!("ok"))
105 end
106
107 def upload_json(conn, %{"media" => media}) do
108 response = TwitterAPI.upload(media, "json")
109 conn
110 |> json_reply(200, response)
111 end
112
113 def config(conn, _params) do
114 response = %{
115 site: %{
116 name: Pleroma.Web.base_url,
117 server: Pleroma.Web.base_url,
118 textlimit: -1
119 }
120 }
121 |> Poison.encode!
122
123 conn
124 |> json_reply(200, response)
125 end
126
127 def favorite(%{assigns: %{user: user}} = conn, %{"id" => id}) do
128 activity = Repo.get(Activity, id)
129 {:ok, status} = TwitterAPI.favorite(user, activity)
130 response = Poison.encode!(status)
131
132 conn
133 |> json_reply(200, response)
134 end
135
136 def unfavorite(%{assigns: %{user: user}} = conn, %{"id" => id}) do
137 activity = Repo.get(Activity, id)
138 {:ok, status} = TwitterAPI.unfavorite(user, activity)
139 response = Poison.encode!(status)
140
141 conn
142 |> json_reply(200, response)
143 end
144
145 def retweet(%{assigns: %{user: user}} = conn, %{"id" => id}) do
146 activity = Repo.get(Activity, id)
147 {:ok, status} = TwitterAPI.retweet(user, activity)
148 response = Poison.encode!(status)
149
150 conn
151 |> json_reply(200, response)
152 end
153
154 def register(conn, params) do
155 with {:ok, user} <- TwitterAPI.register_user(params) do
156 conn
157 |> json_reply(200, Poison.encode!(user))
158 else
159 {:error, errors} ->
160 conn
161 |> json_reply(400, Poison.encode!(errors))
162 end
163 end
164
165 def update_avatar(%{assigns: %{user: user}} = conn, params) do
166 {:ok, object} = ActivityPub.upload(params)
167 change = Ecto.Changeset.change(user, %{avatar: object.data})
168 {:ok, user} = Repo.update(change)
169
170 response = UserRepresenter.to_map(user, %{for: user})
171 |> Poison.encode!
172
173 conn
174 |> json_reply(200, response)
175 end
176
177 defp bad_request_reply(conn, error_message) do
178 json = Poison.encode!(%{"error" => error_message})
179 json_reply(conn, 400, json)
180 end
181
182 defp json_reply(conn, status, json) do
183 conn
184 |> put_resp_content_type("application/json")
185 |> send_resp(status, json)
186 end
187
188 defp forbidden_json_reply(conn, error_message) do
189 json = %{"error" => error_message, "request" => conn.request_path}
190 |> Poison.encode!
191
192 json_reply(conn, 403, json)
193 end
194 end