Skip to content

Commit

Permalink
Merge pull request #1274 from internetee/idn-code-formatting
Browse files Browse the repository at this point in the history
added prepend action to format foreign symbols to punicode
  • Loading branch information
vohmar authored Jun 27, 2024
2 parents 4f87e77 + 1930ec2 commit b69214d
Show file tree
Hide file tree
Showing 8 changed files with 67 additions and 15 deletions.
3 changes: 3 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ RUN yarn install --frozen-lockfile

FROM base

ENV LAUNCHY_DRY_RUN=true
ENV BROWSER=/dev/null

RUN useradd rails
RUN mkdir -p /home/rails && chown rails:rails /home/rails

Expand Down
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ group :development do
gem 'htmlbeautifier'
gem 'i18n-debug'
gem 'letter_opener', '~> 1.8'
gem 'letter_opener_web', '~> 3.0'
gem 'listen', '>= 3.0.5', '< 3.9'
gem 'ruby-lsp-rails'
gem 'web-console', '>= 3.3.0'
Expand Down
10 changes: 8 additions & 2 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -208,8 +208,13 @@ GEM
language_server-protocol (3.17.0.3)
launchy (2.5.2)
addressable (~> 2.8)
letter_opener (1.8.1)
launchy (>= 2.2, < 3)
letter_opener (1.10.0)
launchy (>= 2.2, < 4)
letter_opener_web (3.0.0)
actionmailer (>= 6.1)
letter_opener (~> 1.9)
railties (>= 6.1)
rexml
listen (3.8.0)
rb-fsevent (~> 0.10, >= 0.10.3)
rb-inotify (~> 0.9, >= 0.9.10)
Expand Down Expand Up @@ -503,6 +508,7 @@ DEPENDENCIES
jsbundling-rails
jwt
letter_opener (~> 1.8)
letter_opener_web (~> 3.0)
listen (>= 3.0.5, < 3.9)
lograge
mimemagic (~> 0.4.3)
Expand Down
26 changes: 15 additions & 11 deletions app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@ class ApplicationController < ActionController::Base

protect_from_forgery with: :exception
before_action :set_locale, :clear_flash
before_action :set_notifications
before_action :notifications_for_header

content_security_policy do |policy|
policy.style_src :self, 'www.gstatic.com', :unsafe_inline
end

rescue_from CanCan::AccessDenied do |exception|
rescue_from CanCan::AccessDenied do |_exception|
flash[:alert] = I18n.t('unauthorized.message')

if turbo_frame_request?
Expand All @@ -22,7 +22,9 @@ class ApplicationController < ActionController::Base
end

def store_location
session[:return_to] = request.referer.split('?').first if request.referer
return unless request.referer

session[:return_to] = request.referer.split('?').first
end

# If needed, add updated_by to the params hash. Updated by takes format of "123 - User Surname"
Expand All @@ -36,26 +38,28 @@ def merge_updated_by(update_params)
end
end

def after_sign_in_path_for(_resource)
root_path
end
def after_sign_in_path_for(_resource) = root_path

def set_notifications
def notifications_for_header
# don't change the name, it's used in the header and can be conflict with notification variable in notifications page
@notifications_for_header = current_user&.notifications&.unread&.order(created_at: :desc)&.limit(5)
@notifications_for_header ||= current_user&.notifications&.unread&.order(created_at: :desc)&.limit(5)
end

private

def set_locale
if params[:localize].present? && I18n.available_locales.include?(params[:localize].to_sym)
cookies[:locale] = params[:localize]
end
set_locale_to_cookies

I18n.locale = current_user&.locale || cookies[:locale] || I18n.default_locale
@pagy_locale = I18n.locale.to_s
end

def set_locale_to_cookies
return unless params[:localize].present? && I18n.available_locales.include?(params[:localize].to_sym)

cookies[:locale] = params[:localize]
end

def clear_flash
flash.clear if turbo_frame_request?
end
Expand Down
6 changes: 5 additions & 1 deletion app/controllers/users_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ def create

respond_to do |format|
if @user.save
flash[:notice] = t(:created)

format.html do
sign_in(User, @user)
redirect_to user_path(@user.uuid), notice: t(:created)
Expand All @@ -52,7 +54,9 @@ def create
render :show, status: :created, location: @user
end
else
format.html { render :new }
flash.now[:alert] = @user.errors.full_messages.join(', ')

format.html { render :new, status: :unprocessable_entity }
format.json { render json: @user.errors, status: :unprocessable_entity }
end
end
Expand Down
33 changes: 33 additions & 0 deletions app/mailers/application_mailer.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,37 @@
class ApplicationMailer < ActionMailer::Base
before_action :add_smtputf8_header
after_action :convert_email_to_punycode

default from: Rails.application.config.customization['email_from_address']
layout 'mailer'

private

def add_smtputf8_header
headers['SMTPUTF8'] = 'true'
end

def convert_email_to_punycode
convert_recipient_to_punycode
convert_carbon_copy_to_punycode
convert_blind_carbon_copy_to_punycode
end

def convert_recipient_to_punycode
mail.to = Array(mail.to).map { |email| convert_domain_to_punycode(email) }
end

def convert_carbon_copy_to_punycode
mail.cc = Array(mail.cc).map { |email| convert_domain_to_punycode(email) }
end

def convert_blind_carbon_copy_to_punycode
mail.bcc = Array(mail.bcc).map { |email| convert_domain_to_punycode(email) }
end

def convert_domain_to_punycode(email)
local, domain = email.split('@')
domain = SimpleIDN.to_ascii(domain)
"#{local}@#{domain}"
end
end
2 changes: 1 addition & 1 deletion config/environments/development.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@
config.active_record.verbose_query_logs = true

config.log_formatter = ::Logger::Formatter.new
config.action_mailer.delivery_method = :letter_opener
config.action_mailer.delivery_method = :letter_opener_web
config.action_mailer.default_url_options = {
host: 'localhost:3000',
protocol: 'http'
Expand Down
1 change: 1 addition & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -144,4 +144,5 @@
resources :autobider, param: :uuid, only: [:create, :update, :edit, :new]

mount OkComputer::Engine, at: '/healthcheck', as: :healthcheck
mount LetterOpenerWeb::Engine, at: "/letter_opener" if Rails.env.development?
end

0 comments on commit b69214d

Please sign in to comment.