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 follow(%{assigns: %{user: user}} = conn, %{ "user_id" => followed_id }) do
49 case TwitterAPI.follow(user, followed_id) do
50 { :ok, user, followed, _activity } ->
51 response = followed |> UserRepresenter.to_json(%{for: user})
53 |> json_reply(200, response)
54 { :error, msg } -> forbidden_json_reply(conn, msg)
58 def unfollow(%{assigns: %{user: user}} = conn, %{ "user_id" => followed_id }) do
59 case TwitterAPI.unfollow(user, followed_id) do
60 { :ok, user, followed } ->
61 response = followed |> UserRepresenter.to_json(%{for: user})
64 |> json_reply(200, response)
65 { :error, msg } -> forbidden_json_reply(conn, msg)
69 def fetch_status(%{assigns: %{user: user}} = conn, %{ "id" => id }) do
70 response = TwitterAPI.fetch_status(user, id) |> Poison.encode!
73 |> json_reply(200, response)
76 def fetch_conversation(%{assigns: %{user: user}} = conn, %{ "id" => id }) do
77 id = String.to_integer(id)
78 response = TwitterAPI.fetch_conversation(user, id) |> Poison.encode!
81 |> json_reply(200, response)
84 def upload(conn, %{"media" => media}) do
85 response = TwitterAPI.upload(media)
87 |> put_resp_content_type("application/atom+xml")
88 |> send_resp(200, response)
91 def help_test(conn, _params) do
92 conn |> json_reply(200, Poison.encode!("ok"))
95 def upload_json(conn, %{"media" => media}) do
96 response = TwitterAPI.upload(media, "json")
98 |> json_reply(200, response)
101 def config(conn, _params) do
104 name: Pleroma.Web.base_url,
105 server: Pleroma.Web.base_url,
112 |> json_reply(200, response)
115 def favorite(%{assigns: %{user: user}} = conn, %{"id" => id}) do
116 activity = Repo.get(Activity, id)
117 {:ok, status} = TwitterAPI.favorite(user, activity)
118 response = Poison.encode!(status)
121 |> json_reply(200, response)
124 def unfavorite(%{assigns: %{user: user}} = conn, %{"id" => id}) do
125 activity = Repo.get(Activity, id)
126 {:ok, status} = TwitterAPI.unfavorite(user, activity)
127 response = Poison.encode!(status)
130 |> json_reply(200, response)
133 def retweet(%{assigns: %{user: user}} = conn, %{"id" => id}) do
134 activity = Repo.get(Activity, id)
135 {:ok, status} = TwitterAPI.retweet(user, activity)
136 response = Poison.encode!(status)
139 |> json_reply(200, response)
142 def register(conn, params) do
143 with {:ok, user} <- TwitterAPI.register_user(params) do
145 |> json_reply(200, Poison.encode!(user))
149 |> json_reply(400, Poison.encode!(errors))
153 def update_avatar(%{assigns: %{user: user}} = conn, params) do
154 {:ok, object} = ActivityPub.upload(params)
155 change = Ecto.Changeset.change(user, %{avatar: object.data})
156 {:ok, user} = Repo.update(change)
158 response = UserRepresenter.to_map(user, %{for: user})
162 |> json_reply(200, response)
165 defp json_reply(conn, status, json) do
167 |> put_resp_content_type("application/json")
168 |> send_resp(status, json)
171 defp forbidden_json_reply(conn, error_message) do
172 json = %{"error" => error_message, "request" => conn.request_path}
175 json_reply(conn, 403, json)