Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Configurable Email Settings #2985

Merged
merged 16 commits into from
Jul 20, 2018
27 changes: 26 additions & 1 deletion app/controllers/users_controller.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
class UsersController < ApplicationController
before_action :require_no_user, :only => [:new]
before_action :require_user, :only => %i(edit update)
before_action :require_user, :only => %i(edit update save_settings)
before_action :set_user, only: %i(info followed following followers)

def new
Expand Down Expand Up @@ -309,6 +309,31 @@ def test_digest_email
redirect_to "/"
end

def save_settings
user_settings = ['notify-comment-direct:false']

user_settings.each do |setting|
if params[setting] && params[setting] == "on"
UserTag.remove_if_exists(current_user.uid, setting)
else
UserTag.create_if_absent(current_user.uid, setting)
end
end

if params['digest:weekly'] == "on"
digest_val = 1
elsif params['digest:daily'] == "on"
digest_val = 0
else
digest_val = 2
end
# Digest settings handled separately
current_user.customize_digest(digest_val)

flash[:notice] = "Settings updated successfully!"
render js: "window.location.reload()"
end

private

def set_user
Expand Down
2 changes: 1 addition & 1 deletion app/models/comment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ def notify_users(uids, current_user)
# email all users in this thread
# plus all who've starred it
def notify(current_user)
if parent.uid != current_user.uid
if parent.uid != current_user.uid && !UserTag.exists?(parent.uid, 'notify-comment-direct:false')
CommentMailer.notify_note_author(parent.author, self).deliver_now
end

Expand Down
3 changes: 3 additions & 0 deletions app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -397,7 +397,10 @@ def customize_digest(type)
newtag = 'digest:daily'
elsif type == 1
newtag = 'digest:weekly'
elsif type == 2
UserTag.where('value LIKE (?)', 'digest%').destroy_all
end

unless newtag.blank?
UserTag.where('value LIKE (?)', 'digest%').destroy_all
UserTag.create(uid: id, value: newtag)
Expand Down
12 changes: 12 additions & 0 deletions app/models/user_tag.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,16 @@ def self.create_with_omniauth(auth, uid)
create(value: "oauth:" + auth['provider'] + ":" + auth['uid'],
uid: uid)
end

def self.remove_if_exists(uid, value)
if exists?(uid, value)
UserTag.where(uid: uid, value: value).destroy_all
end
end

def self.create_if_absent(uid, value)
unless exists?(uid, value)
UserTag.create(uid: uid, value: value)
end
end
end
4 changes: 4 additions & 0 deletions app/views/home/subscriptions.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@
<i><%= t('home.subscriptions.receive_mails') %></i>
</p>

<hr />

<p> You can also control which email you want to receive by changing settings using <a href="/settings" class="btn btn-default"><i class="fa fa-envelope"></i> Email Settings </a></p>

</div>

<div class="col-md-9">
Expand Down
1 change: 1 addition & 0 deletions app/views/layouts/_header.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@
<% end %>
<li><a href="/profile/<%= current_user.username %>"><%= t('layout._header.profile') %></a></li>
<li><a href="/profile/edit"><%= t('layout._header.edit_profile') %></a></li>
<li><a href="/settings" >Email Settings</a></li>
<li class="divider"></li>
<li><a href="/subscriptions"><%= t('layout._header.subscriptions') %></a></li>
<li><a href="/profile/<%= current_user.username %>/likes"><%= t('layout._header.notes_liked') %></a></li>
Expand Down
182 changes: 182 additions & 0 deletions app/views/users/settings.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,182 @@
<form name="settingsForm" action="/save_settings" method='post' data-remote="true">
<div class="emailSettings">
<h4><b>Email Notification Settings</b></h4>
<hr style="width: 35%; margin-left: 0;" />

<div style="display: inline-flex; justify-content: space-between; width: 90%;">
<span>Do you want to be notified by email for comments on your posts ? </span>
<span>
<label class="switch">
<input type="checkbox" name="notify-comment-direct:false" <% unless UserTag.exists?(current_user.id, 'notify-comment-direct:false') %>checked<% end %>>
<span class="slider round"></span>
</label>
</span>
</div>

<br />
<br />

<div style="display: inline-flex; justify-content: space-between; width: 90%;">
<span>Do you want to receive customized digest weekly ?</span>
<span>
<label style=" vertical-align: middle;" class="switch">
<input type="checkbox" name="digest:weekly" <% if UserTag.exists?(current_user.id, 'digest:weekly') %>checked<% end %>>
<span class="slider round"></span>
</label>
</span>
</div>

<br />
<br />

<div style="display: inline-flex; justify-content: space-between; width: 90%;">
<span>Do you want to receive customized digest daily ?</span>
<span>
<label style=" vertical-align: middle;" class="switch">
<input type="checkbox" name="digest:daily" <% if UserTag.exists?(current_user.id, 'digest:daily') %>checked<% end %>>
<span class="slider round"></span>
</label>
</span>
</div>

<br />
<br />


<!-- <div style="display: inline-flex; justify-content: space-between; width: 90%;">-->
<!-- <span>Do you want to be notified by email for comments on all posts you have commented on</span>-->
<!-- <span>-->
<!-- <label style=" vertical-align: middle;" class="switch">-->
<!-- <input type="checkbox" checked>-->
<!-- <span class="slider round"></span>-->
<!-- </label>-->
<!-- </span>-->
<!-- </div>-->

<!-- <br />-->
<!-- <br />-->

<!-- <div style="display: inline-flex; justify-content: space-between; width: 90%;">-->
<!-- <span>Do you want to be notified by email for likes on your posts</span>-->
<!-- <span>-->
<!-- <label style=" vertical-align: middle;" class="switch">-->
<!-- <input type="checkbox" checked>-->
<!-- <span class="slider round"></span>-->
<!-- </label>-->
<!-- </span>-->
<!-- </div>-->

<!-- <br />-->
<!-- <br />-->

<!-- <div style="display: inline-flex; justify-content: space-between; width: 90%;">-->
<!-- <span>Do you want to be notified by email for notifications for wiki page edits</span>-->
<!-- <span>-->
<!-- <label style=" vertical-align: middle;" class="switch">-->
<!-- <input type="checkbox">-->
<!-- <span class="slider round"></span>-->
<!-- </label>-->
<!-- </span>-->
<!-- </div>-->

<!-- <br />-->
<!-- <br />-->

</div>


<br />
<br />

<div class="page-footer">
<button type="submit" class="btn btn-success">Save</button>
<a type="button" class="btn btn-default" href="/dashboard">Cancel</a>
</div>

</form>

<style>
.switch {
position: relative;
display: inline-block;
width: 30px;
height: 17px;
margin-left: 90%;
vertical-align: middle;
margin-top: 0;
margin-bottom: 0;
margin-right: 0;


}

.switch input {display:none;}

.slider {
position: absolute;
cursor: pointer;
top: 0;
left: 0;
right: 0;
bottom: 0;
background-color: #ccc;
-webkit-transition: .4s;
transition: .4s;
}

.slider:before {
position: absolute;
content: "";
height: 13px;
width: 13px;
left: 2px;
bottom: 2px;
background-color: white;
-webkit-transition: .4s;
transition: .4s;
}

input:checked + .slider {
background-color: #2196F3;
}

input:focus + .slider {
box-shadow: 0 0 1px #2196F3;
}

input:checked + .slider:before {
-webkit-transform: translateX(13px);
-ms-transform: translateX(13px);
transform: translateX(13px);
}

/* Rounded sliders */
.slider.round {
border-radius: 17px;
}

.slider.round:before {
border-radius: 50%;
}
</style>

<script>
$(document).ready(function() {
$(':input[type="submit"]').prop('disabled', true);
$(':input[type="checkbox"]').change(function() {
$(':input[type="submit"]').prop('disabled', false);
});

$(':input[name="digest:daily"]').change(function () {
if($(':input[name="digest:daily"]').prop('checked') === true){
$(':input[name="digest:weekly"]').prop('checked', false);
}
});

$(':input[name="digest:weekly"]').change(function () {
if($(':input[name="digest:weekly"]').prop('checked') === true){
$(':input[name="digest:daily"]').prop('checked', false);
}
});
});
</script>
2 changes: 2 additions & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,8 @@
get 'profile/:id/edit' => 'users#edit'
get 'profile/:id/likes' => 'users#likes'
get 'feed/:author' => 'users#rss'
get '/settings' => 'users#settings'
post '/save_settings' => 'users#save_settings'

post 'profile/tags/create/:id' => 'user_tags#create'
get 'profile/tags/create/:id' => 'user_tags#create'
Expand Down
5 changes: 5 additions & 0 deletions test/fixtures/user_tags.yml
Original file line number Diff line number Diff line change
Expand Up @@ -64,3 +64,8 @@ github2:
id: 13
uid: 2
value: oauth:github:876543219

notify-comment-direct:
id: 14
uid: 10
value: notify-comment-direct:false
11 changes: 11 additions & 0 deletions test/functional/comment_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -384,6 +384,17 @@ def teardown
assert_equal updated_like_count, like_count-1
end

test 'should not send notification email to author if notify-comment-direct:false usertag is present' do
UserSession.create(users(:jeff))
post :create, params: {
id: nodes(:activity).nid,
body: 'A comment by Jeff on note of author test_user'
}, xhr: true

assert_not ActionMailer::Base.deliveries.collect(&:subject).include?("New comment on #{nodes(:activity).title} (##{nodes(:activity).nid}) ")
assert_not ActionMailer::Base.deliveries.collect(&:to).include?([users(:test_user).email])
end

private

def current_user
Expand Down