From 72c41a5ff8383baa9a41f79d8599131a4f4e9537 Mon Sep 17 00:00:00 2001 From: Raphael Date: Tue, 24 Aug 2010 15:42:24 -0700 Subject: [PATCH 1/3] User post now requires a :to field --- app/models/user.rb | 12 ++++++++---- lib/diaspora/websocket.rb | 1 - spec/controllers/sockets_controller_spec.rb | 2 +- spec/lib/salmon_salmon_spec.rb | 4 ++-- spec/lib/web_hooks_spec.rb | 17 +++-------------- spec/models/group_spec.rb | 2 +- spec/models/retraction_spec.rb | 2 +- spec/models/user/receive_spec.rb | 15 +++++++-------- spec/models/user/visible_posts_spec.rb | 6 +++--- spec/user_encryption_spec.rb | 12 ++++++------ 10 files changed, 32 insertions(+), 41 deletions(-) diff --git a/app/models/user.rb b/app/models/user.rb index b9beb0409..94e104706 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -44,8 +44,12 @@ class User def post(class_name, options = {}) options[:person] = self.person - group_ids = options[:group_ids] - options.delete(:group_ids) + group_ids = options[:to] + group_ids = [group_ids] unless group_ids.is_a? Array + + return nil if group_ids.nil? || group_ids.empty? + group_ids.map!{|gid| ensure_bson gid } + options.delete(:to) model_class = class_name.to_s.camelize.constantize @@ -54,7 +58,7 @@ class User post.save - groups = self.groups.find_all_by_id(group_ids) + groups = self.groups.find_all_by_id( group_ids ) target_people = [] groups.each{ |group| @@ -63,7 +67,7 @@ class User target_people = target_people | group.people } - post.socket_to_uid(id, :group_ids => groups.map{|g| g.id}) if post.respond_to?(:socket_to_uid) + post.socket_to_uid(id, :group_ids => group_ids) if post.respond_to?(:socket_to_uid) post.push_to( target_people ) self.raw_visible_posts << post diff --git a/lib/diaspora/websocket.rb b/lib/diaspora/websocket.rb index 3eb41ed45..f9368356a 100644 --- a/lib/diaspora/websocket.rb +++ b/lib/diaspora/websocket.rb @@ -32,7 +32,6 @@ module Diaspora module Socketable def socket_to_uid(id, opts={}) - puts "#{id}, #{self}, #{opts}" SocketsController.new.outgoing(id, self, opts) end diff --git a/spec/controllers/sockets_controller_spec.rb b/spec/controllers/sockets_controller_spec.rb index 33624cb61..b12cf7ad4 100644 --- a/spec/controllers/sockets_controller_spec.rb +++ b/spec/controllers/sockets_controller_spec.rb @@ -18,7 +18,7 @@ describe 'SocketsController' do describe 'actionhash' do before do - @message = @user.post :status_message, :message => "post through user for victory" + @message = @user.post :status_message, :message => "post through user for victory", :to => @user.group(:name => "losers").id end it 'should actionhash posts' do diff --git a/spec/lib/salmon_salmon_spec.rb b/spec/lib/salmon_salmon_spec.rb index 7b29bf966..273762a41 100644 --- a/spec/lib/salmon_salmon_spec.rb +++ b/spec/lib/salmon_salmon_spec.rb @@ -11,7 +11,7 @@ include Salmon describe Salmon do it 'should verify the signature on a roundtrip' do @user = Factory.create :user - @post = @user.post :status_message, :message => "hi" + @post = @user.post :status_message, :message => "hi", :to => @user.group(:name => "sdg").id x = Salmon::SalmonSlap.create(@user, @post.to_diaspora_xml) z = Salmon::SalmonSlap.parse x.to_xml @@ -29,7 +29,7 @@ describe Salmon do it 'should return the data so it can be "received"' do @user = Factory.create :user - @post = @user.post :status_message, :message => "hi" + @post = @user.post :status_message, :message => "hi", :to => @user.group(:name => "sdg").id x = Salmon::SalmonSlap.create(@user, @post.to_diaspora_xml) z = Salmon::SalmonSlap.parse x.to_xml diff --git a/spec/lib/web_hooks_spec.rb b/spec/lib/web_hooks_spec.rb index 5996a6e3a..fa661049b 100644 --- a/spec/lib/web_hooks_spec.rb +++ b/spec/lib/web_hooks_spec.rb @@ -6,9 +6,8 @@ describe Diaspora do describe Webhooks do before do - @user = Factory.create(:user, :email => "bob@aol.com") - @user.person.save - @person = Factory.create(:person) + @user = Factory.create(:user, :email => "bob@aol.com") + @group = @user.group(:name => "losers") end describe "body" do @@ -22,7 +21,7 @@ describe Diaspora do it "should send an owners post to their people" do message_queue.should_receive :process - @user.post :status_message, :message => "hi" + @user.post :status_message, :message => "hi", :to => @group.id end it "should check that it does not send a person's post to an owners people" do @@ -30,16 +29,6 @@ describe Diaspora do Factory.create(:status_message, :person => Factory.create(:person)) end - it "should ensure one url is created for every person" do - 5.times {@user.friends << Factory.create(:person)} - @user.save - - @post.person.owner.reload - - @post.people_with_permissions.size.should == 5 - end - end end - end diff --git a/spec/models/group_spec.rb b/spec/models/group_spec.rb index 93bc9f949..06ac3b576 100644 --- a/spec/models/group_spec.rb +++ b/spec/models/group_spec.rb @@ -70,7 +70,7 @@ describe Group do it 'should add post to group via post method' do group = @user.group(:name => 'losers', :people => [@friend]) - status_message = @user.post( :status_message, :message => "hey", :group_ids => [group.id] ) + status_message = @user.post( :status_message, :message => "hey", :to => group.id ) group.reload group.posts.include?(status_message).should be true diff --git a/spec/models/retraction_spec.rb b/spec/models/retraction_spec.rb index 5c8695fea..bb344a039 100644 --- a/spec/models/retraction_spec.rb +++ b/spec/models/retraction_spec.rb @@ -3,7 +3,7 @@ require File.dirname(__FILE__) + '/../spec_helper' describe Retraction do before do @user = Factory.create(:user) - @post = @user.post(:status_message, :message => "Destroy!") + @post = @user.post :status_message, :message => "Destroy!", :to => @user.group(:name => "losers").id @person = Factory.create(:person) @user.friends << @person @user.save diff --git a/spec/models/user/receive_spec.rb b/spec/models/user/receive_spec.rb index a657f41e8..bd2afb6dd 100644 --- a/spec/models/user/receive_spec.rb +++ b/spec/models/user/receive_spec.rb @@ -8,12 +8,11 @@ describe User do @user2 = Factory.create(:user) @group2 = @user2.group(:name => 'losers') - #Factory.create :friend, @user friend_users(@user, @group, @user2, @group2) end it 'should be able to parse and store a status message from xml' do - status_message = @user2.post :status_message, :message => 'store this!' + status_message = @user2.post :status_message, :message => 'store this!', :to => @group2.id person = @user2.person xml = status_message.to_diaspora_xml @@ -30,7 +29,7 @@ describe User do num_groups = @user.groups.size (0..5).each{ |n| - status_message = @user2.post :status_message, :message => "store this #{n}!" + status_message = @user2.post :status_message, :message => "store this #{n}!", :to => @group2.id xml = status_message.to_diaspora_xml @user.receive( xml ) } @@ -45,13 +44,13 @@ describe User do end it "should add the post to that user's posts when a user posts it" do - status_message = @user.post :status_message, :message => "hi" + status_message = @user.post :status_message, :message => "hi", :to => @group.id @user.reload @user.raw_visible_posts.include?(status_message).should be true end it 'should be removed on unfriending' do - status_message = @user2.post :status_message, :message => "hi" + status_message = @user2.post :status_message, :message => "hi", :to => @group2.id @user.receive status_message.to_diaspora_xml @user.reload @@ -66,7 +65,7 @@ describe User do end it 'should be remove a post if the noone links to it' do - status_message = @user2.post :status_message, :message => "hi" + status_message = @user2.post :status_message, :message => "hi", :to => @group2.id @user.receive status_message.to_diaspora_xml @user.reload @@ -83,7 +82,7 @@ describe User do end it 'should keep track of user references for one person ' do - status_message = @user2.post :status_message, :message => "hi" + status_message = @user2.post :status_message, :message => "hi", :to => @group2.id @user.receive status_message.to_diaspora_xml @user.reload @@ -107,7 +106,7 @@ describe User do it 'should not override userrefs on receive by another person' do @user3.activate_friend(@user2.person, @group3) - status_message = @user2.post :status_message, :message => "hi" + status_message = @user2.post :status_message, :message => "hi", :to => @group2.id @user.receive status_message.to_diaspora_xml @user3.receive status_message.to_diaspora_xml diff --git a/spec/models/user/visible_posts_spec.rb b/spec/models/user/visible_posts_spec.rb index 92e8c6403..079586138 100644 --- a/spec/models/user/visible_posts_spec.rb +++ b/spec/models/user/visible_posts_spec.rb @@ -21,9 +21,9 @@ describe User do end it 'should generate a valid stream for a group of people' do - status_message1 = @user2.post :status_message, :message => "hi" - status_message2 = @user3.post :status_message, :message => "heyyyy" - status_message3 = @user4.post :status_message, :message => "yooo" + status_message1 = @user2.post :status_message, :message => "hi", :to => @user2_group.id + status_message2 = @user3.post :status_message, :message => "heyyyy", :to => @user3_group.id + status_message3 = @user4.post :status_message, :message => "yooo", :to => @user4_group.id @user.receive status_message1.to_diaspora_xml @user.receive status_message2.to_diaspora_xml diff --git a/spec/user_encryption_spec.rb b/spec/user_encryption_spec.rb index 896fceb8a..47433b80f 100644 --- a/spec/user_encryption_spec.rb +++ b/spec/user_encryption_spec.rb @@ -62,14 +62,14 @@ describe 'user encryption' do describe 'signing and verifying' do it 'should sign a message on create' do - message = @user.post :status_message, :message => "hi" + message = @user.post :status_message, :message => "hi", :to => @group.id message.signature_valid?.should be true end it 'should sign a retraction on create' do unstub_mocha_stubs - message = @user.post :status_message, :message => "hi" + message = @user.post :status_message, :message => "hi", :to => @group.id retraction = @user.retract(message) @@ -110,7 +110,7 @@ describe 'user encryption' do describe 'sending and recieving signatures' do it 'should contain the signature in the xml' do - message = @user.post :status_message, :message => "hi" + message = @user.post :status_message, :message => "hi", :to => @group.id xml = message.to_xml.to_s xml.include?(message.creator_signature).should be true end @@ -118,7 +118,7 @@ describe 'user encryption' do it 'A message with an invalid signature should be rejected' do @user2 = Factory.create :user - message = @user2.post :status_message, :message => "hey" + message = @user2.post :status_message, :message => "hey", :to => @user2.group(:name => "bruisers").id message.creator_signature = "totally valid" message.save(:validate => false) @@ -135,7 +135,7 @@ describe 'user encryption' do @remote_message = Factory.build(:status_message, :person => @person) @remote_message.creator_signature = @remote_message.send(:sign_with_key,@person.encryption_key) @remote_message.save - @message = @user.post :status_message, :message => "hi" + @message = @user.post :status_message, :message => "hi", :to => @group.id end it 'should attach the creator signature if the user is commenting' do @user.comment "Yeah, it was great", :on => @remote_message @@ -143,7 +143,7 @@ describe 'user encryption' do end it 'should sign the comment if the user is the post creator' do - message = @user.post :status_message, :message => "hi" + message = @user.post :status_message, :message => "hi", :to => @group.id @user.comment "Yeah, it was great", :on => message message.comments.first.signature_valid?.should be true message.comments.first.verify_post_creator_signature.should be true From 204c910f660f8ba38a8b69be2e0f75359bcbcefa Mon Sep 17 00:00:00 2001 From: maxwell Date: Tue, 24 Aug 2010 15:47:44 -0700 Subject: [PATCH 2/3] MS fixing small bugs, adding a very dump list of a groups that a friend is in for you --- app/controllers/people_controller.rb | 1 + app/models/user.rb | 4 ++-- app/views/people/show.html.haml | 5 +++-- public/stylesheets/sass/application.sass | 1 + 4 files changed, 7 insertions(+), 4 deletions(-) diff --git a/app/controllers/people_controller.rb b/app/controllers/people_controller.rb index 4019b871c..8b6ae849b 100644 --- a/app/controllers/people_controller.rb +++ b/app/controllers/people_controller.rb @@ -14,6 +14,7 @@ class PeopleController < ApplicationController def show @person = current_user.visible_person_by_id(params[:id]) @profile = @person.profile + @person_groups = current_user.groups_with_person(@person) @posts = Post.where(:person_id => @person.id, :_id.in => current_user.visible_post_ids).paginate :page => params[:page], :order => 'created_at DESC' diff --git a/app/models/user.rb b/app/models/user.rb index b9beb0409..a0f06cb5c 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -8,7 +8,7 @@ class User key :friend_ids, Array key :pending_request_ids, Array - key :visible_post_ids, Array + key :_post_ids, Array one :person, :class_name => 'Person', :foreign_key => :owner_id @@ -213,7 +213,7 @@ class User end def groups_with_person person - id = ensure_bson person.id + id = ensure_bson person.object_id groups.select {|group| group.person_ids.include? id} end diff --git a/app/views/people/show.html.haml b/app/views/people/show.html.haml index 39c60ec7e..8bf59a1aa 100644 --- a/app/views/people/show.html.haml +++ b/app/views/people/show.html.haml @@ -6,7 +6,6 @@ %h1 = @person.real_name - unless @person.id == current_user.person.id - .right = link_to 'remove friend', @person, :confirm => 'Are you sure?', :method => :delete, :class => "button" @@ -16,6 +15,8 @@ %i= "last seen: #{how_long_ago(@posts.first)}" %li %i= "friends since: #{how_long_ago(@person)}" + %li + ="groups: #{@person_groups}" %li url: = @person.url @@ -31,7 +32,7 @@ %h3= "stream - #{@post_count} item(s)" %ul#stream - for post in @posts - = render type_partial(post), :post => post + = render type_partial(post), :post => post unless post.class == Album = will_paginate @posts - else %h3 no posts to display! diff --git a/public/stylesheets/sass/application.sass b/public/stylesheets/sass/application.sass index b603e790e..c549fffae 100644 --- a/public/stylesheets/sass/application.sass +++ b/public/stylesheets/sass/application.sass @@ -185,6 +185,7 @@ h1 :weight 200 :color #999 + h3 :position relativex :font From abafa03e927eee12547a0a7345bce06d6efa01f2 Mon Sep 17 00:00:00 2001 From: Raphael Date: Tue, 24 Aug 2010 15:48:51 -0700 Subject: [PATCH 3/3] Spec for posting to no group --- app/models/user.rb | 4 +++- spec/models/user/posting_spec.rb | 11 +++++++++++ 2 files changed, 14 insertions(+), 1 deletion(-) create mode 100644 spec/models/user/posting_spec.rb diff --git a/app/models/user.rb b/app/models/user.rb index 94e104706..0ab382821 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -45,9 +45,11 @@ class User options[:person] = self.person group_ids = options[:to] + + raise "You must post to someone." if group_ids.nil? || group_ids.empty? + group_ids = [group_ids] unless group_ids.is_a? Array - return nil if group_ids.nil? || group_ids.empty? group_ids.map!{|gid| ensure_bson gid } options.delete(:to) diff --git a/spec/models/user/posting_spec.rb b/spec/models/user/posting_spec.rb new file mode 100644 index 000000000..0589de13e --- /dev/null +++ b/spec/models/user/posting_spec.rb @@ -0,0 +1,11 @@ +require File.dirname(__FILE__) + '/../../spec_helper' + +describe User do + before do + @user = Factory.create(:user) + @group = @user.group(:name => 'heroes') + end + it 'should not be able to post without a group' do + proc {@user.post(:status_message, :message => "heyheyhey")}.should raise_error /You must post to someone/ + end +end