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
8 def verify_credentials(%{assigns: %{user: user}} = conn, _params) do
9 response = user |> UserRepresenter.to_json(%{for: user})
12 |> json_reply(200, response)
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 ))
19 |> json_reply(200, ActivityRepresenter.to_json(activity, %{user: user}))
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)
32 def public_timeline(%{assigns: %{user: user}} = conn, params) do
33 statuses = TwitterAPI.fetch_public_statuses(user, params)
34 {:ok, json} = Poison.encode(statuses)
37 |> json_reply(200, json)
40 def friends_timeline(%{assigns: %{user: user}} = conn, params) do
41 statuses = TwitterAPI.fetch_friend_statuses(user, params)
42 {:ok, json} = Poison.encode(statuses)
45 |> json_reply(200, json)
48 def follow(%{assigns: %{user: user}} = conn, %{ "user_id" => followed_id }) do
49 { :ok, user, follower, _activity } = TwitterAPI.follow(user, followed_id)
51 response = follower |> UserRepresenter.to_json(%{for: user})
54 |> json_reply(200, response)
57 def unfollow(%{assigns: %{user: user}} = conn, %{ "user_id" => followed_id }) do
58 { :ok, user, follower } = TwitterAPI.unfollow(user, followed_id)
60 response = follower |> UserRepresenter.to_json(%{for: user})
63 |> json_reply(200, response)
66 def fetch_status(%{assigns: %{user: user}} = conn, %{ "id" => id }) do
67 response = TwitterAPI.fetch_status(user, id) |> Poison.encode!
70 |> json_reply(200, response)
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!
78 |> json_reply(200, response)
81 def upload(conn, %{"media" => media}) do
82 response = TwitterAPI.upload(media)
84 |> put_resp_content_type("application/atom+xml")
85 |> send_resp(200, response)
88 def help_test(conn, _params) do
89 conn |> json_reply(200, Poison.encode!("ok"))
92 def upload_json(conn, %{"media" => media}) do
93 response = TwitterAPI.upload(media, "json")
95 |> json_reply(200, response)
98 def config(conn, _params) do
101 name: Pleroma.Web.base_url,
102 server: Pleroma.Web.base_url,
109 |> json_reply(200, response)
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)
118 |> json_reply(200, response)
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)
127 |> json_reply(200, response)
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)
136 |> json_reply(200, response)
139 def register(conn, params) do
140 with {:ok, user} <- TwitterAPI.register_user(params) do
142 |> json_reply(200, Poison.encode!(user))
146 |> json_reply(400, Poison.encode!(errors))
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)
155 response = UserRepresenter.to_map(user, %{for: user})
159 |> json_reply(200, response)
162 defp json_reply(conn, status, json) do
164 |> put_resp_content_type("application/json")
165 |> send_resp(status, json)