diff --git a/app/controllers/photos_controller.rb b/app/controllers/photos_controller.rb index 46332992d..aa99cbc82 100644 --- a/app/controllers/photos_controller.rb +++ b/app/controllers/photos_controller.rb @@ -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 diff --git a/app/helpers/sockets_helper.rb b/app/helpers/sockets_helper.rb index 3ac42f740..2f66919f8 100644 --- a/app/helpers/sockets_helper.rb +++ b/app/helpers/sockets_helper.rb @@ -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}.") diff --git a/app/models/photo.rb b/app/models/photo.rb index bedcdc143..5fcb0ea70 100644 --- a/app/models/photo.rb +++ b/app/models/photo.rb @@ -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 diff --git a/app/models/post.rb b/app/models/post.rb index 0ab355968..661fce4dc 100644 --- a/app/models/post.rb +++ b/app/models/post.rb @@ -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} diff --git a/app/models/user.rb b/app/models/user.rb index 5032ef273..340d07432 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -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 diff --git a/app/views/albums/_new_album.haml b/app/views/albums/_new_album.haml index a52f0c9ec..75438989b 100644 --- a/app/views/albums/_new_album.haml +++ b/app/views/albums/_new_album.haml @@ -5,4 +5,5 @@ %p = f.label :name = f.text_field :name + = f.hidden_field :to, :value => :all = f.submit 'create', :class => 'button' diff --git a/app/views/albums/show.html.haml b/app/views/albums/show.html.haml index c0cf97427..f67fb5d33 100644 --- a/app/views/albums/show.html.haml +++ b/app/views/albums/show.html.haml @@ -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 diff --git a/app/views/js/_websocket_js.haml b/app/views/js/_websocket_js.haml index 5305547cb..e70fa1840 100644 --- a/app/views/js/_websocket_js.haml +++ b/app/views/js/_websocket_js.haml @@ -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){ diff --git a/app/views/photos/_photo.haml b/app/views/photos/_photo.haml index 7523a29ba..caeae5c99 100644 --- a/app/views/photos/_photo.haml +++ b/app/views/photos/_photo.haml @@ -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) diff --git a/spec/controllers/sockets_controller_spec.rb b/spec/controllers/sockets_controller_spec.rb index b12cf7ad4..23a02d61e 100644 --- a/spec/controllers/sockets_controller_spec.rb +++ b/spec/controllers/sockets_controller_spec.rb @@ -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 diff --git a/spec/models/status_message_spec.rb b/spec/models/status_message_spec.rb index 948410250..08c66dfdc 100644 --- a/spec/models/status_message_spec.rb +++ b/spec/models/status_message_spec.rb @@ -30,5 +30,7 @@ describe StatusMessage do parsed.valid?.should be_true end end + + end