diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index ef32901..b1fa4e1 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -1,17 +1,17 @@ class ApplicationController < ActionController::Base protect_from_forgery - before_filter :authenticate_user + before_filter :authenticate_user - def authenticate_user - if session[:u] - @user = User.find_by_session_token(session[:u]) + def authenticate_user + if session[:u] + @user = User.find_by_session_token(session[:u]) end true end - def require_logged_in_user - if @user + def require_logged_in_user + if @user true else redirect_to "/login" diff --git a/app/controllers/comments_controller.rb b/app/controllers/comments_controller.rb index 7f11965..a4b69a2 100644 --- a/app/controllers/comments_controller.rb +++ b/app/controllers/comments_controller.rb @@ -95,7 +95,7 @@ class CommentsController < ApplicationController def threads recent_threads = @user.recent_threads - @threads = recent_threads.map{|r| + @threads = recent_threads.map{|r| Comment.ordered_for_story_or_thread_for_user(nil, r, @user.id) } # trim each thread to this user's first response diff --git a/app/controllers/home_controller.rb b/app/controllers/home_controller.rb index 9557786..db8bab4 100644 --- a/app/controllers/home_controller.rb +++ b/app/controllers/home_controller.rb @@ -1,13 +1,13 @@ class HomeController < ApplicationController - def index - conds = [ "is_expired = 0 " ] + def index + conds = [ "is_expired = 0 " ] - if @user && !@newest - # exclude downvoted items - conds[0] << "AND stories.id NOT IN (SELECT story_id FROM votes " << + if @user && !@newest + # exclude downvoted items + conds[0] << "AND stories.id NOT IN (SELECT story_id FROM votes " << "WHERE user_id = ? AND vote < 0) " - conds.push @user.id - end + conds.push @user.id + end if @tag conds[0] << "AND taggings.tag_id = ?" @@ -24,16 +24,16 @@ class HomeController < ApplicationController :limit => 30) end - if @user - votes = Vote.votes_by_user_for_stories_hash(@user.id, + if @user + votes = Vote.votes_by_user_for_stories_hash(@user.id, @stories.map{|s| s.id }) @stories.each do |s| - if votes[s.id] - s.vote = votes[s.id] + if votes[s.id] + s.vote = votes[s.id] end end - end + end if @newest # TODO: better algorithm here @@ -43,7 +43,7 @@ class HomeController < ApplicationController end render :action => "index" - end + end def newest @newest = true diff --git a/app/controllers/login_controller.rb b/app/controllers/login_controller.rb index e4a40ab..e4e44da 100644 --- a/app/controllers/login_controller.rb +++ b/app/controllers/login_controller.rb @@ -1,16 +1,16 @@ class LoginController < ApplicationController - before_filter :authenticate_user + before_filter :authenticate_user - def logout - if @user - reset_session + def logout + if @user + reset_session end - redirect_to "/" - end + redirect_to "/" + end - def index - @page_title = "Login" + def index + @page_title = "Login" render :action => "index" end diff --git a/app/controllers/messages_controller.rb b/app/controllers/messages_controller.rb index ae6ff87..a502791 100644 --- a/app/controllers/messages_controller.rb +++ b/app/controllers/messages_controller.rb @@ -1,107 +1,2 @@ class MessagesController < ApplicationController -# static $verify = array( -# array("method" => "post", -# "only" => array("reply", "send"), -# "redirect_to" => "/", -# ), -# ); -# -# public function index() { -# if (!$this->user) { -# $this->add_flash_error("You must be logged in to read messages."); -# return $this->redirect_to("/login"); -# } -# -# $this->page_title = "Your Messages"; -# -# $this->incoming_messages = -# Message::find_all_by_recipient_user_id($this->user->id, -# array("order" => "created_at DESC")); -# -# $this->sent_messages = -# Message::find_all_by_author_user_id($this->user->id, -# array("order" => "created_at DESC")); -# } -# -# public function show() { -# if (!$this->user) { -# $this->add_flash_error("You must be logged in to read messages."); -# return $this->redirect_to("/login"); -# } -# -# if (!($this->message = Message::find_by_random_hash($this->params["id"]))) { -# $this->add_flash_error("Could not find message."); -# return $this->redirect_to(array("controller" => "messages")); -# } -# -# if (!($this->message->recipient_user_id == $this->user->id || -# $this->message->author_user_id == $this->user->id)) { -# $this->add_flash_error("Could not find message."); -# return $this->redirect_to(array("controller" => "messages")); -# } -# -# if ($this->message->recipient_user_id == $this->user->id && -# !$this->message->has_been_read) { -# $this->message->has_been_read = true; -# $this->message->save(); -# } -# -# $this->page_title = "Message From " -# . $this->message->author->username . " To " -# . $this->message->recipient->username; -# -# $this->reply = new Message; -# $this->reply->author_user_id = $this->user->id; -# $this->reply->recipient_user_id = $this->message->author_user_id; -# $this->reply->subject = preg_match("/^re[: ]/i", -# $this->message->subject) ? "" : "Re: " . $this->message->subject; -# } -# -# /* id is a message id */ -# public function reply() { -# $this->show(); -# -# $this->page_title = "Message From " -# . $this->message->author->username . " To " -# . $this->message->recipient->username; -# -# if ($this->reply->update_attributes($this->params["message"])) { -# $this->add_flash_notice("Your reply has been sent."); -# return $this->redirect_to(array("controller" => "messages")); -# } else { -# return $this->render(array("action" => "show")); -# } -# } -# -# /* id is a username */ -# public function compose() { -# if (!$this->user) { -# $this->add_flash_error("You must be logged in to send messages."); -# return $this->redirect_to("/login"); -# } -# -# if (!($this->recipient_user = -# User::find_by_username($this->params["id"]))) { -# $this->add_flash_error("Could not find recipient user."); -# return $this->redirect_to("/messages"); -# } -# -# $this->page_title = "Compose Message To " -# . $this->recipient_user->username; -# -# $this->message = new Message; -# $this->message->recipient_user_id = $this->recipient_user->id; -# $this->message->author_user_id = $this->user->id; -# } -# -# public function send() { -# $this->compose(); -# -# if ($this->message->update_attributes($this->params["message"])) { -# $this->add_flash_notice("Your message has been sent."); -# return $this->redirect_to(array("controller" => "messages")); -# } else { -# return $this->render(array("action" => "compose")); -# } -# } end diff --git a/app/controllers/stories_controller.rb b/app/controllers/stories_controller.rb index e39ca7c..4dca58d 100644 --- a/app/controllers/stories_controller.rb +++ b/app/controllers/stories_controller.rb @@ -14,7 +14,7 @@ class StoriesController < ApplicationController @story.user_id = @user.id if @story.save - Vote.vote_thusly_on_story_or_comment_for_user_because(1, @story.id, + Vote.vote_thusly_on_story_or_comment_for_user_because(1, @story.id, nil, @user.id, nil) return redirect_to @story.comments_url @@ -83,27 +83,27 @@ class StoriesController < ApplicationController end end - def show - @story = Story.find_by_short_id!(params[:id]) + def show + @story = Story.find_by_short_id!(params[:id]) @page_title = @story.title - @comments = Comment.ordered_for_story_or_thread_for_user( + @comments = Comment.ordered_for_story_or_thread_for_user( @story.id, nil, @user ? @user.id : nil) @comment = Comment.new - if @user - if v = Vote.find_by_user_id_and_story_id(@user.id, @story.id) - @story.vote = v.vote + if @user + if v = Vote.find_by_user_id_and_story_id(@user.id, @story.id) + @story.vote = v.vote end - @votes = Vote.comment_votes_by_user_for_story_hash(@user.id, @story.id) - @comments.each do |c| - if @votes[c.id] - c.current_vote = @votes[c.id] + @votes = Vote.comment_votes_by_user_for_story_hash(@user.id, @story.id) + @comments.each do |c| + if @votes[c.id] + c.current_vote = @votes[c.id] end end - end + end end def undelete @@ -114,48 +114,48 @@ class StoriesController < ApplicationController end def update - @story.is_expired = false + @story.is_expired = false if @story.update_attributes(params[:story].except(:url)) - return redirect_to @story.comments_url - else - return render :action => "edit" + return redirect_to @story.comments_url + else + return render :action => "edit" end end -# public function update() { -# if (!$this->user) { -# $this->add_flash_error("You must be logged in to edit an item."); -# return $this->redirect_to("/login"); -# } +# public function update() { +# if (!$this->user) { +# $this->add_flash_error("You must be logged in to edit an item."); +# return $this->redirect_to("/login"); +# } # -# if ($this->user->is_admin) -# $this->item = Item::find_by_id($this->params["id"]); -# else -# $this->item = Item::find_by_user_id_and_id($this->user->id, -# $this->params["id"]); +# if ($this->user->is_admin) +# $this->item = Item::find_by_id($this->params["id"]); +# else +# $this->item = Item::find_by_user_id_and_id($this->user->id, +# $this->params["id"]); # -# if (!$this->item) { -# $this->add_flash_error("Could not find item or you are not " -# . "authorized to edit it."); -# return $this->redirect_to("/"); -# } +# if (!$this->item) { +# $this->add_flash_error("Could not find item or you are not " +# . "authorized to edit it."); +# return $this->redirect_to("/"); +# } # -# $this->item->is_expired = false; -# if ($this->item->update_attributes($this->params["item"])) { -# $this->add_flash_notice("Successfully saved item changes."); -# return $this->redirect_to(array("controller" => "items", -# "action" => "show", "id" => $this->item->id)); -# } else -# return $this->render(array("action" => "edit")); -# } +# $this->item->is_expired = false; +# if ($this->item->update_attributes($this->params["item"])) { +# $this->add_flash_notice("Successfully saved item changes."); +# return $this->redirect_to(array("controller" => "items", +# "action" => "show", "id" => $this->item->id)); +# } else +# return $this->render(array("action" => "edit")); +# } # def unvote if !(story = Story.find_by_short_id(params[:story_id])) return render :text => "can't find story", :status => 400 end - Vote.vote_thusly_on_story_or_comment_for_user_because(0, story.id, + Vote.vote_thusly_on_story_or_comment_for_user_because(0, story.id, nil, @user.id, nil) render :text => "ok" @@ -166,7 +166,7 @@ class StoriesController < ApplicationController return render :text => "can't find story", :status => 400 end - Vote.vote_thusly_on_story_or_comment_for_user_because(1, story.id, + Vote.vote_thusly_on_story_or_comment_for_user_because(1, story.id, nil, @user.id, nil) render :text => "ok" @@ -176,7 +176,7 @@ class StoriesController < ApplicationController if !(story = Story.find_by_short_id(params[:story_id])) return render :text => "can't find story", :status => 400 end - + if !Vote::STORY_REASONS[params[:reason]] return render :text => "invalid reason", :status => 400 end diff --git a/app/controllers/users_controller.rb b/app/controllers/users_controller.rb index fa68ebb..ccfaeaa 100644 --- a/app/controllers/users_controller.rb +++ b/app/controllers/users_controller.rb @@ -1,8 +1,6 @@ class UsersController < ApplicationController def show @showing_user = User.find_by_username!(params[:id]) - @page_title = "User: #{@showing_user.username}" - end end diff --git a/app/mailers/password_reset.rb b/app/mailers/password_reset.rb index 250d6c3..c897d18 100644 --- a/app/mailers/password_reset.rb +++ b/app/mailers/password_reset.rb @@ -5,7 +5,7 @@ class PasswordReset < ActionMailer::Base @user = user @ip = ip - mail(to: user.email, from: "Lobsters ", subject: "[Lobsters] Reset your password") end end diff --git a/app/models/comment.rb b/app/models/comment.rb index 4f6a908..9d5b21d 100644 --- a/app/models/comment.rb +++ b/app/models/comment.rb @@ -1,5 +1,5 @@ class Comment < ActiveRecord::Base - belongs_to :user + belongs_to :user belongs_to :story has_many :votes, :dependent => :delete_all @@ -13,42 +13,42 @@ class Comment < ActiveRecord::Base after_create :assign_votes after_destroy :unassign_votes - MAX_EDIT_MINS = 45 + MAX_EDIT_MINS = 45 validate do - self.comment.to_s.strip == "" && - errors.add(:comment, "cannot be blank.") + self.comment.to_s.strip == "" && + errors.add(:comment, "cannot be blank.") - self.user_id.blank? && + self.user_id.blank? && errors.add(:user_id, "cannot be blank.") - self.story_id.blank? && - errors.add(:story_id, "cannot be blank.") - + self.story_id.blank? && + errors.add(:story_id, "cannot be blank.") + (m = self.comment.to_s.strip.match(/\A(t)his([\.!])?$\z/i)) && - errors.add(:base, (m[1] == "T" ? "N" : "n") + "ope" + m[2].to_s) + errors.add(:base, (m[1] == "T" ? "N" : "n") + "ope" + m[2].to_s) end - def assign_short_id_and_upvote - (1...10).each do |tries| - if tries == 10 - raise "too many hash collisions" + def assign_short_id_and_upvote + (1...10).each do |tries| + if tries == 10 + raise "too many hash collisions" end - if !Comment.find_by_short_id(self.short_id = Utils.random_str(6)) - break + if !Comment.find_by_short_id(self.short_id = Utils.random_str(6)) + break end - end + end self.upvotes = 1 - end + end def assign_votes - Vote.vote_thusly_on_story_or_comment_for_user_because(1, self.story_id, + Vote.vote_thusly_on_story_or_comment_for_user_because(1, self.story_id, self.id, self.user.id, nil, false) - self.story.update_comment_count! - end + self.story.update_comment_count! + end # http://evanmiller.org/how-not-to-sort-by-average-rating.html # https://github.com/reddit/reddit/blob/master/r2/r2/lib/db/_sorts.pyx @@ -68,28 +68,28 @@ class Comment < ActiveRecord::Base return (left - right) / under end - def unassign_votes - self.story.update_comment_count! - end - - def score - self.upvotes - self.downvotes + def unassign_votes + self.story.update_comment_count! end - def linkified_comment - Markdowner.markdown(self.comment) - end + def score + self.upvotes - self.downvotes + end - def upvote!(amount = 1) - Story.update_counters self.id, :upvotes => amount - end + def linkified_comment + Markdowner.markdown(self.comment) + end - def flag! + def upvote!(amount = 1) + Story.update_counters self.id, :upvotes => amount + end + + def flag! Story.update_counters self.id, :flaggings => 1 end - def self.ordered_for_story_or_thread_for_user(story_id, thread_id, user_id) - parents = {} + def self.ordered_for_story_or_thread_for_user(story_id, thread_id, user_id) + parents = {} if thread_id cs = [ "thread_id = ?", thread_id ] @@ -118,13 +118,13 @@ class Comment < ActiveRecord::Base recursor.call(nil, 0) ordered - end - + end + def is_editable_by_user?(user) - if !user || user.id != self.user_id + if !user || user.id != self.user_id return false end - (Time.now.to_i - self.created_at.to_i < (60 * MAX_EDIT_MINS)) - end + (Time.now.to_i - self.created_at.to_i < (60 * MAX_EDIT_MINS)) + end end diff --git a/app/models/tagging.rb b/app/models/tagging.rb index 8149823..3586d97 100644 --- a/app/models/tagging.rb +++ b/app/models/tagging.rb @@ -1,4 +1,4 @@ class Tagging < ActiveRecord::Base - belongs_to :tag + belongs_to :tag belongs_to :story end diff --git a/app/models/user.rb b/app/models/user.rb index 1a99c11..a76d03b 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -1,5 +1,5 @@ class User < ActiveRecord::Base - has_many :stories, + has_many :stories, :include => :user has_secure_password @@ -22,13 +22,13 @@ class User < ActiveRecord::Base end end - def unread_message_count + def unread_message_count 0 - #Message.where(:recipient_user_id => self.id, :has_been_read => 0).count + #Message.where(:recipient_user_id => self.id, :has_been_read => 0).count end - def karma - Keystore.value_for("user:#{self.id}:karma").to_i + def karma + Keystore.value_for("user:#{self.id}:karma").to_i end def stories_submitted_count @@ -47,7 +47,7 @@ class User < ActiveRecord::Base end def linkified_about - Markdowner.markdown(self.about) + Markdowner.markdown(self.about) end def recent_threads(amount = 20) diff --git a/app/models/vote.rb b/app/models/vote.rb index ae5522e..27afe4d 100644 --- a/app/models/vote.rb +++ b/app/models/vote.rb @@ -1,47 +1,47 @@ class Vote < ActiveRecord::Base - belongs_to :user + belongs_to :user belongs_to :story STORY_REASONS = { - "S" => "Spam", - "A" => "Already Posted", - "L" => "Poorly Titled", - "T" => "Poorly Tagged", - "O" => "Off-topic", - "" => "Cancel", + "S" => "Spam", + "A" => "Already Posted", + "L" => "Poorly Titled", + "T" => "Poorly Tagged", + "O" => "Off-topic", + "" => "Cancel", } COMMENT_REASONS = { - "O" => "Off-topic", - "I" => "Incorrect", - "M" => "Me-too", - "T" => "Troll", - "S" => "Spam", - "" => "Cancel", - } + "O" => "Off-topic", + "I" => "Incorrect", + "M" => "Me-too", + "T" => "Troll", + "S" => "Spam", + "" => "Cancel", + } - def self.votes_by_user_for_stories_hash(user, stories) + def self.votes_by_user_for_stories_hash(user, stories) votes = [] Vote.where(:user_id => user, :story_id => stories, :comment_id => nil).each do |v| - votes[v.story_id] = v.vote - end - - votes - end - - def self.comment_votes_by_user_for_story_hash(user_id, story_id) - votes = {} - - Vote.find(:all, :conditions => [ "user_id = ? AND story_id = ? AND " + - "comment_id IS NOT NULL", user_id, story_id ]).each do |v| - votes[v.comment_id] = { :vote => v.vote, :reason => v.reason } + votes[v.story_id] = v.vote end votes end - def self.vote_thusly_on_story_or_comment_for_user_because(vote, story_id, + def self.comment_votes_by_user_for_story_hash(user_id, story_id) + votes = {} + + Vote.find(:all, :conditions => [ "user_id = ? AND story_id = ? AND " + + "comment_id IS NOT NULL", user_id, story_id ]).each do |v| + votes[v.comment_id] = { :vote => v.vote, :reason => v.reason } + end + + votes + end + + def self.vote_thusly_on_story_or_comment_for_user_because(vote, story_id, comment_id, user_id, reason, update_counters = true) v = if comment_id Vote.find_or_initialize_by_user_id_and_story_id_and_comment_id(user_id,