Skip to content

Commit

Permalink
Remorph confirmation database into functions in namespace
Browse files Browse the repository at this point in the history
  • Loading branch information
teusbenschop committed May 28, 2024
1 parent 8b925d5 commit 2e1ab00
Show file tree
Hide file tree
Showing 6 changed files with 108 additions and 103 deletions.
20 changes: 9 additions & 11 deletions confirm/worker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,7 @@ void setup (Webserver_Request& webserver_request,
const std::string& query,
const std::string& subsequent_subject, const std::string& subsequent_body)
{
Database_Confirm database_confirm {};
const unsigned int confirmation_id = database_confirm.get_new_id ();
const unsigned int confirmation_id = database::confirm::get_new_id ();
pugi::xml_document document;
pugi::xml_node node = document.append_child ("p");
std::string information;
Expand All @@ -103,7 +102,7 @@ void setup (Webserver_Request& webserver_request,
document.print (output, "", pugi::format_raw);
initial_body.append (output.str ());
email_schedule (mailto, initial_subject, initial_body);
database_confirm.store (confirmation_id, query, mailto, subsequent_subject, subsequent_body, username);
database::confirm::store (confirmation_id, query, mailto, subsequent_subject, subsequent_body, username);
}


Expand All @@ -112,32 +111,31 @@ void setup (Webserver_Request& webserver_request,
bool handle_link (Webserver_Request& webserver_request, std::string& email)
{
// Get the confirmation identifier from the link that was clicked.
std::string web_id = webserver_request.query["id"];
const std::string web_id = webserver_request.query["id"];

// If the identifier was not given, the link was not handled successfully.
if (web_id.empty())
return false;

// Find out from the confirmation database whether the subject line contains an active ID.
// If not, bail out.
Database_Confirm database_confirm {};
const unsigned int id = database_confirm.search_id (web_id);
const unsigned int id = database::confirm::search_id (web_id);
if (id == 0) {
return false;
}

// An active ID was found: Execute the associated database query.
const std::string query = database_confirm.get_query (id);
const std::string query = database::confirm::get_query (id);
webserver_request.database_users()->execute (query);

// Send confirmation mail.
const std::string mailto = database_confirm.get_mail_to (id);
const std::string subject = database_confirm.get_subject (id);
const std::string body = database_confirm.get_body (id);
const std::string mailto = database::confirm::get_mail_to (id);
const std::string subject = database::confirm::get_subject (id);
const std::string body = database::confirm::get_body (id);
email_schedule (mailto, subject, body);

// Delete the confirmation record.
database_confirm.erase (id);
database::confirm::erase (id);

// Notify the manager(s).
confirm::worker::inform_managers (mailto, body);
Expand Down
108 changes: 58 additions & 50 deletions database/confirm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,15 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
#ifdef HAVE_CLOUD


const char * Database_Confirm::filename () // Todo no need for a class here.
{
return "confirm";
}
constexpr const char * filename ("confirm");


namespace database::confirm {


void Database_Confirm::create ()
void create ()
{
SqliteDatabase sql (filename ());
SqliteDatabase sql (filename);
sql.add ("CREATE TABLE IF NOT EXISTS confirm ("
" id integer,"
" query text,"
Expand All @@ -54,13 +54,13 @@ void Database_Confirm::create ()
}


void Database_Confirm::upgrade ()
void upgrade ()
{
// Get the existing columns in the database.
SqliteDatabase sql (filename ());
SqliteDatabase sql (filename);
sql.add ("PRAGMA table_info (confirm);");
std::vector <std::string> columns = sql.query () ["name"];

const std::vector <std::string> columns = sql.query () ["name"];
// Add the column for the username if it's not yet there.
if (!in_array (static_cast<std::string> ("username"), columns)) {
sql.clear ();
Expand All @@ -70,16 +70,16 @@ void Database_Confirm::upgrade ()
}


void Database_Confirm::optimize ()
void optimize ()
{
SqliteDatabase sql (filename ());
SqliteDatabase sql (filename);
sql.add ("VACUUM;");
sql.execute ();
}


// getNewID - returns a new unique confirmation ID as an integer
unsigned int Database_Confirm::get_new_id ()
// Returns a new unique confirmation ID as an integer
unsigned int get_new_id ()
{
unsigned int id = 0;
do {
Expand All @@ -90,23 +90,23 @@ unsigned int Database_Confirm::get_new_id ()


// Returns true if the $id exists
bool Database_Confirm::id_exists (unsigned int id)
bool id_exists (unsigned int id)
{
SqliteDatabase sql (filename ());
SqliteDatabase sql (filename);
sql.add ("SELECT id FROM confirm WHERE id =");
sql.add (static_cast<int>(id));
sql.add (";");
std::vector <std::string> ids = sql.query () ["id"];
const std::vector <std::string> ids = sql.query () ["id"];
return !ids.empty ();
}


// Store a confirmation cycle
void Database_Confirm::store (unsigned int id, std::string query,
std::string to, std::string subject, std::string body,
std::string username)
void store (unsigned int id, const std::string& query,
const std::string& to, const std::string& subject, const std::string& body,
const std::string& username)
{
SqliteDatabase sql (filename ());
SqliteDatabase sql (filename);
sql.add ("INSERT INTO confirm VALUES (");
sql.add (static_cast<int>(id));
sql.add (",");
Expand All @@ -128,13 +128,13 @@ void Database_Confirm::store (unsigned int id, std::string query,

// Search the database for an existing ID in $subject.
// If it exists, it returns the ID number, else it returns 0.
unsigned int Database_Confirm::search_id (std::string subject)
unsigned int search_id (const std::string& subject)
{
SqliteDatabase sql (filename ());
SqliteDatabase sql (filename);
sql.add ("SELECT id FROM confirm;");
std::vector <std::string> ids = sql.query () ["id"];
for (std::string id : ids) {
size_t pos = subject.find (id);
const std::vector <std::string> ids = sql.query () ["id"];
for (const auto& id : ids) {
const size_t pos = subject.find (id);
if (pos != std::string::npos) {
return static_cast<unsigned>(filter::strings::convert_to_int (id));
}
Expand All @@ -144,91 +144,99 @@ unsigned int Database_Confirm::search_id (std::string subject)


// Returns the query for $id.
std::string Database_Confirm::get_query (unsigned int id)
std::string get_query (unsigned int id)
{
SqliteDatabase sql (filename ());
SqliteDatabase sql (filename);
sql.add ("SELECT query FROM confirm WHERE id =");
sql.add (static_cast<int>(id));
sql.add (";");
std::vector <std::string> result = sql.query () ["query"];
if (!result.empty ()) return result [0];
const std::vector <std::string> result = sql.query () ["query"];
if (!result.empty ())
return result.front();
return std::string();
}


// Returns the To: address for $id.
std::string Database_Confirm::get_mail_to (unsigned int id)
std::string get_mail_to (unsigned int id)
{
SqliteDatabase sql (filename ());
SqliteDatabase sql (filename);
sql.add ("SELECT mailto FROM confirm WHERE id =");
sql.add (static_cast<int>(id));
sql.add (";");
std::vector <std::string> result = sql.query () ["mailto"];
if (!result.empty ()) return result [0];
const std::vector <std::string> result = sql.query () ["mailto"];
if (!result.empty ())
return result.front();
return std::string();
}


// Returns the Subject: for $id.
std::string Database_Confirm::get_subject (unsigned int id)
std::string get_subject (unsigned int id)
{
SqliteDatabase sql (filename ());
SqliteDatabase sql (filename);
sql.add ("SELECT subject FROM confirm WHERE id =");
sql.add (static_cast<int>(id));
sql.add (";");
std::vector <std::string> result = sql.query () ["subject"];
if (!result.empty ()) return result [0];
const std::vector <std::string> result = sql.query () ["subject"];
if (!result.empty ())
return result.front();
return std::string();
}


// Returns the email's body for $id.
std::string Database_Confirm::get_body (unsigned int id)
std::string get_body (unsigned int id)
{
SqliteDatabase sql (filename ());
SqliteDatabase sql (filename);
sql.add ("SELECT body FROM confirm WHERE id =");
sql.add (static_cast<int>(id));
sql.add (";");
std::vector <std::string> result = sql.query () ["body"];
if (!result.empty ()) return result [0];
const std::vector <std::string> result = sql.query () ["body"];
if (!result.empty ())
return result.front();
return std::string();
}


// Returns the username for $id.
std::string Database_Confirm::get_username (unsigned int id) // Test return valid and invalid username.
std::string get_username (unsigned int id) // Test return valid and invalid username.
{
SqliteDatabase sql (filename ());
SqliteDatabase sql (filename);
sql.add ("SELECT username FROM confirm WHERE id =");
sql.add (static_cast<int>(id));
sql.add (";");
std::vector <std::string> result = sql.query () ["username"];
if (!result.empty ()) return result [0];
const std::vector <std::string> result = sql.query () ["username"];
if (!result.empty ())
return result.front();
return std::string();
}


// Deletes $id from the table.
void Database_Confirm::erase (unsigned int id)
void erase (unsigned int id)
{
SqliteDatabase sql (filename ());
SqliteDatabase sql (filename);
sql.add ("DELETE FROM confirm WHERE id =");
sql.add (static_cast<int>(id));
sql.add (";");
sql.execute ();
}


void Database_Confirm::trim ()
void trim ()
{
// Leave entries for no more than 30 days.
const int time = filter::date::seconds_since_epoch () - 2592000;
SqliteDatabase sql (filename ());
SqliteDatabase sql (filename);
sql.add ("DELETE FROM confirm WHERE timestamp <");
sql.add (time);
sql.add (";");
sql.execute ();
}


}


#endif
42 changes: 22 additions & 20 deletions database/confirm.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,26 +25,28 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
#ifdef HAVE_CLOUD


class Database_Confirm
{
public:
void create ();
void upgrade ();
void optimize ();
unsigned int get_new_id ();
bool id_exists (unsigned int id);
void store (unsigned int id, std::string query, std::string to, std::string subject, std::string body, std::string username);
unsigned int search_id (std::string subject);
std::string get_query (unsigned int id);
std::string get_mail_to (unsigned int id);
std::string get_subject (unsigned int id);
std::string get_body (unsigned int id);
std::string get_username (unsigned int id);
void erase (unsigned int id);
void trim ();
private:
const char * filename ();
};
namespace database::confirm {


void create ();
void upgrade ();
void optimize ();
unsigned int get_new_id ();
bool id_exists (unsigned int id);
void store (unsigned int id, const std::string& query,
const std::string& to, const std::string& subject, const std::string& body,
const std::string& username);
unsigned int search_id (const std::string& subject);
std::string get_query (unsigned int id);
std::string get_mail_to (unsigned int id);
std::string get_subject (unsigned int id);
std::string get_body (unsigned int id);
std::string get_username (unsigned int id);
void erase (unsigned int id);
void trim ();


}


#endif
Expand Down
5 changes: 2 additions & 3 deletions database/maintenance.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,8 @@ void database_maintenance ()


#ifdef HAVE_CLOUD
Database_Confirm database_confirm;
database_confirm.trim ();
database_confirm.optimize ();
database::confirm::trim ();
database::confirm::optimize ();
#endif


Expand Down
5 changes: 2 additions & 3 deletions setup/logic.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -229,9 +229,8 @@ void setup_initialize_data ()
setup_generate_locale_databases (false);
#ifdef HAVE_CLOUD
config_globals_setup_message = "confirmations";
Database_Confirm database_confirm;
database_confirm.create ();
database_confirm.upgrade();
database::confirm::create ();
database::confirm::upgrade();
#endif
config_globals_setup_message = "jobs";
Database_Jobs database_jobs = Database_Jobs ();
Expand Down
Loading

0 comments on commit 2e1ab00

Please sign in to comment.