From d1d0e1e9fe230803715493a41f52ddd8ee3a3145 Mon Sep 17 00:00:00 2001 From: "me@ow.nekobit.net" Date: Sun, 20 Mar 2022 04:17:11 +0000 Subject: [PATCH] Notifications + rework function names FossilOrigin-Name: 5a4873ba910b6b8b852821d0bede6a584f406635aa168633318d19600a7bd2de --- src/account.c | 4 +-- src/base_page.c | 41 +++++++++++++++++++++++--- src/base_page.h | 3 +- src/lists.c | 2 +- src/login.c | 2 +- src/notifications.c | 50 +++++++++++++++++++++++++++----- src/notifications.h | 11 +++++++ src/page_config.c | 2 +- src/status.c | 8 ++--- src/test.c | 2 +- src/timeline.c | 8 +++-- static/index.html | 2 +- static/notification_compact.html | 2 +- 13 files changed, 110 insertions(+), 27 deletions(-) diff --git a/src/account.c b/src/account.c index 1bff693..6643c05 100644 --- a/src/account.c +++ b/src/account.c @@ -100,11 +100,11 @@ void content_account(mastodont_t* api, char** data, size_t size) }; /* Output */ - render_base_page(&b); + render_base_page(&b, api); /* Cleanup */ mastodont_storage_cleanup(&storage); mastodont_storage_cleanup(&status_storage); - cleanup_statuses(statuses, status_len); + mstdnt_cleanup_statuses(statuses, status_len); if (cleanup) free(account_page); } diff --git a/src/base_page.c b/src/base_page.c index 947580e..33d904e 100644 --- a/src/base_page.c +++ b/src/base_page.c @@ -22,24 +22,52 @@ #include "base_page.h" #include "easprintf.h" #include "cookie.h" +#include "notifications.h" #include "../config.h" // Files #include "../static/index.chtml" -void render_base_page(struct base_page* page) +void render_base_page(struct base_page* page, mastodont_t* api) { char* cookie = getenv("HTTP_COOKIE"); enum l10n_locale locale = page->locale; char* login_string = "Login / Register"; + char* sidebar_str = NULL; + // Mastodont, used for notifications sidebar + struct mstdnt_storage storage; + struct mstdnt_notification* notifs; + size_t notifs_len; if (!g_config.changed && cookie) { if (cookies.theme) g_config.theme = cookies.theme; - if (cookies.logged_in && strcmp(cookies.logged_in, "t") == 0) + if (cookies.logged_in) login_string = ""; } + + // Get / Show notifications on sidebar + if (cookies.logged_in) + { + struct mstdnt_get_notifications_args args = { + .exclude_types = 0, + .account_id = NULL, + .exclude_visibilities = 0, + .include_types = 0, + .with_muted = 1, + .max_id = NULL, + .min_id = NULL, + .since_id = NULL, + .offset = 2, + .limit = 15, + }; + + mastodont_get_notifications(api, &args, &storage, ¬ifs, ¬ifs_len); + sidebar_str = construct_notifications_compact(notifs, notifs_len, NULL); + + //mstdnt_cleanup_notifications(notifs, notifs_len); + } char* data; int len = easprintf(&data, data_index_html, @@ -65,17 +93,22 @@ void render_base_page(struct base_page* page) L10N[locale][L10N_DIRECT], config_url_prefix, L10N[locale][L10N_CONFIG], - page->content); + page->content, + sidebar_str ? sidebar_str : "

Not logged in

"); if (!data) { perror("malloc"); - return; + goto cleanup; } fputs("Content-type: text/html\r\n", stdout); printf("Content-Length: %d\r\n\r\n", len + 1); puts(data); + // Cleanup +/* cleanup_all: */ free(data); +cleanup: + if (sidebar_str) free(sidebar_str); } diff --git a/src/base_page.h b/src/base_page.h index b6cfc03..daf0fd0 100644 --- a/src/base_page.h +++ b/src/base_page.h @@ -18,6 +18,7 @@ #ifndef BASE_PAGE_H #define BASE_PAGE_H +#include #include "l10n.h" #include "local_config.h" @@ -28,6 +29,6 @@ struct base_page char* sidebar_right; }; -void render_base_page(struct base_page* page); +void render_base_page(struct base_page* page, mastodont_t* api); #endif // BASE_PAGE_H diff --git a/src/lists.c b/src/lists.c index 25170f2..cc5ce9f 100644 --- a/src/lists.c +++ b/src/lists.c @@ -90,7 +90,7 @@ void content_lists(mastodont_t* api, char** data, size_t size) }; // Output - render_base_page(&b); + render_base_page(&b, api); // Cleanup mastodont_storage_cleanup(&storage); diff --git a/src/login.c b/src/login.c index 5880bb8..cab6adf 100644 --- a/src/login.c +++ b/src/login.c @@ -82,7 +82,7 @@ void content_login(mastodont_t* api, char** data, size_t data_size) }; // Output - render_base_page(&b); + render_base_page(&b, api); // Cleanup mastodont_storage_cleanup(&storage); diff --git a/src/notifications.c b/src/notifications.c index 47d9f83..6fa5fd8 100644 --- a/src/notifications.c +++ b/src/notifications.c @@ -23,21 +23,57 @@ // Pages #include "../static/notifications_page.chtml" +#include "../static/notifications.chtml" +#include "../static/notification.chtml" +#include "../static/notification_compact.chtml" -static char* construct_notifications_voidwrap(void* passed, size_t index, int* res) +char* construct_notification(struct mstdnt_notification* notif, int* size) { - return construct_notifications((struct mstdnt_notification*)passed + index, res); + char* notif_html; + return notif_html; } -char* construct_notifications(struct mstdnt_notification* notifs, size_t size, size_t* ret_size) +char* construct_notification_compact(struct mstdnt_notification* notif, int* size) { - return construct_func_strings(construct_notifications_voidwrap, notifs, size, ret_size); + char* notif_html; + + size_t s = easprintf(¬if_html, data_notification_compact_html, + notif->id); + + if (size) *size = s; + + return notif_html; +} + +static char* construct_notification_voidwrap(void* passed, size_t index, int* res) +{ + return construct_notification((struct mstdnt_notification*)passed + index, res); +} + +static char* construct_notification_compact_voidwrap(void* passed, size_t index, int* res) +{ + return construct_notification_compact((struct mstdnt_notification*)passed + index, res); +} + +char* construct_notifications(struct mstdnt_notification* notifs, + size_t size, + size_t* ret_size) +{ + return construct_func_strings(construct_notification_voidwrap, notifs, size, ret_size); +} + +char* construct_notifications_compact(struct mstdnt_notification* notifs, + size_t size, + size_t* ret_size) +{ + return construct_func_strings(construct_notification_compact_voidwrap, + notifs, + size, + ret_size); } void content_notifications(mastodont_t* api, char** data, size_t data_size) { - - struct base_page b = { .locale = L10N_EN_US, .content = data_notifications_page_html, @@ -45,6 +81,6 @@ void content_notifications(mastodont_t* api, char** data, size_t data_size) }; // Output - render_base_page(&b); + render_base_page(&b, api); } diff --git a/src/notifications.h b/src/notifications.h index 26f5f97..8fd5b85 100644 --- a/src/notifications.h +++ b/src/notifications.h @@ -20,6 +20,17 @@ #define NOTIFICATIONS_H #include + +char* construct_notification(struct mstdnt_notification* notif, int* size); +char* construct_notification_compact(struct mstdnt_notification* notif, int* size); +char* construct_notifications(struct mstdnt_notification* notifs, + size_t size, + size_t* ret_size); +char* construct_notifications_compact(struct mstdnt_notification* notifs, + size_t size, + size_t* ret_size); + +// Page contents void content_notifications(mastodont_t* api, char** data, size_t data_size); #endif // NOTIFICATION_H diff --git a/src/page_config.c b/src/page_config.c index 3dcdc69..cae2040 100644 --- a/src/page_config.c +++ b/src/page_config.c @@ -48,5 +48,5 @@ void content_config(mastodont_t* api, char** data, size_t size) .sidebar_right = NULL }; - render_base_page(&b); + render_base_page(&b, api); } diff --git a/src/status.c b/src/status.c index e0e9330..e0708de 100644 --- a/src/status.c +++ b/src/status.c @@ -221,7 +221,7 @@ void content_status(mastodont_t* api, char** data, size_t data_size, int is_repl }; // Output - render_base_page(&b); + render_base_page(&b, api); // Cleanup if (before_html) free(before_html); @@ -229,8 +229,8 @@ void content_status(mastodont_t* api, char** data, size_t data_size, int is_repl if (after_html) free(after_html); if (output) free(output); if (is_reply) free(stat_reply); - cleanup_statuses(statuses_before, stat_before_len); - cleanup_statuses(statuses_after, stat_after_len); - cleanup_status(&status); + mstdnt_cleanup_statuses(statuses_before, stat_before_len); + mstdnt_cleanup_statuses(statuses_after, stat_after_len); + mstdnt_cleanup_status(&status); mastodont_storage_cleanup(&storage); } diff --git a/src/test.c b/src/test.c index 1e9bcea..48432a5 100644 --- a/src/test.c +++ b/src/test.c @@ -74,6 +74,6 @@ void content_test(mastodont_t* api, char** data, size_t data_size) }; // Output - render_base_page(&b); + render_base_page(&b, api); if (page) free(page); } diff --git a/src/timeline.c b/src/timeline.c index c40d3c3..057b335 100644 --- a/src/timeline.c +++ b/src/timeline.c @@ -70,11 +70,11 @@ void tl_public(mastodont_t* api, int local) }; // Output - render_base_page(&b); + render_base_page(&b, api); // Cleanup mastodont_storage_cleanup(&storage); - cleanup_statuses(statuses, status_count); + mstdnt_cleanup_statuses(statuses, status_count); if (cleanup) free(status_format); if (post_box) free(post_box); if (output) free(output); @@ -121,12 +121,14 @@ void tl_list(mastodont_t* api, char* list_id) }; // Output - render_base_page(&b); + render_base_page(&b, api); // Cleanup mastodont_storage_cleanup(&storage); + mstdnt_cleanup_statuses(statuses, status_count); if (cleanup) free(status_format); if (post_box) free(post_box); + if (output) free(output); } diff --git a/static/index.html b/static/index.html index 5e4b234..605f293 100644 --- a/static/index.html +++ b/static/index.html @@ -42,7 +42,7 @@ - Sidebar + %s diff --git a/static/notification_compact.html b/static/notification_compact.html index dea51b2..042e93e 100644 --- a/static/notification_compact.html +++ b/static/notification_compact.html @@ -1 +1 @@ -notif_compact +%s