diff --git a/app/controllers/person_requests_controller.rb b/app/controllers/person_requests_controller.rb index d6b22f993..37579a187 100644 --- a/app/controllers/person_requests_controller.rb +++ b/app/controllers/person_requests_controller.rb @@ -1,24 +1,26 @@ -class PersonRequestsController < ApplicationController +class RequestsController < ApplicationController before_filter :authenticate_user! - include PersonRequestsHelper + include RequestsHelper def index - @person_requests = PersonRequest.paginate :page => params[:page], :order => 'created_at DESC' - @person_request = PersonRequest.new + @local_person_requests = Request.from_user( User.first ) + @remote_person_requests = Request.for_user( User.first ) + + @person_request = Request.new end def destroy - @person_request = PersonRequest.where(:id => params[:id]).first + @person_request = Request.where(:id => params[:id]).first @person_request.destroy flash[:notice] = "Successfully destroyed person request." redirect_to person_requests_url end def new - @person_request = PersonRequest.new + @person_request = Request.new end def create - @person_request = PersonRequest.for params[:person_request][:url] + @person_request = Request.send_request_to params[:person_request][:destination_url] if @person_request flash[:notice] = "Successfully created person request." diff --git a/app/controllers/requests_controller.rb b/app/controllers/requests_controller.rb new file mode 100644 index 000000000..d8fd3fd60 --- /dev/null +++ b/app/controllers/requests_controller.rb @@ -0,0 +1,34 @@ +class RequestsController < ApplicationController + before_filter :authenticate_user! + include RequestsHelper + def index + @local_requests = Request.from_user( User.first ) + @remote_requests = Request.for_user( User.first ) + + @request = Request.new + end + + def destroy + @request = Request.where(:id => params[:id]).first + @request.destroy + flash[:notice] = "Successfully destroyed person request." + redirect_to person_requests_url + end + + def new + @request = Request.new + end + + def create + @request = current_user.send_friend_request_to(params[:request][:destination_url]) + + if @request + flash[:notice] = "Successfully created person request." + redirect_to person_requests_url + else + render :action => 'new' + end + end + + +end diff --git a/app/helpers/requests_helper.rb b/app/helpers/requests_helper.rb new file mode 100644 index 000000000..53ac95ccc --- /dev/null +++ b/app/helpers/requests_helper.rb @@ -0,0 +1,2 @@ +module RequestsHelper +end diff --git a/app/models/person_request.rb b/app/models/person_request.rb index 3e3027886..19111a5a1 100644 --- a/app/models/person_request.rb +++ b/app/models/person_request.rb @@ -1,8 +1,5 @@ class PersonRequest - require 'lib/common' - include ApplicationHelper include MongoMapper::Document - include ROXML include Diaspora::Webhooks @@ -11,26 +8,26 @@ class PersonRequest xml_accessor :_id xml_accessor :person, :as => Person - key :url, String + key :destination_url, String + key :callback_url, String key :person, Person - validates_presence_of :url + validates_presence_of :destination_url, :callback_url before_save :check_for_person_requests + + scope :for_user, lambda{ |user| where(:destination_url => user.url) } + scope :from_user, lambda{ |user| where(:destination_url.ne => user.url) } - def self.for(url) - request = PersonRequest.new(:url => url, :person => User.first) - request.push_to_url - request.save - request + def self.instantiate(options ={}) + person = options[:from] + self.new(:destination_url => options[:to], :callback_url => person.url, :person => person) end - def check_for_person_requests - p = Person.where(:url => self.url).first - if p - p.active = true - p.save - end + def activate_friend + p = Person.where(:id => self.person_id).first + p.active = true + p.save end end diff --git a/app/models/request.rb b/app/models/request.rb new file mode 100644 index 000000000..bc08a16ea --- /dev/null +++ b/app/models/request.rb @@ -0,0 +1,29 @@ +class Request + include MongoMapper::Document + include Diaspora::Webhooks + include ROXML + + xml_accessor :_id + xml_accessor :person, :as => Person + + key :destination_url, String + key :callback_url, String + key :person, Person + + validates_presence_of :destination_url, :callback_url + + scope :for_user, lambda{ |user| where(:destination_url => user.url) } + scope :from_user, lambda{ |user| where(:destination_url.ne => user.url) } + + def self.instantiate(options ={}) + person = options[:from] + self.new(:destination_url => options[:to], :callback_url => person.url, :person => person) + end + + def activate_friend + p = Person.where(:id => self.person.id).first + p.active = true + p.save + end + +end diff --git a/app/models/user.rb b/app/models/user.rb index 2723c8b5d..ce7c8da7c 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -4,9 +4,14 @@ class User < Person :recoverable, :rememberable, :trackable, :validatable + validates_presence_of :profile + + before_validation :do_bad_things + + ######## Commenting ######## def comment(text, options = {}) - raise "Comment on what, motherfucker?" unless options[:on] + raise "must comment on something!" unless options[:on] c = Comment.new(:person_id => self.id, :text => text, :post => options[:on]) if c.save if mine?(c.post) @@ -18,16 +23,41 @@ class User < Person end false end - - validates_presence_of :profile - - before_validation :do_bad_things + + ######### Friend Requesting + def send_friend_request_to(friend_url) + p = Request.instantiate(:to => friend_url, :from => self) + if p.save + p.push_to_url friend_url + end + end + + def accept_friend_request(friend_request_id) + request = Request.where(:id => friend_request_id).first + request.activate_friend + request.person = self + request.push_to(self.callback_url) + request.destroy + end + + def receive_friend_request(friend_request) + if Request.where(:id => friend_request.id).first + friend_request.activate_person + friend_request.destroy + else + friend_request.save + end + end + + + def mine?(post) + self == post.person + end + + private def do_bad_things self.password_confirmation = self.password end - def mine?(post) - self == post.person - end end diff --git a/app/views/layouts/application.html.haml b/app/views/layouts/application.html.haml index 7365d8b39..ebdaa733f 100644 --- a/app/views/layouts/application.html.haml +++ b/app/views/layouts/application.html.haml @@ -32,7 +32,7 @@ - if user_signed_in? =User.first.real_name | - = link_to "requests", person_requests_path + = link_to "requests", requests_path | = link_to "logout", destroy_user_session_path - else diff --git a/app/views/people/_sidebar.html.haml b/app/views/people/_sidebar.html.haml index bee8cb4b3..4c73aa13c 100644 --- a/app/views/people/_sidebar.html.haml +++ b/app/views/people/_sidebar.html.haml @@ -2,4 +2,4 @@ %ul#friend_stream.nav - for person in @people %li= link_to person.real_name, person_path(person) -= link_to "add a new person", new_person_request_path += link_to "add a new person", new_request_path diff --git a/app/views/person_requests/_form.haml b/app/views/person_requests/_form.haml deleted file mode 100644 index 8aa4eb008..000000000 --- a/app/views/person_requests/_form.haml +++ /dev/null @@ -1,9 +0,0 @@ -= form_for @person_request do |f| - = f.error_messages - - %p - = f.label :url - = f.text_field :url - - %p - = f.submit diff --git a/app/views/person_requests/_new_person_request.haml b/app/views/person_requests/_new_person_request.haml deleted file mode 100644 index 8aa4eb008..000000000 --- a/app/views/person_requests/_new_person_request.haml +++ /dev/null @@ -1,9 +0,0 @@ -= form_for @person_request do |f| - = f.error_messages - - %p - = f.label :url - = f.text_field :url - - %p - = f.submit diff --git a/app/views/person_requests/_person_request.html.haml b/app/views/person_requests/_person_request.html.haml deleted file mode 100644 index 29f16e264..000000000 --- a/app/views/person_requests/_person_request.html.haml +++ /dev/null @@ -1,5 +0,0 @@ -%li.message{:id => person_request.id, :class => "mine"} - = person_request.url - - .destroy_link - = link_to 'Ignore', person_request_path(person_request), :confirm => 'Are you sure?', :method => :delete, :remote => true diff --git a/app/views/person_requests/edit.html.haml b/app/views/person_requests/edit.html.haml deleted file mode 100644 index eba1b657b..000000000 --- a/app/views/person_requests/edit.html.haml +++ /dev/null @@ -1,8 +0,0 @@ -- title "Edit Person Request" - -= render 'form' - -%p - = link_to "Show", person_request_path(@person_request) - | - = link_to "View All", person_request_path diff --git a/app/views/person_requests/index.html.haml b/app/views/person_requests/index.html.haml deleted file mode 100644 index 75a9bbd04..000000000 --- a/app/views/person_requests/index.html.haml +++ /dev/null @@ -1,7 +0,0 @@ -%h1 person requests -= render "person_requests/new_person_request", :person_request => @person_request -%ul#stream - - for person_request in @person_requests - = render "person_request", :person_request => person_request -#pagination - = will_paginate @person_requests diff --git a/app/views/person_requests/new.html.haml b/app/views/person_requests/new.html.haml deleted file mode 100644 index 71260d8e6..000000000 --- a/app/views/person_requests/new.html.haml +++ /dev/null @@ -1,5 +0,0 @@ -- title "New Person Request" - -= render 'form' - -%p= link_to "Back to List", person_requests_path diff --git a/app/views/person_requests/show.html.haml b/app/views/person_requests/show.html.haml deleted file mode 100644 index 1c6ce85d7..000000000 --- a/app/views/person_requests/show.html.haml +++ /dev/null @@ -1,11 +0,0 @@ -- title "Person Request" - -%p - = @person_request.inspect - -%p - = link_to "Edit", edit_person_request_path(@person_request) - | - = link_to "Destroy", @person_request, :confirm => 'Are you sure?', :method => :delete - | - = link_to "View All", person_requests_path diff --git a/app/views/requests/_form.haml b/app/views/requests/_form.haml new file mode 100644 index 000000000..3241d21a5 --- /dev/null +++ b/app/views/requests/_form.haml @@ -0,0 +1,10 @@ += form_for @request do |f| + = f.error_messages + + %p + = f.label :destination_url + = f.text_field :destiation_url + + + %p + = f.submit diff --git a/app/views/requests/_new_request.haml b/app/views/requests/_new_request.haml new file mode 100644 index 000000000..319265e29 --- /dev/null +++ b/app/views/requests/_new_request.haml @@ -0,0 +1,9 @@ += form_for @request do |f| + = f.error_messages + + %p + = f.label :destination_url + = f.text_field :destination_url + + %p + = f.submit diff --git a/app/views/requests/_request.html.haml b/app/views/requests/_request.html.haml new file mode 100644 index 000000000..1fcff3bb4 --- /dev/null +++ b/app/views/requests/_request.html.haml @@ -0,0 +1,5 @@ +%li.message{:id => request.id, :class => "mine"} + = "to : #{request.destination_url}" + + .destroy_link + = link_to 'Ignore', request_path(request), :confirm => 'Are you sure?', :method => :delete, :remote => true diff --git a/app/views/requests/edit.html.haml b/app/views/requests/edit.html.haml new file mode 100644 index 000000000..c1a6ea854 --- /dev/null +++ b/app/views/requests/edit.html.haml @@ -0,0 +1,8 @@ +- title "Edit Request" + += render 'form' + +%p + = link_to "Show", request_path(@person_request) + | + = link_to "View All", request_path diff --git a/app/views/requests/index.html.haml b/app/views/requests/index.html.haml new file mode 100644 index 000000000..45b920beb --- /dev/null +++ b/app/views/requests/index.html.haml @@ -0,0 +1,15 @@ +%h1 requests += render "requests/new_request", :request => @request + +%ul#stream + %li + %h3 incoming + - for request in @remote_requests + = render "request", :request => request + = request.destination_url + " " + User.first.url + + %li + %h3 outgoing + - for request in @local_requests + = render "request", :request => request + diff --git a/app/views/requests/new.html.haml b/app/views/requests/new.html.haml new file mode 100644 index 000000000..9343e8273 --- /dev/null +++ b/app/views/requests/new.html.haml @@ -0,0 +1,5 @@ +- title "New Request" + += render 'form' + +%p= link_to "Back to List", requests_path diff --git a/app/views/requests/show.html.haml b/app/views/requests/show.html.haml new file mode 100644 index 000000000..2b01ac76d --- /dev/null +++ b/app/views/requests/show.html.haml @@ -0,0 +1,11 @@ +- title "Request" + +%p + = @request.inspect + +%p + = link_to "Edit", edit_request_path(@request) + | + = link_to "Destroy", @request, :confirm => 'Are you sure?', :method => :delete + | + = link_to "View All", requests_path diff --git a/config/routes.rb b/config/routes.rb index 914c5a08b..08b1f6f55 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -4,7 +4,7 @@ Diaspora::Application.routes.draw do |map| resources :people resources :status_messages resources :comments - resources :person_requests + resources :requests match 'warzombie', :to => "dashboard#warzombie" diff --git a/lib/common.rb b/lib/common.rb index ca270730b..5c78032e1 100644 --- a/lib/common.rb +++ b/lib/common.rb @@ -53,6 +53,7 @@ module Diaspora module Webhooks def self.included(klass) klass.class_eval do + include ROXML @@queue = MessageHandler.new def notify_people @@ -70,8 +71,8 @@ module Diaspora end end - def push_to_url - hook_url = self.url + "receive/" + def push_to_url(url) + hook_url = url + "receive/" xml = self.class.build_xml_for([self]) @@queue.add_post_request( [hook_url], xml ) @@queue.process diff --git a/spec/factories.rb b/spec/factories.rb index 925f7c5be..5467a6b39 100644 --- a/spec/factories.rb +++ b/spec/factories.rb @@ -19,7 +19,7 @@ Factory.define :user do |u| u.sequence(:email) {|n| "bob#{n}@aol.com"} u.password "bluepin7" u.password_confirmation "bluepin7" - u.url "www.example.com" + u.url "www.example.com/" u.profile Profile.new( :first_name => "Bob", :last_name => "Smith" ) end diff --git a/spec/models/person_request_spec.rb b/spec/models/person_request_spec.rb index acf9528f3..5914a4fc3 100644 --- a/spec/models/person_request_spec.rb +++ b/spec/models/person_request_spec.rb @@ -5,7 +5,7 @@ describe PersonRequest do it 'should require a url' do person_request = PersonRequest.new person_request.valid?.should be false - person_request.url = "http://google.com/" + person_request.destination_url = "http://google.com/" person_request.valid?.should be true end @@ -23,19 +23,59 @@ describe PersonRequest do it 'should be sent to the url upon for action' do PersonRequest.send(:class_variable_get, :@@queue).should_receive(:add_post_request) Factory.create(:user) - PersonRequest.for("http://www.google.com") + PersonRequest.send_to("http://www.google.com") end it "should activate a person if it exists on creation of a request for that url" do user = Factory.create(:user) person = Factory.create(:person, :url => "http://123google.com/") - PersonRequest.for(person.url) + PersonRequest.send_to(person.url) Person.where(:url => person.url).first.active.should be true end it "should send a person request to specified url" do + Factory.create(:user) PersonRequest.send(:class_variable_get, :@@queue).should_receive(:add_post_request) - PersonRequest.for("http://google.com/") + PersonRequest.send_to("http://google.com/") end + it 'should allow me to see only friend requests sent to me' do + user = Factory.create(:user) + remote_person = Factory.build(:user, :email => "robert@grimm.com", :url => "http://king.com/") + + PersonRequest.create(:destination_url => remote_person.url, :person => remote_person) + PersonRequest.create(:destination_url => remote_person.url, :person => remote_person) + PersonRequest.create(:destination_url => user.url, :person => user) + + PersonRequest.for_user(user).all.count.should == 1 + end + + it 'should allow me to see only friend requests sent by me' do + user = Factory.create(:user) + remote_person = Factory.build(:user, :email => "robert@grimm.com", :url => "http://king.com/") + + PersonRequest.create(:destination_url => remote_person.url, :person => remote_person) + PersonRequest.create(:destination_url => user.url, :person => user) + PersonRequest.create(:destination_url => user.url, :person => user) + + PersonRequest.from_user(user).all.count.should == 1 + end + + describe "sending" do + before do + @user = Factory.create(:user) + @remote_person = Factory.build(:user, :email => "robert@grimm.com", :url => "http://king.com/") + end + + + it 'shoud be able to send a friend request' do + user = Factory.create(:user) + remote_person = Factory.build(:user, :email => "robert@grimm.com", :url => "http://king.com/") + + + + + + end + end end diff --git a/spec/models/request_spec.rb b/spec/models/request_spec.rb new file mode 100644 index 000000000..86c9718fb --- /dev/null +++ b/spec/models/request_spec.rb @@ -0,0 +1,57 @@ +require 'spec_helper' + +describe Request do + + it 'should require a destination and callback url' do + person_request = Request.new + person_request.valid?.should be false + person_request.destination_url = "http://google.com/" + person_request.callback_url = "http://foob.com/" + person_request.valid?.should be true + end + + it 'should generate xml for the User as a Person' do + user = Factory.create(:user) + request = Request.new(:url => "http://www.google.com/", :person => user) + + xml = request.to_xml.to_s + xml.include?(user.email).should be true + xml.include?(user.url).should be true + xml.include?(user.profile.first_name).should be true + xml.include?(user.profile.last_name).should be true + end + + + it "should should activate a user" do + remote_person = Factory.create(:person, :email => "robert@grimm.com", :url => "http://king.com/") + f = Request.create(:destination_url => remote_person.url, :person => remote_person) + f.activate_friend + Person.where(:id => remote_person.id).first.active.should be true + end + + + it 'should allow me to see only friend requests sent to me' do + user = Factory.create(:user) + remote_person = Factory.build(:user, :email => "robert@grimm.com", :url => "http://king.com/") + + Request.instantiate(:from => user, :to => remote_person.url).save + Request.instantiate(:from => user, :to => remote_person.url).save + Request.instantiate(:from => user, :to => remote_person.url).save + Request.instantiate(:from => remote_person, :to => user.url).save + + Request.for_user(user).all.count.should == 1 + end + + it 'should allow me to see only friend requests sent by me' do + user = Factory.create(:user) + remote_person = Factory.build(:user, :email => "robert@grimm.com", :url => "http://king.com/") + + Request.instantiate(:from => user, :to => remote_person.url).save + Request.instantiate(:from => user, :to => remote_person.url).save + Request.instantiate(:from => user, :to => remote_person.url).save + Request.instantiate(:from => remote_person, :to => user.url).save + + Request.from_user(user).all.count.should == 3 + end + +end