fixed photos going through the websocket. added a spec to SocketsController
This commit is contained in:
parent
be6dc1d443
commit
55c94c385c
11 changed files with 73 additions and 54 deletions
|
|
@ -2,21 +2,20 @@ class PhotosController < ApplicationController
|
|||
before_filter :authenticate_user!
|
||||
|
||||
def create
|
||||
render :nothing => true
|
||||
begin
|
||||
@photo = current_user.post(:photo, params)
|
||||
|
||||
if @photo.created_at
|
||||
flash[:notice] = "Successfully uploaded photo."
|
||||
else
|
||||
render :action => 'album#new'
|
||||
end
|
||||
render :nothing => true if @photo.created_at
|
||||
|
||||
rescue TypeError
|
||||
flash[:error] = "Photo upload failed. Are you sure an image was added?"
|
||||
redirect_to Album.first(:id => params[:photo][:album_id])
|
||||
rescue CarrierWave::IntegrityError ||
|
||||
redirect_to Album.first(:id => params[:album_id])
|
||||
rescue CarrierWave::IntegrityError
|
||||
flash[:error] = "Photo upload failed. Are you sure that was an image?"
|
||||
redirect_to Album.first(:id => params[:photo][:album_id])
|
||||
redirect_to Album.first(:id => params[:album_id])
|
||||
rescue RuntimeError => e
|
||||
flash[:error] = "Photo upload failed. Are you sure that your seatbelt is fastened?"
|
||||
redirect_to Album.first(:id => params[:album_id])
|
||||
raise e
|
||||
end
|
||||
end
|
||||
|
||||
|
|
|
|||
|
|
@ -7,7 +7,7 @@ module SocketsHelper
|
|||
|
||||
def action_hash(uid, object, opts={})
|
||||
begin
|
||||
user = User.find_by_id(uid)
|
||||
user = User.find_by_id(User.ensure_bson uid)
|
||||
v = render_to_string(:partial => type_partial(object), :locals => {:post => object, :current_user => user}) unless object.is_a? Retraction
|
||||
rescue Exception => e
|
||||
Rails.logger.error("web socket view rendering failed for object #{object.inspect}.")
|
||||
|
|
|
|||
|
|
@ -26,7 +26,6 @@ class Photo < Post
|
|||
photo
|
||||
end
|
||||
|
||||
after_save :log_save_inspection
|
||||
validates_true_for :album_id, :logic => lambda {self.validate_album_person}
|
||||
|
||||
before_destroy :ensure_user_picture
|
||||
|
|
|
|||
|
|
@ -46,14 +46,6 @@ class Post
|
|||
(self.send accessor.to_sym).to_s}.join ';'
|
||||
end
|
||||
|
||||
def log_inspection
|
||||
Rails.logger.debug self.inspect
|
||||
end
|
||||
def log_save_inspection
|
||||
Rails.logger.debug "After saving, object is:"
|
||||
log_inspection
|
||||
end
|
||||
|
||||
protected
|
||||
def destroy_comments
|
||||
comments.each{|c| c.destroy}
|
||||
|
|
|
|||
|
|
@ -44,23 +44,39 @@ class User
|
|||
def post(class_name, options = {})
|
||||
options[:person] = self.person
|
||||
|
||||
group_ids = options[:to]
|
||||
if class_name == :photo
|
||||
raise "No album_id given" unless options[:album_id]
|
||||
group_ids = groups_with_post( options[:album_id] )
|
||||
group_ids.map!{ |group| group.id }
|
||||
else
|
||||
group_ids = options.delete(:to)
|
||||
end
|
||||
|
||||
group_ids = [group_ids] if group_ids.is_a? BSON::ObjectID
|
||||
raise "You must post to someone." if group_ids.nil? || group_ids.empty?
|
||||
|
||||
|
||||
group_ids.map!{|gid| ensure_bson gid }
|
||||
options.delete(:to)
|
||||
|
||||
model_class = class_name.to_s.camelize.constantize
|
||||
|
||||
post = model_class.instantiate(options)
|
||||
post.creator_signature = post.sign_with_key(encryption_key)
|
||||
post.save
|
||||
|
||||
post.socket_to_uid(id, :group_ids => group_ids) if post.respond_to?(:socket_to_uid)
|
||||
|
||||
groups = self.groups.find_all_by_id( group_ids )
|
||||
push_to_groups(post, group_ids)
|
||||
|
||||
self.raw_visible_posts << post
|
||||
self.save
|
||||
post
|
||||
end
|
||||
|
||||
def push_to_groups( post, group_ids )
|
||||
if group_ids == :all || group_ids == "all"
|
||||
groups = self.groups
|
||||
else
|
||||
group_ids.map!{|gid| User.ensure_bson gid }
|
||||
groups = self.groups.find_all_by_id( group_ids )
|
||||
end
|
||||
#send to the groups
|
||||
target_people = []
|
||||
|
||||
groups.each{ |group|
|
||||
|
|
@ -68,15 +84,9 @@ class User
|
|||
group.save
|
||||
target_people = target_people | group.people
|
||||
}
|
||||
|
||||
post.socket_to_uid(id, :group_ids => group_ids) if post.respond_to?(:socket_to_uid)
|
||||
post.push_to( target_people )
|
||||
|
||||
self.raw_visible_posts << post
|
||||
self.save
|
||||
post
|
||||
end
|
||||
|
||||
|
||||
def visible_posts( opts = {} )
|
||||
if opts[:by_members_of]
|
||||
return raw_visible_posts if opts[:by_members_of] == :all
|
||||
|
|
@ -147,7 +157,7 @@ class User
|
|||
else
|
||||
object.perform self.id
|
||||
groups = self.groups_with_person(object.person)
|
||||
groups.each{ |group| group.post_ids.delete(ensure_bson(object.post_id))
|
||||
groups.each{ |group| group.post_ids.delete(User.ensure_bson(object.post_id))
|
||||
group.save
|
||||
}
|
||||
end
|
||||
|
|
@ -205,23 +215,28 @@ class User
|
|||
end
|
||||
|
||||
def visible_person_by_id( id )
|
||||
id = ensure_bson id
|
||||
id = User.ensure_bson id
|
||||
return self.person if id == self.person.id
|
||||
friends.detect{|x| x.id == id }
|
||||
end
|
||||
|
||||
def group_by_id( id )
|
||||
id = ensure_bson id
|
||||
id = User.ensure_bson id
|
||||
groups.detect{|x| x.id == id }
|
||||
end
|
||||
|
||||
def album_by_id( id )
|
||||
id = ensure_bson id
|
||||
id = User.ensure_bson id
|
||||
albums.detect{|x| x.id == id }
|
||||
end
|
||||
|
||||
def groups_with_post( id )
|
||||
id = User.ensure_bson id
|
||||
self.groups.find_all_by_post_ids( id )
|
||||
end
|
||||
|
||||
def groups_with_person person
|
||||
id = ensure_bson person.id
|
||||
id = User.ensure_bson person.id
|
||||
groups.select {|group| group.person_ids.include? id}
|
||||
end
|
||||
|
||||
|
|
@ -243,7 +258,7 @@ class User
|
|||
OpenSSL::PKey::RSA::generate 1024
|
||||
end
|
||||
|
||||
def ensure_bson id
|
||||
def self.ensure_bson id
|
||||
id.class == String ? BSON::ObjectID(id) : id
|
||||
end
|
||||
end
|
||||
|
|
|
|||
|
|
@ -5,4 +5,5 @@
|
|||
%p
|
||||
= f.label :name
|
||||
= f.text_field :name
|
||||
= f.hidden_field :to, :value => :all
|
||||
= f.submit 'create', :class => 'button'
|
||||
|
|
|
|||
|
|
@ -1,7 +1,8 @@
|
|||
:javascript
|
||||
$(document).ready(function(){
|
||||
reset_photo_fancybox();
|
||||
});
|
||||
- content_for :head do
|
||||
:javascript
|
||||
$(document).ready(function(){
|
||||
reset_photo_fancybox();
|
||||
});
|
||||
|
||||
.album_id{:id => @album.id, :style => "display:hidden;"}
|
||||
.back= link_to '⇧ albums', albums_path
|
||||
|
|
|
|||
|
|
@ -21,6 +21,7 @@
|
|||
}else if (obj['class']=='photos' && onPageForClass('albums')){
|
||||
processPhotoInAlbum(obj['photo_hash'])
|
||||
}else{
|
||||
alert("hey");
|
||||
processPost(obj['class'], obj['html'], obj['group_ids'])
|
||||
}
|
||||
|
||||
|
|
@ -75,7 +76,7 @@
|
|||
}
|
||||
|
||||
function onPageForClass(className){
|
||||
return ((location.href.indexOf(className) != -1 ) || (location.pathname == '/')) && onPageOne();
|
||||
return (location.href.indexOf(className) != -1 );
|
||||
}
|
||||
|
||||
function onPageForGroups(groupIds){
|
||||
|
|
|
|||
|
|
@ -9,7 +9,7 @@
|
|||
|
||||
%br
|
||||
|
||||
= render "albums/album", :post => post.album
|
||||
= render "albums/album", :post => post.album, :current_user => current_user
|
||||
|
||||
= link_to (image_tag post.url(:thumb_medium)), object_path(post)
|
||||
|
||||
|
|
|
|||
|
|
@ -1,5 +1,11 @@
|
|||
require File.dirname(__FILE__) + '/../spec_helper'
|
||||
|
||||
class SocketsController
|
||||
def url_options
|
||||
{:host => ""}
|
||||
end
|
||||
end
|
||||
|
||||
describe 'SocketsController' do
|
||||
render_views
|
||||
before do
|
||||
|
|
@ -12,22 +18,25 @@ describe 'SocketsController' do
|
|||
end
|
||||
|
||||
it 'should unstub the websockets' do
|
||||
WebSocket.initialize_channels
|
||||
Diaspora::WebSocket.initialize_channels
|
||||
@controller.class.should == SocketsController
|
||||
end
|
||||
|
||||
describe 'actionhash' do
|
||||
before do
|
||||
@message = @user.post :status_message, :message => "post through user for victory", :to => @user.group(:name => "losers").id
|
||||
@group = @user.group :name => "losers"
|
||||
@message = @user.post :status_message, :message => "post through user for victory", :to => @group.id
|
||||
@fixture_name = File.dirname(__FILE__) + '/../fixtures/button.png'
|
||||
end
|
||||
|
||||
it 'should actionhash photos' do
|
||||
@album = @user.post(:album, :name => "Loser faces", :to => @group.id)
|
||||
photo = @user.post(:photo, :album_id => @album.id, :user_file => [File.open(@fixture_name)])
|
||||
json = @controller.action_hash(@user.id, photo, :group_ids => @user.groups_with_post(@album.id).map{|g| g.id})
|
||||
json.include?('photo').should be_true
|
||||
end
|
||||
|
||||
it 'should actionhash posts' do
|
||||
class SocketsController
|
||||
def url_options
|
||||
{:host => ""}
|
||||
end
|
||||
end
|
||||
|
||||
json = @controller.action_hash(@user.id, @message)
|
||||
json.include?(@message.message).should be_true
|
||||
json.include?('status_message').should be_true
|
||||
|
|
|
|||
|
|
@ -30,5 +30,7 @@ describe StatusMessage do
|
|||
parsed.valid?.should be_true
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
end
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue