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(%{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 ))
19 |> json_reply(200, ActivityRepresenter.to_json(activity, %{user: user}))
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)
32 def public_timeline(%{assigns: %{user: user}} = conn, params) do
33 statuses = TwitterAPI.fetch_public_statuses(user, params)
34 {:ok, json} = Poison.encode(statuses)
37 |> json_reply(200, json)
40 def friends_timeline(%{assigns: %{user: user}} = conn, params) do
41 statuses = TwitterAPI.fetch_friend_statuses(user, params)
42 {:ok, json} = Poison.encode(statuses)
45 |> json_reply(200, json)
48 def user_timeline(%{assigns: %{user: user}} = conn, params) do
49 case TwitterAPI.get_user(user, params) do
51 params = Map.merge(params, %{"actor_id" => target_user.ap_id})
52 statuses = TwitterAPI.fetch_user_statuses(user, params)
54 |> json_reply(200, statuses |> Poison.encode!)
56 bad_request_reply(conn, msg)
60 def mentions_timeline(%{assigns: %{user: user}} = conn, params) do
61 statuses = TwitterAPI.fetch_mentions(user, params)
62 {:ok, json} = Poison.encode(statuses)
65 |> json_reply(200, json)
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})
73 |> json_reply(200, response)
74 { :error, msg } -> forbidden_json_reply(conn, msg)
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})
84 |> json_reply(200, response)
85 { :error, msg } -> forbidden_json_reply(conn, msg)
89 def fetch_status(%{assigns: %{user: user}} = conn, %{ "id" => id }) do
90 response = TwitterAPI.fetch_status(user, id) |> Poison.encode!
93 |> json_reply(200, response)
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!
101 |> json_reply(200, response)
104 def upload(conn, %{"media" => media}) do
105 response = TwitterAPI.upload(media)
107 |> put_resp_content_type("application/atom+xml")
108 |> send_resp(200, response)
111 def help_test(conn, _params) do
112 conn |> json_reply(200, Poison.encode!("ok"))
115 def upload_json(conn, %{"media" => media}) do
116 response = TwitterAPI.upload(media, "json")
118 |> json_reply(200, response)
121 def config(conn, _params) do
124 name: Pleroma.Web.base_url,
125 server: Pleroma.Web.base_url,
132 |> json_reply(200, response)
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)
141 |> json_reply(200, response)
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)
150 |> json_reply(200, response)
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)
159 |> json_reply(200, response)
162 def register(conn, params) do
163 with {:ok, user} <- TwitterAPI.register_user(params) do
165 |> json_reply(200, Poison.encode!(user))
169 |> json_reply(400, Poison.encode!(errors))
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)
178 response = UserRepresenter.to_map(user, %{for: user})
182 |> json_reply(200, response)
185 defp bad_request_reply(conn, error_message) do
186 json = Poison.encode!(%{"error" => error_message})
187 json_reply(conn, 400, json)
190 defp json_reply(conn, status, json) do
192 |> put_resp_content_type("application/json")
193 |> send_resp(status, json)
196 defp forbidden_json_reply(conn, error_message) do
197 json = %{"error" => error_message, "request" => conn.request_path}
200 json_reply(conn, 403, json)