Permalink
Browse files

Don't show messages from before a member joined, anywhere.

  • Loading branch information...
1 parent 0267245 commit 24249689365b0c54bf11402e4ad0fc24408cc044 @alloy committed May 5, 2011
@@ -4,11 +4,12 @@ class MessagesController < ApplicationController
find_parent_resource
def index
+ messages = @room.messages.since_member_joined(@authenticated)
if params[:q]
- @messages = @room.search(params[:q])
+ @messages = messages.search(params[:q])
else
day = params[:day].match(/^(\d{4})-(\d{2})-(\d{2})$/)
- @messages = @room.messages.find_created_on_date(day[1], day[2], day[3])
+ @messages = messages.find_created_on_date(day[1], day[2], day[3])
end
end
@@ -31,6 +31,6 @@ def show
private
def load_recent_messages
- @messages = @room.messages.recent(@authenticated)
+ @messages = @room.messages.since_member_joined(@authenticated).recent
end
end
@@ -26,7 +26,7 @@ def open_link_to(content, url, options = {})
end
def link_to_messages_on_date(date, direction)
- if date
+ if date && @authenticated.created_at < date
path = room_messages_on_day_path(@room, :day => date)
link = open_link_to(date.to_formatted_s(:long_ordinal), path)
direction == :previous ? '' + link : link + ''
@@ -7,8 +7,10 @@ class Message < ActiveRecord::Base
accepts_nested_attributes_for :attachment
before_create :set_attachment_metadata, :if => :attachment
- named_scope :since, lambda { |id| { :conditions => ["messages.id > ?", id] } }
-
+ named_scope :since_member_joined, lambda { |member| { :conditions => ["messages.created_at > ?", member.created_at] } }
+ named_scope :since, lambda { |id| { :conditions => ["messages.id > ?", id] } if id }
+ named_scope :search, lambda { |query| { :conditions => ["messages.body LIKE ?", "%#{query}%"] } }
+
def topic_changed_message?
message_type == 'topic'
end
@@ -17,9 +19,8 @@ def attachment_message?
message_type == 'attachment'
end
- def self.recent(since_member_joined)
+ def self.recent
find(:all,
- :conditions => ["messages.created_at > ?", since_member_joined.created_at],
:order => 'messages.id DESC',
:limit => 25,
:include => :author
View
@@ -30,10 +30,6 @@ def last_attachment_messages
messages.all :include => :attachment, :order => 'messages.id DESC', :conditions => { :message_type => 'attachment' }, :limit => 5
end
- def search(query)
- messages.find :all, :conditions => ["messages.body LIKE ?", "%#{query}%"]
- end
-
private
def date_that_contains_messages(date, order, operator)
@@ -1,4 +1,4 @@
-<% messages = @room.messages.since(params[:since]) -%>
+<% messages = @room.messages.since_member_joined(@authenticated).since(params[:since]) -%>
<% @last_message = @room.message_preceding(messages.first) unless messages.empty? -%>
<%= {
:room_topic => @room.topic,
@@ -113,7 +113,10 @@ PC.Room = Class.create({
},
lastMessageId: function() {
- return this.lastMessage().readAttribute('data-message-id');
+ var last = this.lastMessage();
+ if (last) {
+ return last.readAttribute('data-message-id');
+ }
},
requestData: function() {
@@ -17,6 +17,7 @@
end
it "should create a message and see the newest messages" do
+ @authenticated.update_attribute(:created_at, Date.yesterday)
memberships(:alloy_in_macruby).online!
lambda {
@@ -44,29 +45,52 @@
end
it "should see all messages for the given date" do
- freeze_time!(Time.parse('01/01/2009'))
- messages = Message.all
-
- messages.last.update_attribute(:created_at, "2008-12-30")
- messages[1..2].each { |m| m.update_attribute(:created_at, "2008-12-31") }
- messages.first.update_attribute(:created_at, "2009-01-01")
-
+ @authenticated.update_attribute(:created_at, Date.parse('12/30/2008'))
+ messages = setup_messages_on_a_date
+ get :index, :room_id => @room.to_param, :day => '2008-12-31'
+ status.should.be :success
+ template.should.be 'messages/index'
+ assigns(:messages).should.equal_list messages
+ end
+
+ it "should not see messages for the given date if the member didn't join then yet" do
+ @authenticated.update_attribute(:created_at, Date.parse('01/01/2009'))
+ messages = setup_messages_on_a_date
get :index, :room_id => @room.to_param, :day => '2008-12-31'
-
status.should.be :success
template.should.be 'messages/index'
- assigns(:messages).should.equal_list messages[1..2]
+ assigns(:messages).should.be.empty
end
it "should see all messages matching the search query" do
+ @authenticated.update_attribute(:created_at, Date.yesterday)
get :index, :room_id => @room.to_param, :q => 'itte'
status.should.be :success
template.should.be 'messages/index'
assigns(:messages).should == [messages(:daily_kitten)]
end
+ it "should not see messages matching the search query that were created before the member joined" do
+ @authenticated.update_attribute(:created_at, Date.tomorrow)
+ get :index, :room_id => @room.to_param, :q => 'itte'
+ status.should.be :success
+ template.should.be 'messages/index'
+ assigns(:messages).should.be.empty
+ end
+
should.disallow.get :index, :room_id => rooms(:kitten)
should.disallow.post :create, :room_id => rooms(:kitten), :message => { :body => "Sacre blue!" }
+
+ private
+
+ def setup_messages_on_a_date
+ freeze_time!(Time.parse('01/01/2009'))
+ messages = Message.all
+ messages.last.update_attribute(:created_at, "2008-12-30")
+ messages[1..2].each { |m| m.update_attribute(:created_at, "2008-12-31") }
+ messages.first.update_attribute(:created_at, "2009-01-01")
+ messages[1..2]
+ end
end
describe "On the", MembershipsController, "a visitor" do
@@ -27,12 +27,28 @@
template.should.be 'rooms/show'
end
+ it "should only see messages from after the member joined" do
+ sleep 1
+ member = Member.create!(:full_name => 'new member', :email => 'new@example.com')
+ @room.memberships.create!(:member => member)
+ sleep 1
+ messages = Array.new(2) { @room.messages.create! :author => member, :body => "foo" }
+
+ login(member)
+ get :show, :id => @room.to_param
+ assigns(:room).should == @room
+ assigns(:messages).should.equal_list messages
+ status.should.be :success
+ template.should.be 'rooms/show'
+ end
+
it "should be marked as being online" do
get :show, :id => @room.to_param
@room.members.online.should == [@authenticated]
end
it "should return new messages since the given message id" do
+ @authenticated.update_attribute(:created_at, Date.yesterday)
memberships(:alloy_in_macruby).online!
get :show, :id => @room.to_param, :since => @room.messages.first.to_param, :format => 'json'
status.should.be :success
@@ -46,6 +62,14 @@
data['room_topic'].should == @room.topic
end
+ it "should not return messages since the given id if they are from before the member joined" do
+ @authenticated.update_attribute(:created_at, Date.tomorrow)
+ get :show, :id => @room.to_param, :since => @room.messages.first.to_param, :format => 'json'
+ status.should.be :success
+ data = JSON.parse(response.body)
+ data['messages'].should == nil
+ end
+
it "should be able to set the room topic" do
put :update, :id => @room.to_param, :room => { :topic => 'Oeleboele!' }, :format => 'js'
status.should.be :success
@@ -102,13 +102,23 @@ def should_format_anchor_message(body, url, expected = nil)
end
it "should return a pretty date link" do
+ @authenticated = members(:lrz)
+ @authenticated.update_attribute(:created_at, Date.yesterday)
+
link_to_messages_on_date(nil, :previous).should.be nil
link = open_link_to(Date.today.to_formatted_s(:long_ordinal), room_messages_on_day_path(@room, :day => Date.today))
link_to_messages_on_date(Date.today, :previous).should == '' + link
link_to_messages_on_date(Date.today, :next).should == link + ''
end
+ it "should not return a pretty date link if the date is from before the member joined" do
+ @authenticated = members(:lrz)
+ @authenticated.update_attribute(:created_at, Date.tomorrow)
+ link_to_messages_on_date(Date.yesterday, :previous).should == nil
+ link_to_messages_on_date(Date.yesterday, :next).should == nil
+ end
+
it "should return whether or not a timestamp message is needed" do
freeze_time!
@@ -54,10 +54,22 @@
end
describe Message do
+ it "should return messages matching the given query" do
+ Message.search('hernandez').should == [messages(:patrick_hernandez)]
+ Message.search('kitt').should == [messages(:daily_kitten)]
+ Message.search('a').should == messages(:patrick_hernandez, :daily_kitten, :change_life).sort_by(&:id)
+ end
+
it "should return messages since a given id" do
messages = Message.all
+ Message.since(nil).should == messages
Message.since(messages.first.id).should.equal_list messages[1..-1]
Message.since(messages.second.id.to_s).should.equal_list messages[2..-1]
+
+ member = Member.create!(:email => 'new@example.com')
+ sleep 1
+ message = Message.create!(:author => members(:lrz), :room => rooms(:macruby), :body => 'Ahoy!')
+ Message.since_member_joined(member).since(nil).should == [message]
end
it "should return messages on a given date" do
@@ -86,7 +98,7 @@
messages_after_join = Array.new(20) { room.messages.create! :author => members(:lrz), :body => "foo" }
messages = messages_before_join + messages_after_join
- room.reload.messages.recent(members(:alloy)).should.equal_list messages.last(25)
- room.reload.messages.recent(member).should.equal_list messages_after_join
+ room.reload.messages.since_member_joined(members(:alloy)).recent.should.equal_list messages.last(25)
+ room.reload.messages.since_member_joined(member).recent.should.equal_list messages_after_join
end
end
@@ -65,12 +65,6 @@
@room.next_date_that_contains_messages(2.days.from_now.to_date.to_s).should.be nil
end
- it "should return messages matching the given query" do
- @room.search('hernandez').should == [messages(:patrick_hernandez)]
- @room.search('kitt').should == [messages(:daily_kitten)]
- @room.search('a').should == messages(:patrick_hernandez, :daily_kitten, :change_life).sort_by(&:id)
- end
-
it "should not allow access to label" do
@room.update_attributes(:label => 'IronRuby')
@room.reload.label.should == 'MacRuby'

0 comments on commit 2424968

Please sign in to comment.