end
def status_update(%{assigns: %{user: user}} = conn, %{"status" => status_text} = status_data) do
- l = status_text |> String.trim |> String.length
- if l > 0 && l < 5000 do
- media_ids = extract_media_ids(status_data)
- {:ok, activity} = TwitterAPI.create_status(user, Map.put(status_data, "media_ids", media_ids))
+ with media_ids <- extract_media_ids(status_data),
+ {:ok, activity} <- TwitterAPI.create_status(user, Map.put(status_data, "media_ids", media_ids)) do
conn
- |> json_reply(200, ActivityRepresenter.to_json(activity, %{user: user}))
+ |> json(ActivityRepresenter.to_map(activity, %{user: user}))
else
- empty_status_reply(conn)
+ _ -> empty_status_reply(conn)
end
end
def user_timeline(%{assigns: %{user: user}} = conn, params) do
case TwitterAPI.get_user(user, params) do
{:ok, target_user} ->
- params = Map.merge(params, %{"actor_id" => target_user.ap_id})
+ params = Map.merge(params, %{"actor_id" => target_user.ap_id, "whole_db" => true})
statuses = TwitterAPI.fetch_user_statuses(user, params)
conn
|> json_reply(200, statuses |> Poison.encode!)
end
end
+ def block(%{assigns: %{user: user}} = conn, params) do
+ case TwitterAPI.block(user, params) do
+ {:ok, user, blocked} ->
+ render conn, UserView, "show.json", %{user: blocked, for: user}
+ {:error, msg} -> forbidden_json_reply(conn, msg)
+ end
+ end
+
+ def unblock(%{assigns: %{user: user}} = conn, params) do
+ case TwitterAPI.unblock(user, params) do
+ {:ok, user, blocked} ->
+ render conn, UserView, "show.json", %{user: blocked, for: user}
+ {:error, msg} -> forbidden_json_reply(conn, msg)
+ end
+ end
+
def delete_post(%{assigns: %{user: user}} = conn, %{"id" => id}) do
with {:ok, delete} <- CommonAPI.delete(id, user) do
json = ActivityRepresenter.to_json(delete, %{user: user, for: user})
end
def unfavorite(%{assigns: %{user: user}} = conn, %{"id" => id}) do
- activity = get_by_id_or_ap_id(id)
- {:ok, status} = TwitterAPI.unfavorite(user, activity)
- response = Poison.encode!(status)
-
- conn
- |> json_reply(200, response)
+ with {:ok, status} <- TwitterAPI.unfav(user, id) do
+ json(conn, status)
+ end
end
def retweet(%{assigns: %{user: user}} = conn, %{"id" => id}) do
response <- Poison.encode!(user_map) do
conn
|> json_reply(200, response)
+ else
+ _e ->
+ conn
+ |> put_status(404)
+ |> json(%{error: "Can't find user"})
end
end
end
end
+ def search(%{assigns: %{user: user}} = conn, %{"q" => query} = params) do
+ conn
+ |> json(TwitterAPI.search(user, params))
+ end
+
defp bad_request_reply(conn, error_message) do
json = error_json(conn, error_message)
json_reply(conn, 400, json)