DG RS; friending through groups

This commit is contained in:
danielvincent 2010-08-12 21:30:16 -07:00
parent f7e43ee3d0
commit 3d12e9ab51
7 changed files with 82 additions and 62 deletions

View file

@ -27,7 +27,7 @@ class RequestsController < ApplicationController
def create def create
rel_hash = relationship_flow(params[:request][:destination_url]) rel_hash = relationship_flow(params[:request][:destination_url])
Rails.logger.debug("Sending request: #{rel_hash}") 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 if @request
flash[:notice] = "a friend request was sent to #{@request.destination_url}" flash[:notice] = "a friend request was sent to #{@request.destination_url}"

View file

@ -4,8 +4,11 @@ class Group
key :name, String key :name, String
key :person_ids, Array key :person_ids, Array
key :request_ids, Array
many :people, :in => :person_ids, :class_name => 'Person' many :people, :in => :person_ids, :class_name => 'Person'
many :requests, :in => :request_ids, :class_name => 'Request'
belongs_to :user, :class_name => 'User' belongs_to :user, :class_name => 'User'
timestamps! timestamps!

View file

@ -15,6 +15,7 @@ class Request
key :callback_url, String key :callback_url, String
key :person_id, ObjectId key :person_id, ObjectId
key :exported_key, String key :exported_key, String
key :group_id, ObjectId
belongs_to :person belongs_to :person
@ -30,10 +31,19 @@ class Request
def self.instantiate(options = {}) def self.instantiate(options = {})
person = options[:from] 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 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 def set_pending_friend

View file

@ -38,30 +38,39 @@ class User
end end
######### Friend Requesting ########### ######### 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} unless self.friends.detect{ |x| x.receive_url == friend_url}
p = Request.instantiate(:to => friend_url, :from => self.person) request = Request.instantiate(:to => friend_url, :from => self.person, :into => group_id)
if p.save if request.save
self.pending_requests << p self.pending_requests << request
self.save 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 end
p request
end end
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 request = Request.where(:id => friend_request_id).first
n = pending_requests.delete(request) n = pending_requests.delete(request)
friends << request.person friends << request.person
save save
request.person = self.person group = self.groups.first(:id => group_id)
request.exported_key = self.export_key group.people << request.person
request.destination_url = request.callback_url group.save
request.reverse self
request.push_to_url(request.callback_url) request.push_to_url(request.callback_url)
request.destroy request.destroy
end end
@ -77,8 +86,9 @@ class User
def receive_friend_request(friend_request) def receive_friend_request(friend_request)
Rails.logger.debug("receiving friend request #{friend_request.to_json}") 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)} if request_from_me?(friend_request)
activate_friend friend_request.person activate_friend(friend_request.person, friend_request.group_id)
Rails.logger.debug("#{self.real_name}'s friend request has been accepted") Rails.logger.debug("#{self.real_name}'s friend request has been accepted")
friend_request.destroy friend_request.destroy
else else
@ -105,17 +115,22 @@ class User
end end
end end
def send_request(rel_hash) def send_request(rel_hash, group)
if rel_hash[:friend] if rel_hash[:friend]
self.send_friend_request_to(rel_hash[:friend]) self.send_friend_request_to(rel_hash[:friend], group)
else else
raise "you can't do anything to that url" raise "you can't do anything to that url"
end end
end end
def activate_friend(person) def activate_friend(person, group)
group.people << person
friends << 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 end
###### Receiving ####### ###### Receiving #######
@ -126,6 +141,8 @@ class User
if object.is_a? Retraction if object.is_a? Retraction
object.perform self.id object.perform self.id
elsif object.is_a? Request elsif object.is_a? Request
puts object.inspect
old_request =
person = Diaspora::Parser.get_or_create_person_object_from_xml( xml ) person = Diaspora::Parser.get_or_create_person_object_from_xml( xml )
person.serialized_key ||= object.exported_key person.serialized_key ||= object.exported_key
object.person = person object.person = person

View file

@ -30,13 +30,13 @@ describe PublicsController do
before do before do
@user2 = Factory.create(:user) @user2 = Factory.create(:user)
@user2.person.save @user2.person.save
group = @user2.group(:name => 'disciples')
@user3 = Factory.create(:user) @user3 = Factory.create(:user)
@user3.person.save @user3.person.save
req = @user2.send_friend_request_to(@user.person.url, group.id)
req = @user2.send_friend_request_to(@user.person.url)
#req = Request.instantiate(:from => @user2.person, :to => @user.person.url)
@xml = req.to_diaspora_xml @xml = req.to_diaspora_xml
req.delete req.delete
@ -44,25 +44,19 @@ describe PublicsController do
@user2.pending_requests.count.should be 1 @user2.pending_requests.count.should be 1
end 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 @user2.delete
post :receive, :id => @user.person.id, :xml => @xml post :receive, :id => @user.person.id, :xml => @xml
assigns(:user).should eq(@user) assigns(:user).should eq(@user)
end 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.person.delete
@user2.delete @user2.delete
post :receive, :id => @user.person.id, :xml => @xml post :receive, :id => @user.person.id, :xml => @xml
assigns(:user).should eq(@user) assigns(:user).should eq(@user)
end end
end end
end end

View file

@ -8,6 +8,7 @@ include Diaspora::Parser
describe Diaspora::Parser do describe Diaspora::Parser do
before do before do
@user = Factory.create(:user, :email => "bob@aol.com") @user = Factory.create(:user, :email => "bob@aol.com")
@group = @user.group(:name => 'spies')
@person = Factory.create(:person_with_private_key, :email => "bill@gates.com") @person = Factory.create(:person_with_private_key, :email => "bill@gates.com")
end end
describe 'with encryption' do describe 'with encryption' do
@ -105,23 +106,14 @@ describe Diaspora::Parser do
end end
it "should activate the Person if I initiated a request to that url" do it "should activate the Person if I initiated a request to that url" do
request = Request.instantiate(:to => @person.receive_url, :from => @user) request = @user.send_friend_request_to( @person.receive_url, @group.id)
request.save
@user.pending_requests << request
@user.save
request_remote = Request.new request.reverse @user
request_remote.id = request.id
request_remote.destination_url = @user.receive_url xml = request.to_diaspora_xml
request_remote.callback_url = @user.receive_url
request_remote.person = @person
request_remote.exported_key = @person.export_key
xml = request_remote.to_diaspora_xml
@person.destroy @person.destroy
request_remote.destroy
@user.receive xml @user.receive xml
new_person = Person.first(:url => @person.url) new_person = Person.first(:url => @person.url)
new_person.nil?.should be false new_person.nil?.should be false

View file

@ -3,6 +3,7 @@ require File.dirname(__FILE__) + '/../spec_helper'
describe User do describe User do
before do before do
@user = Factory.create(:user) @user = Factory.create(:user)
@group = @user.group(:name => 'heroes')
end end
it 'should instantiate with a person and be valid' do it 'should instantiate with a person and be valid' do
@ -20,13 +21,25 @@ describe User do
end end
describe 'friend requesting' do 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 it "should be able to accept a pending friend request" do
friend = Factory.create(:person) friend = Factory.create(:person)
r = Request.instantiate(:to => @user.receive_url, :from => friend) r = Request.instantiate(:to => @user.receive_url, :from => friend)
r.save r.save
Person.all.count.should == 2 Person.all.count.should == 2
Request.for_user(@user).all.count.should == 1 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 Request.for_user(@user).all.count.should == 0
end end
@ -50,7 +63,7 @@ describe User do
@user.save @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 end
it 'should be able to give me the terse url for webfinger' do it 'should be able to give me the terse url for webfinger' do
@ -65,6 +78,7 @@ describe User do
@person_one.save @person_one.save
@user2 = Factory.create :user @user2 = Factory.create :user
@group2 = @user2.group(:name => "group two")
@user.pending_requests.empty?.should be true @user.pending_requests.empty?.should be true
@user.friends.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 = Request.instantiate(:to => @user.receive_url, :from => @person_one)
@request_two = Request.instantiate(:to => @user2.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) @request_three = Request.instantiate(:to => @user2.receive_url, :from => @user.person)
@req_xml = @request.to_diaspora_xml @req_xml = @request.to_diaspora_xml
@req_two_xml = @request_two.to_diaspora_xml @req_two_xml = @request_two.to_diaspora_xml
@req_three_xml = @request_three.to_diaspora_xml @req_three_xml = @request_three.to_diaspora_xml
@request.destroy @request.destroy
@request_two.destroy @request_two.destroy
@request_three.destroy @request_three.destroy
@ -90,7 +102,7 @@ describe User do
@user2.receive @req_three_xml @user2.receive @req_three_xml
@user2.pending_requests.size.should be 1 @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 @user2.friends.include?(@user.person).should be true
Person.all.count.should be 3 Person.all.count.should be 3
end end
@ -108,12 +120,12 @@ describe User do
@user.receive @req_xml @user.receive @req_xml
@user.pending_requests.size.should be 1 @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 @user.friends.include?(@person_one).should be true
@user2.receive @req_two_xml @user2.receive @req_two_xml
@user2.pending_requests.size.should be 1 @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 @user2.friends.include?(@person_one).should be true
Person.all.count.should be 3 Person.all.count.should be 3
end end
@ -122,7 +134,7 @@ describe User do
@user.receive @req_xml @user.receive @req_xml
@user.pending_requests.size.should be 1 @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 @user.friends.include?(@person_one).should be true
@user2.receive @req_two_xml @user2.receive @req_two_xml
@ -171,7 +183,7 @@ describe User do
@user.pending_requests.size.should be 2 @user.pending_requests.size.should be 2
@user.friends.size.should be 0 @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.pending_requests.size.should be 1
@user.friends.size.should be 1 @user.friends.size.should be 1
@user.friends.include?(@person_one).should be true @user.friends.include?(@person_one).should be true
@ -182,14 +194,6 @@ describe User do
@user.friends.include?(@person_two).should be false @user.friends.include?(@person_two).should be false
end 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
end end