2026-07-20 10:57:07 +10:00
|
|
|
#include "post.h"
|
|
|
|
|
#include <memory>
|
|
|
|
|
#include <sstream>
|
|
|
|
|
#include <string>
|
|
|
|
|
#include <ctime>
|
|
|
|
|
#include <maddy/parser.h>
|
|
|
|
|
|
2026-07-20 18:26:37 +10:00
|
|
|
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();
|
|
|
|
|
}
|
|
|
|
|
}
|
2026-07-20 17:49:29 +10:00
|
|
|
|
2026-07-20 18:26:37 +10:00
|
|
|
Post::Post(std::string contentin, const std::string& user) : user(user), time(std::time(nullptr)) {
|
|
|
|
|
replaceAll(contentin, "&", "&");
|
|
|
|
|
replaceAll(contentin, "<", "<");
|
|
|
|
|
replaceAll(contentin, ">", ">");
|
|
|
|
|
replaceAll(contentin, "\"", """);
|
|
|
|
|
replaceAll(contentin, "'", "'");
|
2026-07-20 10:57:07 +10:00
|
|
|
// parse the post content markdown
|
2026-07-20 18:26:37 +10:00
|
|
|
std::stringstream input(contentin);
|
2026-07-20 10:57:07 +10:00
|
|
|
std::shared_ptr<maddy::Parser> parser = std::make_shared<maddy::Parser>();
|
|
|
|
|
content = parser->Parse(input);
|
|
|
|
|
}
|
|
|
|
|
|
2026-07-20 18:26:37 +10:00
|
|
|
Post::Post(std::string contentin, uint64_t user) : userId(user), time(std::time(nullptr)) {
|
|
|
|
|
replaceAll(contentin, "&", "&");
|
|
|
|
|
replaceAll(contentin, "<", "<");
|
|
|
|
|
replaceAll(contentin, ">", ">");
|
|
|
|
|
replaceAll(contentin, "\"", """);
|
|
|
|
|
replaceAll(contentin, "'", "'");
|
2026-07-20 10:57:07 +10:00
|
|
|
// parse the post content markdown
|
2026-07-20 18:26:37 +10:00
|
|
|
std::stringstream input(contentin);
|
2026-07-20 10:57:07 +10:00
|
|
|
std::shared_ptr<maddy::Parser> parser = std::make_shared<maddy::Parser>();
|
|
|
|
|
content = parser->Parse(input);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
std::string Post::genHtml() const {
|
|
|
|
|
std::stringstream ss;
|
|
|
|
|
ss << "<hr>\n";
|
|
|
|
|
|
|
|
|
|
// do time
|
|
|
|
|
char buf[256];
|
|
|
|
|
struct tm ts;
|
|
|
|
|
ts = *localtime(&time);
|
|
|
|
|
strftime(buf, sizeof(buf), "%a %Y-%m-%d %H:%M:%S %Z", &ts);
|
|
|
|
|
|
2026-07-20 11:48:27 +10:00
|
|
|
ss << "<p class='postinfo'><i>" << user << "</i> posted this at " << buf << "</p>\n";
|
2026-07-20 10:57:07 +10:00
|
|
|
|
|
|
|
|
ss << content;
|
|
|
|
|
|
2026-07-20 11:48:27 +10:00
|
|
|
ss << "<p class='postinfo'>" << likes << " likes</p>";
|
2026-07-20 18:26:37 +10:00
|
|
|
ss << "<div class='post-buttons'>";
|
2026-07-20 19:02:04 +10:00
|
|
|
ss << "<button onclick='like(\"" << id << "\")' id='likebutton" << id << "'>Like</button>";
|
|
|
|
|
ss << "<button onclick='window.location.href = \"/posts/" << id << "\"'>Open</button>";
|
2026-07-20 18:26:37 +10:00
|
|
|
ss << "</div>";
|
2026-07-20 10:57:07 +10:00
|
|
|
|
|
|
|
|
return ss.str();
|
|
|
|
|
|
|
|
|
|
}
|