From 2aa34fea2a4a3281b8d05c021953896f83beddc9 Mon Sep 17 00:00:00 2001 From: nekobit Date: Wed, 17 Aug 2022 21:20:07 +0000 Subject: [PATCH] perlify_chat(s) FossilOrigin-Name: f77180996ce3b881a0395c015338668fb1db76af35329591c8174ded6803a87e --- src/conversations.c | 63 ++++++++++++++++++++++++++++++-------- src/conversations.h | 3 ++ templates/content_chats.tt | 5 +++ 3 files changed, 59 insertions(+), 12 deletions(-) create mode 100644 templates/content_chats.tt diff --git a/src/conversations.c b/src/conversations.c index effb6d1..440e70e 100644 --- a/src/conversations.c +++ b/src/conversations.c @@ -17,6 +17,7 @@ */ #include +#include "account.h" #include "../config.h" #include "conversations.h" #include "helpers.h" @@ -163,8 +164,6 @@ void content_chats(PATH_ARGS) struct mstdnt_chat* chats = NULL; size_t chats_len = 0; struct mstdnt_storage storage = { 0 }; - char* chats_page = NULL; - char* chats_html = NULL; struct mstdnt_chats_args args = { .with_muted = MSTDNT_TRUE, @@ -175,20 +174,34 @@ void content_chats(PATH_ARGS) .limit = 20, }; - if (mastodont_get_chats_v2(api, &m_args, &args, &storage, &chats, &chats_len)) - { - chats_page = construct_error(storage.error, E_ERROR, 1, NULL); - } - else { - chats_html = construct_chats(api, &m_args, chats, chats_len, NULL); - if (!chats_html) - chats_html = construct_error("No chats", E_NOTICE, 1, NULL); - chats_page = construct_chats_view(chats_html, NULL); - } + mastodont_get_chats_v2(api, &m_args, &args, &storage, &chats, &chats_len); + + 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_chats(&chats, chats_len))); + // ARGS + PUTBACK; + call_pv("chat::content_chats", 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_CHATS, .content = chats_page, + .session = session_hv, .sidebar_left = NULL }; @@ -293,3 +306,29 @@ void content_chat_embed(PATH_ARGS) free(result); } +HV* perlify_chat(const struct mstdnt_chat* chat) +{ + if (!chat) return NULL; + + HV* chat_hv = newHV(); + hvstores_ref(chat_hv, "account", perlify_account(&(chat->account))); + hvstores_str(chat_hv, "id", chat->id); + hvstores_int(chat_hv, "unread", chat->unread); + + return chat_hv; +} + +AV* perlify_chats(const struct mstdnt_chat* chats, size_t len) +{ + if (!(chats && 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_chat(chats + i))); + } + + return av; +} + diff --git a/src/conversations.h b/src/conversations.h index 18ffb37..b1cf1da 100644 --- a/src/conversations.h +++ b/src/conversations.h @@ -49,4 +49,7 @@ char* construct_chat_view(struct session* ssn, mastodont_t* api, char* id, size_ void content_chat_embed(PATH_ARGS); void content_chat_view(PATH_ARGS); +AV* perlify_chats(const struct mstdnt_chat* chats, size_t chats_len); +HV* perlify_chat(const struct mstdnt_chat* chat); + #endif // LISTS_H diff --git a/templates/content_chats.tt b/templates/content_chats.tt new file mode 100644 index 0000000..45a255c --- /dev/null +++ b/templates/content_chats.tt @@ -0,0 +1,5 @@ +

Chats

+ +[% FOREACH chat IN chats %] + [% make_chat(chat) %] +[% END %]