From 9a44578c37e9c823dc5a8446f497f4d02e8990d4 Mon Sep 17 00:00:00 2001 From: Maxwell Salzberg Date: Tue, 22 Jun 2010 23:11:55 -0700 Subject: [PATCH 2/4] DG; Person is now the parent of Friend and User --- app/models/friend.rb | 10 ++-------- app/models/user.rb | 7 +------ app/views/friends/index.html.haml | 6 ++++-- app/views/friends/new.html.haml | 8 ++++++-- app/views/friends/show.html.haml | 7 +++++-- app/views/users/index.html.haml | 4 +++- spec/factories.rb | 2 +- spec/models/friend_spec.rb | 4 ++-- spec/models/post_spec.rb | 2 -- 9 files changed, 24 insertions(+), 26 deletions(-) diff --git a/app/models/friend.rb b/app/models/friend.rb index 7f3463dd2..dba24e580 100644 --- a/app/models/friend.rb +++ b/app/models/friend.rb @@ -1,16 +1,10 @@ -class Friend - include Mongoid::Document - include ROXML +class Friend < Person - xml_accessor :username xml_accessor :url - xml_accessor :real_name - field :username field :url - field :real_name - validates_presence_of :username, :url, :real_name + validates_presence_of :url validates_format_of :url, :with => /^(http|https):\/\/[a-z0-9]+([\-\.]{1}[a-z0-9]+)*\.[a-z]{2,5}(([0-9]{1,5})?\/.*)?$/ix diff --git a/app/models/user.rb b/app/models/user.rb index 45624f10e..a1ac4217f 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -1,13 +1,8 @@ -class User - include Mongoid::Document +class User < Person # Include default devise modules. Others available are: # :token_authenticatable, :confirmable, :lockable and :timeoutable devise :database_authenticatable, :registerable, :recoverable, :rememberable, :trackable, :validatable - field :real_name - - validates_presence_of :real_name - end diff --git a/app/views/friends/index.html.haml b/app/views/friends/index.html.haml index 044ade323..52abf1451 100644 --- a/app/views/friends/index.html.haml +++ b/app/views/friends/index.html.haml @@ -2,11 +2,13 @@ %table %tr - %th username + %th real name + %th email %th url - for friend in @friends %tr - %td= friend.username + %td= friend.real_name + %td= friend.email %td= friend.url %td= link_to 'Show', friend %td= link_to 'Destroy', friend, :confirm => 'Are you sure?', :method => :delete diff --git a/app/views/friends/new.html.haml b/app/views/friends/new.html.haml index e9b1451be..161bd0250 100644 --- a/app/views/friends/new.html.haml +++ b/app/views/friends/new.html.haml @@ -3,9 +3,13 @@ = form_for @friend do |f| = f.error_messages %p - = f.label :username + = f.label :real_name %br - = f.text_field :username + = f.text_field :real_name + %p + = f.label :email + %br + = f.text_field :email %p = f.label :url %br diff --git a/app/views/friends/show.html.haml b/app/views/friends/show.html.haml index 034eb23ce..fedc3e2a3 100644 --- a/app/views/friends/show.html.haml +++ b/app/views/friends/show.html.haml @@ -1,8 +1,11 @@ - title "Friend" %p - %strong Username: - = @friend.username + %strong Real Name: + = @friend.real_name +%p + %strong Email: + = @friend.email %p %strong Url: = @friend.url diff --git a/app/views/users/index.html.haml b/app/views/users/index.html.haml index f0c2e7f87..6f210359c 100644 --- a/app/views/users/index.html.haml +++ b/app/views/users/index.html.haml @@ -2,9 +2,11 @@ %table %tr - %th User + %th Real Name + %th email %th Password - for user in @users %tr + %td= user.real_name %td= user.email %td= user.encrypted_password diff --git a/spec/factories.rb b/spec/factories.rb index 236e2984d..d995e1e88 100644 --- a/spec/factories.rb +++ b/spec/factories.rb @@ -4,7 +4,7 @@ Factory.define :friend do |f| f.real_name 'John Doe' - f.username 'max' + f.email 'max@max.com' f.url 'http://max.com/' end diff --git a/spec/models/friend_spec.rb b/spec/models/friend_spec.rb index 4ec6cf848..668e8e1f5 100644 --- a/spec/models/friend_spec.rb +++ b/spec/models/friend_spec.rb @@ -64,7 +64,7 @@ describe Friend do describe "XML" do before do @f = Factory.build(:friend) - @xml = "\n #{@f.username}\n #{@f.url}\n #{@f.real_name}\n" + @xml = "\n #{@f.url}\n #{@f.email}\n #{@f.real_name}\n" end it 'should serialize to XML' do @@ -73,7 +73,7 @@ describe Friend do it 'should marshal serialized XML to object' do parsed = Friend.from_xml(@xml) - parsed.username.should == @f.username + parsed.email.should == @f.email parsed.url.should == @f.url parsed.valid?.should be_true end diff --git a/spec/models/post_spec.rb b/spec/models/post_spec.rb index f8908e23e..7bc28571f 100644 --- a/spec/models/post_spec.rb +++ b/spec/models/post_spec.rb @@ -5,8 +5,6 @@ describe Post do Factory.create(:user, :email => "bob@aol.com") end - describe 'requirements' do - end describe 'defaults' do before do From 523cb6f15f15c3d3e743c41893cd18b890cf825f Mon Sep 17 00:00:00 2001 From: Maxwell Salzberg Date: Tue, 22 Jun 2010 23:27:31 -0700 Subject: [PATCH 3/4] DG; Post belongs to a Person, and a Person has many Posts. Also, added Person for real this time --- app/models/person.rb | 15 +++++++++++++++ app/models/post.rb | 5 +++++ .../status_messages/_status_message.html.haml | 2 +- spec/models/post_spec.rb | 6 ++++-- 4 files changed, 25 insertions(+), 3 deletions(-) create mode 100644 app/models/person.rb diff --git a/app/models/person.rb b/app/models/person.rb new file mode 100644 index 000000000..399dbb394 --- /dev/null +++ b/app/models/person.rb @@ -0,0 +1,15 @@ +class Person + include Mongoid::Document + include ROXML + + xml_accessor :email + xml_accessor :real_name + + field :email + field :real_name + + has_many_related :posts + + validates_presence_of :email, :real_name + +end diff --git a/app/models/post.rb b/app/models/post.rb index 04cccc690..9dc312309 100644 --- a/app/models/post.rb +++ b/app/models/post.rb @@ -17,6 +17,10 @@ class Post field :source field :snippet + + belongs_to_related :person + + before_create :set_defaults after_save :send_to_view @@ -50,6 +54,7 @@ class Post self.owner ||= user_email self.source ||= user_email self.snippet ||= user_email + self.person ||= User.first end end diff --git a/app/views/status_messages/_status_message.html.haml b/app/views/status_messages/_status_message.html.haml index bfe97c678..1484b2d9e 100644 --- a/app/views/status_messages/_status_message.html.haml +++ b/app/views/status_messages/_status_message.html.haml @@ -1,6 +1,6 @@ %li.message{:class => ("mine" if mine?(post))} %span.from - = link_to post.owner, "#" + = link_to post.person.real_name, "#" = post.message %div.time = "#{time_ago_in_words(post.updated_at)} ago" diff --git a/spec/models/post_spec.rb b/spec/models/post_spec.rb index 7bc28571f..61bd5efbc 100644 --- a/spec/models/post_spec.rb +++ b/spec/models/post_spec.rb @@ -5,12 +5,14 @@ describe Post do Factory.create(:user, :email => "bob@aol.com") end - describe 'defaults' do before do WebSocket.stub!(:update_clients) - @post = Factory.create(:post, :owner => nil, :source => nil, :snippet => nil) + @post = Factory.create(:post, :person => nil, :owner => nil, :source => nil, :snippet => nil) + end + it "should associate the owner if none is present" do + @post.person.should == User.first end it "should add an owner if none is present" do From e6e1c22b07ea75663f526ed21d42d37b9bb93cf2 Mon Sep 17 00:00:00 2001 From: Maxwell Salzberg Date: Wed, 23 Jun 2010 01:20:56 -0700 Subject: [PATCH 4/4] DG; webhooks apply incoming posts to current friends with a silent fail on no match --- app/helpers/application_helper.rb | 49 ++++++++++++++++++--------- lib/common.rb | 2 -- spec/helpers/parser_spec.rb | 55 +++++++++++++++++++++++-------- spec/lib/common_spec.rb | 6 +--- 4 files changed, 76 insertions(+), 36 deletions(-) diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb index 9ae4b3f94..9791f1b55 100644 --- a/app/helpers/application_helper.rb +++ b/app/helpers/application_helper.rb @@ -7,25 +7,42 @@ module ApplicationHelper object.attributes.keys end - def store_posts_from_xml(xml) + def parse_sender_id_from_xml(xml) doc = Nokogiri::XML(xml) { |cfg| cfg.noblanks } - - #i need to check some sort of metadata field - - doc.xpath("/XML/posts/post").each do |post| #this is the post wrapper - post.children.each do|type| #now the text of post itself is the type - #type object to xml is the the thing we want to from_xml - check_and_save_post(type) - end - end + 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) + Person.where(:email => sender_id).first end - def check_and_save_post(type) - begin - object = type.name.camelize.constantize.from_xml type.to_s - object.save if object.is_a? Post - rescue - puts "Not of type post" + def parse_body_contents_from_xml(xml) + doc = Nokogiri::XML(xml) { |cfg| cfg.noblanks } + doc.xpath("/XML/posts/post") + end + + def parse_posts_from_xml(xml) + posts = [] + body = parse_body_contents_from_xml(xml) + body.children.each do |post| + begin + object = post.name.camelize.constantize.from_xml post.to_s + posts << object if object.is_a? Post + rescue + puts "Not a real type: #{post.to_s}" + end + end + posts + end + + def store_posts_from_xml(xml) + sender_object = parse_sender_object_from_xml(xml) + posts = parse_posts_from_xml(xml) + + posts.each do |p| + p.person = sender_object + p.save end end diff --git a/lib/common.rb b/lib/common.rb index 8e34b4591..8da6ddd31 100644 --- a/lib/common.rb +++ b/lib/common.rb @@ -36,11 +36,9 @@ module Diaspora " #{User.first.email} - #{User.first.email} " end - end end end diff --git a/spec/helpers/parser_spec.rb b/spec/helpers/parser_spec.rb index 5d742239f..d1d991a8f 100644 --- a/spec/helpers/parser_spec.rb +++ b/spec/helpers/parser_spec.rb @@ -4,7 +4,8 @@ include ApplicationHelper describe ApplicationHelper do before do - Factory.create(:user) + @user = Factory.create(:user, :email => "bob@aol.com") + @friend =Factory.create(:friend, :email => "bill@gates.com") end it "should store objects sent from xml" do @@ -18,17 +19,25 @@ describe ApplicationHelper do end it 'should discard posts where it does not know the type' do - xml = " - \n Here is another message\n a@a.com\n a@a.com\n a@a.com\n - - \n HEY DUDE\n a@a.com\n a@a.com\n a@a.com\n + xml = " + + + #{User.first.email} + + + \n Here is another message\n a@a.com\n a@a.com\n a@a.com\n \n HEY DUDE\n a@a.com\n a@a.com\n a@a.com\n " store_posts_from_xml(xml) Post.count.should == 2 end it 'should discard types which are not of type post' do - xml = " + xml = " + + + #{User.first.email} + + \n Here is another message\n a@a.com\n a@a.com\n a@a.com\n \n HEY DUDE\n a@a.com\n a@a.com\n a@a.com\n @@ -38,15 +47,35 @@ describe ApplicationHelper do end - describe "parsing a sender" do - it 'should be able to parse the sender of a collection' do - status_messages = [] - 10.times { status_messages << Factory.build(:status_message)} - xml = Post.build_xml_for(status_messages) + describe "parsing compliant XML object" do + before do + status_messages = [] + 10.times { status_messages << Factory.build(:status_message)} + @xml = Post.build_xml_for(status_messages) end - it 'should be able to verify the sender as a friend' do - pending + it 'should be able to parse the sender\'s unique id' do + parse_sender_id_from_xml(@xml).should == @user.email + end + + it 'should be able to retrieve the sender\'s local Person object' do + parse_sender_object_from_xml(@xml).should == @user + end + + it 'should be able to parse the body\'s contents' do + body = parse_body_contents_from_xml(@xml).to_s + body.should_not include "" + body.should_not include "" + body.should_not include "" + body.should_not include "" + body.should include "" + body.should include "" + end + + it 'should be able to extract all posts to an array' do + posts = parse_posts_from_xml(@xml) + posts.is_a?(Array).should be true + posts.count.should == 10 end end diff --git a/spec/lib/common_spec.rb b/spec/lib/common_spec.rb index bc5bdf41d..dc99c3aeb 100644 --- a/spec/lib/common_spec.rb +++ b/spec/lib/common_spec.rb @@ -29,11 +29,7 @@ describe Diaspora do end it "should provide the owner's email" do - @xml.should include "bob@aol.com" - end - - it "should provide the owner's url" do - pending "user does not have url field" + @xml.should include "#{User.first.email}" end end