Skip to content

Commit

Permalink
add resubscribe to stdout
Browse files Browse the repository at this point in the history
  • Loading branch information
ezekg committed Feb 21, 2024
1 parent dee34ef commit 604eaf5
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 6 deletions.
28 changes: 24 additions & 4 deletions app/controllers/stdout/subscribers_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

module Stdout
class SubscribersController < ApplicationController
include Rendering::HTML

skip_verify_authorized

def unsubscribe
Expand All @@ -10,22 +12,40 @@ def unsubscribe
return if
email.nil?

user = User.where(email: email, stdout_unsubscribed_at: nil)
user = User.where(email:, stdout_unsubscribed_at: nil)

# Unsubscribe all users with this email across all accounts
user.update_all(stdout_unsubscribed_at: Time.current)
rescue => e
Keygen.logger.error "[stdout] Unsubscribe failed: err=#{e.message}"
ensure
response.headers['Content-Type'] = 'text/plain'
render html: <<~HTML.html_safe
You've been unsubscribed. To resubscribe, follow this link: #{helpers.link_to(nil, stdout_resubscribe_url(ciphertext))}
HTML
end

render plain: "You've been unsubscribed"
def resubscribe
ciphertext = params.fetch(:ciphertext)
email = decrypt(ciphertext)
return if
email.nil?

user = User.where.not(stdout_unsubscribed_at: nil)
.where(email:)

# Resubscribe all users with this email across all accounts
user.update_all(stdout_unsubscribed_at: nil)
rescue => e
Keygen.logger.error "[stdout] Resubscribe failed: err=#{e.message}"
ensure
render html: <<~HTML.html_safe
You've been resubscribed. To unsubscribe, follow this link: #{helpers.link_to(nil, stdout_unsubscribe_url(ciphertext))}
HTML
end

private

def secret_key = ENV.fetch('STDOUT_SECRET_KEY')

def decrypt(ciphertext)
crypt = ActiveSupport::MessageEncryptor.new(secret_key, serializer: JSON)
enc = ciphertext.split('.')
Expand Down
5 changes: 3 additions & 2 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -439,9 +439,10 @@
end
end

# Routes for Stdout (e.g. unsubscribe)
scope module: :stdout, constraints: { subdomain: 'stdout', **domain_constraints, format: :jsonapi } do
# Routes for Stdout (e.g. unsubscribe, resubscribe)
scope module: :stdout, constraints: { subdomain: 'stdout', **domain_constraints, format: :html } do
get 'unsub/:ciphertext', constraints: { ciphertext: /.*/ }, to: 'subscribers#unsubscribe', as: :stdout_unsubscribe
get 'resub/:ciphertext', constraints: { ciphertext: /.*/ }, to: 'subscribers#resubscribe', as: :stdout_resubscribe
end
end

Expand Down

0 comments on commit 604eaf5

Please sign in to comment.