From 3d12e9ab51e356e6b4b33f2f9e6adb1a99cd9abc Mon Sep 17 00:00:00 2001 From: danielvincent <danielgrippi@gmail.com> Date: Thu, 12 Aug 2010 21:30:16 -0700 Subject: [PATCH] DG RS; friending through groups --- app/controllers/requests_controller.rb | 2 +- app/models/group.rb | 3 ++ app/models/request.rb | 14 +++++- app/models/user.rb | 51 ++++++++++++++------- spec/controllers/publics_controller_spec.rb | 16 ++----- spec/lib/diaspora_parser_spec.rb | 20 +++----- spec/models/user_spec.rb | 38 ++++++++------- 7 files changed, 82 insertions(+), 62 deletions(-) diff --git a/app/controllers/requests_controller.rb b/app/controllers/requests_controller.rb index 38714034f5..9e1fb5cf8f 100644 --- a/app/controllers/requests_controller.rb +++ b/app/controllers/requests_controller.rb @@ -27,7 +27,7 @@ class RequestsController < ApplicationController def create rel_hash = relationship_flow(params[:request][:destination_url]) Rails.logger.debug("Sending request: #{rel_hash}") - @request = current_user.send_request(rel_hash) + @request = current_user.send_request(rel_hash, params[:request][:group]) if @request flash[:notice] = "a friend request was sent to #{@request.destination_url}" diff --git a/app/models/group.rb b/app/models/group.rb index 5c228448dc..0f40d6d9b5 100644 --- a/app/models/group.rb +++ b/app/models/group.rb @@ -4,8 +4,11 @@ class Group key :name, String key :person_ids, Array + key :request_ids, Array many :people, :in => :person_ids, :class_name => 'Person' + many :requests, :in => :request_ids, :class_name => 'Request' + belongs_to :user, :class_name => 'User' timestamps! diff --git a/app/models/request.rb b/app/models/request.rb index 31dcd162e0..f1aae6ea84 100644 --- a/app/models/request.rb +++ b/app/models/request.rb @@ -15,6 +15,7 @@ class Request key :callback_url, String key :person_id, ObjectId key :exported_key, String + key :group_id, ObjectId belongs_to :person @@ -30,10 +31,19 @@ class Request def self.instantiate(options = {}) person = options[:from] - self.new(:destination_url => options[:to], :callback_url => person.receive_url, :person => person, :exported_key => person.export_key) + self.new(:destination_url => options[:to], + :callback_url => person.receive_url, + :person => person, + :exported_key => person.export_key, + :group_id => options[:into]) end - + def reverse accepting_user + self.person = accepting_user.person + self.exported_key = accepting_user.export_key + self.destination_url = self.callback_url + save + end def set_pending_friend diff --git a/app/models/user.rb b/app/models/user.rb index 9fef4edfbf..6eaeacc4dc 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -38,30 +38,39 @@ class User end ######### Friend Requesting ########### - def send_friend_request_to(friend_url) - + def send_friend_request_to(friend_url, group_id) unless self.friends.detect{ |x| x.receive_url == friend_url} - p = Request.instantiate(:to => friend_url, :from => self.person) - if p.save - self.pending_requests << p + request = Request.instantiate(:to => friend_url, :from => self.person, :into => group_id) + if request.save + self.pending_requests << request self.save - p.push_to_url friend_url + + group = self.groups.first(:id => group_id) + + group.requests << request + group.save + + request.push_to_url friend_url end - p + request end end - def accept_friend_request(friend_request_id) + def accept_friend_request(friend_request_id, group_id) request = Request.where(:id => friend_request_id).first n = pending_requests.delete(request) friends << request.person save - request.person = self.person - request.exported_key = self.export_key - request.destination_url = request.callback_url + group = self.groups.first(:id => group_id) + group.people << request.person + group.save + + request.reverse self + request.push_to_url(request.callback_url) + request.destroy end @@ -77,8 +86,9 @@ class User def receive_friend_request(friend_request) Rails.logger.debug("receiving friend request #{friend_request.to_json}") - if pending_requests.detect{|req| (req.callback_url == person.receive_url) && (req.destination_url == person.receive_url)} - activate_friend friend_request.person + if request_from_me?(friend_request) + activate_friend(friend_request.person, friend_request.group_id) + Rails.logger.debug("#{self.real_name}'s friend request has been accepted") friend_request.destroy else @@ -105,17 +115,22 @@ class User end end - def send_request(rel_hash) + def send_request(rel_hash, group) if rel_hash[:friend] - self.send_friend_request_to(rel_hash[:friend]) + self.send_friend_request_to(rel_hash[:friend], group) else raise "you can't do anything to that url" end end - def activate_friend(person) + def activate_friend(person, group) + group.people << person friends << person - save + group.save + end + + def request_from_me?(request) + pending_requests.detect{|req| (req.callback_url == person.receive_url) && (req.destination_url == person.receive_url)} end ###### Receiving ####### @@ -126,6 +141,8 @@ class User if object.is_a? Retraction object.perform self.id elsif object.is_a? Request + puts object.inspect + old_request = person = Diaspora::Parser.get_or_create_person_object_from_xml( xml ) person.serialized_key ||= object.exported_key object.person = person diff --git a/spec/controllers/publics_controller_spec.rb b/spec/controllers/publics_controller_spec.rb index c3455d4621..8ef2a30c86 100644 --- a/spec/controllers/publics_controller_spec.rb +++ b/spec/controllers/publics_controller_spec.rb @@ -30,13 +30,13 @@ describe PublicsController do before do @user2 = Factory.create(:user) @user2.person.save + group = @user2.group(:name => 'disciples') @user3 = Factory.create(:user) @user3.person.save - - req = @user2.send_friend_request_to(@user.person.url) - #req = Request.instantiate(:from => @user2.person, :to => @user.person.url) + req = @user2.send_friend_request_to(@user.person.url, group.id) + @xml = req.to_diaspora_xml req.delete @@ -44,25 +44,19 @@ describe PublicsController do @user2.pending_requests.count.should be 1 end - it 'should add the pending request to the right user, person exists locally' do + it 'should add the pending request to the right user if the target person exists locally' do @user2.delete post :receive, :id => @user.person.id, :xml => @xml assigns(:user).should eq(@user) - - end - it 'should add the pending request to the right user, person does not exist locally' do + it 'should add the pending request to the right user if the target person does not exist locally' do @user2.person.delete @user2.delete post :receive, :id => @user.person.id, :xml => @xml - assigns(:user).should eq(@user) - end - - end end diff --git a/spec/lib/diaspora_parser_spec.rb b/spec/lib/diaspora_parser_spec.rb index d3ab854ed4..ec6aa99667 100644 --- a/spec/lib/diaspora_parser_spec.rb +++ b/spec/lib/diaspora_parser_spec.rb @@ -8,6 +8,7 @@ include Diaspora::Parser describe Diaspora::Parser do before do @user = Factory.create(:user, :email => "bob@aol.com") + @group = @user.group(:name => 'spies') @person = Factory.create(:person_with_private_key, :email => "bill@gates.com") end describe 'with encryption' do @@ -105,23 +106,14 @@ describe Diaspora::Parser do end it "should activate the Person if I initiated a request to that url" do - request = Request.instantiate(:to => @person.receive_url, :from => @user) - request.save - @user.pending_requests << request - @user.save - + request = @user.send_friend_request_to( @person.receive_url, @group.id) - request_remote = Request.new - request_remote.id = request.id - request_remote.destination_url = @user.receive_url - request_remote.callback_url = @user.receive_url - request_remote.person = @person - request_remote.exported_key = @person.export_key + request.reverse @user + + xml = request.to_diaspora_xml - xml = request_remote.to_diaspora_xml - @person.destroy - request_remote.destroy + @user.receive xml new_person = Person.first(:url => @person.url) new_person.nil?.should be false diff --git a/spec/models/user_spec.rb b/spec/models/user_spec.rb index 62cb7dad14..1c75dc1888 100644 --- a/spec/models/user_spec.rb +++ b/spec/models/user_spec.rb @@ -3,6 +3,7 @@ require File.dirname(__FILE__) + '/../spec_helper' describe User do before do @user = Factory.create(:user) + @group = @user.group(:name => 'heroes') end it 'should instantiate with a person and be valid' do @@ -20,13 +21,25 @@ describe User do end describe 'friend requesting' do + it "should assign a request to a group" do + friend = Factory.create(:person) + group = @user.group(:name => "Dudes") + group.requests.size.should == 0 + + @user.send_friend_request_to(friend.receive_url, group.id) + + group.reload + group.requests.size.should == 1 + end + + it "should be able to accept a pending friend request" do friend = Factory.create(:person) r = Request.instantiate(:to => @user.receive_url, :from => friend) r.save Person.all.count.should == 2 Request.for_user(@user).all.count.should == 1 - @user.accept_friend_request(r.id) + @user.accept_friend_request(r.id, @group.id) Request.for_user(@user).all.count.should == 0 end @@ -50,7 +63,7 @@ describe User do @user.save - @user.send_friend_request_to( friend.receive_url ).should be nil + @user.send_friend_request_to( friend.receive_url, @group.id ).should be nil end it 'should be able to give me the terse url for webfinger' do @@ -65,6 +78,7 @@ describe User do @person_one.save @user2 = Factory.create :user + @group2 = @user2.group(:name => "group two") @user.pending_requests.empty?.should be true @user.friends.empty?.should be true @@ -74,13 +88,11 @@ describe User do @request = Request.instantiate(:to => @user.receive_url, :from => @person_one) @request_two = Request.instantiate(:to => @user2.receive_url, :from => @person_one) @request_three = Request.instantiate(:to => @user2.receive_url, :from => @user.person) - @req_xml = @request.to_diaspora_xml @req_two_xml = @request_two.to_diaspora_xml @req_three_xml = @request_three.to_diaspora_xml - @request.destroy @request_two.destroy @request_three.destroy @@ -90,7 +102,7 @@ describe User do @user2.receive @req_three_xml @user2.pending_requests.size.should be 1 - @user2.accept_friend_request @request_three.id + @user2.accept_friend_request @request_three.id, @group2.id @user2.friends.include?(@user.person).should be true Person.all.count.should be 3 end @@ -108,12 +120,12 @@ describe User do @user.receive @req_xml @user.pending_requests.size.should be 1 - @user.accept_friend_request @request.id + @user.accept_friend_request @request.id, @group.id @user.friends.include?(@person_one).should be true @user2.receive @req_two_xml @user2.pending_requests.size.should be 1 - @user2.accept_friend_request @request_two.id + @user2.accept_friend_request @request_two.id, @group2.id @user2.friends.include?(@person_one).should be true Person.all.count.should be 3 end @@ -122,7 +134,7 @@ describe User do @user.receive @req_xml @user.pending_requests.size.should be 1 - @user.accept_friend_request @request.id + @user.accept_friend_request @request.id, @group.id @user.friends.include?(@person_one).should be true @user2.receive @req_two_xml @@ -171,7 +183,7 @@ describe User do @user.pending_requests.size.should be 2 @user.friends.size.should be 0 - @user.accept_friend_request @request.id + @user.accept_friend_request @request.id, @group.id @user.pending_requests.size.should be 1 @user.friends.size.should be 1 @user.friends.include?(@person_one).should be true @@ -182,14 +194,6 @@ describe User do @user.friends.include?(@person_two).should be false end -=begin - it 'should do accept reject for people not on the pod' do - end - it 'should do accept reject for people on the pod' do - end - it 'should do accept reject for mixed people on the pod' do - end -=end end end -- GitLab