Skip to content

Instantly share code, notes, and snippets.

@ruanwz
Created July 14, 2009 15:09
Show Gist options
  • Save ruanwz/147007 to your computer and use it in GitHub Desktop.
Save ruanwz/147007 to your computer and use it in GitHub Desktop.
#by RoBeRt
#check the 'distance' between new posts/anwers and old ones for the same user
require 'text'
module DuplicationFilter
def duplicate_content?
@duplicate
end
def last_question_url
@last_question_url
end
def duplication_check
distance = YAML.load_file("config/duplicate_distance.yml")["default"]
fau=User.find_by_username("fau")
case self
when Msg
if self.user_id == fau.id && AnonymousItem.find_by_kind_and_kind_id(2,self.id)
user_id=AnonymousItem.find_by_kind_and_kind_id(2,self.id).user_id
else
user_id=self.user_id
end
last_msg = Msg.find(:last, :conditions =>["user_id = ? and pending_delete = false and id != ?", user_id, self.id.to_i], :order=>"created_on")
if last_msg
if distance > distance_calculate(self.content, last_msg.content)
@duplicate = true
@last_question_url = last_msg.xtitle
self.pending_delete = true
send_duplication_delete_note
end
end
when Reply
if self.user_id == fau.id && AnonymousItem.find_by_kind_and_kind_id(4,self.id)
user_id=AnonymousItem.find_by_kind_and_kind_id(4,self.id).user_id
else
user_id=self.user_id
end
last_reply = Reply.find(:last, :conditions =>["user_id = ? and pending_delete = false and id != ?", user_id, self.id.to_i], :order=>"created_on")
if last_reply
if distance > distance_calculate(self.content, last_reply.content)
@duplicate = true
self.pending_delete = true
send_duplication_delete_note
end
end
#check the duplicate post in the same group by robert
when GroupPost
last_gp = GroupPost.find(:last, :conditions =>["user_id = ? and pending_delete = false and id != ? and group_id = ?", self.user_id, self.id.to_i, self.group_id], :order=>"created_on")
if last_gp
if distance > distance_calculate(self.content, last_gp.content)
@duplicate = true
self.pending_delete = true
send_duplication_delete_note
end
end
#check the duplicate group comment in the same group post
when GroupPostComment
last_gpc = GroupPostComment.find(:last, :conditions=>["user_id = ? and pending_delete = false and id != ? and group_post_id = ?", self.user_id, self.id.to_i, self.group_post_id], :order=>"created_at")
if last_gpc
if distance > distance_calculate(self.content, last_gpc.content)
@duplicate = true
self.pending_delete = true
send_duplication_delete_note
end
end
#check the duplicate profile comment in the same profile
when Chat
last_profile_comment = Chat.find(:last, :conditions=>["user_id = ? and pending_delete = false and id !=? and owner_id =?", self.user_id, self.id.to_i, self.owner_id], :order =>"created_on")
if last_profile_comment
if distance > distance_calculate(self.content, last_profile_comment.content)
@duplicate = true
self.pending_delete = true
send_duplication_delete_note
end
end
#check the duplicate photo comment in the same photo
when PhotoComment
last_photo_comment = PhotoComment.find(:last, :conditions=>["user_id = ? and pending_delete = false and id !=? and photo_id = ?", self.user_id, self.id.to_i, self.photo_id], :order=>"created_on")
if last_photo_comment
if distance > distance_calculate(self.comment, last_photo_comment.comment)
@duplicate = true
self.pending_delete = true
send_duplication_delete_note
end
end
end
return true
end
def duplication_check_before_create
distance = YAML.load_file("config/duplicate_distance.yml")["default"]
fau=User.find_by_username("fau")
case self
when Msg
if self.user_id == fau.id
user_id=session[:user].id
last_msg2 = Msg.find(:last, :conditions =>["user_id = ? and pending_delete = false and id != ?", fau_id, self.id.to_i], :order=>"created_on")
else
user_id=self.user_id
last_msg2=nil
end
last_msg = Msg.find(:last, :conditions =>["user_id = ? and pending_delete = false and id != ?", user_id, self.id.to_i], :order=>"created_on")
if last_msg
if distance > distance_calculate(self.content, last_msg.content)
@duplicate = true
@last_question_url = last_msg.xtitle
self.pending_delete = true
send_duplication_delete_note
end
end
if last_msg2
if distance > distance_calculate(self.content, last_msg2.content)
@duplicate = true
@last_question_url = last_msg2.xtitle
self.pending_delete = true
send_duplication_delete_note
end
end
when Reply
if self.user_id == fau.id && AnonymousItem.find_by_kind_and_kind_id(4,self.id)
user_id=AnonymousItem.find_by_kind_and_kind_id(4,self.id).user_id
else
user_id=self.user_id
end
last_reply = Reply.find(:last, :conditions =>["user_id = ? and pending_delete = false and id != ?", user_id, self.id.to_i], :order=>"created_on")
if last_reply
if distance > distance_calculate(self.content, last_reply.content)
@duplicate = true
self.pending_delete = true
send_duplication_delete_note
end
end
#check the duplicate post in the same group by robert
when GroupPost
last_gp = GroupPost.find(:last, :conditions =>["user_id = ? and pending_delete = false and id != ? and group_id = ?", self.user_id, self.id.to_i, self.group_id], :order=>"created_on")
if last_gp
if distance > distance_calculate(self.content, last_gp.content)
@duplicate = true
self.pending_delete = true
send_duplication_delete_note
end
end
#check the duplicate group comment in the same group post
when GroupPostComment
last_gpc = GroupPostComment.find(:last, :conditions=>["user_id = ? and pending_delete = false and id != ? and group_post_id = ?", self.user_id, self.id.to_i, self.group_post_id], :order=>"created_at")
if last_gpc
if distance > distance_calculate(self.content, last_gpc.content)
@duplicate = true
self.pending_delete = true
send_duplication_delete_note
end
end
#check the duplicate profile comment in the same profile
when Chat
last_profile_comment = Chat.find(:last, :conditions=>["user_id = ? and pending_delete = false and id !=? and owner_id =?", self.user_id, self.id.to_i, self.owner_id], :order =>"created_on")
if last_profile_comment
if distance > distance_calculate(self.content, last_profile_comment.content)
@duplicate = true
self.pending_delete = true
send_duplication_delete_note
end
end
#check the duplicate photo comment in the same photo
when PhotoComment
last_photo_comment = PhotoComment.find(:last, :conditions=>["user_id = ? and pending_delete = false and id !=? and photo_id = ?", self.user_id, self.id.to_i, self.photo_id], :order=>"created_on")
if last_photo_comment
if distance > distance_calculate(self.comment, last_photo_comment.comment)
@duplicate = true
self.pending_delete = true
send_duplication_delete_note
end
end
end
return true
end
private
def distance_calculate(new_post, last_post)
Text::Levenshtein.distance(new_post, last_post)
end
#some hiddern problems for concurrency
def send_duplication_delete_note
case self
when Msg
link = "http://www.funadvice.com/q/" + Msg.find(:last, :conditions=>["user_id = ? and pending_delete = false", self.user_id], :order=>"created_on").xtitle.to_s
when Reply
reply = Reply.find(:last, :conditions=>["user_id = ? and pending_delete = false", self.user_id], :order =>"created_on")
link = "http://www.funadvice.com/q/" + reply.msg.xtitle.to_s + "#" + reply.id.to_s
when GroupPost
group_post = GroupPost.find(:last, :conditions=>["user_id = ? and pending_delete = false", self.user_id], :order=>"created_on")
link = "http://www.funadvice.com/group/" + group_post.group.user.username + "/" + group_post.id.to_s
when GroupPostComment
gpc = GroupPostComment.find(:last, :conditions=>["user_id = ? and pending_delete = false", self.user_id], :order => "created_at")
link = "http://www.funadvic.com/group/" + gpc.group_post.group.user.username + "/" + gpc.group_post.id.to_s + "#comments"
when Chat
profile_comment = Chat.find(:last,:conditions=>["user_id = ? and pending_delete = false", self.user_id], :order=>"created_on")
link = "http://www.funadvice.com/comments/view/" + profile_comment.owner.username+"#c" + profile_comment.id.to_s
when PhotoComment
photo_comment = PhotoComment.find(:last,:conditions=>["user_id = ? and pending_delete = false", self.user_id], :order=>"created_on")
link = "http://www.funadvice.com/photos/view/" + photo_comment.photo_id.to_s + "#c" + photo_comment.id.to_s
end
fau=User.find_by_username("fau")
if self.user_id == fau.id
case self
when Msg
user_id=AnonymousItem.find_by_kind_and_kind_id(2,self.id).user_id
when Reply
user_id=AnonymousItem.find_by_kind_and_kind_id(4,self.id).user_id
end
else
user_id=self.user_id
end
if User.find(user_id).deny_notifications != 1
Notifier::deliver_duplicate_message(User.find(user_id),link,self.class.to_s)
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment