<commit_before>package ufront.ufadmin.modules;

#if ufront_ufadmin
import ufront.ufadmin.UFAdminModule;
import ufront.mailer.DBMailer;
import ufront.mail.*;
import ufront.web.result.ViewResult;
using Dates;
using CleverSort;

class DBMailerAdminModule extends UFAdminModule {
	public function new() {
		super( "dbmailer", "DB Mailer" );
	}
	
	@:route("/")
	public function index() {
		return listByDate( Date.now() );
	}
	
	function displayEmailList( emailsList:Iterable<UFMailLog>, title:String ) {
		var emails = Lambda.array( emailsList );
		emails.cleverSort( _.date );
		var template = CompileTime.readFile( "/ufront/ufadmin/view/dbmailer-list.html" );
		return UFAdminModule.wrapInLayout( title, template, {
			emails:emails,
			baseUri:baseUri,
			title:title,
		});
	}
	
	@:route("/date/$date/")
	public function listByDate( date:Date ) {
		var dateSnappedDown = Date.fromTime( date.getTime().snap(Day,Down) );
		var dateSnappedUp = Date.fromTime( date.getTime().snap(Day,Up) );
		var dateStr = date.format( "D" );
		var emails = UFMailLog.manager.search( $date>=dateSnappedDown && $date<=dateSnappedUp, { orderBy: -date } );
		return displayEmailList( emails, 'Emails sent on $dateStr' );
	}
	
	@:route("/to/$address/")
	public function listAllEmailsToAddress( address:String ) {
		var emails = UFMailLog.manager.search( $to==address );
		return displayEmailList( emails, 'Emails sent to $address' );
	}
	
	@:route("/from/$address/")
	public function listAllEmailsFromAddress( address:String ) {
		var emails = UFMailLog.manager.search( $from==address );
		return displayEmailList( emails, 'Emails sent from $address' );
	}
	
	@:route("/email/$id/")
	public function showEmail( id:Int ) {
		var mailLog = UFMailLog.manager.get( id );
		var template = CompileTime.readFile( "/ufront/ufadmin/view/dbmailer-view.html" );
		return UFAdminModule.wrapInLayout( title, template, {
			id: mailLog.id,
			to: mailLog.to,
			from: mailLog.from,<commit_msg>Workaround for thx.core not having date functions.<commit_after>package ufront.ufadmin.modules;

#if ufront_ufadmin
import ufront.ufadmin.UFAdminModule;
import ufront.mailer.DBMailer;
import ufront.mail.*;
import ufront.web.result.ViewResult;
using CleverSort;

class DBMailerAdminModule extends UFAdminModule {
	public function new() {
		super( "dbmailer", "DB Mailer" );
	}
	
	@:route("/")
	public function index() {
		return listByDate( Date.now() );
	}
	
	function displayEmailList( emailsList:Iterable<UFMailLog>, title:String ) {
		var emails = Lambda.array( emailsList );
		emails.cleverSort( _.date );
		var template = CompileTime.readFile( "/ufront/ufadmin/view/dbmailer-list.html" );
		return UFAdminModule.wrapInLayout( title, template, {
			emails:emails,
			baseUri:baseUri,
			title:title,
		});
	}
	
	@:route("/date/$date/")
	public function listByDate( date:Date ) {
		// Waiting for these functions to make it into the new thx.core
//		var dateSnappedDown = Date.fromTime( date.getTime().snap(Day,Down) );
//		var dateSnappedUp = Date.fromTime( date.getTime().snap(Day,Up) );
		var dateSnappedDown = date;
		var dateSnappedUp = DateTools.delta( date, 24*60*60*1000 );
		var dateStr = date.toString();
		var emails = UFMailLog.manager.search( $date>=dateSnappedDown && $date<=dateSnappedUp, { orderBy: -date } );
		return displayEmailList( emails, 'Emails sent on $dateStr' );
	}
	
	@:route("/to/$address/")
	public function listAllEmailsToAddress( address:String ) {
		var emails = UFMailLog.manager.search( $to==address );
		return displayEmailList( emails, 'Emails sent to $address' );
	}
	
	@:route("/from/$address/")
	public function listAllEmailsFromAddress( address:String ) {
		var emails = UFMailLog.manager.search( $from==address );
		return displayEmailList( emails, 'Emails sent from $address' );
	}
	
	@:route("/email/$id/")
	public function showEmail( id:Int ) {
		var mailLog = UFMailLog.manager.get( id );
		var template = CompileTime.readFile( "/ufront/ufadmin/view/dbmailer-view.html" );
		return UFAdminModule.wrapInLayout( title, template, {
			id: mailLog.id,
			to: mailLog.to,
			from: mailLog.from,
{'fraction_non_alphanumeric': 0.1409723918885903, 'fraction_numerical': 0.002443195699975568, 'mean_word_length': 4.639118457300276, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3625467', 'n_tokens_mistral': 1495, 'n_tokens_neox': 1396, 'n_words': 303}