From 937301e30796ac303981d49b0b177e46688a8b2f Mon Sep 17 00:00:00 2001 From: nekobit Date: Fri, 5 Aug 2022 19:20:26 +0000 Subject: [PATCH] Notifications perl template FossilOrigin-Name: 7f5ff955aa478f571019fe9d6cf992f229e421dd7bac1b8ea689594baca3621f --- perl/main.pl | 2 +- perl/notifications.pm | 15 ++++++- src/global_perl.c | 2 + src/notifications.c | 88 +++++++++++++++++-------------------- templates/content_notifs.tt | 1 + 5 files changed, 59 insertions(+), 49 deletions(-) create mode 100644 templates/content_notifs.tt diff --git a/perl/main.pl b/perl/main.pl index d672903..a215e98 100644 --- a/perl/main.pl +++ b/perl/main.pl @@ -3,7 +3,7 @@ use warnings; # Modules use Template; use l10n qw( &lang %L10N ); -use notifications qw( generate_notification_compact ); +use notifications qw( generate_notification_compact content_notifications ); use template_helpers qw( &to_template ); use timeline; use status; diff --git a/perl/notifications.pm b/perl/notifications.pm index 322ced7..30c968e 100644 --- a/perl/notifications.pm +++ b/perl/notifications.pm @@ -3,7 +3,7 @@ use strict; use warnings; use Exporter 'import'; -our @EXPORT = qw( generate_notification_compact ); +our @EXPORT = qw( generate_notification_compact content_notifications ); use template_helpers 'to_template'; @@ -19,3 +19,16 @@ sub generate_notification_compact to_template(\%vars, \$data->{'notif_compact.tt'}); } + +sub content_notifications +{ + my ($ssn, $data, $notifs) = @_; + + my %vars = ( + prefix => '', + ssn=> $ssn, + notif => $notif + ); + + to_template(\%vars, \$data->{'content_notifs.tt'}); +} diff --git a/src/global_perl.c b/src/global_perl.c index 8233096..a2bfc3c 100644 --- a/src/global_perl.c +++ b/src/global_perl.c @@ -31,6 +31,7 @@ #include "../templates/account.ctt" #include "../templates/account_statuses.ctt" #include "../templates/account_scrobbles.ctt" +#include "../templates/content_notifs.ctt" HV* template_files; pthread_mutex_t perl_mutex = PTHREAD_MUTEX_INITIALIZER; @@ -51,6 +52,7 @@ void init_template_files() hv_stores(template_files, "account.tt", newSVpv(data_account_tt, data_account_tt_size)); hv_stores(template_files, "account_statuses.tt", newSVpv(data_account_statuses_tt, data_account_statuses_tt_size)); hv_stores(template_files, "account_scrobbles.tt", newSVpv(data_account_scrobbles_tt, data_account_scrobbles_tt_size)); + hv_stores(template_files, "content_notifs.tt", newSVpv(data_content_notifs_tt, data_content_notifs_tt_size)); } void cleanup_template_files() diff --git a/src/notifications.c b/src/notifications.c index 8de544c..85bbd08 100644 --- a/src/notifications.c +++ b/src/notifications.c @@ -195,67 +195,61 @@ void content_notifications(PATH_ARGS) { struct mstdnt_args m_args; set_mstdnt_args(&m_args, ssn); - char* page, *notif_html = NULL; struct mstdnt_storage storage = { 0 }; struct mstdnt_notification* notifs = NULL; size_t notifs_len = 0; - char* start_id; - char* navigation_box = NULL; - - if (keystr(ssn->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 = keystr(ssn->post.max_id), - .min_id = keystr(ssn->post.min_id), - .since_id = NULL, - .offset = 0, - .limit = 20, - }; - - if (mastodont_get_notifications(api, &m_args, &args, &storage, ¬ifs, ¬ifs_len) == 0) - { - if (notifs && notifs_len) - { - notif_html = construct_notifications(ssn, api, notifs, notifs_len, NULL); - start_id = keystr(ssn->post.start_id) ? keystr(ssn->post.start_id) : notifs[0].id; - navigation_box = construct_navigation_box(start_id, - notifs[0].id, - notifs[notifs_len-1].id, - NULL); - mstdnt_cleanup_notifications(notifs, notifs_len); - } - else - notif_html = construct_error("No notifications", E_NOTICE, 1, NULL); - } - else - notif_html = construct_error(storage.error, E_ERROR, 1, NULL); - - } - - struct notifications_page_template tdata = { - .notifications = notif_html, - .navigation = navigation_box + + struct mstdnt_get_notifications_args args = { + .exclude_types = 0, + .account_id = NULL, + .exclude_visibilities = 0, + .include_types = 0, + .with_muted = 1, + .max_id = keystr(ssn->post.max_id), + .min_id = keystr(ssn->post.min_id), + .since_id = NULL, + .offset = 0, + .limit = 20, }; - page = tmpl_gen_notifications_page(&tdata, NULL); + if (keystr(ssn->cookies.logged_in)) + mastodont_get_notifications(api, &m_args, &args, &storage, ¬ifs, ¬ifs_len); + + // Call perl + perl_lock(); + dSP; + ENTER; + SAVETMPS; + PUSHMARK(SP); + + HV* session_hv = perlify_session(ssn); + XPUSHs(newRV_noinc((SV*)session_hv)); + XPUSHs(newRV_noinc((SV*)template_files)); + XPUSHs(newRV_noinc((SV*)perlify_notifications(notifs, notifs_len))); + + // ARGS + PUTBACK; + call_pv("notifications::content_notifications", G_SCALAR); + SPAGAIN; + + // Duplicate so we can free the TMPs + char* dup = savesharedsvpv(POPs); + PUTBACK; + FREETMPS; + LEAVE; + perl_unlock(); struct base_page b = { .category = BASE_CAT_NOTIFICATIONS, - .content = page, + .content = dup, + .session = session_hv, .sidebar_left = NULL }; // Output render_base_page(&b, req, ssn, api); mastodont_storage_cleanup(&storage); - if (notif_html) free(notif_html); - if (navigation_box) free(navigation_box); - if (page) free(page); + Safefree(dup); } void content_notifications_compact(PATH_ARGS) diff --git a/templates/content_notifs.tt b/templates/content_notifs.tt new file mode 100644 index 0000000..f4140c3 --- /dev/null +++ b/templates/content_notifs.tt @@ -0,0 +1 @@ +

Notifications