From 84034ed3dc19f718dcc93a35dbf4c840a55efb1b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?louiz=E2=80=99?= Date: Fri, 23 Jun 2017 00:16:24 +0200 Subject: =?UTF-8?q?Use=20a=20db=20roster=20to=20manage=20biboumi=E2=80=99s?= =?UTF-8?q?=20presence=20with=20the=20contacts?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/database/database.cpp | 51 ++++++++++++++++++++++++++++++++++++++++++++++- src/database/database.hpp | 15 ++++++++++++++ 2 files changed, 65 insertions(+), 1 deletion(-) (limited to 'src/database') diff --git a/src/database/database.cpp b/src/database/database.cpp index 92f7682..85c675e 100644 --- a/src/database/database.cpp +++ b/src/database/database.cpp @@ -13,6 +13,8 @@ Database::MucLogLineTable Database::muc_log_lines("MucLogLine_"); Database::GlobalOptionsTable Database::global_options("GlobalOptions_"); Database::IrcServerOptionsTable Database::irc_server_options("IrcServerOptions_"); Database::IrcChannelOptionsTable Database::irc_channel_options("IrcChannelOptions_"); +Database::RosterTable Database::roster("roster"); + void Database::open(const std::string& filename) { @@ -36,6 +38,8 @@ void Database::open(const std::string& filename) Database::irc_server_options.upgrade(Database::db); Database::irc_channel_options.create(Database::db); Database::irc_channel_options.upgrade(Database::db); + Database::roster.create(Database::db); + Database::roster.upgrade(Database::db); } @@ -177,6 +181,51 @@ std::vector Database::get_muc_logs(const std::string& owne return {result.crbegin(), result.crend()}; } +void Database::add_roster_item(const std::string& local, const std::string& remote) +{ + auto roster_item = Database::roster.row(); + + roster_item.col() = local; + roster_item.col() = remote; + + roster_item.save(Database::db); +} + +void Database::delete_roster_item(const std::string& local, const std::string& remote) +{ + Query query("DELETE FROM "s + Database::roster.get_name()); + query << " WHERE " << Database::RemoteJid{} << "=" << remote << \ + " AND " << Database::LocalJid{} << "=" << local; + + query.execute(Database::db); +} + +bool Database::has_roster_item(const std::string& local, const std::string& remote) +{ + auto query = Database::roster.select(); + query.where() << Database::LocalJid{} << "=" << local << \ + " and " << Database::RemoteJid{} << "=" << remote; + + auto res = query.execute(Database::db); + + return !res.empty(); +} + +std::vector Database::get_contact_list(const std::string& local) +{ + auto query = Database::roster.select(); + query.where() << Database::LocalJid{} << "=" << local; + + return query.execute(Database::db); +} + +std::vector Database::get_full_roster() +{ + auto query = Database::roster.select(); + + return query.execute(Database::db); +} + void Database::close() { sqlite3_close_v2(Database::db); @@ -192,4 +241,4 @@ std::string Database::gen_uuid() return uuid_str; } -#endif \ No newline at end of file +#endif diff --git a/src/database/database.hpp b/src/database/database.hpp index b5f2ff0..c00c938 100644 --- a/src/database/database.hpp +++ b/src/database/database.hpp @@ -72,6 +72,11 @@ class Database struct Persistent: Column { static constexpr auto name = "persistent_"; Persistent(): Column(false) {} }; + struct LocalJid: Column { static constexpr auto name = "local"; }; + + struct RemoteJid: Column { static constexpr auto name = "remote"; }; + + using MucLogLineTable = Table; using MucLogLine = MucLogLineTable::RowType; @@ -84,6 +89,9 @@ class Database using IrcChannelOptionsTable = Table; using IrcChannelOptions = IrcChannelOptionsTable::RowType; + using RosterTable = Table; + using RosterItem = RosterTable::RowType; + Database() = default; ~Database() = default; @@ -109,6 +117,12 @@ class Database static std::string store_muc_message(const std::string& owner, const std::string& chan_name, const std::string& server_name, time_point date, const std::string& body, const std::string& nick); + static void add_roster_item(const std::string& local, const std::string& remote); + static bool has_roster_item(const std::string& local, const std::string& remote); + static void delete_roster_item(const std::string& local, const std::string& remote); + static std::vector get_contact_list(const std::string& local); + static std::vector get_full_roster(); + static void close(); static void open(const std::string& filename); @@ -123,6 +137,7 @@ class Database static GlobalOptionsTable global_options; static IrcServerOptionsTable irc_server_options; static IrcChannelOptionsTable irc_channel_options; + static RosterTable roster; static sqlite3* db; private: -- cgit v1.2.3