From 7aca7f3e2fe0004673320631efddd71cd1cb9159 Mon Sep 17 00:00:00 2001 From: albert Date: Thu, 22 Dec 2011 12:39:27 -0500 Subject: [PATCH] added legacy api for posts --- app/controllers/legacy_controller.rb | 11 +++++++++ app/models/post.rb | 36 ++++++++++++++++++++++++++++ app/views/legacy/posts.json.erb | 1 + app/views/legacy/posts.xml.erb | 6 +++++ config/routes.rb | 4 +++- 5 files changed, 57 insertions(+), 1 deletion(-) create mode 100644 app/controllers/legacy_controller.rb create mode 100644 app/views/legacy/posts.json.erb create mode 100644 app/views/legacy/posts.xml.erb diff --git a/app/controllers/legacy_controller.rb b/app/controllers/legacy_controller.rb new file mode 100644 index 000000000..cbc221b5e --- /dev/null +++ b/app/controllers/legacy_controller.rb @@ -0,0 +1,11 @@ +class LegacyController < ApplicationController + def posts + @post_set = PostSets::Post.new(tag_query, params[:page]) + @posts = @post_set.posts + end + +private + def tag_query + params[:tags] || (params[:post] && params[:post][:tags]) + end +end diff --git a/app/models/post.rb b/app/models/post.rb index 2207b2592..6111b9c56 100644 --- a/app/models/post.rb +++ b/app/models/post.rb @@ -1058,6 +1058,42 @@ class Post < ActiveRecord::Base options[:except] += hidden_attributes super(options, &block) end + + def to_legacy_json + return { + "has_comments" => last_commented_at.present?, + "parent_id" => parent_id, + "status" => status, + "has_children" => has_children?, + "created_at" => created_at.to_formatted_s(:db), + "md5" => md5, + "has_notes" => last_noted_at.present?, + "rating" => rating, + "author" => uploader.name, + "creator_id" => uploader_id, + "width" => image_width, + "source" => source, + "preview_url" => preview_file_url, + "score" => score, + "tags" => tag_string, + "height" => image_height, + "file_size" => file_size, + "id" => id, + "file_url" => file_url + }.to_json + end + + def status + if is_pending? + "pending" + elsif is_deleted? + "deleted" + elsif is_flagged? + "flagged" + else + "active" + end + end end include FileMethods diff --git a/app/views/legacy/posts.json.erb b/app/views/legacy/posts.json.erb new file mode 100644 index 000000000..182aacf3d --- /dev/null +++ b/app/views/legacy/posts.json.erb @@ -0,0 +1 @@ +[<%= @posts.map {|x| x.to_legacy_json}.join(", ").html_safe %>] diff --git a/app/views/legacy/posts.xml.erb b/app/views/legacy/posts.xml.erb new file mode 100644 index 000000000..9a4cafabb --- /dev/null +++ b/app/views/legacy/posts.xml.erb @@ -0,0 +1,6 @@ + + + <% @posts.each do |post| %> + + <% end %> + diff --git a/config/routes.rb b/config/routes.rb index 33bacc7e5..7e2a9a0a5 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -205,7 +205,9 @@ Danbooru::Application.routes.draw do match "/pool/history/:id" => redirect("/pool_versions?search[pool_id]=%{id}") match "/pool/recent_changes" => redirect("/pool_versions") - match "/post/index" => redirect {|params, req| "/posts?tags=#{params[:tags]}&page=#{req.params[:page]}"} + match "/post/index.xml", :controller => "legacy", :action => "posts", :format => "xml" + match "/post/index.json", :controller => "legacy", :action => "posts", :format => "json" + match "/post/index" => redirect {|params, req| "/posts?tags=#{req.params[:tags]}&page=#{req.params[:page]}"} match "/post" => redirect {|params, req| "/posts?tags=#{req.params[:tags]}&page=#{req.params[:page]}"} match "/post/upload" => redirect("/uploads/new") match "/post/moderate" => redirect("/moderator/post/queue")