Skip to content

Commit

Permalink
style(rubocop): Layout/LineLength
Browse files Browse the repository at this point in the history
  • Loading branch information
adamcooke committed Feb 10, 2024
1 parent 59f299b commit e142d0d
Show file tree
Hide file tree
Showing 9 changed files with 70 additions and 24 deletions.
4 changes: 3 additions & 1 deletion .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ Style/WordArray:
EnforcedStyle: brackets

Layout/LineLength:
Enabled: false
# We want to reduce this back down to 120 but there are a fair number of offences
# of this which need addressing individually and carefully.
Max: 200

Metrics/AbcSize:
Enabled: false
Expand Down
7 changes: 6 additions & 1 deletion app/controllers/servers_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,12 @@ def create

def update
extra_params = [:spam_threshold, :spam_failure_threshold, :postmaster_address]
extra_params += [:send_limit, :allow_sender, :privacy_mode, :log_smtp_data, :outbound_spam_threshold, :message_retention_days, :raw_message_retention_days, :raw_message_retention_size] if current_user.admin?

if current_user.admin?
extra_params += [:send_limit, :allow_sender, :privacy_mode, :log_smtp_data, :outbound_spam_threshold,
:message_retention_days, :raw_message_retention_days, :raw_message_retention_size]
end

if @server.update(safe_params(*extra_params))
redirect_to_with_json organization_server_path(organization, @server), notice: "Server settings have been updated"
else
Expand Down
4 changes: 3 additions & 1 deletion app/controllers/user_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,9 @@ def update

if @user.save
if email_changed
redirect_to_with_json verify_path(return_to: settings_path), notice: "Your settings have been updated successfully. As you've changed, your e-mail address you'll need to verify it before you can continue."
redirect_to_with_json verify_path(return_to: settings_path),
notice: "Your settings have been updated successfully. As you've changed, your e-mail " \
"address you'll need to verify it before you can continue."
else
redirect_to_with_json settings_path, notice: "Your settings have been updated successfully."
end
Expand Down
9 changes: 8 additions & 1 deletion app/jobs/unqueue_message_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

class UnqueueMessageJob < Postal::Job

# rubocop:disable Layout/LineLength
def perform
if original_message = QueuedMessage.find_by_id(params["id"])
if original_message.acquire_lock
Expand Down Expand Up @@ -430,7 +431,12 @@ def perform
Sentry.capture_exception(e, extra: { job_id: self.id, server_id: queued_message.server_id, message_id: queued_message.message_id })
end
if queued_message.message
queued_message.message.create_delivery("Error", details: "An internal error occurred while sending this message. This message will be retried automatically. If this persists, contact support for assistance.", output: "#{e.class}: #{e.message}", log_id: "J-#{self.id}")
queued_message.message.create_delivery("Error",
details: "An internal error occurred while sending " \
"this message. This message will be retried " \
"automatically.",
output: "#{e.class}: #{e.message}", log_id: "J-#{self.id}"
)
end
end
end
Expand All @@ -448,6 +454,7 @@ def perform
nil
end
end
# rubocop:enable Layout/LineLength

private

Expand Down
26 changes: 20 additions & 6 deletions app/models/server.rb
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,10 @@ def stats
}
end

# Return the domain which can be used to authenticate emails sent from the given e-mail address.
#
# @param address [String] an e-mail address
# @return [Domain, nil] the domain to use for authentication
def authenticated_domain_for_address(address)
return nil if address.blank?

Expand All @@ -230,14 +234,24 @@ def authenticated_domain_for_address(address)

uname, = uname.split("+", 2)

# Check the server's domain
if domain = Domain.verified.order(owner_type: :desc).where("(owner_type = 'Organization' AND owner_id = ?) OR (owner_type = 'Server' AND owner_id = ?)", organization_id, id).where(name: domain_name).first
return domain
end
# Find a verified domain which directly matches the domain name for the given address.
domain = Domain.verified
.order(owner_type: :desc)
.where("(owner_type = 'Organization' AND owner_id = ?) OR " \
"(owner_type = 'Server' AND owner_id = ?)", organization_id, id)
.where(name: domain_name)
.first

# If there is a matching domain, return it
return domain if domain

return unless any_domain = domains.verified.where(use_for_any: true).order(:name).first
# Otherwise, we need to look to see if there is a domain configured which can be used as the authenticated
# domain for any domain. This will look for domains directly within the server and return that.
any_domain = domains.verified.where(use_for_any: true).order(:name).first
return any_domain if any_domain

any_domain
# Return nil if we can't find anything suitable
nil
end

def find_authenticated_domain_from_headers(headers)
Expand Down
9 changes: 0 additions & 9 deletions lib/postal/countries.rb

This file was deleted.

7 changes: 6 additions & 1 deletion lib/postal/dkim_header.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,12 @@ def header_names

def normalized_headers
[].tap do |new_headers|
headers.select { |h| h.match(/^(from|sender|reply-to|subject|date|message-id|to|cc|mime-version|content-type|content-transfer-encoding|resent-to|resent-cc|resent-from|resent-sender|resent-message-id|in-reply-to|references|list-id|list-help|list-owner|list-unsubscribe|list-subscribe|list-post):/i) }.each do |h|
headers.select { |h| h.match(%r{
^(
from|sender|reply-to|subject|date|message-id|to|cc|mime-version|content-type|content-transfer-encoding|
resent-to|resent-cc|resent-from|resent-sender|resent-message-id|in-reply-to|references|list-id|list-help|
list-owner|list-unsubscribe|list-subscribe|list-post
):}ix) }.each do |h|
new_headers << normalize_header(h)
end
end
Expand Down
18 changes: 16 additions & 2 deletions lib/postal/tracking_middleware.rb
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,22 @@ def dispatch_redirect_request(request, server_token, link_token)
time = Time.now.to_f
if link["message_id"]
message_db.update(:messages, { clicked: time }, where: { id: link["message_id"] })
message_db.insert(:clicks, { message_id: link["message_id"], link_id: link["id"], ip_address: request.ip, user_agent: request.user_agent, timestamp: time })
SendWebhookJob.queue(:main, server_id: message_db.server_id, event: "MessageLinkClicked", payload: { _message: link["message_id"], url: link["url"], token: link["token"], ip_address: request.ip, user_agent: request.user_agent })
message_db.insert(:clicks, {
message_id: link["message_id"],
link_id: link["id"],
ip_address: request.ip,
user_agent: request.user_agent,
timestamp: time })
SendWebhookJob.queue(:main,
server_id: message_db.server_id,
event: "MessageLinkClicked",
payload: {
_message: link["message_id"],
url: link["url"],
token: link["token"],
ip_address: request.ip,
user_agent: request.user_agent
})
end

[307, { "Location" => link["url"] }, ["Redirected to: #{link['url']}"]]
Expand Down
10 changes: 8 additions & 2 deletions script/queue_size.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@
require_relative "../lib/postal/config"
require "mysql2"

client = Mysql2::Client.new(host: Postal.config.main_db.host, username: Postal.config.main_db.username, password: Postal.config.main_db.password, port: Postal.config.main_db.port, database: Postal.config.main_db.database)
result = client.query("SELECT COUNT(id) as size FROM `queued_messages` WHERE retry_after IS NULL OR retry_after <= ADDTIME(UTC_TIMESTAMP(), '30') AND locked_at IS NULL")
client = Mysql2::Client.new(
host: Postal.config.main_db.host,
username: Postal.config.main_db.username,
password: Postal.config.main_db.password,
port: Postal.config.main_db.port,
database: Postal.config.main_db.database)
result = client.query("SELECT COUNT(id) as size FROM `queued_messages` WHERE retry_after IS NULL OR " \
"retry_after <= ADDTIME(UTC_TIMESTAMP(), '30') AND locked_at IS NULL")
puts result.to_a.first["size"]

0 comments on commit e142d0d

Please sign in to comment.