#include "message_repo.hpp" #include "cursor.hpp" #include #include #include #include #include #include #include namespace { std::string make_uuid() { static std::mt19937_64 rng{std::random_device{}()}; std::uniform_int_distribution d16(0, 0xffff); std::uniform_int_distribution d32(0, 0xffffffffu); std::ostringstream out; out << std::hex << std::setfill('0'); out << std::setw(8) << d32(rng); out << '-' << std::setw(4) << d16(rng); out << '-' << std::setw(4) << ((d16(rng) & 0x0fff) | 0x4000); out << '-' << std::setw(4) << ((d16(rng) & 0x3fff) | 0x8000); out << '-' << std::setw(4) << d16(rng) << std::setw(8) << d32(rng); return out.str(); } std::string epoch_to_rfc3339(long long epoch) { const std::time_t t = static_cast(epoch); std::tm tm{}; gmtime_r(&t, &tm); std::ostringstream out; out << std::put_time(&tm, "%Y-%m-%dT%H:%M:%SZ"); return out.str(); } std::string now_epoch_str() { const auto now = std::chrono::system_clock::now(); const auto secs = std::chrono::duration_cast( now.time_since_epoch()).count(); return std::to_string(secs); } Message row_to_message(const pqxx::row& r) { Message m; m.id = r["id"].as(); m.chat_id = r["chat_id"].as(); m.sender_id = r["sender_id"].as(); m.text = r["text"].as(); m.created_at = epoch_to_rfc3339(r["created_at_epoch"].as()); return m; } constexpr const char* kMessageSelect = "id, chat_id, sender_id, text, " "EXTRACT(EPOCH FROM created_at)::bigint AS created_at_epoch"; } std::optional MessageRepo::create(const std::string& chat_id, const std::string& sender_id, const std::string& text) { auto lease = pool_.acquire(); pqxx::work tx(*lease); auto member_check = tx.exec( "SELECT 1 FROM chat_members WHERE chat_id = $1 AND user_id = $2", pqxx::params{chat_id, sender_id}); if (member_check.empty()) { tx.commit(); return std::nullopt; } const std::string id = make_uuid(); const std::string created = now_epoch_str(); auto r = tx.exec( "INSERT INTO messages (id, chat_id, sender_id, text, created_at) " "VALUES ($1, $2, $3, $4, to_timestamp($5::bigint)) " "RETURNING " + std::string(kMessageSelect), pqxx::params{id, chat_id, sender_id, text, created}); tx.commit(); return row_to_message(r[0]); } std::optional MessageRepo::list_page( const std::string& chat_id, int limit, const std::optional& cursor) { std::optional cur; if (cursor) { cur = parse_cursor(*cursor); if (!cur) { return std::nullopt; } } auto lease = pool_.acquire(); pqxx::work tx(*lease); if (cur) { auto owner_check = tx.exec( "SELECT 1 FROM messages WHERE id = $1 AND chat_id = $2", pqxx::params{cur->id, chat_id}); if (owner_check.empty()) { tx.commit(); return std::nullopt; } } std::string sql = "SELECT " + std::string(kMessageSelect) + ", " + kCreatedUsSql + " " "FROM messages " "WHERE chat_id = $1 "; if (cur) { sql += "AND (created_at, id) > (" + std::string(kCursorTimestampSql) + ", $4::uuid) "; } sql += "ORDER BY created_at ASC, id ASC LIMIT $2"; pqxx::result r; if (cur) { r = tx.exec(sql, pqxx::params{chat_id, limit + 1, cur->created_us, cur->id}); } else { r = tx.exec(sql, pqxx::params{chat_id, limit + 1}); } MessagePage page; const bool has_more = (static_cast(r.size()) > limit); const int take = has_more ? limit : static_cast(r.size()); page.items.reserve(take); for (int i = 0; i < take; ++i) { page.items.push_back(row_to_message(r[static_cast(i)])); } if (has_more && take > 0) { const auto& last = r[static_cast(take - 1)]; page.next_cursor = make_cursor(last["created_us"].as(), last["id"].as()); } tx.commit(); return page; }