twitter_api_controller.ex 2.67 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}
5
6

  def verify_credentials(%{assigns: %{user: user}} = conn, _params) do
7
    response = user |> UserRepresenter.to_json(%{for: user})
8
9
10
11
12

    conn
    |> json_reply(200, response)
  end

lain's avatar
lain committed
13
  def status_update(%{assigns: %{user: user}} = conn, status_data) do
lain's avatar
lain committed
14
    media_ids = extract_media_ids(status_data)
lain's avatar
lain committed
15
    {:ok, activity} = TwitterAPI.create_status(user, Map.put(status_data, "media_ids",  media_ids ))
lain's avatar
lain committed
16
17
18
19
    conn
    |> json_reply(200, ActivityRepresenter.to_json(activity, %{user: user}))
  end

lain's avatar
lain committed
20
21
22
23
24
25
26
27
28
29
  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

30
31
  def public_timeline(%{assigns: %{user: user}} = conn, params) do
    statuses = TwitterAPI.fetch_public_statuses(user, params)
lain's avatar
lain committed
32
33
34
35
36
37
    {:ok, json} = Poison.encode(statuses)

    conn
    |> json_reply(200, json)
  end

lain's avatar
lain committed
38
39
40
41
42
43
44
45
  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

lain's avatar
lain committed
46
  def follow(%{assigns: %{user: user}} = conn, %{ "user_id" => followed_id }) do
47
    { :ok, user, follower, _activity } = TwitterAPI.follow(user, followed_id)
lain's avatar
lain committed
48

49
    response = follower |> UserRepresenter.to_json(%{for: user})
lain's avatar
lain committed
50
51
52
53
54

    conn
    |> json_reply(200, response)
  end

lain's avatar
lain committed
55
  def unfollow(%{assigns: %{user: user}} = conn, %{ "user_id" => followed_id }) do
56
    { :ok, user, follower } = TwitterAPI.unfollow(user, followed_id)
lain's avatar
lain committed
57

58
    response = follower |> UserRepresenter.to_json(%{for: user})
lain's avatar
lain committed
59
60
61
62
63

    conn
    |> json_reply(200, response)
  end

lain's avatar
lain committed
64
65
66
67
68
69
70
  def fetch_status(%{assigns: %{user: user}} = conn, %{ "id" => id }) do
    response = TwitterAPI.fetch_status(user, id) |> Poison.encode!

    conn
    |> json_reply(200, response)
  end

71
72
73
74
75
76
77
78
  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
79
80
81
82
83
84
  def upload(conn, %{"media" => media}) do
    response = TwitterAPI.upload(media)
    conn
    |> put_resp_content_type("application/atom+xml")
    |> send_resp(200, response)
  end
85

86
87
88
89
90
91
  defp json_reply(conn, status, json) do
    conn
    |> put_resp_content_type("application/json")
    |> send_resp(status, json)
  end
end