From 683bb0dbef3ff9491484995c1a88fad5f25f5d7e Mon Sep 17 00:00:00 2001 From: Raphael Date: Wed, 11 Aug 2010 10:52:01 -0700 Subject: [PATCH 1/3] Took out create redirects on ajaxy things, authenticating statusmessages --- app/controllers/blogs_controller.rb | 1 - app/controllers/bookmarks_controller.rb | 1 - app/controllers/photos_controller.rb | 1 - app/controllers/status_messages_controller.rb | 3 +-- lib/diaspora/webhooks.rb | 5 +++-- 5 files changed, 4 insertions(+), 7 deletions(-) diff --git a/app/controllers/blogs_controller.rb b/app/controllers/blogs_controller.rb index a74b12e43..4075e04ed 100644 --- a/app/controllers/blogs_controller.rb +++ b/app/controllers/blogs_controller.rb @@ -24,7 +24,6 @@ class BlogsController < ApplicationController if @blog.created_at flash[:notice] = "Successfully created blog." - redirect_to @blog else render :action => 'new' end diff --git a/app/controllers/bookmarks_controller.rb b/app/controllers/bookmarks_controller.rb index 79bf76d12..760a3255c 100644 --- a/app/controllers/bookmarks_controller.rb +++ b/app/controllers/bookmarks_controller.rb @@ -34,7 +34,6 @@ class BookmarksController < ApplicationController if @bookmark.created_at flash[:notice] = "Successfully created bookmark." - redirect_to @bookmark else render :action => 'new' end diff --git a/app/controllers/photos_controller.rb b/app/controllers/photos_controller.rb index 995690213..2996a23ec 100644 --- a/app/controllers/photos_controller.rb +++ b/app/controllers/photos_controller.rb @@ -7,7 +7,6 @@ class PhotosController < ApplicationController if @photo.created_at flash[:notice] = "Successfully uploaded photo." - redirect_to @photo.album else render :action => 'album#new' end diff --git a/app/controllers/status_messages_controller.rb b/app/controllers/status_messages_controller.rb index f499492e8..8cc73bac1 100644 --- a/app/controllers/status_messages_controller.rb +++ b/app/controllers/status_messages_controller.rb @@ -1,5 +1,5 @@ class StatusMessagesController < ApplicationController - #before_filter :authenticate_user! + before_filter :authenticate_user! def index @status_messages = StatusMessage.paginate :page => params[:page], :order => 'created_at DESC' @@ -16,7 +16,6 @@ class StatusMessagesController < ApplicationController if @status_message.created_at flash[:notice] = "Successfully created status message." - redirect_to status_messages_url else render :action => 'new' end diff --git a/lib/diaspora/webhooks.rb b/lib/diaspora/webhooks.rb index ba78ed81a..959010e61 100644 --- a/lib/diaspora/webhooks.rb +++ b/lib/diaspora/webhooks.rb @@ -40,10 +40,11 @@ module Diaspora def people_with_permissions begin - friends = self.person.owner.friends + friends = self.person.owner.friends.all + Rails.logger.error("Dan is wrong!") if friends.nil? friends ||= [] rescue - Rails.logger.fatal("IOUASDVJOISDNVPOIJSDVOUIDSGPUOID") + Rails.logger.fatal("Called people_with_permissions on a post from a remote user. We need to implement this shit.") [] end end From 144e9ed439653eaf58365c291f6a28be96b49794 Mon Sep 17 00:00:00 2001 From: Raphael Date: Wed, 11 Aug 2010 11:18:40 -0700 Subject: [PATCH 2/3] Bookmarks now clean link in instantiate, posting them should work right again --- app/controllers/bookmarks_controller.rb | 5 -- app/models/bookmark.rb | 16 ++++--- spec/models/bookmark_spec.rb | 62 ++++++++++++++----------- 3 files changed, 44 insertions(+), 39 deletions(-) diff --git a/app/controllers/bookmarks_controller.rb b/app/controllers/bookmarks_controller.rb index 760a3255c..5d9959732 100644 --- a/app/controllers/bookmarks_controller.rb +++ b/app/controllers/bookmarks_controller.rb @@ -4,11 +4,6 @@ class BookmarksController < ApplicationController def index @bookmark = Bookmark.new @bookmarks = Bookmark.paginate :page => params[:page], :order => 'created_at DESC' - - - respond_to do |format| - format.html - end end def edit diff --git a/app/models/bookmark.rb b/app/models/bookmark.rb index 8f793fd47..de1eb9258 100644 --- a/app/models/bookmark.rb +++ b/app/models/bookmark.rb @@ -12,8 +12,6 @@ class Bookmark < Post validates_format_of :link, :with => /^(http|https):\/\/[a-z0-9]+([\-\.]{1}[a-z0-9]+)*\.[a-z]{2,5}(([0-9]{1,5})?\/.*)?$/ix - before_validation :clean_link - def to_activity <<-XML @@ -27,12 +25,18 @@ class Bookmark < Post XML end + + def self.instantiate params + params[:link] = clean_link(params[:link]) + create params + end protected - def clean_link - if self.link - self.link = 'http://' + self.link unless self.link.match('https?://') - self.link = self.link + '/' if self.link[-1,1] != '/' + def self.clean_link link + if link + link = 'http://' + link unless link.match('https?://') + link = link + '/' if link[-1,1] != '/' + link end end end diff --git a/spec/models/bookmark_spec.rb b/spec/models/bookmark_spec.rb index f583e0844..ae4567bc1 100644 --- a/spec/models/bookmark_spec.rb +++ b/spec/models/bookmark_spec.rb @@ -10,34 +10,6 @@ describe Bookmark do it 'should validate its link' do bookmark = Factory.build(:bookmark) - - #links changed valid - bookmark.link = "google.com" - bookmark.valid?.should == true - bookmark.link.should == "http://google.com/" - - bookmark.link = "www.google.com" - bookmark.valid?.should == true - bookmark.link.should == "http://www.google.com/" - - bookmark.link = "google.com/" - bookmark.valid?.should == true - bookmark.link.should == "http://google.com/" - - bookmark.link = "www.google.com/" - bookmark.valid?.should == true - bookmark.link.should == "http://www.google.com/" - - bookmark.link = "http://google.com" - bookmark.valid?.should == true - bookmark.link.should == "http://google.com/" - - bookmark.link = "http://www.google.com" - bookmark.valid?.should == true - - #bookmark.link = "http://babycakes.sofaer.net:3000" - #bookmark.valid?.should == true - #invalid links bookmark.link = "zsdvzxdg" bookmark.valid?.should == false @@ -54,6 +26,24 @@ describe Bookmark do bookmark.link = "http:///www.asodij.com/" bookmark.valid?.should == false end + + it 'should clean links' do + bad_links = [ + "google.com", + "www.google.com", + "google.com/", + "www.google.com/", + "http://google.com", + "http://www.google.com" + ] + + bad_links.each{ |link| + Bookmark.clean_link(link).should satisfy{ |link| + /^(http|https):\/\/[a-z0-9]+([\-\.]{1}[a-z0-9]+)*\.[a-z]{2,5}(([0-9]{1,5})?\/.*)?$/ix.match(link) + } + } + + end describe "XML" do it 'should serialize to XML' do @@ -71,4 +61,20 @@ describe Bookmark do parsed.valid?.should be_true end end + + describe 'with encryption' do + before do + unstub_mocha_stubs + @user = Factory.create(:user) + end + + after do + stub_signature_verification + end + + it 'should save a signed bookmark' do + bookmark = @user.post(:bookmark, :title => "I love cryptography", :link => "http://pgp.mit.edu/") + bookmark.created_at.should_not be nil + end + end end From 599d1da6207f4ce96060f894a82fc78bdd8dd072 Mon Sep 17 00:00:00 2001 From: Raphael Date: Wed, 11 Aug 2010 12:03:26 -0700 Subject: [PATCH 3/3] Moved socket code out to lib, unsocketing from view should now work again --- app/controllers/sockets_controller.rb | 2 +- app/models/comment.rb | 8 +-- app/models/post.rb | 12 +--- .../status_messages/_status_message.html.haml | 2 +- config/initializers/socket.rb | 40 ++----------- lib/diaspora/websocket.rb | 56 +++++++++++++++++++ 6 files changed, 65 insertions(+), 55 deletions(-) create mode 100644 lib/diaspora/websocket.rb diff --git a/app/controllers/sockets_controller.rb b/app/controllers/sockets_controller.rb index eb4f57fc9..802aa9cb5 100644 --- a/app/controllers/sockets_controller.rb +++ b/app/controllers/sockets_controller.rb @@ -9,7 +9,7 @@ class SocketsController < ApplicationController def outgoing(uid,object) @_request = ActionDispatch::Request.new({}) - WebSocket.push_to_user(uid, action_hash(uid, object)) + Diaspora::WebSocket.push_to_user(uid, action_hash(uid, object)) end end diff --git a/app/models/comment.rb b/app/models/comment.rb index 79adbb49e..8c66ac62a 100644 --- a/app/models/comment.rb +++ b/app/models/comment.rb @@ -3,6 +3,7 @@ class Comment include ROXML include Diaspora::Webhooks include Encryptable + include Diaspora::Socketable xml_accessor :text xml_accessor :person, :as => Person @@ -75,11 +76,4 @@ class Comment end end - def send_to_view - people_with_permissions.each{|f| - SocketsController.new.outgoing(f.owner_id, self) if f.owner_id - } - SocketsController.new.outgoing(person.owner_id, self) if person.owner_id - end - end diff --git a/app/models/post.rb b/app/models/post.rb index 0768a91c8..61fe5821a 100644 --- a/app/models/post.rb +++ b/app/models/post.rb @@ -5,6 +5,7 @@ class Post include ROXML include Diaspora::Webhooks include Encryptable + include Diaspora::Socketable xml_accessor :_id xml_accessor :person, :as => Person @@ -75,16 +76,7 @@ protected Retraction.for(self).notify_people end - def send_to_view - people_with_permissions.each{|f| - SocketsController.new.outgoing(f.owner_id, self) if f.owner_id - } - SocketsController.new.outgoing(person.owner_id, self) if person.owner_id - end - - def remove_from_view - SocketsController.new.outgoing(Retraction.for(self)) - end + end diff --git a/app/views/status_messages/_status_message.html.haml b/app/views/status_messages/_status_message.html.haml index ac0f55c34..c6099504e 100644 --- a/app/views/status_messages/_status_message.html.haml +++ b/app/views/status_messages/_status_message.html.haml @@ -4,7 +4,7 @@ %span.from = link_to post.person.real_name, post.person - = auto_link post.message + = auto_link sanitize post.message %div.time = link_to(how_long_ago(post), object_path(post)) diff --git a/config/initializers/socket.rb b/config/initializers/socket.rb index 2fec8a328..0f4e68f7b 100644 --- a/config/initializers/socket.rb +++ b/config/initializers/socket.rb @@ -1,9 +1,8 @@ require 'em-websocket' require 'eventmachine' - -module WebSocket +require "lib/diaspora/websocket" EM.next_tick { - initialize_channels + Diaspora::WebSocket.initialize_channels EventMachine::WebSocket.start( :host => "0.0.0.0", @@ -11,43 +10,12 @@ module WebSocket :debug =>APP_CONFIG[:debug]) do |ws| ws.onopen { - sid = self.subscribe(ws.request['Path'].gsub('/',''), ws) + sid = Diaspora::WebSocket.subscribe(ws.request['Path'].gsub('/',''), ws) ws.onmessage { |msg| SocketsController.new.incoming(msg) }#@channel.push msg; puts msg} - ws.onclose { unsubscribe(ws.request['Path'].gsub('/',''), sid) } + ws.onclose { Diaspora::WebSocket.unsubscribe(ws.request['Path'].gsub('/',''), sid) } } end } - def self.initialize_channels - @channels = {} - end - - def self.push_to_user(uid, data) - Rails.logger.debug "Websocketing to #{uid}" - @channels[uid.to_s][0].push(data) if @channels[uid.to_s] - end - - def self.subscribe(uid, ws) - Rails.logger.debug "Subscribing socket to #{User.first(:id => uid).email}" - self.ensure_channel(uid) - @channels[uid][0].subscribe{ |msg| ws.send msg } - @channels[uid][1] += 1 - end - - def self.ensure_channel(uid) - @channels[uid] ||= [EM::Channel.new, 0 ] - end - - def self.unsubscribe(uid,sid) - Rails.logger.debug "Unsubscribing socket #{sid} from #{User.first(:id => uid).email}" - @channels[uid][0].unsubscribe(sid) if @channels[uid] - @channels[uid][1] -= 1 - if @channels[uid][1] <= 0 - @channels.delete(uid) - end - end - -end - diff --git a/lib/diaspora/websocket.rb b/lib/diaspora/websocket.rb new file mode 100644 index 000000000..4ff1fff1a --- /dev/null +++ b/lib/diaspora/websocket.rb @@ -0,0 +1,56 @@ +module Diaspora + module WebSocket + def self.initialize_channels + @channels = {} + end + + def self.push_to_user(uid, data) + Rails.logger.debug "Websocketing to #{uid}" + @channels[uid.to_s][0].push(data) if @channels[uid.to_s] + end + + def self.subscribe(uid, ws) + Rails.logger.debug "Subscribing socket to #{User.first(:id => uid).email}" + self.ensure_channel(uid) + @channels[uid][0].subscribe{ |msg| ws.send msg } + @channels[uid][1] += 1 + end + + def self.ensure_channel(uid) + @channels[uid] ||= [EM::Channel.new, 0 ] + end + + def self.unsubscribe(uid,sid) + Rails.logger.debug "Unsubscribing socket #{sid} from #{User.first(:id => uid).email}" + @channels[uid][0].unsubscribe(sid) if @channels[uid] + @channels[uid][1] -= 1 + if @channels[uid][1] <= 0 + @channels.delete(uid) + end + end + end + + module Socketable + def socket_to_uid id + SocketsController.new.outgoing(id, self) + end + + def unsocket_from_uid id + SocketsController.new.outgoing(id, Retraction.for(self)) + end + + def send_to_view + people_with_permissions.each{|f| + socket_to_uid f.owner_id if f.owner_id + } + socket_to_uid person.owner_id if person.owner_id + end + + def remove_from_view + people_with_permissions.each{|f| + unsocket_from_uid f.owner_id if f.owner_id + } + unsocket_from_uid person.owner_id if person.owner_id + end + end +end