diff --git a/include/data_structs.h b/include/data_structs.h index cb39887..5e645e0 100644 --- a/include/data_structs.h +++ b/include/data_structs.h @@ -64,6 +64,7 @@ namespace db blt::u64 channelID; blt::u64 time_changed; std::string old_channel_name; + std::string old_channel_topic; }; struct message_t @@ -94,6 +95,7 @@ namespace db { blt::u64 messageID; blt::u64 channelID; + blt::u64 userID; std::string content; }; @@ -159,6 +161,7 @@ namespace db make_column("channelID", &channel_history_t::channelID), make_column("time_changed", &channel_history_t::time_changed), make_column("old_channel_name", &channel_history_t::old_channel_name), + make_column("old_channel_topic", &channel_history_t::old_channel_topic), foreign_key(&channel_history_t::channelID).references(&channel_info_t::channelID), primary_key(&channel_history_t::channelID, &channel_history_t::time_changed)); } @@ -213,10 +216,12 @@ namespace db return make_table("message_deletes", make_column("messageID", &message_deletes_t::messageID), make_column("channelID", &message_deletes_t::channelID), + make_column("userID", &message_deletes_t::userID), make_column("content", &message_deletes_t::content), foreign_key(&message_deletes_t::messageID).references(&message_t::messageID), foreign_key(&message_deletes_t::channelID).references(&channel_info_t::channelID), - primary_key(&message_deletes_t::messageID, &message_deletes_t::channelID)); + foreign_key(&message_deletes_t::userID).references(&user_info_t::userID), + primary_key(&message_deletes_t::messageID, &message_deletes_t::channelID, &message_deletes_t::userID)); } using message_deletes_table_t = decltype(make_message_deletes_table()); diff --git a/src/main.cpp b/src/main.cpp index 5c2cf98..6dfe4a1 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -130,7 +130,24 @@ struct db_obj void commit(const channel_info_t& channel) { - + using namespace sql; + auto existing_channel = db + .select(object(), from(), where(c(&channel_info_t::channelID) == channel.channelID)); + + if (!existing_channel.empty()) + { + for (const auto& v : existing_channel) + { + channel_history_t history; + history.channelID = v.channelID; + history.old_channel_name = v.channel_name; + history.old_channel_topic = v.channel_topic; + history.time_changed = blt::system::getCurrentTimeMilliseconds(); + commit(history); + } + } + + db.replace(channel); } void commit(const channel_history_t& channel) @@ -153,8 +170,21 @@ struct db_obj db.insert(edited); } - void commit(const message_deletes_t& deleted) + void commit(message_deletes_t& deleted) { + using namespace sql; + auto message_content = db.select(columns(&message_t::content, &message_t::userID), from(), + where(c(&message_t::messageID) == deleted.messageID)); + + if (message_content.empty()) + { + BLT_WARN("Unable to delete message when no message exists!"); + return; + } + + deleted.content = std::get<0>(message_content[0]); + deleted.userID = std::get<1>(message_content[0]); + db.insert(deleted); }