From d3e5c4e706223e3337221ca1e067af7d8d1cb175 Mon Sep 17 00:00:00 2001 From: nekobit Date: Wed, 25 May 2022 07:32:54 +0000 Subject: [PATCH] Status deletion FossilOrigin-Name: e29e4c081d48cf724df5597e6cb22d9977a47ba91dd1b66921a4ef8b3fd4bc67 --- Makefile | 2 + dist/js/main.js | 93 +++++++++++++++++++++++++++++++++++++++++++ src/account.c | 7 ++++ src/account.h | 2 +- src/base_page.c | 8 +--- src/main.c | 10 ++++- src/session.h | 4 ++ src/status.c | 21 +++++++--- static/menu_item.html | 6 +++ static/status.html | 1 + 10 files changed, 140 insertions(+), 14 deletions(-) create mode 100644 dist/js/main.js create mode 100644 static/menu_item.html diff --git a/Makefile b/Makefile index e3332a6..5e37c5c 100644 --- a/Makefile +++ b/Makefile @@ -62,6 +62,8 @@ $(PAGES_DIR)/emoji_reactions.chtml: $(PAGES_DIR)/emoji_reactions.html ./filec $< data_emoji_reactions_html > $@ $(PAGES_DIR)/emoji_reaction.chtml: $(PAGES_DIR)/emoji_reaction.html ./filec $< data_emoji_reaction_html > $@ +$(PAGES_DIR)/menu_item.chtml: $(PAGES_DIR)/menu_item.html + ./filec $< data_menu_item_html > $@ $(PAGES_DIR)/test.chtml: $(PAGES_DIR)/test.html ./filec $< data_test_html > $@ $(PAGES_DIR)/notifications_page.chtml: $(PAGES_DIR)/notifications_page.html diff --git a/dist/js/main.js b/dist/js/main.js new file mode 100644 index 0000000..4e77a14 --- /dev/null +++ b/dist/js/main.js @@ -0,0 +1,93 @@ +Element.prototype.insertAfter = function(element) { + element.parentNode.insertBefore(this, element.nextSibling); +}; + +function construct_quick_reply_form(replyid) +{ + let src = document.createElement("form"); + src.action = "/status/create"; + src.method = "post"; + src.enctype = "multipart/form-data"; + src.className = "statusbox-quickreply"; + let hiddeninput = document.createElement("input"); + hiddeninput.type = "hidden"; + hiddeninput.name = "replyid" + hiddeninput.value = replyid; + let statusbox = document.createElement("div"); + statusbox.className = "statusbox statusbox-ani"; + + let textarea = document.createElement("textarea"); + textarea.placeholder = "Just landed in N.Y."; + textarea.rows = 5; + textarea.tabindex = 1; + textarea.name = "content"; + + let statusfooter = document.createElement("div"); + statusfooter.className = "statusfooter"; + let statusfooter_sides = { + left: document.createElement("div"), + right: document.createElement("div"), + } + let select = document.createElement("select"); + let files_input = document.createElement("input"); + statusfooter_sides.left.className = "statusfooter-left"; + select.innerHTML = ` + + + + + + `.trim(); + files_input.type = "file"; + files_input.name = "file"; + files_input.tabindex = 4; + files_input.multiple = ""; + + statusfooter_sides.right.className = "statusfooter-right"; + let submitbtn = document.createElement("input"); + submitbtn.className = "btn post-btn"; + submitbtn.type = "submit"; + submitbtn.value = "Post"; + submitbtn.tabindex = 2; + + + statusfooter_sides.left.appendChild(select); + statusfooter_sides.left.appendChild(files_input); + statusfooter_sides.right.appendChild(submitbtn); + statusfooter.appendChild(statusfooter_sides.left); + statusfooter.appendChild(statusfooter_sides.right); + statusbox.appendChild(textarea); + statusbox.appendChild(statusfooter); + src.appendChild(hiddeninput); + src.appendChild(statusbox); + return src; +} + +function create_reply_form(e) +{ + e.preventDefault(); + let status = e.target.closest(".status"); + + if (status.nextSibling.className === "statusbox-quickreply") + { + status.nextSibling.remove(); + } + else { + let form = construct_quick_reply_form(status.id); + form.insertAfter(status); + + } + + + return false; +} + +// Main +(function() { + let reply_btn = document.getElementsByClassName("reply-btn"); + + for (let i = 0; i < reply_btn.length; ++i) + { + reply_btn[i].onclick = create_reply_form; + } +})(); diff --git a/src/account.c b/src/account.c index 1dfcc0d..32a045d 100644 --- a/src/account.c +++ b/src/account.c @@ -157,6 +157,13 @@ static char* account_scrobbles_cb(struct session* ssn, mastodont_t* api, struct return scrobbles_html; } +void get_account_info(mastodont_t* api, struct session* ssn) +{ + if (mastodont_verify_credentials(api, &(ssn->acct), &(ssn->acct_storage)) == 0) + { + ssn->logged_in = 1; + } +} static void fetch_account_page(struct session* ssn, mastodont_t* api, diff --git a/src/account.h b/src/account.h index 71eba04..c6e753c 100644 --- a/src/account.h +++ b/src/account.h @@ -53,7 +53,7 @@ struct account_page struct mstdnt_relationship* relationship; }; - +void get_account_info(mastodont_t* api, struct session* ssn); char* construct_account_sidebar(struct mstdnt_account* acct, size_t* size); char* construct_account(mastodont_t* api, diff --git a/src/base_page.c b/src/base_page.c index 1569224..09d892f 100644 --- a/src/base_page.c +++ b/src/base_page.c @@ -47,7 +47,6 @@ void render_base_page(struct base_page* page, struct session* ssn, mastodont_t* * main_sidebar_str = NULL, * account_sidebar_str = NULL; // Mastodont, used for notifications sidebar - struct mstdnt_account acct = { 0 }; struct mstdnt_storage storage = { 0 }; struct mstdnt_notification* notifs = NULL; size_t notifs_len = 0; @@ -63,12 +62,7 @@ void render_base_page(struct base_page* page, struct session* ssn, mastodont_t* // If user is logged in if (keystr(ssn->cookies.logged_in) && keystr(ssn->cookies.access_token)) { - if (mastodont_verify_credentials(api, &acct, &storage) == 0) - { - account_sidebar_str = construct_account_sidebar(&acct, NULL); - } - mstdnt_cleanup_account(&acct); - mastodont_storage_cleanup(&storage); // reuse it later + account_sidebar_str = construct_account_sidebar(&(ssn->acct), NULL); // Get / Show notifications on sidebar if (ssn->config.notif_embed) diff --git a/src/main.c b/src/main.c index 3d918b1..783866f 100644 --- a/src/main.c +++ b/src/main.c @@ -120,7 +120,10 @@ int main(void) }, .cookies = {{}}, .post = {{}}, - .query = {{}} + .query = {{}}, + .acct = { 0 }, + .acct_storage = { 0 }, + .logged_in = 0, }; // Load cookies @@ -139,6 +142,9 @@ int main(void) // Read config options load_config(&ssn, &api); + // Load current account information + get_account_info(&api, &ssn); + handle_paths(&ssn, &api, paths, sizeof(paths)/sizeof(paths[0])); // Cleanup @@ -146,6 +152,8 @@ int main(void) if (post_str) free(post_str); if (get_str) free(get_str); free_files(&(keyfile(ssn.post.files))); + if (ssn.logged_in) mstdnt_cleanup_account(&(ssn.acct)); + mastodont_storage_cleanup(&(ssn.acct_storage)); ++run_count; } diff --git a/src/session.h b/src/session.h index c2b6200..47604bd 100644 --- a/src/session.h +++ b/src/session.h @@ -18,6 +18,7 @@ #ifndef SESSION_H #define SESSION_H +#include #include "query.h" #include "local_config.h" #include "cookie.h" @@ -28,6 +29,9 @@ struct session struct get_values query; struct cookie_values cookies; struct local_config config; + int logged_in; + struct mstdnt_account acct; + struct mstdnt_storage acct_storage; }; #endif // SESSION_H diff --git a/src/status.c b/src/status.c index 3f68c6f..9a02892 100644 --- a/src/status.c +++ b/src/status.c @@ -45,6 +45,7 @@ #include "../static/likeboost.chtml" #include "../static/reactions_btn.chtml" #include "../static/interaction_buttons.chtml" +#include "../static/menu_item.chtml" #define ACCOUNT_INTERACTIONS_LIMIT 11 #define NUM_STR "%u" @@ -149,6 +150,8 @@ int try_interact_status(struct session* ssn, mastodont_t* api, char* id) mastodont_pin_status(api, id, &storage, NULL); else if (strcmp(keystr(ssn->post.itype), "mute") == 0) mastodont_mute_conversation(api, id, &storage, NULL); + else if (strcmp(keystr(ssn->post.itype), "delete") == 0) + mastodont_delete_status(api, id, &storage, NULL); else if (strcmp(keystr(ssn->post.itype), "unlike") == 0) mastodont_unfavourite_status(api, id, &storage, NULL); else if (strcmp(keystr(ssn->post.itype), "unrepeat") == 0) @@ -479,6 +482,7 @@ char* construct_status(struct session* ssn, char* interaction_btns = NULL; char* notif_info = NULL; char* in_reply_to_str = NULL; + char* delete_status = NULL; char* interactions_html = NULL; struct mstdnt_status* status = local_status; // Create a "fake" notification header which contains information for @@ -555,6 +559,11 @@ char* construct_status(struct session* ssn, free(repl_str); } + + // Delete status menu item, logged in only + if (strcmp(status->account.acct, ssn->acct.acct) == 0) + easprintf(&delete_status, data_menu_item_html, + config_url_prefix, status->id, "delete", "Delete status"); if (status->media_attachments_len) attachments = construct_attachments(ssn, status->sensitive, status->media_attachments, status->media_attachments_len, NULL); @@ -595,6 +604,7 @@ char* construct_status(struct session* ssn, status->id, status->bookmarked ? "un" : "", status->bookmarked ? "Remove Bookmark" : "Bookmark", + delete_status ? delete_status : "", in_reply_to_str ? in_reply_to_str : "", parse_content, attachments ? attachments : "", @@ -606,12 +616,13 @@ char* construct_status(struct session* ssn, // Cleanup if (formatted_display_name != status->account.display_name && formatted_display_name) free(formatted_display_name); - if (interaction_btns) free(interaction_btns); - if (in_reply_to_str) free(in_reply_to_str); - if (attachments) free(attachments); - if (emoji_reactions) free(emoji_reactions); + free(interaction_btns); + free(in_reply_to_str); + free(attachments); + free(emoji_reactions); if (notif) free(notif_info); - if (interactions_html) free(interactions_html); + free(delete_status); + free(interactions_html); if (parse_content != status->content && parse_content) free(parse_content); return stat_html; diff --git a/static/menu_item.html b/static/menu_item.html new file mode 100644 index 0000000..b06b776 --- /dev/null +++ b/static/menu_item.html @@ -0,0 +1,6 @@ +
  • +
    + + +
    +
  • diff --git a/static/status.html b/static/status.html index 0935a55..0c8c5f7 100644 --- a/static/status.html +++ b/static/status.html @@ -31,6 +31,7 @@ + %s