Don't forget to lycansubscribe

added pagination

Changed files
+16 -3
app
+16 -3
app/server.rb
··· 9 9 register Sinatra::ActiveRecordExtension 10 10 set :port, 3000 11 11 12 - PAGE_LIMIT = 5 12 + PAGE_LIMIT = 25 13 13 14 14 helpers do 15 15 def json_response(data) ··· 33 33 34 34 if params[:query] 35 35 query = params[:query].gsub('%', "\\%") 36 - likes = user.likes.joins(:post).includes(:post => :user).where("text ILIKE ?", "%#{query}%").order('likes.time DESC').limit(PAGE_LIMIT) 36 + 37 + likes = user.likes 38 + .joins(:post) 39 + .includes(:post => :user) 40 + .where("text ILIKE ?", "%#{query}%") 41 + .order('likes.time DESC, likes.id DESC') 42 + .limit(PAGE_LIMIT) 43 + 44 + if params[:cursor] 45 + timestamp, id = params[:cursor].split(':') 46 + time = Time.at(timestamp.to_f) 47 + likes = likes.where("likes.time < ? OR (likes.time = ? AND likes.id < ?)", time, time, id) 48 + end 49 + 37 50 post_uris = likes.map(&:post).map(&:at_uri) 38 51 39 - json_response(posts: post_uris) 52 + json_response(posts: post_uris, cursor: likes.last && "#{likes.last.time.to_f}:#{likes.last.id}") 40 53 else 41 54 json_error('MissingParameter', 'Missing "query" parameter') 42 55 end