From 2e21b6fea3d0b594e28ab36e736847a7c4deb180 Mon Sep 17 00:00:00 2001 From: nekobit Date: Mon, 25 Jul 2022 01:35:47 +0000 Subject: [PATCH] More perl stuff FossilOrigin-Name: 7a4d25bfdd0b7eb8e20e91c6f01d719c8e9fb43e0d35c0d46027876b6fab8659 --- .fossil-settings/ignore-glob | 2 +- perl/main.pl | 4 +- src/account.c | 3 +- src/account.h | 1 + src/base_page.c | 78 ++++++++++++++++++------------------ src/global_perl.h | 4 +- src/notifications.c | 30 ++++++++++++++ src/notifications.h | 4 ++ src/session.c | 11 +++-- src/status.c | 30 ++++++++++++++ src/status.h | 4 ++ 11 files changed, 121 insertions(+), 50 deletions(-) diff --git a/.fossil-settings/ignore-glob b/.fossil-settings/ignore-glob index bcc209f..6f37e23 100644 --- a/.fossil-settings/ignore-glob +++ b/.fossil-settings/ignore-glob @@ -11,4 +11,4 @@ config.h treebird test/tests scripts/*.o -perl/*.ctt \ No newline at end of file +templates/*.ctt \ No newline at end of file diff --git a/perl/main.pl b/perl/main.pl index 18f2187..0666e0d 100644 --- a/perl/main.pl +++ b/perl/main.pl @@ -14,7 +14,7 @@ my $template = Template->new( sub base_page { - my ($ssn, $data, $main, $acct) = @_; + my ($ssn, $data, $main) = @_; my $result; my %vars = ( @@ -24,7 +24,7 @@ sub base_page lang => sub { $L10N{'EN_US'}->{shift(@_)} }, main => $main, sidebar_opacity => $ssn->{config}->{sidebar_opacity} / 255, - acct => $acct + acct => $ssn->{account} ); diff --git a/src/account.c b/src/account.c index 299df63..93135a6 100644 --- a/src/account.c +++ b/src/account.c @@ -878,9 +878,8 @@ void content_account_favourites(PATH_ARGS) HV* perlify_account(const struct mstdnt_account* acct) { - HV* acct_hv = newHV(); - if (!acct) return NULL; + HV* acct_hv = newHV(); hvstores_str(acct_hv, "id", acct->id); hvstores_str(acct_hv, "username", acct->username); diff --git a/src/account.h b/src/account.h index 73efe1d..1cbed18 100644 --- a/src/account.h +++ b/src/account.h @@ -22,6 +22,7 @@ #include #include #include "session.h" +#include "path.h" #include "l10n.h" #define ACCOUNT_NOP 0 diff --git a/src/base_page.c b/src/base_page.c index 8ed2729..6d90256 100644 --- a/src/base_page.c +++ b/src/base_page.c @@ -44,53 +44,51 @@ void render_base_page(struct base_page* page, FCGX_Request* req, struct session* SAVETMPS; PUSHMARK(SP); + struct mstdnt_args m_args; + set_mstdnt_args(&m_args, ssn); + HV* session_hv = perlify_session(ssn); - HV* acct_hv = perlify_account(&(ssn->acct)); XPUSHs(sv_2mortal(newRV_inc((SV*)session_hv))); XPUSHs(sv_2mortal(newSVpv(data_main_tt, 0))); XPUSHs(sv_2mortal(newSVpv(page->content, 0))); - XPUSHs(sv_2mortal(newRV_inc((SV*)acct_hv))); - struct mstdnt_args m_args; - set_mstdnt_args(&m_args, ssn); - /* // If user is logged in */ - /* if (keystr(ssn->cookies.logged_in) && keystr(ssn->cookies.access_token)) */ - /* { */ - /* account_sidebar_str = construct_account_sidebar(&(ssn->acct), NULL); */ - - /* // Get / Show notifications on sidebar */ - /* if (ssn->config.notif_embed) */ - /* { */ - /* main_sidebar_str = (char*)sidebar_embed; */ - /* } */ - /* else { */ - /* 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 = 0, */ - /* .limit = 8, */ - /* }; */ + if (keystr(ssn->cookies.logged_in) && keystr(ssn->cookies.access_token)) + { + // Get / Show notifications on sidebar + if (!ssn->config.notif_embed) + { + struct mstdnt_storage storage = { 0 }; + struct mstdnt_notification* notifs = NULL; + size_t notifs_len = 0; + 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 = 0, + .limit = 8, + }; - /* if (mastodont_get_notifications(api, */ - /* &m_args, */ - /* &args, */ - /* &storage, */ - /* ¬ifs, */ - /* ¬ifs_len) == 0) */ - /* { */ - /* main_sidebar_str = construct_notifications_compact(ssn, api, notifs, notifs_len, NULL); */ - /* } */ + if (mastodont_get_notifications(api, + &m_args, + &args, + &storage, + ¬ifs, + ¬ifs_len) == 0) + { + AV* notifs_av = perlify_notifications(notifs, notifs_len); + XPUSHs(sv_2mortal(newRV_inc((SV*)notifs_av))); + } - /* mstdnt_cleanup_notifications(notifs, notifs_len); */ - /* mastodont_storage_cleanup(&storage); */ - /* } */ - /* } */ + + mstdnt_cleanup_notifications(notifs, notifs_len); + mastodont_storage_cleanup(&storage); + } + } // Run function PUTBACK; diff --git a/src/global_perl.h b/src/global_perl.h index fcb8fff..7615cf3 100644 --- a/src/global_perl.h +++ b/src/global_perl.h @@ -22,8 +22,10 @@ #include // hv_stores(ssn_hv, "id", newSVpv(acct->id, 0)); -#define hvstores_str(hv, key, val) hv_stores((hv), key, newSVpv((val), 0)) +#define hvstores_str(hv, key, val) hv_stores((hv), key, ((val) ? newSVpv((val), 0) : &PL_sv_undef)) #define hvstores_int(hv, key, val) hv_stores((hv), key, newSViv((val))) +#define hvstores_ref(hv, key, val) hv_stores((hv), key, \ + ((val) ? newRV_inc((SV* const)(val)) : &PL_sv_undef)) static PerlInterpreter* perl; diff --git a/src/notifications.c b/src/notifications.c index cd4e4a8..8de544c 100644 --- a/src/notifications.c +++ b/src/notifications.c @@ -29,6 +29,7 @@ #include "status.h" #include "error.h" #include "emoji.h" +#include "account.h" #include "../config.h" // Pages @@ -336,6 +337,35 @@ void content_notifications_compact(PATH_ARGS) free(theme_str); } +// Converts it into a perl struct +HV* perlify_notification(struct mstdnt_notification* notif) +{ + if (!notif) return NULL; + HV* notif_hv = newHV(); + + hvstores_str(notif_hv, "id", notif->id); + hvstores_str(notif_hv, "created_at", notif->created_at); + hvstores_ref(notif_hv, "account", perlify_account(notif->account)); + hvstores_ref(notif_hv, "status", perlify_status(notif->status)); + + return notif_hv; +} + +// The same as above, but for multiple +AV* perlify_notifications(struct mstdnt_notification* notifs, size_t len) +{ + if (!(notifs && len)) return NULL; + AV* av = newAV(); + av_extend(av, len-1); + + for (int i = 0; i < len; ++i) + { + av_store(av, i, newRV_inc((SV*)perlify_notification(notifs + i))); + } + + return av; +} + void api_notifications(PATH_ARGS) { send_result(req, NULL, "application/json", "{\"status\":0}", 0); diff --git a/src/notifications.h b/src/notifications.h index fc3b5a4..aa04a64 100644 --- a/src/notifications.h +++ b/src/notifications.h @@ -24,6 +24,7 @@ #include "session.h" #include "path.h" #include "type_string.h" +#include "global_perl.h" char* construct_notification(struct session* ssn, mastodont_t* api, @@ -51,4 +52,7 @@ void content_notifications_compact(PATH_ARGS); void api_notifications(PATH_ARGS); +HV* perlify_notification(struct mstdnt_notification* notif); +AV* perlify_notifications(struct mstdnt_notification* notif, size_t len); + #endif // NOTIFICATION_H diff --git a/src/session.c b/src/session.c index 053953c..ea32235 100644 --- a/src/session.c +++ b/src/session.c @@ -16,6 +16,7 @@ * along with this program. If not, see . */ +#include "account.h" #include "session.h" #include "../config.h" @@ -37,12 +38,14 @@ HV* perlify_session(struct session* ssn) HV* ssn_post_values = perlify_post_values(&(ssn->post)); HV* ssn_get_values = perlify_get_values(&(ssn->query)); HV* ssn_cookie_values = perlify_cookies(&(ssn->cookies)); + HV* acct_hv = perlify_account(&(ssn->acct)); // Config HV* ssn_config = perlify_config(&(ssn->config)); - hv_stores(ssn_hv, "config", newRV_inc((SV*)ssn_config)); - hv_stores(ssn_hv, "cookies", newRV_inc((SV*)ssn_cookie_values)); - hv_stores(ssn_hv, "query", newRV_inc((SV*)ssn_get_values)); - hv_stores(ssn_hv, "post", newRV_inc((SV*)ssn_post_values)); + hvstores_ref(ssn_hv, "config", ssn_config); + hvstores_ref(ssn_hv, "cookies", ssn_cookie_values); + hvstores_ref(ssn_hv, "query", ssn_get_values); + hvstores_ref(ssn_hv, "post", ssn_post_values); + hvstores_ref(ssn_hv, "account", acct_hv); return ssn_hv; } diff --git a/src/status.c b/src/status.c index 483e618..494d021 100644 --- a/src/status.c +++ b/src/status.c @@ -1149,3 +1149,33 @@ void notice_redirect(PATH_ARGS) redirect(req, REDIRECT_303, url); free(url); } + +HV* perlify_status(const struct mstdnt_status* status) +{ + if (!status) return NULL; + + HV* status_hv = newHV(); + hvstores_str(status_hv, "id", status->id); + hvstores_str(status_hv, "uri", status->uri); + hvstores_str(status_hv, "created_at", status->id); + hvstores_ref(status_hv, "account", perlify_account(&(status->account))); + hvstores_str(status_hv, "content", status->content); + hvstores_str(status_hv, "spoiler_text", status->spoiler_text); + hvstores_str(status_hv, "url", status->url); + hvstores_str(status_hv, "in_reply_to_id", status->in_reply_to_id); + hvstores_str(status_hv, "in_reply_to_account_id", status->in_reply_to_account_id); + hvstores_str(status_hv, "language", status->language); + hvstores_str(status_hv, "text", status->text); + + hvstores_int(status_hv, "favourited", status->favourited); + hvstores_int(status_hv, "reblogged", status->reblogged); + hvstores_int(status_hv, "muted", status->muted); + hvstores_int(status_hv, "bookmarked", status->bookmarked); + hvstores_int(status_hv, "pinned", status->pinned); + hvstores_int(status_hv, "reblogs_count", status->reblogs_count); + hvstores_int(status_hv, "favourites_count", status->favourites_count); + hvstores_int(status_hv, "replies_count", status->replies_count); + hvstores_ref(status_hv, "status", perlify_status(status->reblog)); + + return status_hv; +} diff --git a/src/status.h b/src/status.h index 1084105..0041ce7 100644 --- a/src/status.h +++ b/src/status.h @@ -23,6 +23,7 @@ #include "l10n.h" #include "path.h" #include "session.h" +#include "global_perl.h" // Flags #define STATUS_NOOP 0 @@ -134,4 +135,7 @@ void notice_redirect(PATH_ARGS); // API void api_status_interact(PATH_ARGS); +// Perl +HV* perlify_status(const struct mstdnt_status* status); + #endif // STATUS_H