83 lines
2.3 KiB
C++
83 lines
2.3 KiB
C++
#ifndef DB_H
|
|
#define DB_H
|
|
|
|
#include <cstdint>
|
|
#include <optional>
|
|
#include <string>
|
|
#include <sqlite3.h>
|
|
#include <sys/types.h>
|
|
#include <vector>
|
|
|
|
#include "post.h"
|
|
|
|
void sanitize(std::string& str);
|
|
|
|
std::string generateSecureToken(size_t numBytes);
|
|
|
|
struct User {
|
|
uint64_t id = 0;
|
|
std::string name = "";
|
|
std::string passwordHash = "";
|
|
std::string bio = "";
|
|
|
|
User(uint64_t id, const std::string& namein, const std::string& passwordHash, const std::string& bio) :
|
|
id(id), passwordHash(passwordHash), bio(bio), name(namein) {}
|
|
|
|
User(uint64_t id, std::string namein, const std::string& passwordHash, const std::string& bio, int doSanitize) :
|
|
id(id), passwordHash(passwordHash), bio(bio) {
|
|
sanitize(namein);
|
|
name = namein;
|
|
}
|
|
};
|
|
|
|
struct Message {
|
|
uint64_t id = 0;
|
|
uint64_t sender = 0;
|
|
uint64_t reciever = 0;
|
|
std::time_t timestamp;
|
|
std::string content = "";
|
|
|
|
Message(uint64_t id, uint64_t sender, uint64_t reciever, const std::string& content, std::time_t timestamp) :
|
|
id(id), sender(sender), reciever(reciever), content(content), timestamp(timestamp) {}
|
|
|
|
Message(uint64_t id, uint64_t sender, uint64_t reciever, std::string contentin, std::time_t timestamp, int doSanitize) :
|
|
id(id), sender(sender), reciever(reciever), timestamp(timestamp) {
|
|
sanitize(contentin);
|
|
content = contentin;
|
|
}
|
|
};
|
|
|
|
class Database {
|
|
|
|
sqlite3* db;
|
|
|
|
public:
|
|
Database() = delete;
|
|
Database(const std::string& path);
|
|
|
|
~Database();
|
|
|
|
std::optional<Post> getPost(uint64_t id);
|
|
std::vector<Post> getUserPosts(uint64_t userId);
|
|
std::vector<Post> getTopPosts(uint64_t amount);
|
|
void addPost(const Post& post);
|
|
void addLike(uint64_t postId, uint64_t userId);
|
|
|
|
std::optional<User> getUser(uint64_t id);
|
|
std::optional<User> getUserByName(const std::string& name);
|
|
std::optional<User> getUserByToken(const std::string& token);
|
|
std::optional<std::string> createNewToken(uint64_t id);
|
|
|
|
// this will modify the user to have their user ID
|
|
void addUser(User& user);
|
|
void updateUser(const User& user);
|
|
void invalidateUserSessions(const User& user);
|
|
|
|
std::vector<Message> getMessages(uint64_t userA, uint64_t userB, uint64_t amount);
|
|
// modifies the message to have it's id
|
|
void addMessage(Message& message);
|
|
|
|
};
|
|
|
|
#endif
|