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, params) do
49 { :ok, user, followed, _activity } = TwitterAPI.follow(user, params)
51 response = followed |> 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, followed } = TwitterAPI.unfollow(user, followed_id)
60 response = followed |> 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 upload_json(conn, %{"media" => media}) do
89 response = TwitterAPI.upload(media, "json")
91 |> json_reply(200, response)
94 def config(conn, _params) do
97 name: Pleroma.Web.base_url,
98 server: Pleroma.Web.base_url,
105 |> json_reply(200, response)
108 def favorite(%{assigns: %{user: user}} = conn, %{"id" => id}) do
109 activity = Repo.get(Activity, id)
110 {:ok, status} = TwitterAPI.favorite(user, activity)
111 response = Poison.encode!(status)
114 |> json_reply(200, response)
117 def unfavorite(%{assigns: %{user: user}} = conn, %{"id" => id}) do
118 activity = Repo.get(Activity, id)
119 {:ok, status} = TwitterAPI.unfavorite(user, activity)
120 response = Poison.encode!(status)
123 |> json_reply(200, response)
126 def retweet(%{assigns: %{user: user}} = conn, %{"id" => id}) do
127 activity = Repo.get(Activity, id)
128 {:ok, status} = TwitterAPI.retweet(user, activity)
129 response = Poison.encode!(status)
132 |> json_reply(200, response)
135 def register(conn, params) do
136 with {:ok, user} <- TwitterAPI.register_user(params) do
138 |> json_reply(200, Poison.encode!(user))
142 |> json_reply(400, Poison.encode!(errors))
146 def update_avatar(%{assigns: %{user: user}} = conn, params) do
147 {:ok, object} = ActivityPub.upload(params)
148 change = Ecto.Changeset.change(user, %{avatar: object.data})
149 {:ok, user} = Repo.update(change)
151 response = UserRepresenter.to_map(user, %{for: user})
155 |> json_reply(200, response)
158 defp json_reply(conn, status, json) do
160 |> put_resp_content_type("application/json")
161 |> send_resp(status, json)