* Continued work on improving post view templates

* Added statistics-based estimator for related tag calculator
* Fleshed out IpBan class based on changes to Danbooru 1.xx
This commit is contained in:
albert
2010-04-29 17:32:15 -04:00
parent 3666364469
commit 23656e3fa9
39 changed files with 816 additions and 86 deletions

View File

@@ -1,4 +1,5 @@
class Comment < ActiveRecord::Base
class Comment < ActiveRecord::Base
validate :validate_creator_is_not_limited
validates_format_of :body, :with => /\S/, :message => 'has no content'
belongs_to :post
belongs_to :creator, :class_name => "User"
@@ -6,35 +7,24 @@ class Comment < ActiveRecord::Base
after_save :update_last_commented_at
after_destroy :update_last_commented_at
attr_accessible :body
attr_accessor :do_not_bump_post
scope :recent, :order => "comments.id desc", :limit => 6
scope :search_body, lambda {|query| where("body_index @@ plainto_tsquery(?)", query).order("comments.id DESC")}
scope :hidden, lambda {|user| where("score < ?", user.comment_threshold)}
def creator_name
User.find_name(creator_id)
end
def validate_creator_is_not_limited
creator.is_privileged? || Comment.where("creator_id = ? AND created_at >= ?", creator_id, 1.hour.ago).count < 5
end
def update_last_commented_at
return if do_not_bump_post
comment_count = Comment.where(["post_id = ?", post_id]).count
if comment_count <= Danbooru.config.comment_threshold
if Comment.where(["post_id = ?", post_id]).count <= Danbooru.config.comment_threshold
execute_sql("UPDATE posts SET last_commented_at = ? WHERE id = ?", created_at, post_id)
end
end
def can_be_voted_by?(user)
!votes.exists?(["user_id = ?", user.id])
end
def vote!(user, is_positive)
if can_be_voted_by?(user)
if is_positive
increment!(:score)
else
decrement!(:score)
end
votes.create(:user_id => user.id)
else
raise CommentVote::Error.new("You have already voted for this comment")
end
end
end
Comment.connection.extend(PostgresExtensions)

View File

@@ -1,10 +1,21 @@
class CommentVote < ActiveRecord::Base
class Error < Exception ; end
attr_accessor :is_positive
validates_uniqueness_of :ip_addr, :scope => :comment_id
belongs_to :comment
belongs_to :user
after_save :update_comment_score
def self.prune!
destroy_all(["created_at < ?", 14.days.ago])
end
def update_comment_score
if is_positive
comment.increment!(:score)
else
comment.decrement!(:score)
end
end
end

View File

@@ -6,4 +6,24 @@ class IpBan < ActiveRecord::Base
def self.is_banned?(ip_addr)
exists?(["ip_addr = ?", ip_addr])
end
def self.search(user_ids)
comments = count_by_ip_addr("comments", user_ids, "creator_id")
posts = count_by_ip_addr("post_versions", user_ids, "updater_id")
notes = count_by_ip_addr("note_versions", user_ids, "updater_id")
pools = count_by_ip_addr("pool_updates", user_ids, "updater_id")
wiki_pages = count_by_ip_addr("wiki_page_versions", user_ids, "updater_id")
return {
"comments" => comments,
"posts" => posts,
"notes" => notes,
"pools" => pools,
"wiki_pages" => wiki_pages
}
end
def self.count_by_ip_addr(table, user_ids, user_id_field = "user_id")
select_all_sql("SELECT ip_addr, count(*) FROM #{table} WHERE #{user_id_field} IN (?) GROUP BY ip_addr ORDER BY count(*) DESC", user_ids)
end
end

View File

@@ -1,5 +1,5 @@
class Post < ActiveRecord::Base
attr_accessor :updater_id, :updater_ip_addr, :old_tag_string
attr_accessor :updater_id, :updater_ip_addr, :old_tag_string, :should_create_pool
after_destroy :delete_files
after_destroy :delete_favorites
after_destroy :update_tag_post_counts
@@ -17,6 +17,7 @@ class Post < ActiveRecord::Base
has_many :versions, :class_name => "PostVersion", :dependent => :destroy
has_many :votes, :class_name => "PostVote", :dependent => :destroy
has_many :notes, :dependent => :destroy
has_many :comments
validates_presence_of :updater_id, :updater_ip_addr
validates_uniqueness_of :md5
attr_accessible :source, :rating, :tag_string, :old_tag_string, :updater_id, :updater_ip_addr, :last_noted_at
@@ -136,7 +137,7 @@ class Post < ActiveRecord::Base
def medium_image_height
ratio = Danbooru.config.medium_image_width.to_f / image_width.to_f
if ratio < 1
image_height * ratio
(image_height * ratio).to_i
else
image_height
end
@@ -145,7 +146,7 @@ class Post < ActiveRecord::Base
def large_image_height
ratio = Danbooru.config.large_image_width.to_f / image_width.to_f
if ratio < 1
image_height * ratio
(image_height * ratio).to_i
else
image_height
end
@@ -515,10 +516,10 @@ class Post < ActiveRecord::Base
relation = relation.order("posts.image_width * posts.image_height / 1000000.0, posts.id DESC")
when "portrait"
relation = relation.order("1.0 * image_width / GREATEST(1, image_height), posts.id DESC")
relation = relation.order("1.0 * posts.image_width / GREATEST(1, posts.image_height), posts.id DESC")
when "landscape"
relation = relation.order("1.0 * image_width / GREATEST(1, image_height) DESC, posts.id DESC")
relation = relation.order("1.0 * posts.image_width / GREATEST(1, posts.image_height) DESC, posts.id DESC")
when "filesize", "filesize_desc"
relation = relation.order("posts.file_size DESC")
@@ -538,6 +539,10 @@ class Post < ActiveRecord::Base
relation = relation.offset(options[:offset])
end
if options[:select]
relation = relation.select(options[:select])
end
relation
end
end
@@ -649,3 +654,5 @@ class Post < ActiveRecord::Base
@presenter ||= PostPresenter.new(self)
end
end
Post.connection.extend(PostgresExtensions)

View File

@@ -1,5 +1,16 @@
class PostVote < ActiveRecord::Base
class Error < Exception ; end
attr_accessor :is_positive
validates_uniqueness_of :ip_addr, :scope => :post_id
after_save :update_post_score
belongs_to :post
def update_post_score
if is_positive
post.increment!(:score)
else
post.decrement!(:score)
end
end
end