mirror of
https://0xacab.org/sutty/sutty
synced 2024-11-28 20:26:22 +00:00
77 lines
1.7 KiB
Ruby
77 lines
1.7 KiB
Ruby
|
class Invitadx < ApplicationRecord
|
||
|
has_secure_password
|
||
|
validates_uniqueness_of :email
|
||
|
|
||
|
after_create :create_invitadx_directory!
|
||
|
after_save :add_sites!
|
||
|
|
||
|
# Para facilitar la serialización de Warden
|
||
|
def username
|
||
|
email
|
||
|
end
|
||
|
|
||
|
def path
|
||
|
File.join(Rails.root, '_invitadxs', email)
|
||
|
end
|
||
|
|
||
|
# TODO convertir el Pathname en un helper
|
||
|
def site_dirs
|
||
|
return [] unless Dir.exists? path
|
||
|
Pathname.new(path).children.map(&:expand_path).map(&:to_s)
|
||
|
end
|
||
|
|
||
|
def sites
|
||
|
@sites ||= site_dirs.map do |site|
|
||
|
Site.find(File.basename(site))
|
||
|
end.compact
|
||
|
end
|
||
|
|
||
|
private
|
||
|
|
||
|
# Crea el directorio en _sites
|
||
|
def create_invitadx_directory!
|
||
|
FileUtils.mkdir_p(path)
|
||
|
end
|
||
|
|
||
|
# Agrega el sitio a las invitadxs y viceversa
|
||
|
def add_sites!
|
||
|
sites.each do |site|
|
||
|
dir = File.join(path, site.name)
|
||
|
file = site.invitadxs_file
|
||
|
|
||
|
unless File.exists? dir
|
||
|
FileUtils.ln_s File.join('..', '..', '_sites', site.name), dir
|
||
|
end
|
||
|
|
||
|
if File.exists? file
|
||
|
invitadxs = File.read(file).split("\n")
|
||
|
invitadxs_orig = invitadxs.dup
|
||
|
invitadxs << email unless invitadxs.include? email
|
||
|
else
|
||
|
invitadxs_orig = []
|
||
|
invitadxs = [email]
|
||
|
end
|
||
|
|
||
|
# Solo escribir los cambios si hubo cambios en la lista
|
||
|
return if invitadxs_orig == invitadxs
|
||
|
|
||
|
r = File.open(file, File::RDWR | File::CREAT, 0o640) do |f|
|
||
|
# Bloquear el archivo para que no sea accedido por otro
|
||
|
# proceso u otra editora
|
||
|
f.flock(File::LOCK_EX)
|
||
|
|
||
|
# Empezar por el principio
|
||
|
f.rewind
|
||
|
|
||
|
# Escribir la fecha de creación
|
||
|
f.write(invitadxs.join("\n"))
|
||
|
|
||
|
# Eliminar el resto
|
||
|
f.flush
|
||
|
f.truncate(f.pos)
|
||
|
end
|
||
|
|
||
|
end
|
||
|
end
|
||
|
end
|