diff --git a/app/controllers/post_visibilities_controller.rb b/app/controllers/post_visibilities_controller.rb index 572c34cba..49488f261 100644 --- a/app/controllers/post_visibilities_controller.rb +++ b/app/controllers/post_visibilities_controller.rb @@ -7,13 +7,19 @@ class PostVisibilitiesController < ApplicationController before_filter :authenticate_user! def destroy - @vis = ConversationVisibility.where(:person_id => current_user.person.id, - :conversation_id => params[:conversation_id]).first + #note :id is garbage + + @post = Post.where(:id => params[:post_id]).select("id, author_id").first + @contact = current_user.contact_for( @post.author) + @vis = PostVisibility.where(:contact_id => @contact.id, + :post_id => params[:post_id]).first if @vis - if @vis.destroy - flash[:notice] = "Conversation successfully removed" + @vis.hidden = true + if @vis.save + render :nothing => true, :status => 200 + return end end - redirect_to conversations_path + render :nothing => true, :status => 403 end end diff --git a/app/models/post_visibility.rb b/app/models/post_visibility.rb index 69122f046..060b16fee 100644 --- a/app/models/post_visibility.rb +++ b/app/models/post_visibility.rb @@ -3,6 +3,8 @@ # the COPYRIGHT file. class PostVisibility < ActiveRecord::Base + default_scope where(:hidden => false) + belongs_to :contact belongs_to :post end diff --git a/app/views/shared/_stream_element.html.haml b/app/views/shared/_stream_element.html.haml index 25d923381..852f6ce72 100644 --- a/app/views/shared/_stream_element.html.haml +++ b/app/views/shared/_stream_element.html.haml @@ -9,6 +9,9 @@ - unless reshare_aspects.empty? = render 'shared/reshare', :aspects => reshare_aspects, :post => post = link_to image_tag('deletelabel.png'), status_message_path(post), :confirm => t('are_you_sure'), :method => :delete, :remote => true, :class => "delete stream_element_delete", :title => t('delete') + - else + .right.controls + = link_to image_tag('deletelabel.png'), post_visibility_path(:id => "42", :post_id => post.id), :confirm => t('are_you_sure'), :method => :delete, :remote => true, :class => "delete stream_element_delete", :title => t('hide') = person_image_link(post.author, :size => :thumb_small) diff --git a/app/views/status_messages/show.mobile.haml b/app/views/status_messages/show.mobile.haml index af12b3098..6299f7a21 100644 --- a/app/views/status_messages/show.mobile.haml +++ b/app/views/status_messages/show.mobile.haml @@ -17,7 +17,9 @@ %br - if current_user.owns? @status_message - = link_to t('.destroy'), @status_message, :confirm => t('are_you_sure'), :method => :delete + = link_to t('delete'), @status_message, :confirm => t('are_you_sure'), :method => :delete + - else + = link_to t('hide'), post_visibility_path(:id => "42", :post_id => post.id), :confirm => t('are_you_sure'), :method => :delete, :remote => true .stream.show{:data=>{:guid=>@status_message.id}} = render "comments/comments", :post => @status_message, :comments => @status_message.comments, :always_expanded => true diff --git a/config/locales/diaspora/en.yml b/config/locales/diaspora/en.yml index cb9bf792c..08c719ed8 100644 --- a/config/locales/diaspora/en.yml +++ b/config/locales/diaspora/en.yml @@ -14,6 +14,7 @@ en: ok: "OK" cancel: "Cancel" delete: "Delete" + hide: "Hide" or: "or" ago: "%{time} ago" username: "Username" @@ -432,6 +433,10 @@ en: posts: doesnt_exist: "that post does not exist!" + post_visibility: + destroy: + success: "Post successfully hidden" + profiles: edit: your_public_profile: "Your public profile" diff --git a/config/routes.rb b/config/routes.rb index fb86493e8..ad50fafd8 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -72,6 +72,7 @@ Diaspora::Application.routes.draw do resources :contacts, :except => [:index, :update] resources :aspect_memberships, :only => [:destroy, :create, :update] + resources :post_visibilities, :only => [:destroy] resources :people, :except => [:edit, :update] do resources :status_messages diff --git a/db/migrate/20110328175936_add_hidden_to_post_visibilities.rb b/db/migrate/20110328175936_add_hidden_to_post_visibilities.rb index 5ef34a810..f9880522d 100644 --- a/db/migrate/20110328175936_add_hidden_to_post_visibilities.rb +++ b/db/migrate/20110328175936_add_hidden_to_post_visibilities.rb @@ -1,9 +1,11 @@ class AddHiddenToPostVisibilities < ActiveRecord::Migration def self.up - add_column :post_visibilities, :hidden, :boolean, :defalut => false, :null => false + add_column :post_visibilities, :hidden, :boolean, :default => false, :null => false + add_index :post_visibilities, :hidden end def self.down + add_index :post_visibilities, :hidden remove_column :post_visibilities, :hidden end end diff --git a/db/schema.rb b/db/schema.rb index 6195f73fc..244a946df 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -212,15 +212,16 @@ ActiveRecord::Schema.define(:version => 20110330230206) do add_index "people", ["owner_id"], :name => "index_people_on_owner_id", :unique => true create_table "post_visibilities", :force => true do |t| - t.integer "post_id", :null => false + t.integer "post_id", :null => false t.datetime "created_at" t.datetime "updated_at" - t.boolean "hidden", :null => false - t.integer "contact_id", :null => false + t.boolean "hidden", :default => false, :null => false + t.integer "contact_id", :null => false end add_index "post_visibilities", ["contact_id", "post_id"], :name => "index_post_visibilities_on_contact_id_and_post_id", :unique => true add_index "post_visibilities", ["contact_id"], :name => "index_post_visibilities_on_contact_id" + add_index "post_visibilities", ["hidden"], :name => "index_post_visibilities_on_hidden" add_index "post_visibilities", ["post_id"], :name => "index_post_visibilities_on_post_id" create_table "posts", :force => true do |t| diff --git a/features/posts.feature b/features/posts.feature index fdb9d31ba..7edc6c13a 100644 --- a/features/posts.feature +++ b/features/posts.feature @@ -26,7 +26,7 @@ Feature: posting Scenario: hide a post Given I expand the publisher - When I fill in "status_message_fake_text" with "I am eating a yogurt" + When I fill in "status_message_fake_text" with "Here is a post for you to hide" And I press "Share" And I wait for the ajax to finish @@ -36,10 +36,12 @@ Feature: posting And I hover over the post And I preemptively confirm the alert - And I click to hide the first post + And I click to delete the first post And I wait for the ajax to finish + And I go to "bob@bob.bob"'s page + Then I should not see "Here is a post for you to hide" And I follow "All Aspects" - Then I should not see "I am eating a yogurt" + Then I should not see "Here is a post for you to hide" Scenario: delete a post Given I expand the publisher diff --git a/features/step_definitions/custom_web_steps.rb b/features/step_definitions/custom_web_steps.rb index 51212497b..40640a85e 100644 --- a/features/step_definitions/custom_web_steps.rb +++ b/features/step_definitions/custom_web_steps.rb @@ -49,10 +49,6 @@ When /^I click to delete the first post$/ do page.execute_script('$(".stream_element").first().find(".stream_element_delete").click()') end -When /^I click to hide the first post$/ do - page.execute_script('$(".stream_element").first().find(".stream_element_hide").click()') -end - When /^I click to delete the first comment$/ do page.execute_script('$(".comment.posted").first().find(".comment_delete").click()') end diff --git a/lib/diaspora/user/querying.rb b/lib/diaspora/user/querying.rb index 97f7b32be..edd0b25cb 100644 --- a/lib/diaspora/user/querying.rb +++ b/lib/diaspora/user/querying.rb @@ -16,10 +16,11 @@ module Diaspora opts[:type] ||= ['StatusMessage', 'Photo'] opts[:limit] ||= 20 opts[:order] ||= 'updated_at DESC' + opts[:hidden] ||= false opts[:order] = '`posts`.' + opts[:order] opts[:limit] = opts[:limit].to_i * opts[:page].to_i if opts[:page] - posts_from_others = Post.joins(:contacts).where(:contacts => {:user_id => self.id}) + posts_from_others = Post.joins(:contacts).where( :post_visibilities => {:hidden => opts[:hidden]}, :contacts => {:user_id => self.id}) posts_from_self = self.person.posts.joins(:aspect_visibilities => :aspect).where(:aspects => {:user_id => self.id}) if opts[:by_members_of] diff --git a/public/javascripts/stream.js b/public/javascripts/stream.js index 8f981ca97..7caa92b6a 100644 --- a/public/javascripts/stream.js +++ b/public/javascripts/stream.js @@ -8,6 +8,8 @@ var Stream = { var $stream = $(".stream"); var $publisher = $("#publisher"); + $(".status_message_delete").tipsy({trigger: 'hover', gravity: 'n'}); + Diaspora.widgets.timeago.updateTimeAgo(); $stream.not(".show").delegate("a.show_post_comments", "click", Stream.toggleComments); //audio linx diff --git a/spec/controllers/aspects_controller_spec.rb b/spec/controllers/aspects_controller_spec.rb index cd6abd11d..3826a7ae5 100644 --- a/spec/controllers/aspects_controller_spec.rb +++ b/spec/controllers/aspects_controller_spec.rb @@ -9,6 +9,7 @@ describe AspectsController do render_views before do + @bob = bob @alice = alice @alice.getting_started = false @alice.save @@ -107,6 +108,23 @@ describe AspectsController do @alice.build_comment('lalala', :on => @posts.first ).save end + describe "post visibilities" do + before do + @status = @bob.post(:status_message, :text=> "hello", :to => @bob.aspects.first) + @vis = @status.post_visibilities.first + end + + it "pulls back non hidden posts" do + get :index + assigns[:posts].include?(@status).should be_true + end + it "does not pull back hidden posts" do + @vis.update_attributes( :hidden => true ) + get :index + assigns[:posts].include?(@status).should be_false + end + end + describe "ordering" do it "orders posts by updated_at by default" do get :index diff --git a/spec/controllers/post_visibilities_controller_spec.rb b/spec/controllers/post_visibilities_controller_spec.rb index 0e860bf7b..83987841a 100644 --- a/spec/controllers/post_visibilities_controller_spec.rb +++ b/spec/controllers/post_visibilities_controller_spec.rb @@ -9,28 +9,47 @@ describe PostVisibilitiesController do before do @user1 = alice + @bob = bob sign_in :user, @user1 + a2 = bob.aspects.create(:name => "two") + a2.contacts << bob.contact_for(alice.person) + a2.save + - status = @user1.post(:status_message, :text => "hello", :public => true, :to => 'all') - @vis = status.post_visibilities.first - pp @vis + @status = bob.post(:status_message, :text => "hello", :public => true, :to => a2) + @vis = @status.post_visibilities.first @vis.reload.hidden.should == false end describe '#destroy' do - it 'deletes the visibility' do - delete :destroy, :conversation_id => @vis.id - @vis.reload.hidden.should == true + context "on a post you can see" do + it 'succeeds' do + delete :destroy, :id => 42, :post_id => @status.id + response.should be_success + end + + it 'deletes the visibility' do + delete :destroy, :id => 42, :post_id => @status.id + @vis.reload.hidden.should == true + end + end - it 'does not let a user destroy a visibility that is not theirs' do - user2 = eve - sign_in :user, user2 - - lambda { - delete :destroy, :conversation_id => @vis.id - }.should_not change(@vis.reload, :hidden).to(true) + context "post you do not see" do + before do + user2 = eve + sign_in :user, user2 + end + it 'does not let a user destroy a visibility that is not theirs' do + lambda { + delete :destroy, :id => 42, :post_id => @status.id + }.should_not change(@vis.reload, :hidden).to(true) + end + it 'does not succceed' do + delete :destroy, :id => 42, :post_id => @status.id + response.should_not be_success + end end end end diff --git a/spec/models/post_visibility_spec.rb b/spec/models/post_visibility_spec.rb new file mode 100644 index 000000000..2c3fcfd0a --- /dev/null +++ b/spec/models/post_visibility_spec.rb @@ -0,0 +1,22 @@ +# Copyright (c) 2010, Diaspora Inc. This file is +# licensed under the Affero General Public License version 3 or later. See +# the COPYRIGHT file. +# +require 'spec_helper' + +describe PostVisibility do + before do + @alice = alice + @bob = bob + + @status = @alice.post(:status_message, :text => "hello", :public => true, :to => @alice.aspects.first) + @vis = @status.post_visibilities.first + @vis.hidden = true + @vis.save + end + + it 'is default scoped to not-hidden' do + PostVisibility.where(:id => @vis.id).should == [] + PostVisibility.unscoped.where(:id => @vis.id).should == [@vis] + end +end