diff --git a/app/controllers/users_controller.rb b/app/controllers/users_controller.rb index 9d122ec36..52a157ba8 100644 --- a/app/controllers/users_controller.rb +++ b/app/controllers/users_controller.rb @@ -13,37 +13,43 @@ class UsersController < ApplicationController def edit @aspect = :user_edit @user = current_user + @email_prefs = Hash.new(true) + @user.user_preferences.each do |pref| + @email_prefs[pref.email_type] = false + end end def update + + u = params[:user] @user = current_user - params[:user].delete(:password) if params[:user][:password].blank? - params[:user].delete(:password_confirmation) if params[:user][:password].blank? and params[:user][:password_confirmation].blank? - params[:user].delete(:language) if params[:user][:language].blank? + u.delete(:password) if u[:password].blank? + u.delete(:password_confirmation) if u[:password].blank? and u[:password_confirmation].blank? + u.delete(:language) if u[:language].blank? # change email notifications - if params[:user][:disable_mail] - @user.update_attributes(:disable_mail => params[:user][:disable_mail]) + if u[:email_preferences] + @user.update_user_preferences(u[:email_preferences]) flash[:notice] = I18n.t 'users.update.email_notifications_changed' # change passowrd - elsif params[:user][:current_password] && params[:user][:password] && params[:user][:password_confirmation] - if @user.update_with_password(params[:user]) + elsif u[:current_password] && u[:password] && u[:password_confirmation] + if @user.update_with_password(u) flash[:notice] = I18n.t 'users.update.password_changed' else flash[:error] = I18n.t 'users.update.password_not_changed' end - elsif params[:user][:language] - if @user.update_attributes(:language => params[:user][:language]) + elsif u[:language] + if @user.update_attributes(:language => u[:language]) I18n.locale = @user.language flash[:notice] = I18n.t 'users.update.language_changed' else flash[:error] = I18n.t 'users.update.language_not_changed' end - elsif params[:user][:a_ids] + elsif u[:a_ids] @user.aspects.update_all(:open => false) - unless params[:user][:a_ids] == ["home"] - @user.aspects.where(:id => params[:user][:a_ids]).update_all(:open => true) + unless u[:a_ids] == ["home"] + @user.aspects.where(:id => u[:a_ids]).update_all(:open => true) end end diff --git a/app/models/user.rb b/app/models/user.rb index dc366fc60..056b8c13b 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -37,6 +37,7 @@ class User < ActiveRecord::Base has_many :contacts has_many :contact_people, :through => :contacts, :source => :person has_many :services + has_many :user_preferences before_destroy :disconnect_everyone, :remove_person before_save do @@ -45,6 +46,25 @@ class User < ActiveRecord::Base attr_accessible :getting_started, :password, :password_confirmation, :language, :disable_mail + def update_user_preferences(pref_hash) + if self.disable_mail + mails = ['mentioned', 'request_received', 'comment_on_post', 'request_acceptence', 'also_commented', 'private_message'] + mails.each{|x| self.user_preferences.find_or_create_by_email_type(x)} + self.update_attributes(:disable_mail => false) + end + + pref_hash.keys.each do |key| + if pref_hash[key] == 'true' + self.user_preferences.find_or_create_by_email_type(key) + else + block = self.user_preferences.where(:email_type => key).first + if block + block.destroy + end + end + end + end + def strip_and_downcase_username if username.present? username.strip! @@ -158,7 +178,8 @@ class User < ActiveRecord::Base ######### Mailer ####################### def mail(job, *args) - unless self.disable_mail + pref = job.to_s.gsub('Job::Mail', '').underscore + unless self.disable_mail || self.user_preferences.exists?(:email_type => pref) Resque.enqueue(job, *args) end end diff --git a/app/models/user_preference.rb b/app/models/user_preference.rb new file mode 100644 index 000000000..9584d13e4 --- /dev/null +++ b/app/models/user_preference.rb @@ -0,0 +1,3 @@ +class UserPreference < ActiveRecord::Base + belongs_to :user +end diff --git a/app/views/users/edit.html.haml b/app/views/users/edit.html.haml index 8f6043977..f32d7b002 100644 --- a/app/views/users/edit.html.haml +++ b/app/views/users/edit.html.haml @@ -73,15 +73,43 @@ %br %h3 - = t('.email_notifications') + = t('.receive_email_notifications') = form_for @user do |f| = f.error_messages - %p.checkbox_select - = f.label :disable_mail, t('.receive_email_notifications') - = f.check_box :disable_mail, {:checked => !@user.disable_mail}, false, true - %br - = f.submit t('.change') + = f.fields_for :email_preferences do |type| + #email_prefs + %p.checkbox_select + = type.label t('.also_commented') + = type.check_box :also_commented, {:checked => @email_prefs['also_commented']}, false, true + %br + %p.checkbox_select + = type.label t('.mentioned') + = type.check_box :mentioned, {:checked => @email_prefs['mentioned']}, false, true + + + %br + %p.checkbox_select + = type.label t('.comment_on_post') + = type.check_box :comment_on_post, {:checked => @email_prefs['comment_on_post']}, false, true + + %br + %p.checkbox_select + = type.label t('.request_received') + = type.check_box :request_received, {:checked => @email_prefs['request_received']}, false, true + + %br + %p.checkbox_select + = type.label t('.private_message') + = type.check_box :private_message, {:checked => @email_prefs['private_message']}, false, true + + %br + %p.checkbox_select + = type.label t('.request_acceptence') + = type.check_box :request_acceptence, {:checked => @email_prefs['request_accpetence']}, false, true + + %br + = f.submit t('.change') %br %br @@ -100,5 +128,5 @@ %h3 = t('.close_account') = link_to t('.close_account'), current_user, - :confirm => t('are_you_sure'), :method => :delete, - :class => "button" + :confirm => t('are_you_sure'), :method => :delete, + :class => "button" diff --git a/config/locales/diaspora/en.yml b/config/locales/diaspora/en.yml index 6ff6e944a..e80504bd8 100644 --- a/config/locales/diaspora/en.yml +++ b/config/locales/diaspora/en.yml @@ -164,6 +164,7 @@ en: contacts_visible: "Contacts in this aspect will be able to see each other." contacts_not_visible: "Contacts in this aspect will not be able to see each other." edit: + make_aspect_list_visible: 'make aspect list visible?' remove_aspect: "Delete this aspect" confirm_remove_aspect: "Are you sure you want to delete this aspect?" add_existing: "Add an existing contact" @@ -250,8 +251,13 @@ en: your_handle: "Your diaspora handle" your_email: "Your email" edit_account: "Edit account" - email_notifications: "Email notifications" - receive_email_notifications: "Receive email notifications?" + receive_email_notifications: "Receive email notifications when..." + also_commented: "...someone also comments on your contacts post?" + comment_on_post: "...someone comments on your post?" + mentioned: "...you are mentioned in a post?" + request_received: "...you receive a new share request?" + request_acceptence: "...your share request is accepted?" + private_message: "...you recevie a private message?" change: "Change" destroy: "Account successfully closed." getting_started: diff --git a/db/migrate/20110311183826_create_user_preferences.rb b/db/migrate/20110311183826_create_user_preferences.rb new file mode 100644 index 000000000..a8c942693 --- /dev/null +++ b/db/migrate/20110311183826_create_user_preferences.rb @@ -0,0 +1,14 @@ +class CreateUserPreferences < ActiveRecord::Migration + def self.up + create_table :user_preferences do |t| + t.string :email_type + t.integer :user_id + + t.timestamps + end + end + + def self.down + drop_table :user_preferences + end +end diff --git a/db/schema.rb b/db/schema.rb index 5ef44075f..1b6d6195d 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -476,6 +476,13 @@ ActiveRecord::Schema.define(:version => 20110311220249) do t.string "name" end + create_table "user_preferences", :force => true do |t| + t.string "email_type" + t.integer "user_id" + t.datetime "created_at" + t.datetime "updated_at" + end + create_table "users", :force => true do |t| t.string "username" t.text "serialized_private_key" diff --git a/spec/controllers/users_controller_spec.rb b/spec/controllers/users_controller_spec.rb index 587376a35..e32ff0c07 100644 --- a/spec/controllers/users_controller_spec.rb +++ b/spec/controllers/users_controller_spec.rb @@ -95,6 +95,24 @@ describe UsersController do @user.language.should_not == old_language end end + + describe 'email settings' do + it 'lets the user turn off mail' do + par = {:id => @user.id, :user => {:email_preferences => {'mentioned' => 'true'}}} + proc{ + put :update, par + }.should change(@user.user_preferences, :count).by(1) + end + + it 'lets the user get mail again' do + @user.user_preferences.create(:email_type => 'mentioned') + par = {:id => @user.id, :user => {:email_preferences => {'mentioned' => 'false'}}} + proc{ + put :update, par + }.should change(@user.user_preferences, :count).by(-1) + + end + end end describe '#edit' do @@ -102,5 +120,11 @@ describe UsersController do get 'edit', :id => @user.id response.status.should == 200 end + + it 'set @email_pref to false when there is a user pref' do + @user.user_preferences.create(:email_type => 'mentioned') + get 'edit', :id => @user.id + assigns[:email_prefs]['mentioned'].should be_false + end end end diff --git a/spec/models/user_preference_spec.rb b/spec/models/user_preference_spec.rb new file mode 100644 index 000000000..66613b87d --- /dev/null +++ b/spec/models/user_preference_spec.rb @@ -0,0 +1,5 @@ +require 'spec_helper' + +describe UserPreference do + pending "add some examples to (or delete) #{__FILE__}" +end diff --git a/spec/models/user_spec.rb b/spec/models/user_spec.rb index 3b974a026..bce2ffc5c 100644 --- a/spec/models/user_spec.rb +++ b/spec/models/user_spec.rb @@ -215,6 +215,22 @@ describe User do end end end + + describe 'update_user_preferences' do + it 'unsets disable mail and makes the right amount of prefs' do + alice.disable_mail = true + proc { + alice.update_user_preferences({}) + }.should change(alice.user_preferences, :count).by(6) + end + it 'still sets new prefs to false on update' do + alice.disable_mail = true + proc { + alice.update_user_preferences({'mentioned' => false}) + }.should change(alice.user_preferences, :count).by(5) + end + + end describe ".find_for_authentication" do it 'finds a user' do @@ -450,11 +466,8 @@ describe User do alice.mail(Job::MailRequestReceived, alice.id, 'contactrequestid') end - it 'does not enqueue a mail job' do - alice.disable_mail = true - alice.save - alice.reload - + it 'does not enqueue a mail job if the correct corresponding job has a prefrence entry' do + alice.user_preferences.create(:email_type => 'request_received') Resque.should_not_receive(:enqueue) alice.mail(Job::MailRequestReceived, alice.id, 'contactrequestid') end