DG MS; person request is now request. refactoring as well. user verbiage for request actions
This commit is contained in:
parent
900f1080a2
commit
06eec9204a
27 changed files with 296 additions and 95 deletions
|
|
@ -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."
|
||||
|
|
|
|||
34
app/controllers/requests_controller.rb
Normal file
34
app/controllers/requests_controller.rb
Normal 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
|
||||
2
app/helpers/requests_helper.rb
Normal file
2
app/helpers/requests_helper.rb
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
module RequestsHelper
|
||||
end
|
||||
|
|
@ -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
|
||||
|
||||
def self.for(url)
|
||||
request = PersonRequest.new(:url => url, :person => User.first)
|
||||
request.push_to_url
|
||||
request.save
|
||||
request
|
||||
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 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
|
||||
|
|
|
|||
29
app/models/request.rb
Normal file
29
app/models/request.rb
Normal 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
|
||||
|
|
@ -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)
|
||||
|
|
@ -19,15 +24,40 @@ class User < Person
|
|||
false
|
||||
end
|
||||
|
||||
validates_presence_of :profile
|
||||
|
||||
before_validation :do_bad_things
|
||||
def do_bad_things
|
||||
self.password_confirmation = self.password
|
||||
######### 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
|
||||
|
||||
|
||||
end
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -1,9 +0,0 @@
|
|||
= form_for @person_request do |f|
|
||||
= f.error_messages
|
||||
|
||||
%p
|
||||
= f.label :url
|
||||
= f.text_field :url
|
||||
|
||||
%p
|
||||
= f.submit
|
||||
|
|
@ -1,9 +0,0 @@
|
|||
= form_for @person_request do |f|
|
||||
= f.error_messages
|
||||
|
||||
%p
|
||||
= f.label :url
|
||||
= f.text_field :url
|
||||
|
||||
%p
|
||||
= f.submit
|
||||
|
|
@ -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
|
||||
|
|
@ -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
|
||||
|
|
@ -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
|
||||
|
|
@ -1,5 +0,0 @@
|
|||
- title "New Person Request"
|
||||
|
||||
= render 'form'
|
||||
|
||||
%p= link_to "Back to List", person_requests_path
|
||||
|
|
@ -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
|
||||
10
app/views/requests/_form.haml
Normal file
10
app/views/requests/_form.haml
Normal 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
|
||||
9
app/views/requests/_new_request.haml
Normal file
9
app/views/requests/_new_request.haml
Normal 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
|
||||
5
app/views/requests/_request.html.haml
Normal file
5
app/views/requests/_request.html.haml
Normal 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
|
||||
8
app/views/requests/edit.html.haml
Normal file
8
app/views/requests/edit.html.haml
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
- title "Edit Request"
|
||||
|
||||
= render 'form'
|
||||
|
||||
%p
|
||||
= link_to "Show", request_path(@person_request)
|
||||
|
|
||||
= link_to "View All", request_path
|
||||
15
app/views/requests/index.html.haml
Normal file
15
app/views/requests/index.html.haml
Normal 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
|
||||
|
||||
5
app/views/requests/new.html.haml
Normal file
5
app/views/requests/new.html.haml
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
- title "New Request"
|
||||
|
||||
= render 'form'
|
||||
|
||||
%p= link_to "Back to List", requests_path
|
||||
11
app/views/requests/show.html.haml
Normal file
11
app/views/requests/show.html.haml
Normal 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
|
||||
|
|
@ -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"
|
||||
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
57
spec/models/request_spec.rb
Normal file
57
spec/models/request_spec.rb
Normal 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
|
||||
Loading…
Reference in a new issue