Merge branch 'bugfix/repeated-follow-unfollow' into 'develop'
[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 alias Pleroma.{Repo, Activity}
6 alias Pleroma.Web.ActivityPub.ActivityPub
7
8 def verify_credentials(%{assigns: %{user: user}} = conn, _params) do
9 response = user |> UserRepresenter.to_json(%{for: user})
10
11 conn
12 |> json_reply(200, response)
13 end
14
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 ))
18 conn
19 |> json_reply(200, ActivityRepresenter.to_json(activity, %{user: user}))
20 end
21
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)
26 do
27 clean_ids
28 else _e -> []
29 end
30 end
31
32 def public_timeline(%{assigns: %{user: user}} = conn, params) do
33 statuses = TwitterAPI.fetch_public_statuses(user, params)
34 {:ok, json} = Poison.encode(statuses)
35
36 conn
37 |> json_reply(200, json)
38 end
39
40 def friends_timeline(%{assigns: %{user: user}} = conn, params) do
41 statuses = TwitterAPI.fetch_friend_statuses(user, params)
42 {:ok, json} = Poison.encode(statuses)
43
44 conn
45 |> json_reply(200, json)
46 end
47
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})
52 conn
53 |> json_reply(200, response)
54 { :error, msg } -> forbidden_json_reply(conn, msg)
55 end
56 end
57
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})
62
63 conn
64 |> json_reply(200, response)
65 { :error, msg } -> forbidden_json_reply(conn, msg)
66 end
67 end
68
69 def fetch_status(%{assigns: %{user: user}} = conn, %{ "id" => id }) do
70 response = TwitterAPI.fetch_status(user, id) |> Poison.encode!
71
72 conn
73 |> json_reply(200, response)
74 end
75
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!
79
80 conn
81 |> json_reply(200, response)
82 end
83
84 def upload(conn, %{"media" => media}) do
85 response = TwitterAPI.upload(media)
86 conn
87 |> put_resp_content_type("application/atom+xml")
88 |> send_resp(200, response)
89 end
90
91 def help_test(conn, _params) do
92 conn |> json_reply(200, Poison.encode!("ok"))
93 end
94
95 def upload_json(conn, %{"media" => media}) do
96 response = TwitterAPI.upload(media, "json")
97 conn
98 |> json_reply(200, response)
99 end
100
101 def config(conn, _params) do
102 response = %{
103 site: %{
104 name: Pleroma.Web.base_url,
105 server: Pleroma.Web.base_url,
106 textlimit: -1
107 }
108 }
109 |> Poison.encode!
110
111 conn
112 |> json_reply(200, response)
113 end
114
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)
119
120 conn
121 |> json_reply(200, response)
122 end
123
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)
128
129 conn
130 |> json_reply(200, response)
131 end
132
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)
137
138 conn
139 |> json_reply(200, response)
140 end
141
142 def register(conn, params) do
143 with {:ok, user} <- TwitterAPI.register_user(params) do
144 conn
145 |> json_reply(200, Poison.encode!(user))
146 else
147 {:error, errors} ->
148 conn
149 |> json_reply(400, Poison.encode!(errors))
150 end
151 end
152
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)
157
158 response = UserRepresenter.to_map(user, %{for: user})
159 |> Poison.encode!
160
161 conn
162 |> json_reply(200, response)
163 end
164
165 defp json_reply(conn, status, json) do
166 conn
167 |> put_resp_content_type("application/json")
168 |> send_resp(status, json)
169 end
170
171 defp forbidden_json_reply(conn, error_message) do
172 json = %{"error" => error_message, "request" => conn.request_path}
173 |> Poison.encode!
174
175 json_reply(conn, 403, json)
176 end
177 end