Merge branch 'master' of github.com:diaspora/diaspora_rails

This commit is contained in:
danielvincent 2010-06-28 20:39:34 -07:00
commit ab086e5b0e
24 changed files with 211 additions and 158 deletions

View file

@ -4,6 +4,9 @@ class CommentsController < ApplicationController
def create def create
target = Post.first(:id => params[:comment][:post_id]) target = Post.first(:id => params[:comment][:post_id])
text = params[:comment][:text] text = params[:comment][:text]
puts params.inspect
if current_user.comment text, :on => target if current_user.comment text, :on => target
render :text => "Woo!" render :text => "Woo!"
else else

View file

@ -7,6 +7,7 @@ class FriendsController < ApplicationController
def show def show
@friend = Friend.where(:id => params[:id]).first @friend = Friend.where(:id => params[:id]).first
@friend_profile = @friend.profile
@friend_posts = Post.where(:person_id => @friend.id).sort(:created_at.desc) @friend_posts = Post.where(:person_id => @friend.id).sort(:created_at.desc)
end end

View file

@ -1,4 +1,6 @@
module ApplicationHelper module ApplicationHelper
require 'lib/common'
include Diaspora::XMLParser
def object_path(object) def object_path(object)
eval("#{object.class.to_s.underscore}_path(object)") eval("#{object.class.to_s.underscore}_path(object)")
end end
@ -7,46 +9,6 @@ module ApplicationHelper
object.attributes.keys object.attributes.keys
end end
def parse_sender_id_from_xml(xml)
doc = Nokogiri::XML(xml) { |cfg| cfg.noblanks }
doc.xpath("/XML/head/sender/email").text.to_s
end
def parse_sender_object_from_xml(xml)
sender_id = parse_sender_id_from_xml(xml)
Friend.where(:email => sender_id).first
end
def parse_body_contents_from_xml(xml)
doc = Nokogiri::XML(xml) { |cfg| cfg.noblanks }
doc.xpath("/XML/posts/post")
end
def parse_objects_from_xml(xml)
objects = []
sender = parse_sender_object_from_xml(xml)
body = parse_body_contents_from_xml(xml)
body.children.each do |post|
begin
object = post.name.camelize.constantize.from_xml post.to_s
object.person = sender if object.is_a? Post
objects << object
rescue
puts "Not a real type: #{object.to_s}"
end
end
objects
end
def store_objects_from_xml(xml)
objects = parse_objects_from_xml(xml)
objects.each do |p|
p.save if p.respond_to?(:person) && !(p.person.nil?) #WTF
#p.save if p.respond_to?(:person) && !(p.person == nil) #WTF
end
end
def mine?(post) def mine?(post)
post.person == User.first post.person == User.first
end end

View file

@ -3,7 +3,7 @@ module StatusMessagesHelper
def my_latest_message def my_latest_message
message = StatusMessage.my_newest message = StatusMessage.my_newest
unless message.nil? unless message.nil?
return message.message + " " + how_long_ago(message) return message.message + " - " + how_long_ago(message)
else else
return "No message to display." return "No message to display."
end end

View file

@ -3,18 +3,15 @@ class Person
include ROXML include ROXML
xml_accessor :email xml_accessor :email
xml_accessor :real_name
key :email, String key :email, String
key :real_name, String
#key :post_ids, Array#, :typecast => ObjectId
one :profile, :class_name => 'Profile', :foreign_key => :person_id
many :posts, :class_name => 'Post', :foreign_key => :person_id many :posts, :class_name => 'Post', :foreign_key => :person_id
validates_presence_of :email, :real_name validates_presence_of :email
# def newest(type = nil) def real_name
# type.constantize.where(:person_id => id).last self.profile.first_name + " " + self.profile.last_name
# end end
end end

View file

@ -8,19 +8,13 @@ class Post
xml_accessor :_id xml_accessor :_id
key :person_id, ObjectId key :person_id, ObjectId
belongs_to :person, :class_name => 'Person'
many :comments, :class_name => 'Comment', :foreign_key => :post_id many :comments, :class_name => 'Comment', :foreign_key => :post_id
belongs_to :person, :class_name => 'Person'
timestamps! timestamps!
after_save :send_to_view after_save :send_to_view
#validates_presence_of :person
def self.stream def self.stream
Post.sort(:created_at.desc).all Post.sort(:created_at.desc).all
@ -43,8 +37,7 @@ class Post
protected protected
def send_to_view def send_to_view
self.reload WebSocket.update_clients(self)
WebSocket.update_clients(self)
end end

13
app/models/profile.rb Normal file
View file

@ -0,0 +1,13 @@
class Profile
include MongoMapper::Document
key :first_name, String
key :last_name, String
key :person_id, ObjectId
belongs_to :person
validates_presence_of :first_name, :last_name, :person_id
end

View file

@ -14,6 +14,7 @@ class User < Person
Comment.new(:person_id => self.id, :text => text, :post => options[:on]).save Comment.new(:person_id => self.id, :text => text, :post => options[:on]).save
end end
validates_presence_of :profile
before_validation :do_bad_things before_validation :do_bad_things
def do_bad_things def do_bad_things

View file

@ -0,0 +1,12 @@
= form_for @bookmark do |f|
= f.error_messages
%p
= f.label :title
%br
= f.text_field :title
%p
= f.label :link
%br
= f.text_field :link
%p
= f.submit

View file

@ -1,6 +1,5 @@
= form_for Comment.new, :remote => true do |f| = form_tag("/comments", :remote => true, :class =>"new_comment", :id => "new_comment-#{post.id}") do
= f.error_messages
%p %p
= f.text_field :text, :value => "dislike!" = text_field_tag "comment_text", 'dislike!', :size => 30, :name => 'comment[text]'
= f.hidden_field :post_id, :value => post.id = hidden_field_tag "comment_post_id", "#{post.id}", :name => "comment[post_id]"
= f.submit 'comment', :class => 'button' = submit_tag 'comment', :id => "comment_submit_#{post.id}", :name => "commit"

View file

@ -2,10 +2,6 @@
= form_for @friend do |f| = form_for @friend do |f|
= f.error_messages = f.error_messages
%p
= f.label :real_name
%br
= f.text_field :real_name
%p %p
= f.label :email = f.label :email
%br %br

View file

@ -1,5 +1,22 @@
%h1= "#{@friend.real_name}'s network stream" %h1= "#{@friend.real_name}"
- if @friend_profile
%p
%b First Name
%p
= @friend_profile.first_name
%p
%b Last Name
%p
= @friend_profile.last_name
%br
%br
%br
%br
- if @friend.posts - if @friend.posts
%h3 stream
%ul#stream %ul#stream
- for post in @friend_posts - for post in @friend_posts
= render type_partial(post), :post => post = render type_partial(post), :post => post

View file

@ -8,7 +8,7 @@
= stylesheet_link_tag "blueprint/screen", :media => 'screen' = stylesheet_link_tag "blueprint/screen", :media => 'screen'
= stylesheet_link_tag "application" = stylesheet_link_tag "application"
/= javascript_include_tag"http://ajax.googleapis.com/ajax/libs/jquery/1.4.1/jquery.min.js" /= javascript_include_tag"http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"
= javascript_include_tag 'jquery142', 'rails', 'view' = javascript_include_tag 'jquery142', 'rails', 'view'
= javascript_include_tag 'tiny_mce/tiny_mce.js' = javascript_include_tag 'tiny_mce/tiny_mce.js'
@ -30,17 +30,17 @@
ws = new WebSocket("ws://#{request.host}:8080/"); ws = new WebSocket("ws://#{request.host}:8080/");
ws.onmessage = function(evt) { ws.onmessage = function(evt) {
var obj = jQuery.parseJSON(evt.data); var obj = jQuery.parseJSON(evt.data);
if((location.href.indexOf(obj['class']) != -1 ) || (location.pathname == '/')) { debug("got a " + obj['class']);
$("#stream").prepend($(obj['html']).fadeIn("fast")); if((location.href.indexOf(obj['class']) != -1 ) || (location.pathname == '/')) {
}; $("#stream").prepend($(obj['html']).fadeIn("fast"));
} };
ws.onclose = function() { debug("socket closed"); }; };
ws.onopen = function() { ws.onclose = function() { debug("socket closed"); };
ws.send(location.pathname); ws.onopen = function() {
debug("connected..."); ws.send(location.pathname);
}; debug("connected...");
};
}); });
});
%body %body
- flash.each do |name, msg| - flash.each do |name, msg|
= content_tag :div, msg, :id => "flash_#{name}" = content_tag :div, msg, :id => "flash_#{name}"

View file

@ -3,7 +3,7 @@
= link_to_person post.person = link_to_person post.person
= post.message = post.message
%div.time %div.time
= link_to "#{time_ago_in_words(post.updated_at)} ago", status_message_path(post) = link_to(how_long_ago(post), status_message_path(post))
= render "comments/comments", :post => post = render "comments/comments", :post => post
- if mine?(post) - if mine?(post)
= link_to 'Destroy', status_message_path(post), :confirm => 'Are you sure?', :method => :delete = link_to 'Destroy', status_message_path(post), :confirm => 'Are you sure?', :method => :delete

View file

@ -5,19 +5,26 @@ module WebSocket
EM.next_tick { EM.next_tick {
EM.add_timer(0.1) do EM.add_timer(0.1) do
@channel = EM::Channel.new @channel = EM::Channel.new
puts @channel.inspect
@view = ActionView::Base.new(ActionController::Base.view_paths, {}) @view = ActionView::Base.new(ActionController::Base.view_paths, {})
class << @view class << @view
include ApplicationHelper include ApplicationHelper
include Rails.application.routes.url_helpers include Rails.application.routes.url_helpers
include ActionController::RequestForgeryProtection::ClassMethods
include ActionView::Helpers::FormTagHelper
include ActionView::Helpers::UrlHelper
def protect_against_forgery?
false
end
end end
end end
EventMachine::WebSocket.start(:host => "0.0.0.0", :port => 8080, :debug =>false) do |ws| EventMachine::WebSocket.start(:host => "0.0.0.0", :port => 8080, :debug =>true) do |ws|
ws.onopen { ws.onopen {
sid = @channel.subscribe { |msg| ws.send msg } sid = @channel.subscribe { |msg| ws.send msg }
ws.onmessage { |msg|}#@channel.push msg; puts msg} ws.onmessage { |msg| }#@channel.push msg; puts msg}
ws.onclose { @channel.unsubscribe(sid) } ws.onclose { @channel.unsubscribe(sid) }
} }
@ -29,8 +36,18 @@ module WebSocket
end end
def self.view_hash(object) def self.view_hash(object)
v = WebSocket.view_for(object) begin
puts v puts "I be working hard"
v = WebSocket.view_for(object)
puts v.inspect
rescue Exception => e
puts "in failzord " + v.inspect
puts object.inspect
puts e.message
raise e
end
puts "i made it here"
{:class =>object.class.to_s.underscore.pluralize, :html => v} {:class =>object.class.to_s.underscore.pluralize, :html => v}
end end

View file

@ -1,4 +1,47 @@
module Diaspora module Diaspora
module XMLParser
def parse_sender_id_from_xml(xml)
doc = Nokogiri::XML(xml) { |cfg| cfg.noblanks }
doc.xpath("/XML/head/sender/email").text.to_s
end
def parse_sender_object_from_xml(xml)
sender_id = parse_sender_id_from_xml(xml)
Friend.where(:email => sender_id).first
end
def parse_body_contents_from_xml(xml)
doc = Nokogiri::XML(xml) { |cfg| cfg.noblanks }
doc.xpath("/XML/posts/post")
end
def parse_objects_from_xml(xml)
objects = []
sender = parse_sender_object_from_xml(xml)
body = parse_body_contents_from_xml(xml)
body.children.each do |post|
begin
object = post.name.camelize.constantize.from_xml post.to_s
object.person = sender if object.is_a? Post
objects << object
rescue
puts "Not a real type: #{object.to_s}"
end
end
objects
end
def store_objects_from_xml(xml)
objects = parse_objects_from_xml(xml)
objects.each do |p|
p.save if p.respond_to?(:person) && !(p.person.nil?) #WTF
#p.save if p.respond_to?(:person) && !(p.person == nil) #WTF
end
end
end
module Webhooks module Webhooks
def self.included(klass) def self.included(klass)
klass.class_eval do klass.class_eval do

View file

@ -1,7 +1,3 @@
# require 'addressable/uri'
# require 'eventmachine'
# require 'em-http'
class MessageHandler class MessageHandler
NUM_TRIES = 3 NUM_TRIES = 3
@ -25,7 +21,7 @@ class MessageHandler
case query.type case query.type
when :post when :post
http = EventMachine::HttpRequest.new(query.destination).post :timeout => TIMEOUT, :body =>{:xml => query.body} http = EventMachine::HttpRequest.new(query.destination).post :timeout => TIMEOUT, :body =>{:xml => query.body}
http.callback {puts query.inspect; process} http.callback {process}
when :get when :get
http = EventMachine::HttpRequest.new(query.destination).get :timeout => TIMEOUT http = EventMachine::HttpRequest.new(query.destination).get :timeout => TIMEOUT
http.callback {send_to_seed(query, http.response); process} http.callback {send_to_seed(query, http.response); process}
@ -34,6 +30,7 @@ class MessageHandler
end end
http.errback { http.errback {
puts query.destination + " failed!"
query.try_count +=1 query.try_count +=1
@queue.push query unless query.try_count >= NUM_TRIES @queue.push query unless query.try_count >= NUM_TRIES
process process

View file

@ -30,6 +30,7 @@ $(document).ready(function(){
$('#bookmark_title').click(clearForm); $('#bookmark_title').click(clearForm);
$('#bookmark_link').click(clearForm); $('#bookmark_link').click(clearForm);
$('#debug_more').hide();
function clearForm(){ function clearForm(){
$(this).val(""); $(this).val("");

View file

@ -1,11 +1,30 @@
#For Guidance #For Guidance
#http://github.com/thoughtbot/factory_girl #http://github.com/thoughtbot/factory_girl
#http://railscasts.com/episodes/158-factories-not-fixtures # http://railscasts.com/episodes/158-factories-not-fixtures
Factory.define :profile do |p|
p.first_name "Robert"
p.last_name "Grimm"
p.person Person.new( :email => "bob@aol.com" )
end
Factory.define :person do |p|
p.email "bob@aol.com"
p.profile Profile.new( :first_name => "Robert", :last_name => "Grimm" )
end
Factory.define :user do |u|
u.sequence(:email) {|n| "bob#{n}@aol.com"}
u.password "bluepin7"
u.password_confirmation "bluepin7"
u.profile Profile.new( :first_name => "Bob", :last_name => "Smith" )
end
Factory.define :friend do |f| Factory.define :friend do |f|
f.real_name 'John Doe'
f.email 'max@max.com' f.email 'max@max.com'
f.url 'http://max.com/' f.url 'http://max.com/'
f.profile Profile.new( :first_name => "Robert", :last_name => "Grimm" )
end end
Factory.define :status_message do |m| Factory.define :status_message do |m|
@ -17,12 +36,6 @@ Factory.define :blog do |b|
b.sequence(:body) {|n| "jimmy's huge #{n} whales"} b.sequence(:body) {|n| "jimmy's huge #{n} whales"}
end end
Factory.define :user do |u|
u.real_name 'Bob Smith'
u.sequence(:email) {|n| "bob#{n}@aol.com"}
u.password "bluepin7"
u.password_confirmation "bluepin7"
end
Factory.define :bookmark do |b| Factory.define :bookmark do |b|
b.link "http://www.yahooligans.com/" b.link "http://www.yahooligans.com/"

View file

@ -15,19 +15,7 @@ describe "parser in application helper" do
store_objects_from_xml(xml) store_objects_from_xml(xml)
StatusMessage.count.should == 0 StatusMessage.count.should == 0
end end
it 'should discard posts where it does not know the type' do
xml = "<XML>
<head>
<sender>
<email>#{Friend.first.email}</email>
</sender>
</head><posts>
<post><status_message>\n <message>Here is another message</message>\n <owner>a@a.com</owner>\n <snippet>a@a.com</snippet>\n <source>a@a.com</source>\n</status_message></post> <post><not_a_real_type></not_a_real_type></post> <post><status_message>\n <message>HEY DUDE</message>\n <owner>a@a.com</owner>\n <snippet>a@a.com</snippet>\n <source>a@a.com</source>\n</status_message></post>
</posts></XML>"
store_objects_from_xml(xml)
Post.count.should == 2
Post.first.person.email.should == Friend.first.email
end
it "should reject xml with no sender" do it "should reject xml with no sender" do
xml = "<XML> xml = "<XML>
<head> <head>
@ -40,6 +28,7 @@ describe "parser in application helper" do
Post.count.should == 0 Post.count.should == 0
end end
it "should reject xml with a sender not in the database" do it "should reject xml with a sender not in the database" do
xml = "<XML> xml = "<XML>
<head> <head>
@ -54,6 +43,7 @@ describe "parser in application helper" do
store_objects_from_xml(xml) store_objects_from_xml(xml)
Post.count.should == 0 Post.count.should == 0
end end
it 'should discard types which are not of type post' do it 'should discard types which are not of type post' do
xml = "<XML> xml = "<XML>
<head> <head>
@ -62,13 +52,11 @@ describe "parser in application helper" do
</sender> </sender>
</head> </head>
<posts> <posts>
<post><status_message>\n <message>Here is another message</message>\n <owner>a@a.com</owner>\n <snippet>a@a.com</snippet>\n <source>a@a.com</source>\n</status_message></post>
<post><friend></friend></post> <post><friend></friend></post>
<post><status_message>\n <message>HEY DUDE</message>\n <owner>a@a.com</owner>\n <snippet>a@a.com</snippet>\n <source>a@a.com</source>\n</status_message></post>
</posts></XML>" </posts></XML>"
store_objects_from_xml(xml) store_objects_from_xml(xml)
Post.count.should == 2 Post.count.should == 0
Post.first.person.email.should == Friend.first.email
end end

View file

@ -2,21 +2,15 @@ require File.dirname(__FILE__) + '/../spec_helper'
describe Friend do describe Friend do
it 'should require a diaspora username and diaspora url' do describe 'requirements' do
n = Factory.build(:friend, :url => nil) it 'should include a url' do
n.valid?.should be false n = Factory.build(:friend, :url => nil)
n.url = "http://max.com/" n.valid?.should be false
n.valid?.should be true n.url = "http://max.com/"
n.valid?.should be true
end
end end
it 'should require a real name' do
n = Factory.build(:friend, :real_name => nil)
n.valid?.should be false
n.real_name = "John Smith"
n.valid?.should be true
end
it 'should validate its url' do it 'should validate its url' do
friend = Factory.build(:friend) friend = Factory.build(:friend)
@ -65,23 +59,4 @@ describe Friend do
friend.url = "http:///www.asodij.com/" friend.url = "http:///www.asodij.com/"
friend.valid?.should == false friend.valid?.should == false
end end
describe "XML" do
before do
@f = Factory.build(:friend)
@xml = "<friend>\n <url>#{@f.url}</url>\n <email>#{@f.email}</email>\n <real_name>#{@f.real_name}</real_name>\n</friend>"
end
it 'should serialize to XML' do
@f.to_xml.to_s.should == @xml
end
it 'should marshal serialized XML to object' do
parsed = Friend.from_xml(@xml)
parsed.email.should == @f.email
parsed.url.should == @f.url
parsed.valid?.should be_true
end
end
end end

View file

@ -0,0 +1,4 @@
require 'spec_helper'
describe Person do
end

View file

@ -0,0 +1,32 @@
require 'spec_helper'
describe Profile do
before do
@person = Factory.build(:person)
end
describe 'requirements' do
it "should include a first name" do
@person.profile = Factory.build(:profile, :person => @person, :first_name => nil)
@person.profile.valid?.should be false
@person.profile.first_name = "Bob"
@person.profile.valid?.should be true
end
it "should include a last name" do
@person.profile = Factory.build(:profile, :person => @person, :last_name => nil)
@person.profile.valid?.should be false
@person.profile.last_name = "Smith"
@person.profile.valid?.should be true
end
it "should include a person" do
profile = Factory.build(:profile, :person => nil)
profile.valid?.should be false
profile.person = @person
profile.valid?.should be true
end
end
end

View file

@ -1,20 +1,9 @@
require 'spec_helper' require 'spec_helper'
describe User do describe User do
it "should require a real name" do
u = Factory.build(:user, :real_name => nil)
u.valid?.should be false
u.real_name = "John Smith"
u.valid?.should be true
end
it "should create a valid user with the factory" do
u = Factory.build(:user)
u.valid?.should be true
end
it "should be a person" do it "should be a person" do
n = Person.count n = Person.count
Factory.create(:user) Factory.create(:user)
Person.count.should == n+1 Person.count.should == n+1
end end
end end