From 416f128ba8c7d51dfdf2c4e556d1bd80095b3c4b Mon Sep 17 00:00:00 2001
From: maxwell <maxwell@joindiaspora.com>
Date: Sat, 24 Jul 2010 15:48:42 -0700
Subject: [PATCH] MS pushing code around into seperate files for clairty, many
 specs moved around to much more sane names.  i still need to break out
 diaspora generator out of webhooks module

---
 app/controllers/blogs_controller.rb           |   2 +-
 app/controllers/bookmarks_controller.rb       |   2 +-
 app/controllers/status_messages_controller.rb |   2 +-
 app/models/album.rb                           |   3 +-
 app/models/blog.rb                            |  14 ++
 app/models/bookmark.rb                        |  15 ++-
 app/models/status_message.rb                  |  16 ++-
 app/views/layouts/application.html.haml       |   2 +-
 lib/diaspora/ostatus_generator.rb             |  70 ++++++++++
 lib/diaspora/webhooks.rb                      | 125 +-----------------
 .../controllers/dashboards_controller_spec.rb |   1 -
 ....rb => diaspora_ostatus_generator_spec.rb} |  12 +-
 ...parser_spec.rb => diaspora_parser_spec.rb} |  75 +----------
 spec/lib/ostatus_parser_spec.rb               |  68 ++++++++++
 .../lib/{common_spec.rb => web_hooks_spec.rb} |   0
 spec/lib/web_socket_spec.rb                   |   5 -
 16 files changed, 197 insertions(+), 215 deletions(-)
 create mode 100644 lib/diaspora/ostatus_generator.rb
 rename spec/lib/{xml_spec.rb => diaspora_ostatus_generator_spec.rb} (59%)
 rename spec/lib/{parser_spec.rb => diaspora_parser_spec.rb} (69%)
 create mode 100644 spec/lib/ostatus_parser_spec.rb
 rename spec/lib/{common_spec.rb => web_hooks_spec.rb} (100%)
 delete mode 100644 spec/lib/web_socket_spec.rb

diff --git a/app/controllers/blogs_controller.rb b/app/controllers/blogs_controller.rb
index 4a548de06d..e7461cedc8 100644
--- a/app/controllers/blogs_controller.rb
+++ b/app/controllers/blogs_controller.rb
@@ -6,7 +6,7 @@ class BlogsController < ApplicationController
     
     respond_to do |format|
       format.html 
-      format.atom {render :xml => Diaspora::XML::generate(:current_url => request.url, :objects => @blogs)}
+      format.atom {render :xml => Diaspora::OStatus::generate(:current_url => request.url, :objects => @blogs)}
     end
   
 
diff --git a/app/controllers/bookmarks_controller.rb b/app/controllers/bookmarks_controller.rb
index 1c7b8d986f..d22e934c21 100644
--- a/app/controllers/bookmarks_controller.rb
+++ b/app/controllers/bookmarks_controller.rb
@@ -8,7 +8,7 @@ class BookmarksController < ApplicationController
 
     respond_to do |format|
       format.html 
-      format.atom {render :xml => Diaspora::XML::generate(:current_url => request.url, :objects => @bookmarks)}
+      format.atom {render :xml => Diaspora::OStatus::generate(:current_url => request.url, :objects => @bookmarks)}
     end
   end
   
diff --git a/app/controllers/status_messages_controller.rb b/app/controllers/status_messages_controller.rb
index 1e68a74da6..356612181b 100644
--- a/app/controllers/status_messages_controller.rb
+++ b/app/controllers/status_messages_controller.rb
@@ -7,7 +7,7 @@ class StatusMessagesController < ApplicationController
 
     respond_to do |format|
       format.html 
-      format.atom {render :xml => Diaspora::XML::generate(:current_url => request.url, :objects => @status_messages)}
+      format.atom {render :xml => Diaspora::OStatus::generate(:current_url => request.url, :objects => @status_messages)}
     end
 
   end
diff --git a/app/models/album.rb b/app/models/album.rb
index 85a8de14fe..1307cfd2c3 100644
--- a/app/models/album.rb
+++ b/app/models/album.rb
@@ -18,8 +18,8 @@ class Album
 
   before_destroy :destroy_photos
   after_save :notify_people
-
   before_destroy :propagate_retraction
+  
   def prev_photo(photo)
     n_photo = self.photos.where(:created_at.lt => photo.created_at).sort(:created_at.desc).first
     n_photo ? n_photo : self.photos.sort(:created_at.desc).first
@@ -31,7 +31,6 @@ class Album
   end
 
   protected
-
   def destroy_photos
     photos.each{|p| p.destroy}
   end
diff --git a/app/models/blog.rb b/app/models/blog.rb
index 8cb375f45e..0df0dd5582 100644
--- a/app/models/blog.rb
+++ b/app/models/blog.rb
@@ -9,4 +9,18 @@ class Blog < Post
   
   validates_presence_of :title, :body
   
+  def to_activity
+          <<-XML
+  <entry>
+  <activity:verb>http://activitystrea.ms/schema/1.0/post</activity:verb>
+  <title>#{self.title}</title>
+  <content>#{self.body}</content>
+  <link rel="alternate" type="text/html" href="#{User.owner.url}blogs/#{self.id}"/>
+  <id>#{User.owner.url}blogs/#{self.id}</id>
+  <published>#{self.created_at.xmlschema}</published>
+  <updated>#{self.updated_at.xmlschema}</updated>
+  </entry>
+          XML
+  end
+  
 end
diff --git a/app/models/bookmark.rb b/app/models/bookmark.rb
index 3406437168..8f793fd47b 100644
--- a/app/models/bookmark.rb
+++ b/app/models/bookmark.rb
@@ -14,8 +14,21 @@ class Bookmark < Post
 
   before_validation :clean_link
 
-  protected
+  def to_activity
+        <<-XML
+  <entry>
+  <activity:verb>http://activitystrea.ms/schema/1.0/post</activity:verb>
+  <title>#{self.title}</title>
+  <link rel="alternate" type="text/html" href="#{User.owner.url}bookmarks/#{self.id}"/>
+  <link rel="related" type="text/html" href="#{self.link}"/>
+  <id>#{User.owner.url}bookmarks/#{self.id}</id>
+  <published>#{self.created_at.xmlschema}</published>
+  <updated>#{self.updated_at.xmlschema}</updated>
+  </entry>
+        XML
+  end
 
+  protected
   def clean_link
     if self.link
       self.link = 'http://' + self.link unless self.link.match('https?://')
diff --git a/app/models/status_message.rb b/app/models/status_message.rb
index 65e578dd6c..accad2a8cd 100644
--- a/app/models/status_message.rb
+++ b/app/models/status_message.rb
@@ -5,10 +5,22 @@ class StatusMessage < Post
   xml_accessor :message
 
   key :message, String
-
-
   validates_presence_of :message
   
+  
+  def to_activity
+        <<-XML
+  <entry>
+  <activity:verb>http://activitystrea.ms/schema/1.0/post</activity:verb>
+  <title>#{self.message}</title>
+  <link rel="alternate" type="text/html" href="#{User.owner.url}status_messages/#{self.id}"/>
+  <id>#{User.owner.url}status_messages/#{self.id}</id>
+  <published>#{self.created_at.xmlschema}</published>
+  <updated>#{self.updated_at.xmlschema}</updated>
+  </entry>
+        XML
+  end
+  
   def ==(other)
     (self.message == other.message) && (self.person.email == other.person.email)
   end
diff --git a/app/views/layouts/application.html.haml b/app/views/layouts/application.html.haml
index 3d0d5f3e48..0665430a06 100644
--- a/app/views/layouts/application.html.haml
+++ b/app/views/layouts/application.html.haml
@@ -53,7 +53,7 @@
         .span-18.append-3.last
           = yield
         .span-3.last
-          = link_to (image_tag(current_user.profile.image_url)), root_path
+          = link_to (person_image_tag(current_user), root_path)
           = link_to "Edit your profile", edit_user_path(current_user)
           %br
           %br
diff --git a/lib/diaspora/ostatus_generator.rb b/lib/diaspora/ostatus_generator.rb
new file mode 100644
index 0000000000..d4bf553799
--- /dev/null
+++ b/lib/diaspora/ostatus_generator.rb
@@ -0,0 +1,70 @@
+module Diaspora
+  module OStatus
+    def self.generate(opts= {})
+      xml = Generate::headers(opts[:current_url])
+      xml << Generate::author
+      xml << Generate::endpoints
+      xml << Generate::subject
+      xml << Generate::entries(opts[:objects])
+      xml << Generate::footer
+    end
+
+    module Generate
+      def self.headers(current_url)
+        #this is retarded
+        @@user = User.owner
+        <<-XML
+  <?xml version="1.0" encoding="UTF-8"?>
+  <feed xml:lang="en-US" xmlns="http://www.w3.org/2005/Atom" xmlns:thr="http://purl.org/syndication/thread/1.0" xmlns:georss="http://www.georss.org/georss" xmlns:activity="http://activitystrea.ms/spec/1.0/" xmlns:media="http://purl.org/syndication/atommedia" xmlns:poco="http://portablecontacts.net/spec/1.0" xmlns:ostatus="http://ostatus.org/schema/1.0" xmlns:statusnet="http://status.net/schema/api/1/">
+  <generator uri="http://joindiaspora.com/">Diaspora</generator>
+  <id>#{current_url}</id>
+  <title>Stream</title>
+  <subtitle>its a stream </subtitle>
+  <updated>#{Time.now.xmlschema}</updated>
+        XML
+      end
+
+      def self.author
+        <<-XML
+  <author>
+  <name>#{@@user.real_name}</name>
+  <uri>#{@@user.url}</uri>
+  </author>
+        XML
+      end
+
+      def self.endpoints
+          <<-XML
+  <link href="#{APP_CONFIG[:pubsub_server]}" rel="hub"/>
+          XML
+      end
+
+      def self.subject
+        <<-XML
+  <activity:subject>
+  <activity:object-type>http://activitystrea.ms/schema/1.0/person</activity:object-type>
+  <id>#{@@user.url}</id>
+  <title>#{@@user.real_name}</title>
+  <link rel="alternative" type="text/html" href="#{@@user.url}"/>
+  </activity:subject>
+        XML
+      end
+
+      def self.entries(objects)
+        xml = ""
+        if objects.respond_to? :each
+          objects.each {|x| xml << x.to_ostatus}
+        else
+          xml << objects.to_activity
+        end
+        xml
+      end
+
+      def self.footer
+        <<-XML.strip
+  </feed>
+        XML
+      end
+    end
+  end
+end
\ No newline at end of file
diff --git a/lib/diaspora/webhooks.rb b/lib/diaspora/webhooks.rb
index 9c9af0e5d0..1a886a1704 100644
--- a/lib/diaspora/webhooks.rb
+++ b/lib/diaspora/webhooks.rb
@@ -1,7 +1,7 @@
 module Diaspora
-   module Webhooks
-      def self.included(klass)
-        klass.class_eval do
+  module Webhooks
+    def self.included(klass)
+      klass.class_eval do
           include ROXML
           require 'message_handler'
           @@queue = MessageHandler.new
@@ -57,124 +57,7 @@ module Diaspora
             xml += "</posts>"
             xml += "</XML>"
           end
-
-        end
-      end
-    end
-
-    module XML
-
-      def self.generate(opts= {})
-        xml = Generate::headers(opts[:current_url])
-        xml << Generate::author
-        xml << Generate::endpoints
-        xml << Generate::subject
-        xml << Generate::entries(opts[:objects])
-        xml << Generate::footer
-      end
-
-      module Generate
-        def self.headers(current_url)
-          #this is retarded
-          @@user = User.owner
-          <<-XML
-  <?xml version="1.0" encoding="UTF-8"?>
-  <feed xml:lang="en-US" xmlns="http://www.w3.org/2005/Atom" xmlns:thr="http://purl.org/syndication/thread/1.0" xmlns:georss="http://www.georss.org/georss" xmlns:activity="http://activitystrea.ms/spec/1.0/" xmlns:media="http://purl.org/syndication/atommedia" xmlns:poco="http://portablecontacts.net/spec/1.0" xmlns:ostatus="http://ostatus.org/schema/1.0" xmlns:statusnet="http://status.net/schema/api/1/">
-  <generator uri="http://joindiaspora.com/">Diaspora</generator>
-  <id>#{current_url}</id>
-  <title>Stream</title>
-  <subtitle>its a stream </subtitle>
-  <updated>#{Time.now.xmlschema}</updated>
-          XML
-        end
-
-        def self.author
-          <<-XML
-  <author>
-  <name>#{@@user.real_name}</name>
-  <uri>#{@@user.url}</uri>
-  </author>
-          XML
-        end
-
-        def self.endpoints
-            <<-XML
-   <link href="#{APP_CONFIG[:pubsub_server]}" rel="hub"/>
-            XML
-        end
-
-        def self.subject
-          <<-XML
-  <activity:subject>
-  <activity:object-type>http://activitystrea.ms/schema/1.0/person</activity:object-type>
-  <id>#{@@user.url}</id>
-  <title>#{@@user.real_name}</title>
-  <link rel="alternative" type="text/html" href="#{@@user.url}"/>
-  </activity:subject>
-          XML
-        end
-
-        def self.entries(objects)
-          xml = ""
-          if objects.respond_to? :each
-            objects.each {|x| xml << self.entry(x)}
-          else
-            xml << self.entry(objects)
-          end
-          xml
-        end
-
-        def self.entry(object)
-          eval "#{object.class}_build_entry(object)"
-        end
-
-        def self.StatusMessage_build_entry(status_message)
-          <<-XML
-  <entry>
-  <activity:verb>http://activitystrea.ms/schema/1.0/post</activity:verb>
-  <title>#{status_message.message}</title>
-  <link rel="alternate" type="text/html" href="#{@@user.url}status_messages/#{status_message.id}"/>
-  <id>#{@@user.url}status_messages/#{status_message.id}</id>
-  <published>#{status_message.created_at.xmlschema}</published>
-  <updated>#{status_message.updated_at.xmlschema}</updated>
-  </entry>
-          XML
-        end
-
-        def self.Bookmark_build_entry(bookmark)
-          <<-XML
-  <entry>
-  <activity:verb>http://activitystrea.ms/schema/1.0/post</activity:verb>
-  <title>#{bookmark.title}</title>
-  <link rel="alternate" type="text/html" href="#{@@user.url}bookmarks/#{bookmark.id}"/>
-  <link rel="related" type="text/html" href="#{bookmark.link}"/>
-  <id>#{@@user.url}bookmarks/#{bookmark.id}</id>
-  <published>#{bookmark.created_at.xmlschema}</published>
-  <updated>#{bookmark.updated_at.xmlschema}</updated>
-  </entry>
-          XML
-        end
-
-
-        def self.Blog_build_entry(blog)
-          <<-XML
-  <entry>
-  <activity:verb>http://activitystrea.ms/schema/1.0/post</activity:verb>
-  <title>#{blog.title}</title>
-  <content>#{blog.body}</content>
-  <link rel="alternate" type="text/html" href="#{@@user.url}blogs/#{blog.id}"/>
-  <id>#{@@user.url}blogs/#{blog.id}</id>
-  <published>#{blog.created_at.xmlschema}</published>
-  <updated>#{blog.updated_at.xmlschema}</updated>
-  </entry>
-          XML
-        end
-
-        def self.footer
-          <<-XML.strip
-  </feed>
-          XML
         end
-      end
     end
+  end
 end
\ No newline at end of file
diff --git a/spec/controllers/dashboards_controller_spec.rb b/spec/controllers/dashboards_controller_spec.rb
index 87cfe80496..215fd09d46 100644
--- a/spec/controllers/dashboards_controller_spec.rb
+++ b/spec/controllers/dashboards_controller_spec.rb
@@ -2,7 +2,6 @@ require File.dirname(__FILE__) + '/../spec_helper'
  
 describe DashboardsController do
  render_views
-  
   before do
     @user = Factory.create(:user, :profile => Profile.new( :first_name => "bob", :last_name => "smith"))
     request.env['warden'] = mock_model(Warden, :authenticate? => @user, :authenticate! => @user, :authenticate => @user)
diff --git a/spec/lib/xml_spec.rb b/spec/lib/diaspora_ostatus_generator_spec.rb
similarity index 59%
rename from spec/lib/xml_spec.rb
rename to spec/lib/diaspora_ostatus_generator_spec.rb
index 42b26d01c5..b524e605bd 100644
--- a/spec/lib/xml_spec.rb
+++ b/spec/lib/diaspora_ostatus_generator_spec.rb
@@ -1,16 +1,18 @@
 require File.dirname(__FILE__) + '/../spec_helper'
 
-describe Diaspora::XML do
+require 'lib/diaspora/ostatus_generator'
+
+describe Diaspora::OStatus do
   before do
     @user = Factory.create(:user) 
-    Diaspora::XML::OWNER = @user
+    Diaspora::OStatus::OWNER = @user
   end
 
-  describe Diaspora::XML::Generate do
+  describe Diaspora::OStatus::Generate do
 
     describe "header" do
       it 'should generate an OStatus compliant header' do
-        Diaspora::XML::Generate::headers(:current_url => @user.url).should include @user.url
+        Diaspora::OStatus::Generate::headers(:current_url => @user.url).should include @user.url
       end
     end
 
@@ -20,7 +22,7 @@ describe Diaspora::XML do
       end
 
       it "should encode to activity stream xml" do
-        sm_entry = Diaspora::XML::generate(:objects => @status_message, :current_url => "http://diaspora.com/")
+        sm_entry = Diaspora::OStatus::generate(:objects => @status_message, :current_url => "http://diaspora.com/")
         sm_entry.should include(@status_message.message)
         sm_entry.should include('title')
       end
diff --git a/spec/lib/parser_spec.rb b/spec/lib/diaspora_parser_spec.rb
similarity index 69%
rename from spec/lib/parser_spec.rb
rename to spec/lib/diaspora_parser_spec.rb
index be2d4c2c21..b01f6f247a 100644
--- a/spec/lib/parser_spec.rb
+++ b/spec/lib/diaspora_parser_spec.rb
@@ -1,9 +1,8 @@
 require File.dirname(__FILE__) + '/../spec_helper'
 
 include ApplicationHelper 
-include Diaspora::OStatusParser
 
-describe "parser in application helper" do
+describe Diaspora::DiasporaParser do
   before do
     @user = Factory.create(:user, :email => "bob@aol.com")
     @person = Factory.create(:person, :email => "bill@gates.com")
@@ -60,77 +59,6 @@ describe "parser in application helper" do
     Post.count.should == 0
   end
 
-  describe 'ostatus parsing' do
-    it 'should be able to get the hub of an ostatus feed' do
-      xml_path = File.dirname(__FILE__) + '/../fixtures/identica_feed.atom'
-      xml = File.open(xml_path).read
-
-      Diaspora::OStatusParser::find_hub(xml).should == 'http://identi.ca/main/push/hub'
-    end
-
-
-    describe 'subscriber info' do
-      before do
-        #load file
-        xml_path = File.dirname(__FILE__) + '/../fixtures/ostatus_update.xml'
-        @xml = File.open(xml_path).read
-        @xml = Nokogiri::HTML(@xml)
-      end
-
-
-      it 'should parse the users service' do
-        Diaspora::OStatusParser::parse_service(@xml).should == 'StatusNet'
-      end
-
-      it 'should parse the feed_url' do
-        Diaspora::OStatusParser::parse_feed_url(@xml).should == 'http://identi.ca/api/statuses/user_timeline/217769.atom'
-      end
-  
-      it 'should parse the avatar thumbnail' do 
-        Diaspora::OStatusParser::parse_avatar_thumbnail(@xml).should == 'http://theme.status.net/0.9.3/identica/default-avatar-profile.png'
-      end
-      
-      it 'should parse the username' do
-        Diaspora::OStatusParser::parse_username(@xml).should == 'danielgrippi'
-      end
-
-      it 'should parse the profile_url' do
-        Diaspora::OStatusParser::parse_profile_url(@xml).should == 'http://identi.ca/user/217769'
-      end
-
-    end
-
-    describe 'entry' do
-      before do
-        #load file
-        xml_path = File.dirname(__FILE__) + '/../fixtures/ostatus_update.xml'
-        @xml = File.open(xml_path).read
-        @xml = Nokogiri::HTML(@xml)
-      end
-
-      it 'should parse the message' do
-        Diaspora::OStatusParser::parse_message(@xml).should == 'SOAP!'
-      end
-
-      it 'should parse the permalink' do
-        Diaspora::OStatusParser::parse_permalink(@xml).should == 'http://identi.ca/notice/43074747'
-      end
-
-      it 'should parse published at date' do
-        Diaspora::OStatusParser::parse_published_at(@xml).should == '2010-07-22T22:15:31+00:00'
-
-      end
-
-      it 'should parse the updated at date' do
-        Diaspora::OStatusParser::parse_updated_at(@xml).should == '2010-07-22T22:15:31+00:00'
-      end
-    end
-
-
-
-    
-
-  end
 
   describe "parsing compliant XML object" do 
     before do
@@ -224,7 +152,6 @@ describe "parser in application helper" do
       Person.count.should == 1
 
     end
-
   end
 end
 
diff --git a/spec/lib/ostatus_parser_spec.rb b/spec/lib/ostatus_parser_spec.rb
new file mode 100644
index 0000000000..4bde6b19e7
--- /dev/null
+++ b/spec/lib/ostatus_parser_spec.rb
@@ -0,0 +1,68 @@
+require File.dirname(__FILE__) + '/../spec_helper'
+
+describe Diaspora::OStatusParser do
+  it 'should be able to get the hub of an ostatus feed' do
+    xml_path = File.dirname(__FILE__) + '/../fixtures/identica_feed.atom'
+    xml = File.open(xml_path).read
+
+    Diaspora::OStatusParser::find_hub(xml).should == 'http://identi.ca/main/push/hub'
+  end
+
+
+  describe 'subscriber info' do
+    before do
+      #load file
+      xml_path = File.dirname(__FILE__) + '/../fixtures/ostatus_update.xml'
+      @xml = File.open(xml_path).read
+      @xml = Nokogiri::HTML(@xml)
+    end
+
+
+    it 'should parse the users service' do
+      Diaspora::OStatusParser::parse_service(@xml).should == 'StatusNet'
+    end
+
+    it 'should parse the feed_url' do
+      Diaspora::OStatusParser::parse_feed_url(@xml).should == 'http://identi.ca/api/statuses/user_timeline/217769.atom'
+    end
+
+    it 'should parse the avatar thumbnail' do 
+      Diaspora::OStatusParser::parse_avatar_thumbnail(@xml).should == 'http://theme.status.net/0.9.3/identica/default-avatar-profile.png'
+    end
+    
+    it 'should parse the username' do
+      Diaspora::OStatusParser::parse_username(@xml).should == 'danielgrippi'
+    end
+
+    it 'should parse the profile_url' do
+      Diaspora::OStatusParser::parse_profile_url(@xml).should == 'http://identi.ca/user/217769'
+    end
+
+  end
+
+  describe 'entry' do
+    before do
+      #load file
+      xml_path = File.dirname(__FILE__) + '/../fixtures/ostatus_update.xml'
+      @xml = File.open(xml_path).read
+      @xml = Nokogiri::HTML(@xml)
+    end
+
+    it 'should parse the message' do
+      Diaspora::OStatusParser::parse_message(@xml).should == 'SOAP!'
+    end
+
+    it 'should parse the permalink' do
+      Diaspora::OStatusParser::parse_permalink(@xml).should == 'http://identi.ca/notice/43074747'
+    end
+
+    it 'should parse published at date' do
+      Diaspora::OStatusParser::parse_published_at(@xml).should == '2010-07-22T22:15:31+00:00'
+
+    end
+
+    it 'should parse the updated at date' do
+      Diaspora::OStatusParser::parse_updated_at(@xml).should == '2010-07-22T22:15:31+00:00'
+    end
+  end
+end
\ No newline at end of file
diff --git a/spec/lib/common_spec.rb b/spec/lib/web_hooks_spec.rb
similarity index 100%
rename from spec/lib/common_spec.rb
rename to spec/lib/web_hooks_spec.rb
diff --git a/spec/lib/web_socket_spec.rb b/spec/lib/web_socket_spec.rb
deleted file mode 100644
index 4613dc98ab..0000000000
--- a/spec/lib/web_socket_spec.rb
+++ /dev/null
@@ -1,5 +0,0 @@
-require File.dirname(__FILE__) + '/../spec_helper'
-
-
-
-
-- 
GitLab