From f1ee32145d0c0e042fc8f829ed23aa8fb7ee3cb0 Mon Sep 17 00:00:00 2001 From: danielvincent <danielgrippi@gmail.com> Date: Tue, 14 Dec 2010 18:45:41 -0800 Subject: [PATCH] Remove pending_requests from user, just use Request.from and Request.to. --- app/controllers/application_controller.rb | 2 +- app/controllers/people_controller.rb | 3 ++ app/controllers/users_controller.rb | 2 +- app/helpers/people_helper.rb | 5 --- app/models/user.rb | 8 ---- app/views/people/show.html.haml | 4 +- features/step_definitions/custom_web_steps.rb | 2 +- features/support/paths.rb | 2 +- lib/diaspora/user/connecting.rb | 17 +------ lib/tasks/migrations.rake | 16 +++++-- .../invitations_controller_spec.rb | 7 +-- spec/controllers/requests_controller_spec.rb | 6 +-- spec/helper_methods.rb | 2 +- spec/lib/diaspora/parser_spec.rb | 2 +- spec/models/request_spec.rb | 8 ---- spec/models/user/connecting_spec.rb | 44 ++++++++----------- spec/models/user/invite_spec.rb | 10 ++--- spec/models/user_spec.rb | 22 ---------- 18 files changed, 56 insertions(+), 106 deletions(-) diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 0bf2bef0db..98f024e9e5 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -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 diff --git a/app/controllers/people_controller.rb b/app/controllers/people_controller.rb index 35d84bbe0a..478122e9cb 100644 --- a/app/controllers/people_controller.rb +++ b/app/controllers/people_controller.rb @@ -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) diff --git a/app/controllers/users_controller.rb b/app/controllers/users_controller.rb index 7a45d186cd..5d21795314 100644 --- a/app/controllers/users_controller.rb +++ b/app/controllers/users_controller.rb @@ -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 diff --git a/app/helpers/people_helper.rb b/app/helpers/people_helper.rb index 3a042b0228..4711433ed2 100644 --- a/app/helpers/people_helper.rb +++ b/app/helpers/people_helper.rb @@ -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 diff --git a/app/models/user.rb b/app/models/user.rb index b5496f47d0..f5de024c86 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -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 diff --git a/app/views/people/show.html.haml b/app/views/people/show.html.haml index 6a0e4a2bf0..ec32a4fdf9 100644 --- a/app/views/people/show.html.haml +++ b/app/views/people/show.html.haml @@ -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') diff --git a/features/step_definitions/custom_web_steps.rb b/features/step_definitions/custom_web_steps.rb index 328bdac31d..bd1f617d92 100644 --- a/features/step_definitions/custom_web_steps.rb +++ b/features/step_definitions/custom_web_steps.rb @@ -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 diff --git a/features/support/paths.rb b/features/support/paths.rb index e5dfce0990..39d4e20baf 100644 --- a/features/support/paths.rb +++ b/features/support/paths.rb @@ -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 /^"(\/.*)"/ diff --git a/lib/diaspora/user/connecting.rb b/lib/diaspora/user/connecting.rb index d8ea45f364..d866f2e0b2 100644 --- a/lib/diaspora/user/connecting.rb +++ b/lib/diaspora/user/connecting.rb @@ -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 diff --git a/lib/tasks/migrations.rake b/lib/tasks/migrations.rake index 60f33144c9..c3ec99a4c5 100644 --- a/lib/tasks/migrations.rake +++ b/lib/tasks/migrations.rake @@ -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) diff --git a/spec/controllers/invitations_controller_spec.rb b/spec/controllers/invitations_controller_spec.rb index 83901b998a..6c4486d53b 100644 --- a/spec/controllers/invitations_controller_spec.rb +++ b/spec/controllers/invitations_controller_spec.rb @@ -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 diff --git a/spec/controllers/requests_controller_spec.rb b/spec/controllers/requests_controller_spec.rb index 146843649b..a1823cb427 100644 --- a/spec/controllers/requests_controller_spec.rb +++ b/spec/controllers/requests_controller_spec.rb @@ -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 diff --git a/spec/helper_methods.rb b/spec/helper_methods.rb index 377f278af6..018bb1520c 100644 --- a/spec/helper_methods.rb +++ b/spec/helper_methods.rb @@ -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 diff --git a/spec/lib/diaspora/parser_spec.rb b/spec/lib/diaspora/parser_spec.rb index f8baa96cdd..b3c86e7e6d 100644 --- a/spec/lib/diaspora/parser_spec.rb +++ b/spec/lib/diaspora/parser_spec.rb @@ -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 diff --git a/spec/models/request_spec.rb b/spec/models/request_spec.rb index 6a7026cd1d..ca3ff5edfd 100644 --- a/spec/models/request_spec.rb +++ b/spec/models/request_spec.rb @@ -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 diff --git a/spec/models/user/connecting_spec.rb b/spec/models/user/connecting_spec.rb index 7735898b6b..d34ce3a30d 100644 --- a/spec/models/user/connecting_spec.rb +++ b/spec/models/user/connecting_spec.rb @@ -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 diff --git a/spec/models/user/invite_spec.rb b/spec/models/user/invite_spec.rb index 32de1ad5d2..57e2949400 100644 --- a/spec/models/user/invite_spec.rb +++ b/spec/models/user/invite_spec.rb @@ -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 diff --git a/spec/models/user_spec.rb b/spec/models/user_spec.rb index 1af1454ab8..e33d004e89 100644 --- a/spec/models/user_spec.rb +++ b/spec/models/user_spec.rb @@ -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!!!" -- GitLab