diff --git a/lib/epochtalk_server/smf_query.ex b/lib/epochtalk_server/smf_query.ex index cf9cb0e5..cbfcd1e2 100644 --- a/lib/epochtalk_server/smf_query.ex +++ b/lib/epochtalk_server/smf_query.ex @@ -472,6 +472,9 @@ defmodule EpochtalkServer.SmfQuery do {:ok, [], _} -> {:error, "Posts not found for thread_id: #{id}"} + {:error, :page_does_not_exist} -> + {:error, :page_does_not_exist} + {:ok, posts, data} -> return_tuple(posts, data) end diff --git a/lib/epochtalk_server_web/controllers/post.ex b/lib/epochtalk_server_web/controllers/post.ex index f08b6c04..abf500fd 100644 --- a/lib/epochtalk_server_web/controllers/post.ex +++ b/lib/epochtalk_server_web/controllers/post.ex @@ -579,7 +579,7 @@ defmodule EpochtalkServerWeb.Controllers.Post do defp proxy_by_thread(conn, attrs) do with thread_id <- Validate.cast(attrs, "thread_id", :integer, required: true), page <- Validate.cast(attrs, "page", :integer, default: 1), - limit <- Validate.cast(attrs, "limit", :integer, default: 5), + limit <- Validate.cast(attrs, "limit", :integer, default: 25), user <- Guardian.Plug.current_resource(conn), user_priority <- ACL.get_user_priority(conn), :ok <- ACL.allow!(conn, "posts.byThread"), @@ -602,6 +602,9 @@ defmodule EpochtalkServerWeb.Controllers.Post do pagination_data: data }) else + {:error, :page_does_not_exist} -> + ErrorHelpers.render_json_error(conn, 400, "Error, page does not exist") + _ -> ErrorHelpers.render_json_error(conn, 400, "Error, cannot get posts by thread") end diff --git a/lib/epochtalk_server_web/helpers/proxy_pagination.ex b/lib/epochtalk_server_web/helpers/proxy_pagination.ex index a2d239ea..73106bed 100644 --- a/lib/epochtalk_server_web/helpers/proxy_pagination.ex +++ b/lib/epochtalk_server_web/helpers/proxy_pagination.ex @@ -85,19 +85,23 @@ defmodule EpochtalkServerWeb.Helpers.ProxyPagination do total_pages = total_pages(total_records, per_page) - result = records(query, page, total_pages, per_page) - - pagination_data = %{ - next: page < total_pages, - prev: page > 1, - page: page, - per_page: per_page, - total_records: total_records, - total_pages: total_pages, - desc: desc - } + if page > total_pages do + {:error, :page_does_not_exist} + else + result = records(query, page, total_pages, per_page) + + pagination_data = %{ + next: page < total_pages, + prev: page > 1, + page: page, + per_page: per_page, + total_records: total_records, + total_pages: total_pages, + desc: desc + } - {:ok, result, pagination_data} + {:ok, result, pagination_data} + end end def page_next_prev(query, page, per_page: per_page, desc: desc) do diff --git a/parse_by_thread.exs b/parse_by_thread.exs index 2170bee7..c15713eb 100644 --- a/parse_by_thread.exs +++ b/parse_by_thread.exs @@ -12,20 +12,35 @@ defmodule ParseByThread do @start_id 5_485_059 @end_id 1 + @max_page 999_999 - def run do - for id <- @start_id..@end_id//-1 do - case HTTPoison.get("http://localhost:4000/api/posts?thread_id=#{id}") do + def run, + do: for(id <- @start_id..@end_id//-1, do: process_thread(id)) + + def process_thread(id) do + Enum.reduce_while(1..@max_page, nil, fn page, _acc -> + case HTTPoison.get("http://localhost:4000/api/posts?thread_id=#{id}&page=#{page}") do {:ok, %HTTPoison.Response{status_code: 200, body: _body}} -> - Logger.info("Successfully parsed thread with id (#{id})") + {:cont, nil} + + {:ok, + %HTTPoison.Response{ + status_code: 400, + body: + "{\"error\":\"Bad Request\",\"message\":\"Error, page does not exist\",\"status\":400}" + }} -> + Logger.info("Successfully parsed #{page - 1} page(s) of thread with id (#{id})") + {:halt, nil} {:ok, %HTTPoison.Response{status_code: status_code}} -> Logger.error("Thread with id (#{id}) received response with status code #{status_code}") + {:halt, nil} {:error, %HTTPoison.Error{reason: reason}} -> Logger.error("Thread with id (#{id}) HTTP request failed: #{reason}") + {:halt, nil} end - end + end) end end