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