API: remove various associated fields included by default.

Remove various associated fields that were included by default on
certain endpoints. API users can use the only param to include the
full association if they need these fields.

* /artists.json: urls.
* /artist_urls.json: artist.
* /comments.json: creator_name and updater_name.
* /notes.json: creator_name.
* /pools.json: creator_name.
* /posts.json: uploader_name, children_ids, pixiv_ugoira_frame_data.
* /post_appeals.json: is_resolved.
* /post_versions.json: updater_name.
* /uploads.json: uploader_name.
This commit is contained in:
evazion
2020-02-14 20:15:51 -06:00
parent c5bcfb962f
commit 8649ff6dbe
15 changed files with 10 additions and 84 deletions

View File

@@ -4,10 +4,7 @@ class ArtistUrlsController < ApplicationController
def index def index
@artist_urls = ArtistUrl.paginated_search(params).includes(model_includes(params)) @artist_urls = ArtistUrl.paginated_search(params).includes(model_includes(params))
respond_with(@artist_urls) do |format| respond_with(@artist_urls)
format.json { render json: @artist_urls.to_json(format_params) }
format.xml { render xml: @artist_urls.to_xml(format_params) }
end
end end
def update def update
@@ -20,25 +17,12 @@ class ArtistUrlsController < ApplicationController
def default_includes(params) def default_includes(params)
if ["json", "xml"].include?(params[:format]) if ["json", "xml"].include?(params[:format])
[{artist: [:urls]}] []
else else
[:artist] [:artist]
end end
end end
def format_params
param_hash = {}
if params[:only]
param_hash[:only] = params[:only]
else
param_hash[:include] = [:artist]
end
if request.format.symbol == :xml
param_hash[:root] = "artist-urls"
end
param_hash
end
def artist_url_params def artist_url_params
permitted_params = %i[is_active] permitted_params = %i[is_active]

View File

@@ -80,7 +80,7 @@ class ArtistsController < ApplicationController
def default_includes(params) def default_includes(params)
if ["json", "xml"].include?(params[:format]) if ["json", "xml"].include?(params[:format])
[:urls] []
else else
[:urls, :tag] [:urls, :tag]
end end

View File

@@ -76,7 +76,7 @@ class CommentsController < ApplicationController
def default_includes(params) def default_includes(params)
if ["json", "xml"].include?(params[:format]) if ["json", "xml"].include?(params[:format])
[:creator, :updater] []
elsif params[:format] == "atom" elsif params[:format] == "atom"
[:creator, :post] [:creator, :post]
else else

View File

@@ -61,7 +61,7 @@ class NotesController < ApplicationController
def default_includes(params) def default_includes(params)
if ["json", "xml"].include?(params[:format]) if ["json", "xml"].include?(params[:format])
[:creator] []
else else
[:creator, :post] [:creator, :post]
end end

View File

@@ -100,7 +100,7 @@ class PostsController < ApplicationController
def default_includes(params) def default_includes(params)
if ["json", "xml", "atom"].include?(params[:format]) if ["json", "xml", "atom"].include?(params[:format])
[:uploader] []
else else
(CurrentUser.user.is_moderator? ? [:uploader] : []) (CurrentUser.user.is_moderator? ? [:uploader] : [])
end end

View File

@@ -60,7 +60,7 @@ class UploadsController < ApplicationController
def default_includes(params) def default_includes(params)
if ["json", "xml"].include?(params[:format]) if ["json", "xml"].include?(params[:format])
[:uploader] []
else else
[:uploader, {post: [:uploader]}] [:uploader, {post: [:uploader]}]
end end

View File

@@ -4,7 +4,6 @@ class Artist < ApplicationRecord
attr_accessor :url_string_changed attr_accessor :url_string_changed
array_attribute :other_names array_attribute :other_names
api_attributes including: [:urls]
before_validation :normalize_name before_validation :normalize_name
before_validation :normalize_other_names before_validation :normalize_other_names

View File

@@ -24,8 +24,6 @@ class Comment < ApplicationRecord
:body => ->(user_name) {"@#{creator.name} mentioned you in a \"comment\":/posts/#{post_id}#comment-#{id} on post ##{post_id}:\n\n[quote]\n#{DText.extract_mention(body, "@" + user_name)}\n[/quote]\n"} :body => ->(user_name) {"@#{creator.name} mentioned you in a \"comment\":/posts/#{post_id}#comment-#{id} on post ##{post_id}:\n\n[quote]\n#{DText.extract_mention(body, "@" + user_name)}\n[/quote]\n"}
) )
api_attributes including: [:creator_name, :updater_name]
module SearchMethods module SearchMethods
def deleted def deleted
where("comments.is_deleted = true") where("comments.is_deleted = true")
@@ -155,14 +153,6 @@ class Comment < ApplicationRecord
select { |comment| comment.visibility(user) == :visible } select { |comment| comment.visibility(user) == :visible }
end end
def creator_name
creator.name
end
def updater_name
updater.name
end
def delete! def delete!
update(is_deleted: true) update(is_deleted: true)
end end

View File

@@ -11,8 +11,6 @@ class Note < ApplicationRecord
after_save :create_version after_save :create_version
validate :post_must_not_be_note_locked validate :post_must_not_be_note_locked
api_attributes including: [:creator_name]
module SearchMethods module SearchMethods
def active def active
where("is_active = TRUE") where("is_active = TRUE")
@@ -28,10 +26,6 @@ class Note < ApplicationRecord
end end
end end
def creator_name
creator.name
end
extend SearchMethods extend SearchMethods
def post_must_not_be_note_locked def post_must_not_be_note_locked

View File

@@ -15,7 +15,7 @@ class Pool < ApplicationRecord
after_save :create_version after_save :create_version
after_create :synchronize! after_create :synchronize!
api_attributes including: [:creator_name, :post_count] api_attributes including: [:post_count]
module SearchMethods module SearchMethods
def deleted def deleted
@@ -279,10 +279,6 @@ class Pool < ApplicationRecord
(post_count / CurrentUser.user.per_page.to_f).ceil (post_count / CurrentUser.user.per_page.to_f).ceil
end end
def creator_name
creator.name
end
def validate_name def validate_name
case name case name
when /\A(any|none|series|collection)\z/i when /\A(any|none|series|collection)\z/i

View File

@@ -971,10 +971,6 @@ class Post < ApplicationRecord
self.uploader_ip_addr = CurrentUser.ip_addr self.uploader_ip_addr = CurrentUser.ip_addr
end end
end end
def uploader_name
uploader.name
end
end end
module PoolMethods module PoolMethods
@@ -1220,12 +1216,6 @@ class Post < ApplicationRecord
def has_visible_children def has_visible_children
has_visible_children? has_visible_children?
end end
def children_ids
if has_children?
children.map(&:id).join(' ')
end
end
end end
module DeletionMethods module DeletionMethods
@@ -1382,24 +1372,13 @@ class Post < ApplicationRecord
module ApiMethods module ApiMethods
def api_attributes def api_attributes
attributes = super attributes = super
attributes += [:uploader_name, :has_large, :has_visible_children, :children_ids, :is_favorited?] + TagCategory.categories.map {|x| "tag_string_#{x}".to_sym} attributes += [:has_large, :has_visible_children, :is_favorited?] + TagCategory.categories.map {|x| "tag_string_#{x}".to_sym}
attributes += [:file_url, :large_file_url, :preview_file_url] if visible? attributes += [:file_url, :large_file_url, :preview_file_url] if visible?
attributes -= [:md5, :file_ext] if !visible? attributes -= [:md5, :file_ext] if !visible?
attributes -= [:fav_string] if !CurrentUser.is_moderator? attributes -= [:fav_string] if !CurrentUser.is_moderator?
attributes attributes
end end
def associated_attributes
[:pixiv_ugoira_frame_data]
end
def as_json(options = {})
options ||= {}
options[:include] ||= []
options[:include] += associated_attributes
super(options)
end
def legacy_attributes def legacy_attributes
hash = { hash = {
"has_comments" => last_commented_at.present?, "has_comments" => last_commented_at.present?,

View File

@@ -10,8 +10,6 @@ class PostAppeal < ApplicationRecord
validate :validate_creator_is_not_limited validate :validate_creator_is_not_limited
validates_uniqueness_of :creator_id, :scope => :post_id, :message => "have already appealed this post" validates_uniqueness_of :creator_id, :scope => :post_id, :message => "have already appealed this post"
api_attributes including: [:is_resolved]
module SearchMethods module SearchMethods
def resolved def resolved
joins(:post).where("posts.is_deleted = false and posts.is_flagged = false") joins(:post).where("posts.is_deleted = false and posts.is_flagged = false")

View File

@@ -266,12 +266,8 @@ class PostArchive < ApplicationRecord
post&.visible? && user.is_member? post&.visible? && user.is_member?
end end
def updater_name
updater&.name
end
def api_attributes def api_attributes
super + [:obsolete_added_tags, :obsolete_removed_tags, :unchanged_tags, :updater_name] super + [:obsolete_added_tags, :obsolete_removed_tags, :unchanged_tags]
end end
def self.available_includes def self.available_includes

View File

@@ -70,8 +70,6 @@ class Upload < ApplicationRecord
scope :preprocessed, -> { where(status: "preprocessed") } scope :preprocessed, -> { where(status: "preprocessed") }
api_attributes including: [:uploader_name]
def initialize_attributes def initialize_attributes
self.uploader_id = CurrentUser.id self.uploader_id = CurrentUser.id
self.uploader_ip_addr = CurrentUser.ip_addr self.uploader_ip_addr = CurrentUser.ip_addr
@@ -175,12 +173,6 @@ class Upload < ApplicationRecord
end end
end end
module UploaderMethods
def uploader_name
uploader.name
end
end
module VideoMethods module VideoMethods
def video def video
@video ||= FFMPEG::Movie.new(file.path) @video ||= FFMPEG::Movie.new(file.path)
@@ -229,7 +221,6 @@ class Upload < ApplicationRecord
include FileMethods include FileMethods
include StatusMethods include StatusMethods
include UploaderMethods
include VideoMethods include VideoMethods
extend SearchMethods extend SearchMethods
include SourceMethods include SourceMethods

View File

@@ -241,7 +241,6 @@ class PostsControllerTest < ActionDispatch::IntegrationTest
assert_nil(response.parsed_body["md5"]) assert_nil(response.parsed_body["md5"])
assert_nil(response.parsed_body["file_url"]) assert_nil(response.parsed_body["file_url"])
assert_nil(response.parsed_body["fav_string"]) assert_nil(response.parsed_body["fav_string"])
assert_equal(@post.uploader_name, response.parsed_body["uploader_name"])
end end
end end
end end