diff --git a/app/controllers/maintenance/user/email_notifications_controller.rb b/app/controllers/maintenance/user/email_notifications_controller.rb new file mode 100644 index 000000000..0452a8706 --- /dev/null +++ b/app/controllers/maintenance/user/email_notifications_controller.rb @@ -0,0 +1,33 @@ +module Maintenance + module User + class EmailNotificationsController < ApplicationController + class VerificationError < Exception ; end + + before_filter :validate_sig, :only => [:destroy] + rescue_from VerificationError, :with => :render_403 + + def show + end + + def destroy + @user = User.find(params[:user_id]) + @user.receive_email_notifications = false + @user.save + end + + private + + def render_403 + render :nothing => true, :status => 403 + end + + def validate_sig + digest = OpenSSL::Digest.new("sha256") + calc_sig = OpenSSL::HMAC.hexdigest(digest, Danbooru.config.email_key, params[:user_id].to_s) + if calc_sig != params[:sig] + raise VerificationError.new + end + end + end + end +end diff --git a/app/helpers/users_helper.rb b/app/helpers/users_helper.rb index 2310a240d..93a2e4ac8 100644 --- a/app/helpers/users_helper.rb +++ b/app/helpers/users_helper.rb @@ -1,2 +1,6 @@ module UsersHelper + def email_sig(user) + digest = OpenSSL::Digest.new("sha256") + OpenSSL::HMAC.hexdigest(digest, Danbooru.config.email_key, user.id.to_s) + end end diff --git a/app/mailers/user_mailer.rb b/app/mailers/user_mailer.rb index 865725638..20458435e 100644 --- a/app/mailers/user_mailer.rb +++ b/app/mailers/user_mailer.rb @@ -1,5 +1,6 @@ class UserMailer < ActionMailer::Base add_template_helper ApplicationHelper + add_template_helper UsersHelper default :from => Danbooru.config.contact_email, :content_type => "text/html" def dmail_notice(dmail) @@ -16,6 +17,7 @@ class UserMailer < ActionMailer::Base end def forum_notice(user, forum_topic, forum_posts) + @user = user @forum_topic = forum_topic @forum_posts = forum_posts mail(:to => "#{user.name} <#{user.email}>", :subject => "#{Danbooru.config.app_name} forum topic #{forum_topic.title} updated") diff --git a/app/views/maintenance/user/email_notifications/destroy.html.erb b/app/views/maintenance/user/email_notifications/destroy.html.erb new file mode 100644 index 000000000..ecdfcac17 --- /dev/null +++ b/app/views/maintenance/user/email_notifications/destroy.html.erb @@ -0,0 +1 @@ +

You have been unsubscribed from all email notifications.

diff --git a/app/views/maintenance/user/email_notifications/show.html.erb b/app/views/maintenance/user/email_notifications/show.html.erb new file mode 100644 index 000000000..e30c4a6e7 --- /dev/null +++ b/app/views/maintenance/user/email_notifications/show.html.erb @@ -0,0 +1,9 @@ +

Unsubscribe

+ +

Do you wish to stop receiving all email notifications?

+ +<%= form_tag(maintenance_user_email_notification_path, :method => "delete") do %> + <%= hidden_field_tag "sig", params[:sig] %> + <%= hidden_field_tag "user_id", params[:user_id] %> + <%= submit_tag "Yes" %> +<% end %> diff --git a/app/views/user_mailer/dmail_notice.html.erb b/app/views/user_mailer/dmail_notice.html.erb index b683419dd..fce085ff2 100644 --- a/app/views/user_mailer/dmail_notice.html.erb +++ b/app/views/user_mailer/dmail_notice.html.erb @@ -1,8 +1,5 @@ - - <%= stylesheet_link_tag "application", :media => "screen" %> -

<%= h @dmail.from.name %> said:

@@ -10,6 +7,6 @@ <%= DText.parse(@dmail.body) %> -

<%= link_to "View message", dmail_url(@dmail, :host => Danbooru.config.hostname, :only_path => false) %>

+

<%= link_to "View message", dmail_url(@dmail, :host => Danbooru.config.hostname, :only_path => false) %> | <%= link_to "Unsubscribe", maintenance_user_email_notification_url(:user_id => @dmail.owner.id, :sig => email_sig(@dmail.owner), :host => Danbooru.config.hostname, :only_path => false) %>

diff --git a/app/views/user_mailer/forum_notice.html.erb b/app/views/user_mailer/forum_notice.html.erb index c12cd6c11..39bb1fd88 100644 --- a/app/views/user_mailer/forum_notice.html.erb +++ b/app/views/user_mailer/forum_notice.html.erb @@ -10,4 +10,4 @@
<% end %> -

<%= link_to "View topic", forum_topic_path(@forum_topic, :page => @forum_topic.last_page, :host => Danbooru.config.hostname, :only_path => false) %>

+

<%= link_to "View topic", forum_topic_path(@forum_topic, :page => @forum_topic.last_page, :host => Danbooru.config.hostname, :only_path => false) %> | <%= link_to "Unsubscribe", maintenance_user_email_notification_url(:user_id => @user.id, :sig => email_sig(@user), :host => Danbooru.config.hostname, :only_path => false) %>

diff --git a/config/danbooru_default_config.rb b/config/danbooru_default_config.rb index 3fa0dd7ed..c54f605ce 100644 --- a/config/danbooru_default_config.rb +++ b/config/danbooru_default_config.rb @@ -360,6 +360,10 @@ module Danbooru false end + def email_key + "zDMSATq0W3hmA5p3rKTgD" + end + # For downloads, if the host matches any of these IPs, block it def banned_ip_for_download?(ip_addr) raise ArgumentError unless ip_addr.is_a?(IPAddr) diff --git a/config/routes.rb b/config/routes.rb index 03f1e6b05..45d4de9cd 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -53,6 +53,7 @@ Rails.application.routes.draw do end namespace :maintenance do namespace :user do + resource :email_notification, :only => [:show, :destroy] resource :password_reset, :only => [:new, :create, :edit, :update] resource :login_reminder, :only => [:new, :create] resource :deletion, :only => [:show, :destroy]