Merge branch 'feature/help-test' 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 { :ok, user, follower, _activity } = TwitterAPI.follow(user, followed_id)
50
51 response = follower |> UserRepresenter.to_json(%{for: user})
52
53 conn
54 |> json_reply(200, response)
55 end
56
57 def unfollow(%{assigns: %{user: user}} = conn, %{ "user_id" => followed_id }) do
58 { :ok, user, follower } = TwitterAPI.unfollow(user, followed_id)
59
60 response = follower |> UserRepresenter.to_json(%{for: user})
61
62 conn
63 |> json_reply(200, response)
64 end
65
66 def fetch_status(%{assigns: %{user: user}} = conn, %{ "id" => id }) do
67 response = TwitterAPI.fetch_status(user, id) |> Poison.encode!
68
69 conn
70 |> json_reply(200, response)
71 end
72
73 def fetch_conversation(%{assigns: %{user: user}} = conn, %{ "id" => id }) do
74 id = String.to_integer(id)
75 response = TwitterAPI.fetch_conversation(user, id) |> Poison.encode!
76
77 conn
78 |> json_reply(200, response)
79 end
80
81 def upload(conn, %{"media" => media}) do
82 response = TwitterAPI.upload(media)
83 conn
84 |> put_resp_content_type("application/atom+xml")
85 |> send_resp(200, response)
86 end
87
88 def help_test(conn, _params) do
89 conn |> json_reply(200, Poison.encode!("ok"))
90 end
91
92 def upload_json(conn, %{"media" => media}) do
93 response = TwitterAPI.upload(media, "json")
94 conn
95 |> json_reply(200, response)
96 end
97
98 def config(conn, _params) do
99 response = %{
100 site: %{
101 name: Pleroma.Web.base_url,
102 server: Pleroma.Web.base_url,
103 textlimit: -1
104 }
105 }
106 |> Poison.encode!
107
108 conn
109 |> json_reply(200, response)
110 end
111
112 def favorite(%{assigns: %{user: user}} = conn, %{"id" => id}) do
113 activity = Repo.get(Activity, id)
114 {:ok, status} = TwitterAPI.favorite(user, activity)
115 response = Poison.encode!(status)
116
117 conn
118 |> json_reply(200, response)
119 end
120
121 def unfavorite(%{assigns: %{user: user}} = conn, %{"id" => id}) do
122 activity = Repo.get(Activity, id)
123 {:ok, status} = TwitterAPI.unfavorite(user, activity)
124 response = Poison.encode!(status)
125
126 conn
127 |> json_reply(200, response)
128 end
129
130 def retweet(%{assigns: %{user: user}} = conn, %{"id" => id}) do
131 activity = Repo.get(Activity, id)
132 {:ok, status} = TwitterAPI.retweet(user, activity)
133 response = Poison.encode!(status)
134
135 conn
136 |> json_reply(200, response)
137 end
138
139 def register(conn, params) do
140 with {:ok, user} <- TwitterAPI.register_user(params) do
141 conn
142 |> json_reply(200, Poison.encode!(user))
143 else
144 {:error, errors} ->
145 conn
146 |> json_reply(400, Poison.encode!(errors))
147 end
148 end
149
150 def update_avatar(%{assigns: %{user: user}} = conn, params) do
151 {:ok, object} = ActivityPub.upload(params)
152 change = Ecto.Changeset.change(user, %{avatar: object.data})
153 {:ok, user} = Repo.update(change)
154
155 response = UserRepresenter.to_map(user, %{for: user})
156 |> Poison.encode!
157
158 conn
159 |> json_reply(200, response)
160 end
161
162 defp json_reply(conn, status, json) do
163 conn
164 |> put_resp_content_type("application/json")
165 |> send_resp(status, json)
166 end
167 end