From aa1f3c643856c471e7dc1bc09224c7a688c68aa8 Mon Sep 17 00:00:00 2001 From: Raphael Date: Fri, 10 Sep 2010 10:31:23 -0700 Subject: [PATCH] Comments now use salmon, the whole salmon is encrypted, user querying moved to lib file --- app/controllers/people_controller.rb | 2 +- app/controllers/publics_controller.rb | 6 +- app/models/user.rb | 74 +++++--------------- lib/diaspora/user/querying.rb | 54 ++++++++++++++ lib/salmon/salmon.rb | 12 +--- spec/controllers/publics_controller_spec.rb | 5 +- spec/controllers/requests_controller_spec.rb | 13 ++-- spec/lib/{hcard.rb => hcard_spec.rb} | 0 spec/lib/salmon_salmon_spec.rb | 9 --- spec/models/retraction_spec.rb | 2 +- spec/models/user/receive_spec.rb | 2 +- 11 files changed, 88 insertions(+), 91 deletions(-) create mode 100644 lib/diaspora/user/querying.rb rename spec/lib/{hcard.rb => hcard_spec.rb} (100%) diff --git a/app/controllers/people_controller.rb b/app/controllers/people_controller.rb index 2f516d5bc..8f1ea9553 100644 --- a/app/controllers/people_controller.rb +++ b/app/controllers/people_controller.rb @@ -14,7 +14,7 @@ class PeopleController < ApplicationController @profile = @person.profile @groups_with_person = current_user.groups_with_person(@person) @groups_dropdown_array = current_user.groups.collect{|x| [x.to_s, x.id]} - @posts = current_user.posts_visible_to_me(:from => @person).paginate :page => params[:page], :order => 'created_at DESC' + @posts = current_user.visible_posts_from_others(:from => @person).paginate :page => params[:page], :order => 'created_at DESC' @latest_status_message = current_user.raw_visible_posts.find_all_by__type_and_person_id("StatusMessage", params[:id]).last @post_count = @posts.count respond_with @person diff --git a/app/controllers/publics_controller.rb b/app/controllers/publics_controller.rb index bf9a61e21..da7d8219e 100644 --- a/app/controllers/publics_controller.rb +++ b/app/controllers/publics_controller.rb @@ -31,11 +31,7 @@ class PublicsController < ApplicationController Rails.logger.error("Received post #{params[:xml]} for nonexistent person #{params[:id]}") return end - if params[:xml].include? "xml version='1.0'" - @user.receive_salmon params[:xml] - else - @user.receive params[:xml] - end + @user.receive_salmon params[:xml] end end diff --git a/app/models/user.rb b/app/models/user.rb index ea4a7c877..21054c020 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -1,10 +1,13 @@ require 'lib/diaspora/user/friending.rb' +require 'lib/diaspora/user/querying.rb' require 'lib/salmon/salmon' class User include MongoMapper::Document include Diaspora::UserModules::Friending + include Diaspora::UserModules::Querying include Encryptor::Private + QUEUE = MessageHandler.new devise :database_authenticatable, :registerable, :recoverable, :rememberable, :trackable, :validatable @@ -72,18 +75,6 @@ class User false end -##querying with permissions - def posts_visible_to_me(opts ={}) - if opts[:from].class == Person - Post.where(:person_id => opts[:from].id, :_id.in => self.visible_post_ids) - elsif opts[:from].class == Group - Post.where(:_id.in => opts[:from].post_ids) unless opts[:from].user != self - else - Post.where(:_id.in => self.visible_post_ids) - end - end - - ######## Posting ######## def post(class_name, options = {}) @@ -149,19 +140,20 @@ class User } end + def push_to_person( person, xml ) + Rails.logger.debug("Adding xml for #{self} to message queue to #{url}") + QUEUE.add_post_request( person.receive_url, person.encrypt(xml) ) + QUEUE.process + + end + def salmon( post, opts = {} ) - salmon = Salmon::SalmonSlap.create(self, post.encrypted_xml_for(opts[:to])) - salmon.push_to_url opts[:to].receive_url + salmon = Salmon::SalmonSlap.create(self, post.to_diaspora_xml) + push_to_person( opts[:to], salmon.to_xml) salmon end - def visible_posts( opts = {} ) - if opts[:by_members_of] - return raw_visible_posts if opts[:by_members_of] == :all - group = self.groups.find_by_id( opts[:by_members_of].id ) - group.posts - end - end + ######## Commenting ######## def comment(text, options = {}) @@ -211,11 +203,12 @@ class User end ###### Receiving ####### - def receive_salmon xml - Rails.logger.info("Received a salmon: #{xml}") - salmon = Salmon::SalmonSlap.parse xml + def receive_salmon ciphertext + cleartext = decrypt( ciphertext) + Rails.logger.info("Received a salmon: #{cleartext}") + salmon = Salmon::SalmonSlap.parse cleartext if salmon.verified_for_key?(salmon.author.public_key) - self.receive(decrypt(salmon.data)) + self.receive(salmon.data) end end @@ -303,42 +296,13 @@ class User self.password_confirmation = self.password end - def visible_person_by_id( id ) - id = id.to_id - return self.person if id == self.person.id - result = friends.detect{|x| x.id == id } - result = visible_people.detect{|x| x.id == id } unless result - result - end - - def group_by_id( id ) - id = id.to_id - groups.detect{|x| x.id == id } - end - - def album_by_id( id ) - id = id.to_id - albums.detect{|x| x.id == id } - end - - def groups_with_post( id ) - self.groups.find_all_by_post_ids( id.to_id ) - end - - def groups_with_person person - id = person.id.to_id - groups.select { |g| g.person_ids.include? id} - end - def setup_person self.person.serialized_key ||= User.generate_key.export self.person.email ||= email self.person.save! end - def all_group_ids - self.groups.all.collect{|x| x.id} - end + def as_json(opts={}) { diff --git a/lib/diaspora/user/querying.rb b/lib/diaspora/user/querying.rb new file mode 100644 index 000000000..7346a8ab2 --- /dev/null +++ b/lib/diaspora/user/querying.rb @@ -0,0 +1,54 @@ +module Diaspora + module UserModules + module Querying + def visible_posts_from_others(opts ={}) + if opts[:from].class == Person + Post.where(:person_id => opts[:from].id, :_id.in => self.visible_post_ids) + elsif opts[:from].class == Group + Post.where(:_id.in => opts[:from].post_ids) unless opts[:from].user != self + else + Post.where(:_id.in => self.visible_post_ids) + end + end + + def visible_posts( opts = {} ) + if opts[:by_members_of] + return raw_visible_posts if opts[:by_members_of] == :all + group = self.groups.find_by_id( opts[:by_members_of].id ) + group.posts + end + end + + def visible_person_by_id( id ) + id = id.to_id + return self.person if id == self.person.id + result = friends.detect{|x| x.id == id } + result = visible_people.detect{|x| x.id == id } unless result + result + end + + def group_by_id( id ) + id = id.to_id + groups.detect{|x| x.id == id } + end + + def album_by_id( id ) + id = id.to_id + albums.detect{|x| x.id == id } + end + + def groups_with_post( id ) + self.groups.find_all_by_post_ids( id.to_id ) + end + + def groups_with_person person + id = person.id.to_id + groups.select { |g| g.person_ids.include? id} + end + + def all_group_ids + self.groups.all.collect{|x| x.id} + end + end + end +end diff --git a/lib/salmon/salmon.rb b/lib/salmon/salmon.rb index f94118e12..c1e14c021 100644 --- a/lib/salmon/salmon.rb +++ b/lib/salmon/salmon.rb @@ -35,7 +35,6 @@ end # Verify documents secured with Magic Signatures module Salmon - QUEUE = MessageHandler.new class SalmonSlap attr_accessor :magic_sig, :author, :author_email, :data, :data_type, :sig @@ -93,11 +92,7 @@ ENTRY end end - def push_to_url(url) - Rails.logger.debug("Adding xml for #{self} to message queue to #{url}") - QUEUE.add_post_request( url, self.to_xml ) - QUEUE.process - end + # Decode URL-safe-Base64. This implements @@ -170,11 +165,6 @@ ENTRY key end - - - - - end class MagicSigEnvelope diff --git a/spec/controllers/publics_controller_spec.rb b/spec/controllers/publics_controller_spec.rb index d574b28f1..cc9f3064d 100644 --- a/spec/controllers/publics_controller_spec.rb +++ b/spec/controllers/publics_controller_spec.rb @@ -20,7 +20,7 @@ describe PublicsController do @user.reload @user.visible_post_ids.include?(message.id).should be false - xml = user2.salmon(message, :to => @user.person).to_xml + xml = @user.person.encrypt(user2.salmon(message, :to => @user.person).to_xml) post :receive, :id => @user.person.id, :xml => xml @@ -39,7 +39,7 @@ describe PublicsController do req = @user2.send_friend_request_to(@user.person, group) - @xml = req.to_diaspora_xml + @xml = @user.person.encrypt(@user2.salmon(req, :to => @user.person).to_xml) req.delete @user2.reload @@ -54,6 +54,7 @@ describe PublicsController do end it 'should add the pending request to the right user if the target person does not exist locally' do + Person.should_receive(:by_webfinger).with(@user2.person.email).and_return(@user2.person) @user2.person.delete @user2.delete post :receive, :id => @user.person.id, :xml => @xml diff --git a/spec/controllers/requests_controller_spec.rb b/spec/controllers/requests_controller_spec.rb index 579ee620e..f6ddb849a 100644 --- a/spec/controllers/requests_controller_spec.rb +++ b/spec/controllers/requests_controller_spec.rb @@ -2,14 +2,15 @@ require File.dirname(__FILE__) + '/../spec_helper' include ApplicationHelper include RequestsHelper describe RequestsController do - before do + render_views + before do + @user = Factory.create :user @tom = Redfinger.finger('tom@tom.joindiaspora.com') @evan = Redfinger.finger('evan@status.net') @max = Redfinger.finger('mbs348@gmail.com') + sign_in :user, @user + end + it 'should return the correct tag and url for a given address' do + relationship_flow('tom@tom.joindiaspora.com')[:friend].include?("receive/user").should == true end - it 'should return the correct tag and url for a given address' do - relationship_flow('tom@tom.joindiaspora.com')[:friend].include?("receive/user").should == true - end - - end diff --git a/spec/lib/hcard.rb b/spec/lib/hcard_spec.rb similarity index 100% rename from spec/lib/hcard.rb rename to spec/lib/hcard_spec.rb diff --git a/spec/lib/salmon_salmon_spec.rb b/spec/lib/salmon_salmon_spec.rb index f53c9d228..de85fe838 100644 --- a/spec/lib/salmon_salmon_spec.rb +++ b/spec/lib/salmon_salmon_spec.rb @@ -20,15 +20,6 @@ describe Salmon do @parsed_salmon.verified_for_key?(OpenSSL::PKey::RSA.new(@user.exported_key)).should be true @sent_salmon.verified_for_key?(OpenSSL::PKey::RSA.new(@user.exported_key)).should be true end - - it 'should have an accessible queue' do - Salmon::QUEUE.is_a?(MessageHandler).should be true - end - - it 'should push to a url' do - Salmon::QUEUE.should_receive(:add_post_request) - @sent_salmon.push_to_url("example.com") - end it 'should return the data so it can be "received"' do diff --git a/spec/models/retraction_spec.rb b/spec/models/retraction_spec.rb index 9b742fae3..8b6802ea1 100644 --- a/spec/models/retraction_spec.rb +++ b/spec/models/retraction_spec.rb @@ -18,7 +18,7 @@ describe Retraction do describe 'dispatching' do it 'should dispatch a message on delete' do Factory.create(:person) - Salmon::QUEUE.should_receive :add_post_request + User::QUEUE.should_receive :add_post_request @post.destroy end end diff --git a/spec/models/user/receive_spec.rb b/spec/models/user/receive_spec.rb index 41b619dbb..4d2827545 100644 --- a/spec/models/user/receive_spec.rb +++ b/spec/models/user/receive_spec.rb @@ -173,7 +173,7 @@ describe User do end it 'should receive a salmon for a post' do - @user2.receive_salmon( @salmon.to_xml ) + @user2.receive_salmon( @user2.person.encrypt(@salmon.to_xml) ) @user2.visible_post_ids.include?(@post.id).should be true end end