Add --delete-older-than to delete messages older than a time modifier every time a mail is recieved

This commit is contained in:
Travis Burtrum 2016-08-10 16:15:56 -04:00
parent d42f2bf11d
commit 93dd5d1758
2 changed files with 15 additions and 0 deletions

View File

@ -81,6 +81,7 @@ module MailCatcher extend self
:browse => false, :browse => false,
:quit => true, :quit => true,
:sqlite_db => ':memory:', :sqlite_db => ':memory:',
:delete_older_than => nil,
} }
def options def options
@ -125,6 +126,10 @@ module MailCatcher extend self
options[:sqlite_db] = db options[:sqlite_db] = db
end end
parser.on("--delete-older-than TIME_MODIFIER", "On mail receipt, delete all mail older than this, examples: '-5 minutes', '-2 days'") do |db|
options[:delete_older_than] = db
end
if mac? if mac?
parser.on("--[no-]growl") do |growl| parser.on("--[no-]growl") do |growl|
puts "Growl is no longer supported" puts "Growl is no longer supported"

View File

@ -57,6 +57,9 @@ module MailCatcher::Mail extend self
EventMachine.next_tick do EventMachine.next_tick do
message = MailCatcher::Mail.message message_id message = MailCatcher::Mail.message message_id
MailCatcher::Events::MessageAdded.push message MailCatcher::Events::MessageAdded.push message
if MailCatcher.options[:delete_older_than]
MailCatcher::Mail.delete_messages_older_than!(MailCatcher.options[:delete_older_than])
end
end end
end end
@ -159,4 +162,11 @@ module MailCatcher::Mail extend self
@delete_messages_query.execute(message_id) and @delete_messages_query.execute(message_id) and
@delete_message_parts_query.execute(message_id) @delete_message_parts_query.execute(message_id)
end end
def delete_messages_older_than!(modifier)
@delete_messages_older_than_query ||= db.prepare "DELETE FROM message WHERE created_at < datetime('now', ?)"
@delete_message_parts_older_than_query ||= db.prepare "DELETE FROM message_part WHERE created_at < datetime('now', ?)"
@delete_messages_older_than_query.execute(modifier) and
@delete_message_parts_older_than_query.execute(modifier)
end
end end