5
0
Fork 0
mirror of https://0xacab.org/sutty/sutty synced 2024-06-02 08:34:17 +00:00

feat: refactorizar mailer para que use menos recursors

y soporte varias urls
This commit is contained in:
f 2023-02-08 18:55:05 -03:00
parent f15bcf453b
commit b091b3212a
4 changed files with 73 additions and 37 deletions

View file

@ -32,7 +32,7 @@ class DeployJob < ApplicationJob
status: deploy_locally,
seconds: deploy_local.build_stats.last.seconds,
size: deploy_local.size,
url: deploy_local.url
urls: [deploy_local.url]
}
}
@ -74,7 +74,7 @@ class DeployJob < ApplicationJob
status: status,
seconds: build_stat.try(:seconds) || 0,
size: d.size,
url: d.url
urls: d.respond_to?(:urls) ? d.urls : [d.url]
}
end
end

View file

@ -8,21 +8,64 @@
# TODO: Agregar firma GPG y header Autocrypt
# TODO: Cifrar con GPG si le usuarie nos dio su llave
class DeployMailer < ApplicationMailer
include ActionView::Helpers::NumberHelper
include ActionView::Helpers::DateHelper
# rubocop:disable Metrics/AbcSize
def deployed(which_ones)
@usuarie = Usuarie.find(params[:usuarie])
@site = @usuarie.sites.find(params[:site])
@deploys = which_ones
@deploy_local = @site.deploys.find_by(type: 'DeployLocal')
def deployed(deploys)
usuarie = Usuarie.find(params[:usuarie])
site = usuarie.sites.find(params[:site])
subject = t('.subject', site: site.name)
hostname = site.hostname
@hi = t('.hi')
@explanation = t('.explanation', fqdn: hostname)
@help = t('.help')
@headers = %w[type status url seconds size].map do |header|
t(".th.#{header}")
end
@table = deploys.each_pair.map do |deploy, value|
{
title: t(".#{deploy}.title"),
status: t(".#{deploy}.#{value[:status] ? 'success' : 'error'}"),
urls: value[:urls],
seconds: {
human: distance_of_time_in_words(value[:seconds].seconds),
machine: "PT#{value[:seconds]}S"
},
size: number_to_human_size(value[:size], precision: 2)
}
end
@terminal_table = Terminal::Table.new do |t|
t << @headers
t.add_separator
@table.each do |row|
row[:urls].each do |url|
t << (row.map do |k, v|
case k
when :seconds then v[:human]
when :urls then url
else v
end
end)
end
end
end
# Informamos a cada quien en su idioma y damos una dirección de
# respuesta porque a veces les usuaries nos escriben
I18n.with_locale(@usuarie.lang) do
mail(to: @usuarie.email,
reply_to: "sutty@#{Site.domain}",
subject: I18n.t('deploy_mailer.deployed.subject',
site: @site.name))
I18n.with_locale(usuarie.lang) do
mail(to: usuarie.email, reply_to: "sutty@#{Site.domain}", subject: subject)
end
end
# rubocop:enable Metrics/AbcSize
private
def t(key, **args)
I18n.t("deploy_mailer.deployed#{key}", **args)
end
end

View file

@ -1,24 +1,21 @@
%h1= t('.hi')
%h1= @hi
= sanitize_markdown t('.explanation', fqdn: @deploy_local.site.hostname),
tags: %w[p a strong em]
= sanitize_markdown @explanation, tags: %w[p a strong em]
%table
%thead
%tr
%th= t('.th.type')
%th= t('.th.status')
%th= t('.th.url')
%th= t('.th.seconds')
%th= t('.th.size')
- @headers.each do |header|
%th= header
%tbody
- @deploys.each_pair do |deploy, value|
%tr
%td= t(".#{deploy}.title")
%td= value[:status] ? t(".#{deploy}.success") : t(".#{deploy}.error")
%td= link_to value[:url], value[:url]
%td
%time{ datetime: "PT#{value[:seconds]}S" }= distance_of_time_in_words value[:seconds].seconds
%td= number_to_human_size value[:size], precision: 2
- @table.each do |row|
- row[:urls].each do |url|
%tr
%td= row[:title]
%td= row[:status]
%td= link_to url, url
%td
%time{ datetime: row[:seconds][:machine] }= row[:seconds][:human]
%td= row[:size]
= sanitize_markdown t('.help'), tags: %w[p a strong em]
= sanitize_markdown @help, tags: %w[p a strong em]

View file

@ -1,11 +1,7 @@
= '# ' + t('.hi')
= "# #{@hi}"
\
= t('.explanation', fqdn: @deploy_local.site.hostname)
= @explanation
\
= Terminal::Table.new do |table|
- table << [t('.th.type'), t('.th.status'), t('.th.url'), t('.th.seconds'), t('.th.size')]
- table.add_separator
- @deploys.each_pair do |deploy, value|
- table << [t(".#{deploy}.title"), value[:status] ? t(".#{deploy}.success") : t(".#{deploy}.error"), value[:url], distance_of_time_in_words(value[:seconds].seconds), number_to_human_size(value[:size], precision: 2)]
= @terminal_table
\
= t('.help')
= @help