Skip to content

Commit 1b1cac6

Browse files
committed
Finalize API for posts and topics
1 parent 9ef12e2 commit 1b1cac6

File tree

10 files changed

+1886
-6
lines changed

10 files changed

+1886
-6
lines changed

.clang-format

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
---
2+
Language: Cpp
3+
BasedOnStyle: LLVM
4+
IndentWidth: 4
5+
TabWidth: 4
6+
UseTab: Always
7+
BreakBeforeBraces: Attach
8+
PointerAlignment: Left
9+
AllowShortIfStatementsOnASingleLine: false
10+
AllowShortFunctionsOnASingleLine: Inline
11+
AlwaysBreakTemplateDeclarations: Yes
12+
SpacesInParentheses: false
13+
SpaceBeforeParens: ControlStatements
14+
SpaceAfterCStyleCast: true
15+
KeepEmptyLinesAtTheStartOfBlocks: false
16+
ColumnLimit: 100
17+
AlignConsecutiveAssignments: true
18+
AlignConsecutiveDeclarations: true
19+
AlignTrailingComments: true
20+
IncludeBlocks: Regroup
21+
ReflowComments: true
22+
...

css/ff.css

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,10 @@
3636
background-color: #000000;
3737
color: #fff;
3838
}
39+
#forum-button, #forum-window {
40+
background-color: #f0f0f0;
41+
color: #000;
42+
}
3943
#sandbox-button, #sandbox-window, #file-window {
4044
background-color: #c4c4c4;
4145
color: #000;
@@ -203,7 +207,7 @@ body {
203207
min-height: 60%;
204208
}
205209

206-
#view-window, #file-window {
210+
#view-window, #file-window, #forum-window {
207211
min-width: 70%;
208212
min-height: 70%;
209213
}

http/create_topic.http

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
POST /api/create_topic HTTP/1.1
2+
Host: localhost:8080
3+
Content-Type: application/json
4+
5+
{
6+
"title": "New Topic",
7+
"description": "This is a new topic created for testing purposes.",
8+
9+
"username": "jacob",
10+
"key": "baR5ZE7v07z5MygYKlHuWUPJbB0pDDkfmLFNbMuna6lrrwxOH96zQXJojUrUtHq2"
11+
}

http/rate_forwarder.http

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,5 @@ Content-Type: application/json
55
"forwarder_id": "jRF5k1YB",
66
"rating": 5,
77
"username": "jacob",
8-
"key": "DQFLbDpU8tmK0uWrVi3vxXeL0iVCOf5PhXH9YgAz9wpOBaYOmovXBBvE7aeLLHVO"
8+
"key": "baR5ZE7v07z5MygYKlHuWUPJbB0pDDkfmLFNbMuna6lrrwxOH96zQXJojUrUtHq2"
99
}

include/ff.hpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,7 @@ namespace ff {
116116
bool cache_exists{false};
117117
bool convert_images_to_webp{true};
118118
bool convert_videos_to_webm{false};
119+
bool topics_require_admin{false};
119120
};
120121

121122
enum class AccountCreationStatus {
@@ -418,4 +419,18 @@ namespace ff {
418419
limhamn::http::server::response handle_api_delete_comment_file_endpoint(const limhamn::http::server::request& request, database& db);
419420
limhamn::http::server::response handle_api_stay_logged_in(const limhamn::http::server::request& request, database& db);
420421
limhamn::http::server::response handle_api_try_logout_endpoint(const limhamn::http::server::request& request, database& db);
422+
423+
limhamn::http::server::response handle_api_create_post(const limhamn::http::server::request& request, database& db);
424+
limhamn::http::server::response handle_api_delete_post(const limhamn::http::server::request& request, database& db);
425+
limhamn::http::server::response handle_api_edit_post(const limhamn::http::server::request& request, database& db);
426+
limhamn::http::server::response handle_api_close_post(const limhamn::http::server::request& request, database& db);
427+
limhamn::http::server::response handle_api_get_posts(const limhamn::http::server::request& request, database& db);
428+
limhamn::http::server::response handle_api_comment_post(const limhamn::http::server::request& request, database& db);
429+
limhamn::http::server::response handle_api_delete_comment_post(const limhamn::http::server::request& request, database& db);
430+
431+
limhamn::http::server::response handle_api_create_topic(const limhamn::http::server::request& request, database& db);
432+
limhamn::http::server::response handle_api_delete_topic(const limhamn::http::server::request& request, database& db);
433+
limhamn::http::server::response handle_api_get_topics(const limhamn::http::server::request& request, database& db);
434+
limhamn::http::server::response handle_api_edit_topic(const limhamn::http::server::request& request, database& db);
435+
limhamn::http::server::response handle_api_close_topic(const limhamn::http::server::request& request, database& db);
421436
} // namespace ff

js/ff.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5210,6 +5210,13 @@ const generate_stars = (n, w) => {
52105210
}
52115211
}
52125212

5213+
function show_forum_index() {
5214+
set_path('/topic');
5215+
hide_initial();
5216+
5217+
const forum = create_window('forum-window');
5218+
}
5219+
52135220
function show_credits() {
52145221
set_path('/');
52155222
hide_initial();
@@ -5501,6 +5508,13 @@ function init_page() {
55015508
id: "sandbox-button",
55025509
onclick: "play_click(); show_sandbox()"
55035510
}))
5511+
list.push(get_link_box({
5512+
title: "Forum",
5513+
description: "Check out the Forwarder Factory forum.",
5514+
background_color: "",
5515+
id: "forum-button",
5516+
onclick: "play_click(); show_forum_index()",
5517+
}))
55045518

55055519
if (get_cookie("username") === null) {
55065520
list.push(get_link_box({
@@ -5629,6 +5643,7 @@ document.addEventListener('DOMContentLoaded', async () => {
56295643
if (get_path() === "/admin" && is_logged_in()) show_admin();
56305644
if (get_path() === "/admin" && !is_logged_in()) show_login();
56315645
if (get_path() === "/logout" && is_logged_in()) show_logout();
5646+
if (get_path() === "/topic") show_forum_index();
56325647

56335648
if (get_path().startsWith("/view/")) {
56345649
const id = get_path().substring(6);

src/config.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ ff::Settings ff::load_settings(const std::string& _config_file) {
7979
if (config["upload"]["convert_images_to_webp"]) settings.convert_images_to_webp = config["upload"]["convert_images_to_webp"].as<bool>();
8080
if (config["upload"]["convert_videos_to_webm"]) settings.convert_videos_to_webm = config["upload"]["convert_videos_to_webm"].as<bool>();
8181
if (config["download"]["preview_files"]) settings.preview_files = config["download"]["preview_files"].as<bool>();
82+
if (config["topic"]["topics_require_admin"]) settings.topics_require_admin = config["topic"]["topics_require_admin"].as<bool>();
8283
if (config["smtp"]["server"]) settings.smtp_server = config["smtp"]["server"].as<std::string>();
8384
if (config["smtp"]["port"]) settings.smtp_port = config["smtp"]["port"].as<int>();
8485
if (config["smtp"]["username"]) settings.email_username = config["smtp"]["username"].as<std::string>();
@@ -278,6 +279,10 @@ std::string ff::generate_default_config() {
278279
ss << " convert_images_to_webp: " << (ff::settings.convert_images_to_webp ? "true" : "false") << "\n";
279280
ss << " convert_videos_to_webm: " << (ff::settings.convert_videos_to_webm ? "true" : "false") << "\n";
280281
ss << "\n";
282+
ss << "# Topic options:\n";
283+
ss << "# topics_require_admin: Whether to require admin when creating topics.\n";
284+
ss << "topic:\n";
285+
ss << " topics_require_admin: " << (ff::settings.topics_require_admin ? "true" : "false") << "\n";
281286
ss << "# Download options:\n";
282287
ss << "# preview_files: Whether to preview files in the browser when downloading them.\n";
283288
ss << "download:\n";

src/database.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,20 @@ void ff::setup_database(database& database) {
6363
throw std::runtime_error{"Error creating the general table."};
6464
}
6565

66+
// id: the topic id
67+
// identifier: the topic identifier
68+
// json: the json of the topic (including title, description, etc.)
69+
if (!database.exec("CREATE TABLE IF NOT EXISTS topics (" + primary + ", identifier TEXT NOT NULL, json TEXT NOT NULL);")) {
70+
throw std::runtime_error{"Error creating the topics table."};
71+
}
72+
73+
// id: the post id
74+
// identifier: the post identifier
75+
// json: the json of the post (including content, author, etc.)
76+
if (!database.exec("CREATE TABLE IF NOT EXISTS posts (" + primary + ", identifier INTEGER NOT NULL, json TEXT NOT NULL);")) {
77+
throw std::runtime_error{"Error creating the posts table."};
78+
}
79+
6680
const auto query = database.query("SELECT * FROM general;");
6781
if (query.empty()) {
6882
nlohmann::json json;

src/ff.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -290,6 +290,20 @@ void ff::start_server() {
290290
{"/api/try_logout", ff::handle_api_try_logout_endpoint},
291291
{"/api/delete_forwarder", ff::handle_api_delete_forwarder_endpoint},
292292
{"/api/delete_file", ff::handle_api_delete_file_endpoint},
293+
294+
{"/api/create_post", ff::handle_api_create_post},
295+
{"/api/delete_post", ff::handle_api_delete_post},
296+
{"/api/edit_post", ff::handle_api_edit_post},
297+
{"/api/close_post", ff::handle_api_close_post},
298+
{"/api/get_posts", ff::handle_api_get_posts},
299+
{"/api/comment_post", ff::handle_api_comment_post},
300+
{"/api/delete_comment_post", ff::handle_api_delete_comment_post},
301+
{"/api/create_topic", ff::handle_api_create_topic},
302+
{"/api/delete_topic", ff::handle_api_delete_topic},
303+
{"/api/get_topics", ff::handle_api_get_topics},
304+
{"/api/edit_topic", ff::handle_api_edit_topic},
305+
{"/api/close_topic", ff::handle_api_close_topic},
306+
//{"/api/pin_post_to_topic", ff::handle_api_pin_post_to_topic},
293307
};
294308
const std::unordered_map<std::string, std::function<limhamn::http::server::response(const limhamn::http::server::request&, ff::database&)>> setup_handlers{
295309
{virtual_favicon_path, ff::handle_virtual_favicon_endpoint},
@@ -377,6 +391,10 @@ void ff::start_server() {
377391
return handlers.at("/")(request, *database);
378392
} else if (file.find("/profile/") != std::string::npos) {
379393
return handlers.at("/")(request, *database);
394+
} else if (file.find("/topic") != std::string::npos) {
395+
return handlers.at("/")(request, *database);
396+
} else if (file.find("/post/") != std::string::npos) {
397+
return handlers.at("/")(request, *database);
380398
}
381399

382400
// handle activation URLs

0 commit comments

Comments
 (0)