app/controllers/elt_controller.rb in parlement-0.13 vs app/controllers/elt_controller.rb in parlement-0.14

- old
+ new

@@ -23,83 +23,95 @@ redirect_to '/' end def list @elt = Elt.find(params[:id]) if @elt == nil - render :partial => '/elt/list', :locals => { :elt => @elt } + render :partial => '/elt/list/children', :locals => { :elt => @elt } end def listByDate @elt = Elt.find params[:id] unless @elt - render :partial => 'listByDate' + render :partial => '/elt/list/byDate' end def listByVote @elt = Elt.find params[:id] unless @elt - render :partial => 'listByVote' + render :partial => '/elt/list/byVote' end def listVisitors @elt = Elt.find params[:id] unless @elt - render :partial => 'listVisitors' - end + render :partial => '/elt/list/visitors' + end def listSubscribers @elt = Elt.find params[:id] unless @elt - render :partial => '/subscriber/list' - end + render :partial => '/elt/list/subscribers' + end def updateView - session[:lastUpdatedView] ||= Time.now - 10 - acts = "" - visits = Visit.count \ - :joins => "JOIN elts e1 ON e1.id = '#{params[:id]}' \ - JOIN elts e2 ON visits.elt_id = e2.id \ - AND ((e1.lft <= e2.lft AND e2.rgt <= e1.rgt) \ - OR (e1.lft > e2.lft AND e2.rgt > e1.rgt))", - :conditions => "visits.updated_on >= '#{session[:lastUpdatedView]}'" - acts += "new Ajax.Updater('listVisitors', '/elt/listVisitors/#{params[:id]}', \ - {asynchronous:true, evalScripts:true}); \ - " if visits > 0 + if session[:lastUpdatedView] && session[:lastUpdatedView] > Time.now - 9 then + # Protection against some browsers updating too fast + logger.info "Too soon" + render :inline => "" + return + end - subscribers = Subscription.count \ - :joins => "JOIN elts e1 ON e1.id = '#{params[:id]}' \ - JOIN elts e2 ON subscriptions.elt_id = e2.id \ - AND ((e1.lft <= e2.lft AND e2.rgt <= e1.rgt) \ - OR (e1.lft > e2.lft AND e2.rgt > e1.rgt))", - :conditions => "subscriptions.created_on >= '#{session[:lastUpdatedView]}'" - acts += "new Ajax.Updater('listSubscribers', '/elt/listSubscribers/#{params[:id]}', \ - {asynchronous:true, evalScripts:true}); \ - " if subscribers > 0 + session[:lastUpdatedView] ||= Time.now - 10 - elts = Elt.count \ - :joins => "LEFT JOIN choices ON choices.elt_id = elts.id \ - JOIN elts e2 ON elts.lft <= e2.lft AND e2.rgt <= elts.rgt", - :conditions => "elts.id = '#{params[:id]}' \ - AND e2.created_on >= '#{session[:lastUpdatedView]}'" - acts += "new Ajax.Updater('listByDate', '/elt/listByDate/#{params[:id]}', \ - {asynchronous:true, evalScripts:true});" \ - + "new Ajax.Updater('listByVote', '/elt/listByVote/#{params[:id]}', \ - {asynchronous:true, evalScripts:true}); \ - " if subscribers > 0 + @elt = Elt.find(params[:id]) + acts = "" + acts += " \ + <script type=\"text/javascript\"> \ + new Ajax.Updater('listByDate', '/elt/listByDate/#{params[:id]}', \ + {asynchronous:true, evalScripts:true}); \ + new Ajax.Updater('listByVote', '/elt/listByVote/#{params[:id]}', \ + {asynchronous:true, evalScripts:true}); \ + </script> \ + " if @elt.last_activity > session[:lastUpdatedView] - if person = session[:person] - visit = Visit.find_by_person_id_and_elt_id(person, params[:id]) - if visit and person.last_login and person.last_login > visit.created_on then - logger.info "New visit" - visit.destroy - visit = nil - end - visit = Visit.new(:person => person, :elt_id => params[:id]) unless visit - visit.filter = filter - visit.save! - end + if person = session[:person] + visits = Visit.count \ + :joins => "JOIN elts e1 ON e1.id = '#{params[:id]}' \ + JOIN elts e2 ON visits.elt_id = e2.id \ + AND ((e1.lft <= e2.lft AND e2.rgt <= e1.rgt) \ + OR (e1.lft > e2.lft AND e2.rgt > e1.rgt))", + :conditions => "visits.updated_on >= '#{session[:lastUpdatedView]}'" + acts += " \ + <script type=\"text/javascript\"> \ + new Ajax.Updater('listVisitors', '/elt/listVisitors/#{params[:id]}', \ + {asynchronous:true, evalScripts:true}); \ + </script> \ + " if visits > 0 - logger.info "#{visits} visits, #{subscribers} subscribers, #{elts} elts" - session[:lastUpdatedView] = Time.now - render :inline => " \ - <script type=\"text/javascript\">#{acts}</script>" + subscribers = Subscription.count \ + :joins => "JOIN elts e1 ON e1.id = '#{params[:id]}' \ + JOIN elts e2 ON subscriptions.elt_id = e2.id \ + AND ((e1.lft <= e2.lft AND e2.rgt <= e1.rgt) \ + OR (e1.lft > e2.lft AND e2.rgt > e1.rgt))", + :conditions => "subscriptions.created_on >= '#{session[:lastUpdatedView]}'" + acts += " \ + <script type=\"text/javascript\"> \ + new Ajax.Updater('listSubscribers', '/elt/listSubscribers/#{params[:id]}', \ + {asynchronous:true, evalScripts:true}); \ + </script> \ + " if subscribers > 0 + + visit = Visit.find_by_person_id_and_elt_id(person, params[:id]) + if visit and person.last_login and person.last_login > visit.created_on then + logger.info "New visit" + visit.destroy + visit = nil + end + visit = Visit.new(:person => person, :elt_id => params[:id]) unless visit + visit.filter = filter + visit.save! + logger.info "#{visits} visit(s), #{subscribers} subscriber(s)" + end + + session[:lastUpdatedView] = Time.now + render :inline => acts end def rss params[:id] = params[:id].gsub(/.rss/, '') headers["Content-Type"] = "text/xml; charset=utf-8" @@ -125,28 +137,42 @@ def create @elt = Elt.new(params[:elt]) @elt.person = session[:person] - if !session[:person] and \ - (@elt.subject =~ /([<>\/]|href)/ \ + if !session[:person] \ + and (@elt.subject =~ /([<>\/]|href)/ \ or @elt.body =~ /(.*(http|href)(.*\n)*){3}/ \ or @elt.body =~ /^([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})$/i \ or @elt.body =~ /([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,}.*){3}/i) then logger.error red { underline { "SPAM! '#{@elt.subject}'" } } logger.error "SPAM! '#{@elt.body}'" - flash[:error] = 'Sorry, to fight spam "<" ">" or "href" are forbidden in the subject, and there can not be more than 3 links in the body, you also can\'t input one simple email or more than 3 emails!' + flash[:error] = _('Sorry, to fight spam "<" ">" or "href" are forbidden in the subject, and there can not be more than 3 links in the body, you also can\'t input one simple email or more than 3 emails!') headers["Status"] = "404 Post considered as spam" render :controller => 'elt', :action => 'new', :status => 404 - elsif params[:submit] == "preview" or (@elt.publish and @elt.parent.add_child(@elt)) then - #headers["Status"] = "201 Created" - render :partial => '/elt/elt', :locals => { :elt => @elt, :eltTop => false } + else - logger.error "Strange error, can't preview or save an element" - puts "Strange error, can't preview or save an element" - flash[:notice] = 'Error' - render :controller => 'elt', :action => 'new' + if not @elt.person and params[:person] and login = params[:person][:name] and !login.empty? then + @elt.person = Person.find_by_name(login) || Person.new(params[:person]) + if @elt.person.user + flash[:warning] = _('This name is password protected, login in top right box') + @elt.person = nil + elsif params[:submit] != "preview" + @elt.person.save! + end + elsif params[:person] and email = params[:person][:email] and !email.empty? then + @elt.person.save! + end + + if params[:submit] == "preview" or (@elt.publish and headers["Status"] = "201 Created") then + render :partial => '/elt/elt', :locals => { :elt => @elt, :eltTop => false, :created => true } + else + logger.error "Strange error, can't preview or save an element" + puts "Strange error, can't preview or save an element" + flash[:notice] = 'Error' + render :controller => 'elt', :action => 'new' + end end end def raw_elt @mail = Elt.find(params[:id]).mail @@ -183,11 +209,6 @@ @elt.add_child vote expire_fragment :action => 'show', :id => @elt.id render :partial => '/elt/choice', :locals => { :elt => @elt } end - - def choices - @elt = Elt.find params[:id] - end end -