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
8 def verify_credentials(%{assigns: %{user: user}} = conn, _params) do
9 response = user |> UserRepresenter.to_json(%{for: user})
12 |> json_reply(200, response)
15 def status_update(conn, %{"status" => ""} = _status_data) do
16 empty_status_reply(conn)
19 def status_update(%{assigns: %{user: user}} = conn, %{"status" => _status_text} = status_data) do
20 media_ids = extract_media_ids(status_data)
21 {:ok, activity} = TwitterAPI.create_status(user, Map.put(status_data, "media_ids", media_ids ))
23 |> json_reply(200, ActivityRepresenter.to_json(activity, %{user: user}))
26 def status_update(conn, _status_data) do
27 empty_status_reply(conn)
30 defp empty_status_reply(conn) do
31 bad_request_reply(conn, "Client must provide a 'status' parameter with a value.")
34 defp extract_media_ids(status_data) do
35 with media_ids when not is_nil(media_ids) <- status_data["media_ids"],
36 split_ids <- String.split(media_ids, ","),
37 clean_ids <- Enum.reject(split_ids, fn (id) -> String.length(id) == 0 end)
44 def public_timeline(%{assigns: %{user: user}} = conn, params) do
45 statuses = TwitterAPI.fetch_public_statuses(user, params)
46 {:ok, json} = Poison.encode(statuses)
49 |> json_reply(200, json)
52 def friends_timeline(%{assigns: %{user: user}} = conn, params) do
53 statuses = TwitterAPI.fetch_friend_statuses(user, params)
54 {:ok, json} = Poison.encode(statuses)
57 |> json_reply(200, json)
60 def user_timeline(%{assigns: %{user: user}} = conn, params) do
61 case TwitterAPI.get_user(user, params) do
63 params = Map.merge(params, %{"actor_id" => target_user.ap_id})
64 statuses = TwitterAPI.fetch_user_statuses(user, params)
66 |> json_reply(200, statuses |> Poison.encode!)
68 bad_request_reply(conn, msg)
72 def mentions_timeline(%{assigns: %{user: user}} = conn, params) do
73 statuses = TwitterAPI.fetch_mentions(user, params)
74 {:ok, json} = Poison.encode(statuses)
77 |> json_reply(200, json)
80 def follow(%{assigns: %{user: user}} = conn, params) do
81 case TwitterAPI.follow(user, params) do
82 { :ok, user, followed, _activity } ->
83 response = followed |> UserRepresenter.to_json(%{for: user})
85 |> json_reply(200, response)
86 { :error, msg } -> forbidden_json_reply(conn, msg)
90 def unfollow(%{assigns: %{user: user}} = conn, params) do
91 case TwitterAPI.unfollow(user, params) do
92 { :ok, user, unfollowed, } ->
93 response = unfollowed |> UserRepresenter.to_json(%{for: user})
95 |> json_reply(200, response)
96 { :error, msg } -> forbidden_json_reply(conn, msg)
100 def fetch_status(%{assigns: %{user: user}} = conn, %{ "id" => id }) do
101 response = TwitterAPI.fetch_status(user, id) |> Poison.encode!
104 |> json_reply(200, response)
107 def fetch_conversation(%{assigns: %{user: user}} = conn, %{ "id" => id }) do
108 id = String.to_integer(id)
109 response = TwitterAPI.fetch_conversation(user, id) |> Poison.encode!
112 |> json_reply(200, response)
115 def upload(conn, %{"media" => media}) do
116 response = TwitterAPI.upload(media)
118 |> put_resp_content_type("application/atom+xml")
119 |> send_resp(200, response)
122 def help_test(conn, _params) do
123 conn |> json_reply(200, Poison.encode!("ok"))
126 def upload_json(conn, %{"media" => media}) do
127 response = TwitterAPI.upload(media, "json")
129 |> json_reply(200, response)
132 def config(conn, _params) do
135 name: Pleroma.Web.base_url,
136 server: Pleroma.Web.base_url,
143 |> json_reply(200, response)
146 def favorite(%{assigns: %{user: user}} = conn, %{"id" => id}) do
147 activity = Repo.get(Activity, id)
148 {:ok, status} = TwitterAPI.favorite(user, activity)
149 response = Poison.encode!(status)
152 |> json_reply(200, response)
155 def unfavorite(%{assigns: %{user: user}} = conn, %{"id" => id}) do
156 activity = Repo.get(Activity, id)
157 {:ok, status} = TwitterAPI.unfavorite(user, activity)
158 response = Poison.encode!(status)
161 |> json_reply(200, response)
164 def retweet(%{assigns: %{user: user}} = conn, %{"id" => id}) do
165 activity = Repo.get(Activity, id)
166 if activity.data["actor"] == user.ap_id do
167 bad_request_reply(conn, "You cannot repeat your own notice.")
169 {:ok, status} = TwitterAPI.retweet(user, activity)
170 response = Poison.encode!(status)
174 |> json_reply(200, response)
178 def register(conn, params) do
179 with {:ok, user} <- TwitterAPI.register_user(params) do
181 |> json_reply(200, Poison.encode!(user))
185 |> json_reply(400, Poison.encode!(errors))
189 def update_avatar(%{assigns: %{user: user}} = conn, params) do
190 {:ok, object} = ActivityPub.upload(params)
191 change = Ecto.Changeset.change(user, %{avatar: object.data})
192 {:ok, user} = Repo.update(change)
194 response = UserRepresenter.to_map(user, %{for: user})
198 |> json_reply(200, response)
201 defp bad_request_reply(conn, error_message) do
202 json = error_json(conn, error_message)
203 json_reply(conn, 400, json)
206 defp json_reply(conn, status, json) do
208 |> put_resp_content_type("application/json")
209 |> send_resp(status, json)
212 defp forbidden_json_reply(conn, error_message) do
213 json = error_json(conn, error_message)
214 json_reply(conn, 403, json)
217 defp error_json(conn, error_message) do
218 %{"error" => error_message, "request" => conn.request_path} |> Poison.encode!