twitter_api_controller.ex 6.11 KB
Newer Older
1
2
defmodule Pleroma.Web.TwitterAPI.Controller do
  use Pleroma.Web, :controller
lain's avatar
lain committed
3
4
  alias Pleroma.Web.TwitterAPI.TwitterAPI
  alias Pleroma.Web.TwitterAPI.Representers.{UserRepresenter, ActivityRepresenter}
lain's avatar
lain committed
5
  alias Pleroma.{Repo, Activity}
lain's avatar
lain committed
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
12
13
14

    conn
    |> json_reply(200, response)
  end

dtluna's avatar
dtluna committed
15
16
17
18
  def status_update(conn, %{"status" => ""} = _status_data) do
    empty_status_reply(conn)
  end

dtluna's avatar
dtluna committed
19
20
21
22
23
24
25
26
27
  def status_update(%{assigns: %{user: user}} = conn, %{"status" => status_text} = status_data) do
    if status_text |> String.trim |> String.length != 0 do
      media_ids = extract_media_ids(status_data)
      {:ok, activity} = TwitterAPI.create_status(user, Map.put(status_data, "media_ids",  media_ids ))
      conn
      |> json_reply(200, ActivityRepresenter.to_json(activity, %{user: user}))
    else
      empty_status_reply(conn)
    end
lain's avatar
lain committed
28
29
  end

dtluna's avatar
dtluna committed
30
31
32
33
34
35
36
37
  def status_update(conn, _status_data) do
    empty_status_reply(conn)
  end

  defp empty_status_reply(conn) do
    bad_request_reply(conn, "Client must provide a 'status' parameter with a value.")
  end

lain's avatar
lain committed
38
39
40
41
42
43
44
45
46
47
  defp extract_media_ids(status_data) do
    with media_ids when not is_nil(media_ids) <- status_data["media_ids"],
         split_ids <- String.split(media_ids, ","),
         clean_ids <- Enum.reject(split_ids, fn (id) -> String.length(id) == 0 end)
      do
        clean_ids
      else _e -> []
    end
  end

48
49
  def public_timeline(%{assigns: %{user: user}} = conn, params) do
    statuses = TwitterAPI.fetch_public_statuses(user, params)
lain's avatar
lain committed
50
51
52
53
54
55
    {:ok, json} = Poison.encode(statuses)

    conn
    |> json_reply(200, json)
  end

lain's avatar
lain committed
56
57
58
59
60
61
62
63
  def friends_timeline(%{assigns: %{user: user}} = conn, params) do
    statuses = TwitterAPI.fetch_friend_statuses(user, params)
    {:ok, json} = Poison.encode(statuses)

    conn
    |> json_reply(200, json)
  end

dtluna's avatar
dtluna committed
64
  def user_timeline(%{assigns: %{user: user}} = conn, params) do
65
66
67
68
69
70
71
72
73
    case TwitterAPI.get_user(user, params) do
      {:ok, target_user} ->
        params = Map.merge(params, %{"actor_id" => target_user.ap_id})
        statuses  = TwitterAPI.fetch_user_statuses(user, params)
        conn
        |> json_reply(200, statuses |> Poison.encode!)
      {:error, msg} ->
        bad_request_reply(conn, msg)
    end
dtluna's avatar
dtluna committed
74
75
  end

dtluna's avatar
dtluna committed
76
77
78
79
80
81
82
83
  def mentions_timeline(%{assigns: %{user: user}} = conn, params) do
    statuses = TwitterAPI.fetch_mentions(user, params)
    {:ok, json} = Poison.encode(statuses)

    conn
    |> json_reply(200, json)
  end

84
85
  def follow(%{assigns: %{user: user}} = conn, params) do
    case TwitterAPI.follow(user, params) do
86
      { :ok, user, followed, _activity } ->
87
        response = followed |> UserRepresenter.to_json(%{for: user})
88
89
        conn
        |> json_reply(200, response)
90
91
      { :error, msg } -> forbidden_json_reply(conn, msg)
    end
lain's avatar
lain committed
92
93
  end

94
  def unfollow(%{assigns: %{user: user}} = conn, params) do
95
96
97
    case TwitterAPI.unfollow(user, params) do
      { :ok, user, unfollowed, } ->
        response = unfollowed |> UserRepresenter.to_json(%{for: user})
98
99
100
101
        conn
        |> json_reply(200, response)
      { :error, msg } -> forbidden_json_reply(conn, msg)
    end
lain's avatar
lain committed
102
103
  end

lain's avatar
lain committed
104
105
106
107
108
109
110
  def fetch_status(%{assigns: %{user: user}} = conn, %{ "id" => id }) do
    response = TwitterAPI.fetch_status(user, id) |> Poison.encode!

    conn
    |> json_reply(200, response)
  end

111
112
113
114
115
116
117
118
  def fetch_conversation(%{assigns: %{user: user}} = conn, %{ "id" => id }) do
    id = String.to_integer(id)
    response = TwitterAPI.fetch_conversation(user, id) |> Poison.encode!

    conn
    |> json_reply(200, response)
  end

lain's avatar
lain committed
119
120
121
122
123
124
  def upload(conn, %{"media" => media}) do
    response = TwitterAPI.upload(media)
    conn
    |> put_resp_content_type("application/atom+xml")
    |> send_resp(200, response)
  end
125

126
  def help_test(conn, _params) do
dtluna's avatar
dtluna committed
127
128
129
    conn |> json_reply(200, Poison.encode!("ok"))
  end

130
131
132
133
134
135
  def upload_json(conn, %{"media" => media}) do
    response = TwitterAPI.upload(media, "json")
    conn
    |> json_reply(200, response)
  end

lain's avatar
lain committed
136
137
138
139
140
141
142
143
144
145
146
147
148
149
  def config(conn, _params) do
    response = %{
      site: %{
        name: Pleroma.Web.base_url,
        server: Pleroma.Web.base_url,
        textlimit: -1
      }
    }
    |> Poison.encode!

    conn
    |> json_reply(200, response)
  end

lain's avatar
lain committed
150
151
152
153
154
155
156
157
158
  def favorite(%{assigns: %{user: user}} = conn, %{"id" => id}) do
    activity = Repo.get(Activity, id)
    {:ok, status} = TwitterAPI.favorite(user, activity)
    response = Poison.encode!(status)

    conn
    |> json_reply(200, response)
  end

lain's avatar
lain committed
159
160
161
162
163
164
165
166
167
  def unfavorite(%{assigns: %{user: user}} = conn, %{"id" => id}) do
    activity = Repo.get(Activity, id)
    {:ok, status} = TwitterAPI.unfavorite(user, activity)
    response = Poison.encode!(status)

    conn
    |> json_reply(200, response)
  end

lain's avatar
lain committed
168
169
170
171
172
173
174
175
176
  def retweet(%{assigns: %{user: user}} = conn, %{"id" => id}) do
    activity = Repo.get(Activity, id)
    {:ok, status} = TwitterAPI.retweet(user, activity)
    response = Poison.encode!(status)

    conn
    |> json_reply(200, response)
  end

177
178
179
180
181
182
183
184
185
186
187
  def register(conn, params) do
    with {:ok, user} <- TwitterAPI.register_user(params) do
      conn
      |> json_reply(200, Poison.encode!(user))
    else
      {:error, errors} ->
      conn
      |> json_reply(400, Poison.encode!(errors))
    end
  end

lain's avatar
lain committed
188
189
190
191
192
193
194
195
196
197
198
199
  def update_avatar(%{assigns: %{user: user}} = conn, params) do
    {:ok, object} = ActivityPub.upload(params)
    change = Ecto.Changeset.change(user, %{avatar: object.data})
    {:ok, user} = Repo.update(change)

    response = UserRepresenter.to_map(user, %{for: user})
    |> Poison.encode!

    conn
    |> json_reply(200, response)
  end

200
  defp bad_request_reply(conn, error_message) do
dtluna's avatar
dtluna committed
201
    json = error_json(conn, error_message)
202
203
204
    json_reply(conn, 400, json)
  end

205
206
207
208
209
  defp json_reply(conn, status, json) do
    conn
    |> put_resp_content_type("application/json")
    |> send_resp(status, json)
  end
210
211

  defp forbidden_json_reply(conn, error_message) do
dtluna's avatar
dtluna committed
212
    json = error_json(conn, error_message)
213
214
    json_reply(conn, 403, json)
  end
dtluna's avatar
dtluna committed
215
216
217
218

  defp error_json(conn, error_message) do
    %{"error" => error_message, "request" => conn.request_path} |> Poison.encode!
  end
219
end