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 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})
65 |> json_reply(200, response)
66 { :error, msg } -> forbidden_json_reply(conn, msg)
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})
76 |> json_reply(200, response)
77 { :error, msg } -> forbidden_json_reply(conn, msg)
81 def fetch_status(%{assigns: %{user: user}} = conn, %{ "id" => id }) do
82 response = TwitterAPI.fetch_status(user, id) |> Poison.encode!
85 |> json_reply(200, response)
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!
93 |> json_reply(200, response)
96 def upload(conn, %{"media" => media}) do
97 response = TwitterAPI.upload(media)
99 |> put_resp_content_type("application/atom+xml")
100 |> send_resp(200, response)
103 def help_test(conn, _params) do
104 conn |> json_reply(200, Poison.encode!("ok"))
107 def upload_json(conn, %{"media" => media}) do
108 response = TwitterAPI.upload(media, "json")
110 |> json_reply(200, response)
113 def config(conn, _params) do
116 name: Pleroma.Web.base_url,
117 server: Pleroma.Web.base_url,
124 |> json_reply(200, response)
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)
133 |> json_reply(200, response)
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)
142 |> json_reply(200, response)
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)
151 |> json_reply(200, response)
154 def register(conn, params) do
155 with {:ok, user} <- TwitterAPI.register_user(params) do
157 |> json_reply(200, Poison.encode!(user))
161 |> json_reply(400, Poison.encode!(errors))
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)
170 response = UserRepresenter.to_map(user, %{for: user})
174 |> json_reply(200, response)
177 defp bad_request_reply(conn, error_message) do
178 json = Poison.encode!(%{"error" => error_message})
179 json_reply(conn, 400, json)
182 defp json_reply(conn, status, json) do
184 |> put_resp_content_type("application/json")
185 |> send_resp(status, json)
188 defp forbidden_json_reply(conn, error_message) do
189 json = %{"error" => error_message, "request" => conn.request_path}
192 json_reply(conn, 403, json)