start work on like button, better html sanitisation

This commit is contained in:
2026-07-20 18:26:37 +10:00
parent d015b12277
commit 78aae0714c
4 changed files with 27 additions and 14 deletions

View File

@@ -119,8 +119,6 @@ std::vector<Post> Database::getTopPosts(uint64_t amount) {
int64_t timestamp = sqlite3_column_int64(stmt, 3); int64_t timestamp = sqlite3_column_int64(stmt, 3);
int64_t likes = sqlite3_column_int64(stmt, 4); int64_t likes = sqlite3_column_int64(stmt, 4);
std::cout << userid << std::endl;
// get username from id // get username from id
std::optional<User> user = getUser(userid); std::optional<User> user = getUser(userid);
if (!user.has_value()) { if (!user.has_value()) {

View File

@@ -36,8 +36,6 @@ int main() {
svr.Get("/", [&header, &footer, &database, &data_mutex](const httplib::Request& request, httplib::Response& response) { svr.Get("/", [&header, &footer, &database, &data_mutex](const httplib::Request& request, httplib::Response& response) {
std::cout << "requested /" << std::endl;
std::stringstream ss; std::stringstream ss;
ss << header; ss << header;
@@ -61,8 +59,6 @@ int main() {
svr.Post("/make_post", [&header, &footer, &database, &data_mutex](const httplib::Request& request, httplib::Response& response) { svr.Post("/make_post", [&header, &footer, &database, &data_mutex](const httplib::Request& request, httplib::Response& response) {
std::cout << "making a post at /make_post" << std::endl;
std::string username = request.get_param_value("username"); std::string username = request.get_param_value("username");
std::string password = request.get_param_value("password"); std::string password = request.get_param_value("password");
std::string post = request.get_param_value("post"); std::string post = request.get_param_value("post");

View File

@@ -1,23 +1,38 @@
#include "post.h" #include "post.h"
#include <memory> #include <memory>
#include <regex>
#include <sstream> #include <sstream>
#include <string> #include <string>
#include <ctime> #include <ctime>
#include <maddy/parser.h> #include <maddy/parser.h>
std::regex tags("<[^<]*>"); void replaceAll(std::string& str, const std::string& from, const std::string& to) {
size_t start_pos = 0;
while ((start_pos = str.find(from, start_pos)) != std::string::npos) {
str.replace(start_pos, from.length(), to);
start_pos += to.length();
}
}
Post::Post(const std::string& contentin, const std::string& user) : user(user), time(std::time(nullptr)) { Post::Post(std::string contentin, const std::string& user) : user(user), time(std::time(nullptr)) {
replaceAll(contentin, "&", "&amp;");
replaceAll(contentin, "<", "&lt;");
replaceAll(contentin, ">", "&gt;");
replaceAll(contentin, "\"", "&quot;");
replaceAll(contentin, "'", "&apos;");
// parse the post content markdown // parse the post content markdown
std::stringstream input(std::regex_replace(contentin, tags, "")); std::stringstream input(contentin);
std::shared_ptr<maddy::Parser> parser = std::make_shared<maddy::Parser>(); std::shared_ptr<maddy::Parser> parser = std::make_shared<maddy::Parser>();
content = parser->Parse(input); content = parser->Parse(input);
} }
Post::Post(const std::string& contentin, uint64_t user) : userId(user), time(std::time(nullptr)) { Post::Post(std::string contentin, uint64_t user) : userId(user), time(std::time(nullptr)) {
replaceAll(contentin, "&", "&amp;");
replaceAll(contentin, "<", "&lt;");
replaceAll(contentin, ">", "&gt;");
replaceAll(contentin, "\"", "&quot;");
replaceAll(contentin, "'", "&apos;");
// parse the post content markdown // parse the post content markdown
std::stringstream input(std::regex_replace(contentin, tags, "")); std::stringstream input(contentin);
std::shared_ptr<maddy::Parser> parser = std::make_shared<maddy::Parser>(); std::shared_ptr<maddy::Parser> parser = std::make_shared<maddy::Parser>();
content = parser->Parse(input); content = parser->Parse(input);
} }
@@ -37,6 +52,10 @@ std::string Post::genHtml() const {
ss << content; ss << content;
ss << "<p class='postinfo'>" << likes << " likes</p>"; ss << "<p class='postinfo'>" << likes << " likes</p>";
ss << "<div class='post-buttons'>";
ss << "<button onclick='like(" << id << ")'>Like</button>";
ss << "<button onclick='" << id << "'>Open</button>";
ss << "</div>";
return ss.str(); return ss.str();

View File

@@ -18,8 +18,8 @@ struct Post {
std::string genHtml() const; std::string genHtml() const;
Post(const std::string& contentin, const std::string& user); Post(std::string contentin, const std::string& user);
Post(const std::string& contentin, uint64_t user); Post(std::string contentin, uint64_t user);
// Constructor for DB // Constructor for DB
Post( Post(