public_controller.rb 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. # frozen_string_literal: true
  2. class Api::V1::Timelines::PublicController < Api::BaseController
  3. before_action :require_user!, only: [:show], if: :require_auth?
  4. after_action :insert_pagination_headers, unless: -> { @statuses.empty? }
  5. def show
  6. @statuses = load_statuses
  7. render json: @statuses, each_serializer: REST::StatusSerializer, relationships: StatusRelationshipsPresenter.new(@statuses, current_user&.account_id)
  8. end
  9. private
  10. def call_api
  11. #bida = URI('https://mastodon.bida.im/api/v1/timelines/public?local=true&only_media=false')
  12. #bida_res = Net::HTTP.get_response(bida)
  13. #json_bida_res=bida_res.body
  14. cisti = URI('https://mastodon.cisti.org/api/v1/timelines/public?local=true&only_media=false')
  15. cisti_res = Net::HTTP.get_response(cisti)
  16. json_cisti_res=cisti_res.body
  17. nebbia = URI('https://nebbia.fail/api/v1/timelines/public?local=true&only_media=false')
  18. nebbia_res = Net::HTTP.get_response(nebbia)
  19. json_cisti_res=cisti_res.body
  20. #bida_arr = JSON.parse(json_bida_res)
  21. cisti_arr = JSON.parse(json_cisti_res)
  22. nebbia_arr = JSON.parse(json_cisti_res)
  23. union = cisti_arr + nebbia_arr
  24. union.sort_by{ |e| e['created_at'] }.to_json
  25. end
  26. def require_auth?
  27. !Setting.timeline_preview
  28. end
  29. def load_statuses
  30. cached_public_statuses_page
  31. end
  32. def cached_public_statuses_page
  33. cache_collection(public_statuses, Status)
  34. end
  35. def public_statuses
  36. public_feed.get(
  37. limit_param(DEFAULT_STATUSES_LIMIT),
  38. params[:max_id],
  39. params[:since_id],
  40. params[:min_id]
  41. )
  42. end
  43. def public_feed
  44. PublicFeed.new(
  45. current_account,
  46. local: truthy_param?(:local),
  47. remote: truthy_param?(:remote),
  48. only_media: truthy_param?(:only_media)
  49. )
  50. end
  51. def insert_pagination_headers
  52. set_pagination_headers(next_path, prev_path)
  53. end
  54. def pagination_params(core_params)
  55. params.slice(:local, :remote, :limit, :only_media).permit(:local, :remote, :limit, :only_media).merge(core_params)
  56. end
  57. def next_path
  58. api_v1_timelines_public_url pagination_params(max_id: pagination_max_id)
  59. end
  60. def prev_path
  61. api_v1_timelines_public_url pagination_params(min_id: pagination_since_id)
  62. end
  63. def pagination_max_id
  64. @statuses.last.id
  65. end
  66. def pagination_since_id
  67. @statuses.first.id
  68. end
  69. end