Merge branch 'develop' of ssh.gitgud.io:lambadalambda/pleroma into feature/user-timeline
[akkoma] / lib / pleroma / web / twitter_api / twitter_api_controller.ex
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
6 def verify_credentials(%{assigns: %{user: user}} = conn, _params) do
7 response = user |> UserRepresenter.to_json(%{for: user})
8
9 conn
10 |> json_reply(200, response)
11 end
12
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 ))
16 conn
17 |> json_reply(200, ActivityRepresenter.to_json(activity, %{user: user}))
18 end
19
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)
24 do
25 clean_ids
26 else _e -> []
27 end
28 end
29
30 def public_timeline(%{assigns: %{user: user}} = conn, params) do
31 statuses = TwitterAPI.fetch_public_statuses(user, params)
32 {:ok, json} = Poison.encode(statuses)
33
34 conn
35 |> json_reply(200, json)
36 end
37
38 def friends_timeline(%{assigns: %{user: user}} = conn, params) do
39 statuses = TwitterAPI.fetch_friend_statuses(user, params)
40 {:ok, json} = Poison.encode(statuses)
41
42 conn
43 |> json_reply(200, json)
44 end
45
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")
50
51 friends_timeline(conn, params)
52 end
53
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")
58
59 friends_timeline(conn, params)
60 end
61
62 def user_timeline(conn, params) do
63 friends_timeline(conn, params)
64 end
65
66 def follow(%{assigns: %{user: user}} = conn, %{ "user_id" => followed_id }) do
67 { :ok, user, follower, _activity } = TwitterAPI.follow(user, followed_id)
68
69 response = follower |> UserRepresenter.to_json(%{for: user})
70
71 conn
72 |> json_reply(200, response)
73 end
74
75 def unfollow(%{assigns: %{user: user}} = conn, %{ "user_id" => followed_id }) do
76 { :ok, user, follower } = TwitterAPI.unfollow(user, followed_id)
77
78 response = follower |> UserRepresenter.to_json(%{for: user})
79
80 conn
81 |> json_reply(200, response)
82 end
83
84 def fetch_status(%{assigns: %{user: user}} = conn, %{ "id" => id }) do
85 response = TwitterAPI.fetch_status(user, id) |> Poison.encode!
86
87 conn
88 |> json_reply(200, response)
89 end
90
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!
94
95 conn
96 |> json_reply(200, response)
97 end
98
99 def upload(conn, %{"media" => media}) do
100 response = TwitterAPI.upload(media)
101 conn
102 |> put_resp_content_type("application/atom+xml")
103 |> send_resp(200, response)
104 end
105
106 def config(conn, _params) do
107 response = %{
108 site: %{
109 name: Pleroma.Web.base_url,
110 server: Pleroma.Web.base_url,
111 textlimit: -1
112 }
113 }
114 |> Poison.encode!
115
116 conn
117 |> json_reply(200, response)
118 end
119
120 defp json_reply(conn, status, json) do
121 conn
122 |> put_resp_content_type("application/json")
123 |> send_resp(status, json)
124 end
125 end