Merge branch 'friend-refactor' of github.com:diaspora/diaspora_rails into friend-refactor

This commit is contained in:
ilya 2010-08-11 12:29:35 -07:00
commit 29b1217fcd
13 changed files with 113 additions and 101 deletions

View file

@ -24,7 +24,6 @@ class BlogsController < ApplicationController
if @blog.created_at
flash[:notice] = "Successfully created blog."
redirect_to @blog
else
render :action => 'new'
end

View file

@ -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
@ -34,7 +29,6 @@ class BookmarksController < ApplicationController
if @bookmark.created_at
flash[:notice] = "Successfully created bookmark."
redirect_to @bookmark
else
render :action => 'new'
end

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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
<entry>
@ -27,12 +25,18 @@ class Bookmark < Post
</entry>
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

View file

@ -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

View file

@ -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

View file

@ -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))

View file

@ -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

View file

@ -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

56
lib/diaspora/websocket.rb Normal file
View file

@ -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

View file

@ -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