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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@@ -4,7 +4,6 @@ class Artist < ApplicationRecord
attr_accessor :url_string_changed
array_attribute :other_names
api_attributes including: [:urls]
before_validation :normalize_name
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"}
)
api_attributes including: [:creator_name, :updater_name]
module SearchMethods
def deleted
where("comments.is_deleted = true")
@@ -155,14 +153,6 @@ class Comment < ApplicationRecord
select { |comment| comment.visibility(user) == :visible }
end
def creator_name
creator.name
end
def updater_name
updater.name
end
def delete!
update(is_deleted: true)
end

View File

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

View File

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

View File

@@ -971,10 +971,6 @@ class Post < ApplicationRecord
self.uploader_ip_addr = CurrentUser.ip_addr
end
end
def uploader_name
uploader.name
end
end
module PoolMethods
@@ -1220,12 +1216,6 @@ class Post < ApplicationRecord
def has_visible_children
has_visible_children?
end
def children_ids
if has_children?
children.map(&:id).join(' ')
end
end
end
module DeletionMethods
@@ -1382,24 +1372,13 @@ class Post < ApplicationRecord
module ApiMethods
def api_attributes
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 -= [:md5, :file_ext] if !visible?
attributes -= [:fav_string] if !CurrentUser.is_moderator?
attributes
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
hash = {
"has_comments" => last_commented_at.present?,

View File

@@ -10,8 +10,6 @@ class PostAppeal < ApplicationRecord
validate :validate_creator_is_not_limited
validates_uniqueness_of :creator_id, :scope => :post_id, :message => "have already appealed this post"
api_attributes including: [:is_resolved]
module SearchMethods
def resolved
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?
end
def updater_name
updater&.name
end
def api_attributes
super + [:obsolete_added_tags, :obsolete_removed_tags, :unchanged_tags, :updater_name]
super + [:obsolete_added_tags, :obsolete_removed_tags, :unchanged_tags]
end
def self.available_includes

View File

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

View File

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