This commit is contained in:
2025-07-03 01:26:32 +09:00
parent 42cf25246f
commit f47d7bbb87
4 changed files with 81 additions and 31 deletions
+24 -11
View File
@@ -5,16 +5,26 @@ require 'nokogiri'
class PostsController < ApplicationController
# GET /posts
def index
latest_id = params[:latest].to_i
offset = params[:offset].to_i
limit = (params[:limit] || 20).to_i
cursor = params[:cursor]
posts = filtered_posts.order(created_at: :desc)
posts = if posts.first&.id == latest_id
posts.limit(20).offset(offset)
else
posts.limit(offset + 20)
end
render json: posts.as_json(include: { tags: { only: [:id, :name, :category, :post_count] } })
q = filtered_posts.order(created_at: :desc)
next_cursor = nil
if cursor.present?
q = q.where('posts.created_at < ?', Time.iso8601(cursor))
end
posts = q.limit(limit + 1)
next_cursor = nil
if posts.size > limit
next_cursor = posts.last.created_at.iso8601(6)
posts = posts.first(limit)
end
render json: { posts: posts.as_json(include: { tags: { only: [:id, :name, :category, :post_count] } }),
next_cursor: }
end
def random
@@ -31,7 +41,10 @@ class PostsController < ApplicationController
# GET /posts/1
def show
post = Post.includes(:tags).find(params[:id])
viewed = current_user&.viewed?(post)
return head :not_found unless post
viewed = current_user&.viewed?(post) || false
render json: (post
.as_json(include: { tags: { only: [:id, :name, :category] } })
.merge(viewed: viewed))
@@ -95,7 +108,7 @@ class PostsController < ApplicationController
private
def filtered_posts
tag_names = params[:tags]&.split(',')
tag_names = params[:tags]&.split(?\ )
match_type = params[:match]
tag_names.present? ? filter_posts_by_tags(tag_names, match_type) : Post.all
end