DG MS; person request is now request. refactoring as well. user verbiage for request actions

This commit is contained in:
maxwell 2010-07-08 18:28:39 -07:00
parent 900f1080a2
commit 06eec9204a
27 changed files with 296 additions and 95 deletions

View file

@ -1,24 +1,26 @@
class PersonRequestsController < ApplicationController class RequestsController < ApplicationController
before_filter :authenticate_user! before_filter :authenticate_user!
include PersonRequestsHelper include RequestsHelper
def index def index
@person_requests = PersonRequest.paginate :page => params[:page], :order => 'created_at DESC' @local_person_requests = Request.from_user( User.first )
@person_request = PersonRequest.new @remote_person_requests = Request.for_user( User.first )
@person_request = Request.new
end end
def destroy def destroy
@person_request = PersonRequest.where(:id => params[:id]).first @person_request = Request.where(:id => params[:id]).first
@person_request.destroy @person_request.destroy
flash[:notice] = "Successfully destroyed person request." flash[:notice] = "Successfully destroyed person request."
redirect_to person_requests_url redirect_to person_requests_url
end end
def new def new
@person_request = PersonRequest.new @person_request = Request.new
end end
def create def create
@person_request = PersonRequest.for params[:person_request][:url] @person_request = Request.send_request_to params[:person_request][:destination_url]
if @person_request if @person_request
flash[:notice] = "Successfully created person request." flash[:notice] = "Successfully created person request."

View file

@ -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

View file

@ -0,0 +1,2 @@
module RequestsHelper
end

View file

@ -1,8 +1,5 @@
class PersonRequest class PersonRequest
require 'lib/common'
include ApplicationHelper
include MongoMapper::Document include MongoMapper::Document
include ROXML
include Diaspora::Webhooks include Diaspora::Webhooks
@ -11,26 +8,26 @@ class PersonRequest
xml_accessor :_id xml_accessor :_id
xml_accessor :person, :as => Person xml_accessor :person, :as => Person
key :url, String key :destination_url, String
key :callback_url, String
key :person, Person key :person, Person
validates_presence_of :url validates_presence_of :destination_url, :callback_url
before_save :check_for_person_requests 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) def self.instantiate(options ={})
request = PersonRequest.new(:url => url, :person => User.first) person = options[:from]
request.push_to_url self.new(:destination_url => options[:to], :callback_url => person.url, :person => person)
request.save
request
end end
def check_for_person_requests def activate_friend
p = Person.where(:url => self.url).first p = Person.where(:id => self.person_id).first
if p p.active = true
p.active = true p.save
p.save
end
end end
end end

29
app/models/request.rb Normal file
View file

@ -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

View file

@ -4,9 +4,14 @@ class User < Person
:recoverable, :rememberable, :trackable, :validatable :recoverable, :rememberable, :trackable, :validatable
validates_presence_of :profile
before_validation :do_bad_things
######## Commenting ########
def comment(text, options = {}) 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]) c = Comment.new(:person_id => self.id, :text => text, :post => options[:on])
if c.save if c.save
if mine?(c.post) if mine?(c.post)
@ -18,16 +23,41 @@ class User < Person
end end
false false
end end
validates_presence_of :profile ######### Friend Requesting
def send_friend_request_to(friend_url)
before_validation :do_bad_things 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 def do_bad_things
self.password_confirmation = self.password self.password_confirmation = self.password
end end
def mine?(post)
self == post.person
end
end end

View file

@ -32,7 +32,7 @@
- if user_signed_in? - if user_signed_in?
=User.first.real_name =User.first.real_name
| |
= link_to "requests", person_requests_path = link_to "requests", requests_path
| |
= link_to "logout", destroy_user_session_path = link_to "logout", destroy_user_session_path
- else - else

View file

@ -2,4 +2,4 @@
%ul#friend_stream.nav %ul#friend_stream.nav
- for person in @people - for person in @people
%li= link_to person.real_name, person_path(person) %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

View file

@ -1,9 +0,0 @@
= form_for @person_request do |f|
= f.error_messages
%p
= f.label :url
= f.text_field :url
%p
= f.submit

View file

@ -1,9 +0,0 @@
= form_for @person_request do |f|
= f.error_messages
%p
= f.label :url
= f.text_field :url
%p
= f.submit

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -1,5 +0,0 @@
- title "New Person Request"
= render 'form'
%p= link_to "Back to List", person_requests_path

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -0,0 +1,8 @@
- title "Edit Request"
= render 'form'
%p
= link_to "Show", request_path(@person_request)
|
= link_to "View All", request_path

View file

@ -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

View file

@ -0,0 +1,5 @@
- title "New Request"
= render 'form'
%p= link_to "Back to List", requests_path

View file

@ -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

View file

@ -4,7 +4,7 @@ Diaspora::Application.routes.draw do |map|
resources :people resources :people
resources :status_messages resources :status_messages
resources :comments resources :comments
resources :person_requests resources :requests
match 'warzombie', :to => "dashboard#warzombie" match 'warzombie', :to => "dashboard#warzombie"

View file

@ -53,6 +53,7 @@ module Diaspora
module Webhooks module Webhooks
def self.included(klass) def self.included(klass)
klass.class_eval do klass.class_eval do
include ROXML
@@queue = MessageHandler.new @@queue = MessageHandler.new
def notify_people def notify_people
@ -70,8 +71,8 @@ module Diaspora
end end
end end
def push_to_url def push_to_url(url)
hook_url = self.url + "receive/" hook_url = url + "receive/"
xml = self.class.build_xml_for([self]) xml = self.class.build_xml_for([self])
@@queue.add_post_request( [hook_url], xml ) @@queue.add_post_request( [hook_url], xml )
@@queue.process @@queue.process

View file

@ -19,7 +19,7 @@ Factory.define :user do |u|
u.sequence(:email) {|n| "bob#{n}@aol.com"} u.sequence(:email) {|n| "bob#{n}@aol.com"}
u.password "bluepin7" u.password "bluepin7"
u.password_confirmation "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" ) u.profile Profile.new( :first_name => "Bob", :last_name => "Smith" )
end end

View file

@ -5,7 +5,7 @@ describe PersonRequest do
it 'should require a url' do it 'should require a url' do
person_request = PersonRequest.new person_request = PersonRequest.new
person_request.valid?.should be false 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 person_request.valid?.should be true
end end
@ -23,19 +23,59 @@ describe PersonRequest do
it 'should be sent to the url upon for action' do it 'should be sent to the url upon for action' do
PersonRequest.send(:class_variable_get, :@@queue).should_receive(:add_post_request) PersonRequest.send(:class_variable_get, :@@queue).should_receive(:add_post_request)
Factory.create(:user) Factory.create(:user)
PersonRequest.for("http://www.google.com") PersonRequest.send_to("http://www.google.com")
end end
it "should activate a person if it exists on creation of a request for that url" do it "should activate a person if it exists on creation of a request for that url" do
user = Factory.create(:user) user = Factory.create(:user)
person = Factory.create(:person, :url => "http://123google.com/") 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 Person.where(:url => person.url).first.active.should be true
end end
it "should send a person request to specified url" do 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.send(:class_variable_get, :@@queue).should_receive(:add_post_request)
PersonRequest.for("http://google.com/") PersonRequest.send_to("http://google.com/")
end 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 end

View file

@ -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