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}
6 def verify_credentials(%{assigns: %{user: user}} = conn, _params) do
7 response = user |> UserRepresenter.to_json(%{for: user})
10 |> json_reply(200, response)
13 def status_update(%{assigns: %{user: user}} = conn, status_data) do
14 media_ids = extract_media_ids(status_data)
15 {:ok, activity} = TwitterAPI.create_status(user, Map.put(status_data, "media_ids", media_ids ))
17 |> json_reply(200, ActivityRepresenter.to_json(activity, %{user: user}))
20 defp extract_media_ids(status_data) do
21 with media_ids when not is_nil(media_ids) <- status_data["media_ids"],
22 split_ids <- String.split(media_ids, ","),
23 clean_ids <- Enum.reject(split_ids, fn (id) -> String.length(id) == 0 end)
30 def public_timeline(%{assigns: %{user: user}} = conn, params) do
31 statuses = TwitterAPI.fetch_public_statuses(user, params)
32 {:ok, json} = Poison.encode(statuses)
35 |> json_reply(200, json)
38 def friends_timeline(%{assigns: %{user: user}} = conn, params) do
39 statuses = TwitterAPI.fetch_friend_statuses(user, params)
40 {:ok, json} = Poison.encode(statuses)
43 |> json_reply(200, json)
46 def user_timeline(conn, %{ "user_id" => user_id } = params) do
47 user = Repo.get(User, user_id)
48 conn = Map.merge(conn, %{assigns: %{user: user}})
49 params = Map.delete(params, "user_id")
51 friends_timeline(conn, params)
54 def user_timeline(conn, %{ "screen_name" => nickname} = params) do
55 user = Repo.get_by!(User, nickname: nickname)
56 conn = Map.merge(conn, %{assigns: %{user: user}})
57 params = Map.delete(params, "screen_name")
59 friends_timeline(conn, params)
62 def user_timeline(conn, params) do
63 friends_timeline(conn, params)
66 def follow(%{assigns: %{user: user}} = conn, %{ "user_id" => followed_id }) do
67 { :ok, user, follower, _activity } = TwitterAPI.follow(user, followed_id)
69 response = follower |> UserRepresenter.to_json(%{for: user})
72 |> json_reply(200, response)
75 def unfollow(%{assigns: %{user: user}} = conn, %{ "user_id" => followed_id }) do
76 { :ok, user, follower } = TwitterAPI.unfollow(user, followed_id)
78 response = follower |> UserRepresenter.to_json(%{for: user})
81 |> json_reply(200, response)
84 def fetch_status(%{assigns: %{user: user}} = conn, %{ "id" => id }) do
85 response = TwitterAPI.fetch_status(user, id) |> Poison.encode!
88 |> json_reply(200, response)
91 def fetch_conversation(%{assigns: %{user: user}} = conn, %{ "id" => id }) do
92 id = String.to_integer(id)
93 response = TwitterAPI.fetch_conversation(user, id) |> Poison.encode!
96 |> json_reply(200, response)
99 def upload(conn, %{"media" => media}) do
100 response = TwitterAPI.upload(media)
102 |> put_resp_content_type("application/atom+xml")
103 |> send_resp(200, response)
106 def config(conn, _params) do
109 name: Pleroma.Web.base_url,
110 server: Pleroma.Web.base_url,
117 |> json_reply(200, response)
120 defp json_reply(conn, status, json) do
122 |> put_resp_content_type("application/json")
123 |> send_resp(status, json)