186 lines
5.5 KiB
C++
186 lines
5.5 KiB
C++
#include "chat_repo.hpp"
|
|
#include "cursor.hpp"
|
|
|
|
#include <pqxx/pqxx>
|
|
|
|
#include <chrono>
|
|
#include <ctime>
|
|
#include <iomanip>
|
|
#include <random>
|
|
#include <sstream>
|
|
#include <string>
|
|
|
|
namespace {
|
|
|
|
std::string make_uuid() {
|
|
static std::mt19937_64 rng{std::random_device{}()};
|
|
std::uniform_int_distribution<uint32_t> d16(0, 0xffff);
|
|
std::uniform_int_distribution<uint32_t> 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<std::time_t>(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<std::chrono::seconds>(
|
|
now.time_since_epoch()).count();
|
|
return std::to_string(secs);
|
|
}
|
|
|
|
Chat row_to_chat(const pqxx::row& r) {
|
|
Chat c;
|
|
c.id = r["id"].as<std::string>();
|
|
c.title = r["title"].as<std::string>();
|
|
c.created_at = epoch_to_rfc3339(r["created_at_epoch"].as<long long>());
|
|
return c;
|
|
}
|
|
|
|
std::vector<std::string> load_member_ids(pqxx::work& tx,
|
|
const std::string& chat_id) {
|
|
std::vector<std::string> ids;
|
|
auto r = tx.exec(
|
|
"SELECT user_id FROM chat_members WHERE chat_id = $1 ORDER BY user_id",
|
|
pqxx::params{chat_id});
|
|
ids.reserve(r.size());
|
|
for (const auto& row : r) {
|
|
ids.push_back(row["user_id"].as<std::string>());
|
|
}
|
|
return ids;
|
|
}
|
|
|
|
constexpr const char* kChatSelect =
|
|
"id, title, "
|
|
"EXTRACT(EPOCH FROM created_at)::bigint AS created_at_epoch";
|
|
|
|
}
|
|
|
|
std::optional<Chat> ChatRepo::create(const std::string& title,
|
|
const std::vector<std::string>& member_ids) {
|
|
auto lease = pool_.acquire();
|
|
pqxx::work tx(*lease);
|
|
|
|
const std::string chat_id = make_uuid();
|
|
const std::string created = now_epoch_str();
|
|
|
|
try {
|
|
tx.exec(
|
|
"INSERT INTO chats (id, title, created_at) "
|
|
"VALUES ($1, $2, to_timestamp($3::bigint))",
|
|
pqxx::params{chat_id, title, created});
|
|
|
|
for (const auto& uid : member_ids) {
|
|
tx.exec(
|
|
"INSERT INTO chat_members (chat_id, user_id) VALUES ($1, $2)",
|
|
pqxx::params{chat_id, uid});
|
|
}
|
|
} catch (const pqxx::foreign_key_violation&) {
|
|
tx.abort();
|
|
return std::nullopt;
|
|
} catch (const pqxx::unique_violation&) {
|
|
tx.abort();
|
|
return std::nullopt;
|
|
}
|
|
|
|
auto r = tx.exec(
|
|
"SELECT " + std::string(kChatSelect) + " FROM chats WHERE id = $1",
|
|
pqxx::params{chat_id});
|
|
Chat c = row_to_chat(r[0]);
|
|
c.member_ids = load_member_ids(tx, chat_id);
|
|
tx.commit();
|
|
return c;
|
|
}
|
|
|
|
std::optional<Chat> ChatRepo::find_by_id(const std::string& id) {
|
|
auto lease = pool_.acquire();
|
|
pqxx::work tx(*lease);
|
|
auto r = tx.exec(
|
|
"SELECT " + std::string(kChatSelect) + " FROM chats WHERE id = $1",
|
|
pqxx::params{id});
|
|
if (r.empty()) {
|
|
tx.commit();
|
|
return std::nullopt;
|
|
}
|
|
Chat c = row_to_chat(r[0]);
|
|
c.member_ids = load_member_ids(tx, id);
|
|
tx.commit();
|
|
return c;
|
|
}
|
|
|
|
|
|
std::optional<ChatPage> ChatRepo::list_for_user(
|
|
const std::string& user_id,
|
|
int limit,
|
|
const std::optional<std::string>& cursor) {
|
|
|
|
std::optional<Cursor> 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 chats WHERE id = $1 AND id IN "
|
|
"(SELECT chat_id FROM chat_members WHERE user_id = $2)",
|
|
pqxx::params{cur->id, user_id});
|
|
if (owner_check.empty()) {
|
|
tx.commit();
|
|
return std::nullopt;
|
|
}
|
|
}
|
|
|
|
std::string sql =
|
|
"SELECT " + std::string(kChatSelect) + ", " + kCreatedUsSql + " "
|
|
"FROM chats "
|
|
"WHERE id IN (SELECT chat_id FROM chat_members WHERE user_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{user_id, limit + 1, cur->created_us, cur->id});
|
|
} else {
|
|
r = tx.exec(sql, pqxx::params{user_id, limit + 1});
|
|
}
|
|
|
|
ChatPage page;
|
|
const bool has_more = (static_cast<int>(r.size()) > limit);
|
|
const int take = has_more ? limit : static_cast<int>(r.size());
|
|
page.items.reserve(take);
|
|
for (int i = 0; i < take; ++i) {
|
|
Chat c = row_to_chat(r[static_cast<std::size_t>(i)]);
|
|
c.member_ids = load_member_ids(tx, c.id);
|
|
page.items.push_back(std::move(c));
|
|
}
|
|
|
|
if (has_more && take > 0) {
|
|
const auto& last = r[static_cast<std::size_t>(take - 1)];
|
|
page.next_cursor = make_cursor(last["created_us"].as<std::string>(),
|
|
last["id"].as<std::string>());
|
|
}
|
|
|
|
tx.commit();
|
|
return page;
|
|
} |