Skip to content

Commit 20215a5

Browse files
committed
Add support for deleting comments
1 parent 66cf2ed commit 20215a5

4 files changed

Lines changed: 386 additions & 8 deletions

File tree

include/ff.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -412,6 +412,8 @@ namespace ff {
412412
limhamn::http::server::response handle_api_rate_file_endpoint(const limhamn::http::server::request& request, database& db);
413413
limhamn::http::server::response handle_api_comment_forwarder_endpoint(const limhamn::http::server::request& request, database& db);
414414
limhamn::http::server::response handle_api_comment_file_endpoint(const limhamn::http::server::request& request, database& db);
415+
limhamn::http::server::response handle_api_delete_comment_forwarder_endpoint(const limhamn::http::server::request& request, database& db);
416+
limhamn::http::server::response handle_api_delete_comment_file_endpoint(const limhamn::http::server::request& request, database& db);
415417
limhamn::http::server::response handle_api_stay_logged_in(const limhamn::http::server::request& request, database& db);
416418
limhamn::http::server::response handle_api_try_logout_endpoint(const limhamn::http::server::request& request, database& db);
417419
} // namespace ff

js/ff.js

Lines changed: 61 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3131,6 +3131,52 @@ function post_comment_file(id, comment_str) {
31313131
});
31323132
}
31333133

3134+
function delete_comment_file(id, comment_id) {
3135+
const json = {
3136+
file_identifier: id,
3137+
comment_identifier: comment_id
3138+
};
3139+
3140+
fetch('/api/delete_comment_file', {
3141+
method: 'POST',
3142+
headers: {
3143+
'Content-Type': 'application/json',
3144+
},
3145+
body: JSON.stringify(json),
3146+
})
3147+
.then(response => {
3148+
if (response.status === 204) {
3149+
show_file(id);
3150+
}
3151+
})
3152+
.catch((error) => {
3153+
console.error('Error:', error);
3154+
});
3155+
}
3156+
3157+
function delete_comment_forwarder(id, comment_id) {
3158+
const json = {
3159+
forwarder_identifier: id,
3160+
comment_identifier: comment_id
3161+
};
3162+
3163+
fetch('/api/delete_comment_forwarder', {
3164+
method: 'POST',
3165+
headers: {
3166+
'Content-Type': 'application/json',
3167+
},
3168+
body: JSON.stringify(json),
3169+
})
3170+
.then(response => {
3171+
if (response.status === 204) {
3172+
show_forwarder(id);
3173+
}
3174+
})
3175+
.catch((error) => {
3176+
console.error('Error:', error);
3177+
});
3178+
}
3179+
31343180
function update_stars_for_file(id, stars) {
31353181
const json = {
31363182
file_identifier: id,
@@ -3745,7 +3791,7 @@ async function draw_article(type, forwarder, id) {
37453791
const next = reviews.slice(index, index + bsize);
37463792
index += bsize;
37473793

3748-
for (const review of next) {
3794+
for (const [comment_id, review] of next.entries()) {
37493795
const profile = await get_profile_for_user(review.username);
37503796

37513797
const comment_div = document.createElement('div');
@@ -3774,6 +3820,20 @@ async function draw_article(type, forwarder, id) {
37743820
comment_author.className = 'view_floating_window_comment_author';
37753821
comment_author.innerHTML = `<strong>${review.username} </strong>`;
37763822

3823+
if (comment_author === get_cookie("username") || get_cookie("user_type") === '1') {
3824+
const delete_button = document.createElement('button');
3825+
delete_button.className = 'view_floating_window_comment_delete';
3826+
delete_button.innerHTML = '<i class="fa-solid fa-trash"></i>';
3827+
delete_button.onclick = () => {
3828+
if (type === Forwarder) {
3829+
delete_comment_forwarder(id, comment_id);
3830+
} else {
3831+
delete_comment_file(id, comment_id);
3832+
}
3833+
};
3834+
comment_author.appendChild(delete_button);
3835+
}
3836+
37773837
const comment_date = document.createElement('span');
37783838
comment_date.className = 'view_floating_window_comment_date';
37793839
comment_date.textContent = new Date(review.timestamp).toLocaleString();

src/ff.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -278,6 +278,8 @@ void ff::start_server() {
278278
{"/api/rate_file", ff::handle_api_rate_file_endpoint},
279279
{"/api/comment_forwarder", ff::handle_api_comment_forwarder_endpoint},
280280
{"/api/comment_file", ff::handle_api_comment_file_endpoint},
281+
{"/api/delete_comment_forwarder", ff::handle_api_delete_comment_forwarder_endpoint},
282+
{"/api/delete_comment_file", ff::handle_api_delete_comment_file_endpoint},
281283
{"/api/update_profile", ff::handle_api_update_profile},
282284
{"/api/get_profile", ff::handle_api_get_profile},
283285
{"/api/create_announcement", ff::handle_api_create_announcement},

0 commit comments

Comments
 (0)