Remove pending_requests from user, just use Request.from and Request.to.

This commit is contained in:
danielvincent 2010-12-14 18:45:41 -08:00
parent 0119de4afc
commit f1ee32145d
18 changed files with 56 additions and 106 deletions

View file

@ -31,7 +31,7 @@ class ApplicationController < ActionController::Base
end
def count_requests
@request_count = current_user.pending_requests.count if current_user
@request_count = Request.to(current_user.person).count if current_user
end
def set_invites

View file

@ -46,6 +46,9 @@ class PeopleController < ApplicationController
@post_type = :all
if @person
@incoming_request = Request.to(current_user).from(@person).first
@outgoing_request = Request.from(current_user).to(@person).first
@profile = @person.profile
@contact = current_user.contact_for(@person)

View file

@ -71,7 +71,7 @@ class UsersController < ApplicationController
@person = @user.person
@profile = @user.profile
@services = @user.services
@requests = @user.pending_requests
@requests = Request.to(@person).all
@step = ((params[:step].to_i>0)&&(params[:step].to_i<5)) ? params[:step].to_i : 1
@step ||= 1

View file

@ -19,9 +19,4 @@ module PeopleHelper
I18n.t "people.helper.people_on_pod_are_aware_of"
end
end
def pending_request_for(person)
current_user.request_for(person)
end
end

View file

@ -21,7 +21,6 @@ class User
key :invites, Integer, :default => 5
key :invitation_token, String
key :invitation_sent_at, DateTime
key :pending_request_ids, Array, :typecast => 'ObjectId'
key :visible_post_ids, Array, :typecast => 'ObjectId'
key :visible_person_ids, Array, :typecast => 'ObjectId'
@ -48,7 +47,6 @@ class User
many :invitations_to_me, :class => Invitation, :foreign_key => :to_id
many :contacts, :class => Contact, :foreign_key => :user_id
many :visible_people, :in => :visible_person_ids, :class => Person # One of these needs to go
many :pending_requests, :in => :pending_request_ids, :class => Request
many :raw_visible_posts, :in => :visible_post_ids, :class => Post
many :aspects, :class => Aspect, :dependent => :destroy
@ -82,12 +80,6 @@ class User
super
end
def has_incoming_request_from(person)
self.pending_requests.select do |req|
req.to_id == self.person.id
end.any? { |req| req.from_id == person.id }
end
######## Making things work ########
key :email, String

View file

@ -23,7 +23,7 @@
.span-15.last
- unless @contact || current_user.person == @person
- if current_user.has_incoming_request_from(@person)
- if @incoming_request
.floating
%h3
= t('.incoming_request')
@ -36,7 +36,7 @@
%h3
= t('.not_connected', :name => @person.name)
- unless pending_request_for(@person)
- unless @outgoing_request
%h3
.description
= t('.request_people')

View file

@ -27,7 +27,7 @@ When /^I wait for the aspects page to load$/ do
end
When /^I wait for the request's profile page to load$/ do
wait_until { current_path == person_path(@me.reload.pending_requests.first.from) }
wait_until { current_path == person_path(Request.to(@me).first.from) }
end
When /^I wait for the ajax to finish$/ do

View file

@ -12,7 +12,7 @@ module NavigationHelpers
when /^my acceptance form page$/
accept_user_invitation_path(:invitation_token => @me.invitation_token)
when /^the requestor's profile page$/
person_path(@me.reload.pending_requests.first.from)
person_path(Request.to(@me).first.from)
when /^"([^\"]*)"'s page$/
person_path(User.find_by_email($1).person)
when /^"(\/.*)"/

View file

@ -20,7 +20,6 @@ module Diaspora
end
def accept_contact_request(request, aspect)
pending_request_ids.delete(request.id.to_id)
activate_contact(request.from, aspect)
request.destroy
request.reverse_for(self)
@ -32,20 +31,14 @@ module Diaspora
end
def accept_and_respond(contact_request_id, aspect_id)
request = pending_requests.find!(contact_request_id)
request = Request.to(self.person).find!(contact_request_id)
requester = request.from
reversed_request = accept_contact_request(request, aspect_by_id(aspect_id))
dispatch_contact_acceptance reversed_request, requester
end
def ignore_contact_request(contact_request_id)
request = pending_requests.find!(contact_request_id)
person = request.from
self.pending_request_ids.delete(request.id)
self.save
person.save
request = Request.to(self.person).find!(contact_request_id)
request.destroy
end
@ -58,8 +51,6 @@ module Diaspora
#this is a new contact request
elsif contact_request.from != self.person
if contact_request.save!
self.pending_requests << contact_request
self.save!
Rails.logger.info("event=contact_request status=received_new_request from=#{contact_request.from.diaspora_handle} to=#{self.diaspora_handle}")
self.mail(Jobs::MailRequestReceived, self.id, contact_request.from.id)
end
@ -125,10 +116,6 @@ module Diaspora
save!
aspect.save!
end
def requests_for_me
pending_requests.select { |req| req.to == self.person }
end
end
end
end

View file

@ -2,16 +2,15 @@
# licensed under the Affero General Public License version 3 or later. See
# the COPYRIGHT file.
require File.join(Rails.root, 'lib/rake_helpers')
include RakeHelpers
namespace :migrations do
namespace :migrations do
desc 'make old registered services into the new class specific services'
task :service_reclassify do
require File.join(Rails.root,"config/environment")
Service.all.each do |s|
provider = s.provider
if provider
s._type = "Services::#{provider.camelize}"
@ -28,6 +27,17 @@ namespace :migrations do
RakeHelpers::fix_diaspora_handle_spaces(false)
end
task :contacts_as_requests do
require File.join(Rails.root,"config/environment")
old_contacts = Contact.all(:pending => nil)
old_contacts.each{|contact| contact.pending = false; contact.save}
puts "all done"
end
desc 'allow to upgrade old image urls to use rel path'
task :swtich_image_urls do
end
desc 'fix usernames with periods in them'
task :fix_periods_in_username do
RakeHelpers::fix_periods_in_usernames(false)

View file

@ -74,19 +74,20 @@ describe InvitationsController do
end
context 'success' do
let(:invited) {User.find_by_username(@accept_params[:user][:username])}
it 'creates user' do
put :update, @accept_params
User.find_by_username(@accept_params[:user][:username]).should_not be_nil
invited.should_not be_nil
end
it 'seeds the aspects' do
put :update, @accept_params
User.find_by_username(@accept_params[:user][:username]).aspects.count.should == 2
invited.aspects.count.should == 2
end
it 'adds a pending request' do
put :update, @accept_params
User.find_by_username(@accept_params[:user][:username]).pending_requests.count.should == 1
Request.to(invited.person).count.should == 1
end
end
context 'failure' do

View file

@ -24,7 +24,7 @@ describe RequestsController do
before do
@other_user.send_contact_request_to(@user.person, @other_user.aspects.first)
@user.reload # so it can find its pending requests.
@friend_request = @user.pending_requests.first
@friend_request = Request.to(@user.person).first
end
describe 'when accepting a contact request' do
it "succeeds" do
@ -68,14 +68,14 @@ describe RequestsController do
end
it 'autoaccepts and when sending a request to someone who sent me a request' do
@other_user.send_contact_request_to(@user.person, @other_user.aspects[0])
@user.reload.pending_requests.count.should == 1
Request.to(@user).count.should == 1
@user.contact_for(@other_user.person).should be_nil
post(:create, :request => {
:to => @other_user.diaspora_handle,
:into => @user.aspects[0].id}
)
@user.reload.pending_requests.count.should == 0
Request.to(@user).count.should == 0
@user.contact_for(@other_user.person).should_not be_nil
@user.aspects[0].contacts.all(:person_id => @other_user.person.id).should_not be_nil
end

View file

@ -25,7 +25,7 @@ module HelperMethods
user2.reload
aspect2.reload
new_request = user2.pending_requests.find_by_from_id!(user1.person.id)
new_request = Request.from(user1.person).to(user2.person).first
user1.reload
aspect1.reload

View file

@ -59,7 +59,7 @@ describe Diaspora::Parser do
it "should activate the Person if I initiated a request to that url" do
user.send_contact_request_to(user2.person, aspect)
request = user2.reload.pending_requests.find_by_to_id!(user2.person.id)
request = Request.to(user2).from(user).first
fantasy_resque do
user2.accept_and_respond(request.id, aspect2.id)
end

View file

@ -79,14 +79,6 @@ describe Request do
end
end
context 'quering request through user' do
it 'finds requests for that user' do
request
user2.reload
user2.requests_for_me.detect{|r| r.from == user.person}.should_not be_nil
end
end
describe '.hashes_for_person' do
before do
@user = make_user

View file

@ -41,7 +41,7 @@ describe Diaspora::UserModules::Connecting do
it 'persists no request for requester' do
proc {
user.send_contact_request_to(user2.person, aspect1)
}.should_not change{user.reload.pending_requests.count}
}.should_not change{Request.to(user).count}
end
end
@ -53,7 +53,7 @@ describe Diaspora::UserModules::Connecting do
it 'adds a request to pending if it was not sent by user' do
user.receive_contact_request(@r)
user.reload.pending_requests.should include @r
Request.to(user).all.should include @r
end
it 'enqueues a mail job' do
@ -73,12 +73,12 @@ describe Diaspora::UserModules::Connecting do
end
it 'deletes the original request' do
user.receive_request(@acceptance, user2.person)
user.pending_requests.include?(@original_request).should be_false
Request.to(user).all.include?(@original_request).should be_false
Request.find(@original_request.id).should be_nil
end
it 'deletes the acceptance' do
user.receive_request(@acceptance, user2.person)
user.pending_requests.include?(@acceptance).should be_false
Request.to(user).all.include?(@original_request).should be_false
Request.find(@acceptance.id).should be_nil
end
it 'enqueues a mail job' do
@ -101,19 +101,14 @@ describe Diaspora::UserModules::Connecting do
user.reload
end
it "should delete an accepted contact request from pending_requests" do
proc {
user.accept_contact_request(@received_request, aspect)
}.should change(user.reload.pending_requests, :count ).by(-1)
end
it "should delete an accepted contact request" do
proc {
user.accept_contact_request(@received_request, aspect)
}.should change(Request, :count ).by(-1)
end
it 'should be able to ignore a pending contact request' do
proc { user.ignore_contact_request(@received_request.id) }.should change(
user.reload.pending_requests, :count ).by(-1)
proc { user.ignore_contact_request(@received_request.id)
}.should change(Request, :count ).by(-1)
end
it 'should ignore a contact request from yourself' do
@ -127,9 +122,9 @@ describe Diaspora::UserModules::Connecting do
describe 'multiple users accepting/rejecting the same person' do
before do
user.pending_requests.empty?.should be true
Request.to(user).count.should == 0
user.contacts.empty?.should be true
user2.pending_requests.empty?.should be true
Request.to(user2).count.should == 0
user2.contacts.empty?.should be true
@request = Request.instantiate(:to => user.person, :from => person_one)
@ -190,10 +185,10 @@ describe Diaspora::UserModules::Connecting do
it 'should keep the person around if the users ignores them' do
user.ignore_contact_request user.pending_requests.first.id
user.ignore_contact_request Request.to(user).first.id
user.contact_for(person_one).should be_nil
user2.ignore_contact_request user2.pending_requests.first.id #@request_two.id
user2.ignore_contact_request Request.to(user2).first.id
user2.contact_for(person_one).should be_nil
end
end
@ -203,9 +198,6 @@ describe Diaspora::UserModules::Connecting do
describe 'a user accepting rejecting multiple people' do
before do
user.pending_requests.empty?.should be true
user.contacts.empty?.should be true
@request = Request.instantiate(:to => user.person, :from => person_one)
@request_two = Request.instantiate(:to => user.person, :from => person_two)
end
@ -213,21 +205,21 @@ describe Diaspora::UserModules::Connecting do
it "keeps the right counts of contacts" do
received_req = user.receive @request.to_diaspora_xml, person_one
user.reload.pending_requests.size.should == 1
user.contacts.size.should be 0
Request.to(user).count.should == 1
user.reload.contacts.size.should be 0
received_req2 = user.receive @request_two.to_diaspora_xml, person_two
user.reload.pending_requests.size.should == 2
user.contacts.size.should be 0
Request.to(user).count.should == 2
user.reload.contacts.size.should be 0
user.accept_contact_request received_req, aspect
user.reload.pending_requests.size.should == 1
user.contacts.size.should be 1
Request.to(user).count.should == 1
user.reload.contacts.size.should be 1
user.contact_for(person_one).should_not be_nil
user.ignore_contact_request received_req2.id
user.reload.pending_requests.size.should == 0
user.contacts.size.should be 1
Request.to(user).count.should == 0
user.reload.contacts.size.should be 1
user.contact_for(person_two).should be_nil
end
end

View file

@ -74,13 +74,13 @@ describe User do
end
it 'resolves incoming invitations into contact requests' do
invited_user.reload.pending_requests.count.should == 1
Request.to(invited_user).count.should == 1
end
context 'after request acceptance' do
before do
fantasy_resque do
invited_user.accept_and_respond(invited_user.pending_requests.first.id,
invited_user.accept_and_respond(Request.to(invited_user).first.id,
invited_user.aspects.create(
:name => 'first aspect!').id)
end
@ -89,12 +89,12 @@ describe User do
end
it 'successfully connects invited_user to inviter' do
invited_user.contact_for(inviter.person).should_not be_nil
invited_user.pending_requests.count.should == 0
invited_user.contact_for(inviter.person).should_not be_pending
Request.to(invited_user).count.should == 0
end
it 'successfully connects inviter to invited_user' do
inviter.contact_for(invited_user.person).should_not be_nil
inviter.pending_requests.size.should == 0
inviter.contact_for(invited_user.person).should_not be_pending
end
end
end

View file

@ -14,28 +14,6 @@ describe User do
user.encryption_key.should_not be nil
end
describe "#has_incoming_request_from" do
it "returns true if the user has an incoming request from the person" do
user2.send_contact_request_to(user.person, aspect2)
user.reload
user2.reload
user.has_incoming_request_from(user2.person).should be_true
end
it "returns false if the user does not have an incoming request from the person" do
user.has_incoming_request_from(user2.person).should be_false
end
it "returns false if the user has requested to be contacts with the person" do
user.send_contact_request_to(user2.person, aspect)
user.reload
user2.reload
user.has_incoming_request_from(user2.person).should be_false
end
end
describe 'overwriting people' do
it 'does not overwrite old users with factory' do
pending "Why do you want to set ids directly? MONGOMAPPERRRRR!!!"