Permalink
Browse files

Merge branch 'dev' of github.com:umamao/umamao into unlogged-untracke…

…d-users
  • Loading branch information...
2 parents e52e174 + acc388c commit 309e625e370a6a588f9b0be5167e607412187f3d @andlima andlima committed Nov 21, 2011
View
@@ -1,6 +1,8 @@
class Suggestion
include MongoMapper::Document
+ before_destroy :propagate_destruction
+
key :user_id, :required => true, :index => true
belongs_to :user
@@ -17,4 +19,14 @@ class Suggestion
def reject!
self.destroy
end
+
+ def propagate_destruction(options = { :delayed => true })
+ if user
+ if options[:delayed]
+ user.delay.remove_suggestion(self)
+ else
+ user.remove_suggestion(self)
+ end
+ end
+ end
end
@@ -82,25 +82,21 @@ def suggest(thing, options = {})
# suggestion.
def remove_suggestion(suggestion_or_entry)
return if suggestion_or_entry.blank?
- if suggestion_or_entry.is_a?(Suggestion)
- suggestion = suggestion_or_entry
- entry_type = suggestion.entry_type
- else
- entry_id = suggestion_or_entry.id
- entry_type = suggestion_or_entry.class <= Topic ? "Topic" : "User"
- suggestion = Suggestion.first(
- :entry_id => entry_id, :entry_type => entry_type,
- :rejected_at => nil, :accepted_at => nil,
- :origin_id => nil, :user_id => self.user.id)
- end
- return if !suggestion
- if entry_type.constantize < Topic
- self.topic_suggestion_ids.delete(suggestion.id)
- elsif entry_type == "User"
- self.user_suggestion_ids.delete(suggestion.id)
+ suggestion =
+ if suggestion_or_entry.is_a?(Suggestion)
+ suggestion_or_entry
+ else
+ Suggestion.first({ :entry_id => suggestion_or_entry.id,
+ :rejected_at => nil, :accepted_at => nil,
+ :origin_id => nil, :user_id => self.user.id })
+ end
+
+ if suggestion
+ self.remove_from_suggestions!(suggestion.id, suggestion.entry_id)
+ else
+ self.remove_from_suggestions!(suggestion_or_entry.id)
end
- suggestion.reject!
end
# Mark something as uninteresting. Uninteresting users and topics
@@ -124,7 +120,7 @@ def mark_as_uninteresting(thing)
def refuse_suggestion(suggestion)
entry = suggestion.entry
self.mark_as_uninteresting(entry)
- self.remove_suggestion(suggestion)
+ suggestion.reject!
end
# Find suggestions from the user's external accounts.
@@ -214,6 +210,19 @@ def refresh_topic_suggestions
end
end
+ def remove_from_suggestions!(*ids)
+ ids.each do |an_id|
+ user.
+ collection.
+ update({ :_id => user.id },
+ { :$pull =>
+ { 'suggestion_list.user_suggestion_ids' => an_id,
+ 'suggestion_list.topic_suggestion_ids' => an_id,
+ 'suggestion_list.uninteresting_user_ids' => an_id,
+ 'suggestion_list.uninteresting_topic_ids' => an_id } })
+ end
+ end
+
protected
def configured_suggestions
ids = AppConfig.topic_suggestion
View
@@ -392,19 +392,15 @@ def remove_from_question_versions
def remove_from_suggestions
Suggestion.query(:entry_id => self.id,
:entry_type => "Topic").each do |suggestion|
- if suggestion.user.present?
- suggestion.user.remove_suggestion(suggestion)
- suggestion.user.save
- end
+ suggestion.reject!
end
# TODO: We should replace this with a better query, but this would
# incur on changes in the models. Right now this isn't too much of
# an issue since topics are rarely deleted.
User.query(:select => :suggestion_list).each do |user|
if user.suggestion_list
- user.suggestion_list.uninteresting_topic_ids.delete(self.id)
- user.save
+ user.remove_from_suggestions!(self.id)
end
end
end
View
@@ -79,7 +79,8 @@ class Helper
has_one :suggestion_list, :dependent => :destroy
delegate :topic_suggestions, :user_suggestions, :suggest,
:remove_suggestion, :mark_as_uninteresting, :refuse_suggestion,
- :refresh_suggestions, :find_first_suggestions, :to => :suggestion_list
+ :refresh_suggestions, :find_first_suggestions,
+ :remove_from_suggestions!, :to => :suggestion_list
has_many :favorites, :class_name => "Favorite", :foreign_key => "user_id"
@@ -25,17 +25,15 @@ def rejected?
def accept!
self.send_accept_notification_to_origin
- self.accepted_at = Time.zone.now
- self.save!
+ self.set(:accepted_at => Time.zone.now)
end
def delete!
self.destroy
end
def reject!
- self.rejected_at = Time.zone.now
- self.save!
+ self.set(:rejected_at => Time.zone.now)
end
def send_notification
View
@@ -43,6 +43,8 @@ def self.query(q, options = {})
types = [:user, :question, :topic] & (options[:in] || [])
+ q = CGI.escape(q)
+
# Decide whether to filter the result by type.
if types.present? && types.length != 3
q = "(#{q}) AND entry\\_type:(" +
@@ -53,7 +55,7 @@ def self.query(q, options = {})
per_page = options[:per_page] || 25
start = (page - 1) * per_page
- query_path = "/solr/select?wt=json&q=#{CGI.escape(q)}&start=#{start}" +
+ query_path = "/solr/select?wt=json&q=#{q}&start=#{start}" +
"&rows=#{per_page}"
solr_response_raw =
View
@@ -1,7 +1,6 @@
task :cron => :environment do
Rake::Task["cron_tasks:send_survey_to_newcomers"].execute
Rake::Task["cron_tasks:refresh_related_topics"].execute
- Rake::Task["suggestions:refresh"].execute
end
namespace :cron_tasks do
View
@@ -1,15 +1,13 @@
namespace :suggestions do
desc "Remove suggestions whose suggested entry doesn't exist anymore"
task :prune => :environment do
- Suggestion.query.each do |suggestion|
- if suggestion.entry.blank?
- suggestion.user.remove_suggestion(suggestion)
- elsif suggestion.user &&
- !suggestion.user.suggestion_list.
- topic_suggestion_ids.include?(suggestion.id) &&
- !suggestion.user.suggestion_list.user_suggestion_ids.
- include?(suggestion.id)
- suggestion.destroy
+ Suggestion.find_each(:batch_size => 10_000) do |suggestion|
+ if !suggestion.user || !suggestion.entry
+ if suggestion.user && suggestion.entry_id.present?
+ suggestion.propagate_destruction(:delayed => false)
+ end
+ suggestion.delete
+ print('.')
end
end
end

0 comments on commit 309e625

Please sign in to comment.