From 9f7ba4b13ba1783366c43654d834c9a1290835f2 Mon Sep 17 00:00:00 2001 From: "me@ow.nekobit.net" Date: Thu, 24 Feb 2022 02:59:13 +0000 Subject: [PATCH] Implement lists FossilOrigin-Name: 6b814f02c65e5628f97d59ad29a8914a58aa8e720542b8dfd0171e98cf061b0b --- dist/skel.css | 11 +++++++- src/index.c | 3 +- src/lists.c | 50 +++++++++++++++++++++++++++++++-- src/lists.h | 3 ++ src/main.c | 1 + src/status.c | 48 +++++++++----------------------- src/status.h | 1 + src/string_helpers.c | 66 ++++++++++++++++++++++++++++++++++++++++++++ src/string_helpers.h | 38 +++++++++++++++++++++++++ static/lists.html | 7 +++-- 10 files changed, 185 insertions(+), 43 deletions(-) create mode 100644 src/string_helpers.c create mode 100644 src/string_helpers.h diff --git a/dist/skel.css b/dist/skel.css index 8dc27e2..ef7ab11 100644 --- a/dist/skel.css +++ b/dist/skel.css @@ -13,11 +13,20 @@ body .center { - text-align: center; margin-left: auto; margin-right: auto; } +.center-text +{ + text-align: center; +} + +.bold-text +{ + font-weight: bold; +} + #display { overflow: hidden; diff --git a/src/index.c b/src/index.c index 30aa03b..3b305b6 100644 --- a/src/index.c +++ b/src/index.c @@ -44,8 +44,7 @@ void content_index(mastodont_t* api) { status_format = "An error occured loading the timeline"; } - else - { + else { /* Construct statuses into HTML */ status_format = construct_statuses(statuses, status_count, &statuses_html_count); if (!status_format) diff --git a/src/lists.c b/src/lists.c index 4ac03d1..e487ee3 100644 --- a/src/lists.c +++ b/src/lists.c @@ -24,6 +24,7 @@ #include "easprintf.h" #include "status.h" #include "lists.h" +#include "string_helpers.h" // Files #include "../static/index.chtml" @@ -31,20 +32,60 @@ #include "../static/list.chtml" #include "../static/lists.chtml" -void construct_list(struct mstdnt_list* list, int* size) +char* construct_list(struct mstdnt_list* list, int* size) { char* list_html; size_t s = easprintf(&list_html, data_list_html, - ""); + config_url_prefix, + list->id, + list->title); if (size) *size = s; return list_html; } +static char* construct_list_voidwrap(void* passed, size_t index, int* res) +{ + return construct_list((struct mstdnt_list*)passed + index, res); +} + +char* construct_lists(struct mstdnt_list* lists, size_t size, size_t* ret_size) +{ + return construct_func_strings(construct_list_voidwrap, lists, size, ret_size); +} + +char* construct_lists_view(char* lists_string, int* size) +{ + char* list_string; + size_t s = easprintf(&list_string, data_lists_html, lists_string, config_url_prefix); + if (size) *size = s; + return list_string; +} + void content_lists(mastodont_t* api, char** data, size_t size) { + int cleanup = 0; + struct mstdnt_list* lists; + size_t size_list; + struct mstdnt_storage storage; + char* lists_format; + char* lists_page = NULL; + + if (mastodont_get_lists(api, &lists, &storage, &size_list)) + { + lists_format = "An error occured while fetching lists"; + } + else { + lists_format = construct_lists(lists, size_list, NULL); + if (!lists_format) + lists_format = "Error in malloc!"; + cleanup = 1; + } + + lists_page = construct_lists_view(lists_format, NULL); + struct base_page b = { .locale = L10N_EN_US, - .content = data_lists_html, + .content = lists_page, .sidebar_right = NULL }; @@ -52,4 +93,7 @@ void content_lists(mastodont_t* api, char** data, size_t size) render_base_page(&b); // Cleanup + mastodont_storage_cleanup(&storage); + if (cleanup) free(lists_format); + if (lists_page) free(lists_page); } diff --git a/src/lists.h b/src/lists.h index 826a8e2..7852b1a 100644 --- a/src/lists.h +++ b/src/lists.h @@ -21,6 +21,9 @@ #include #include +char* construct_list(struct mstdnt_list* list, int* size); +char* construct_lists(struct mstdnt_list* lists, size_t size, size_t* ret_size); +char* construct_lists_view(char* lists_string, int* size); void content_lists(mastodont_t* api, char** data, size_t size); #endif // LISTS_H diff --git a/src/main.c b/src/main.c index 1aaecf6..218ef56 100644 --- a/src/main.c +++ b/src/main.c @@ -60,6 +60,7 @@ int main(void) { "/config", content_config }, { "/login", content_login }, { "/@:", content_account }, + { "/status/:/interact", status_interact }, { "/status/:", content_status }, { "/lists", content_lists } }; diff --git a/src/status.c b/src/status.c index fe3bcdb..3ab62ed 100644 --- a/src/status.c +++ b/src/status.c @@ -23,6 +23,7 @@ #include "easprintf.h" #include "query.h" #include "cookie.h" +#include "string_helpers.h" #include "../config.h" #include "../static/status.chtml" @@ -103,44 +104,21 @@ char* construct_status(struct mstdnt_status* status, int* size) return stat_html; } +static char* construct_status_voidwrap(void* passed, size_t index, int* res) +{ + return construct_status((struct mstdnt_status*)passed + index, res); +} + char* construct_statuses(struct mstdnt_status* statuses, size_t size, size_t* ret_size) { - char* stat_html, *result = NULL; - int curr_parse_size = 0, last_parse_size, parse_size; + return construct_func_strings(construct_status_voidwrap, statuses, size, ret_size); +} - if (size <= 0) return NULL; - - for (size_t i = 0; i < size; ++i) - { - stat_html = construct_status(statuses + i, &parse_size); - - if (parse_size == -1) /* Malloc error */ - { - if (result) free(result); - return NULL; - } - last_parse_size = curr_parse_size; - curr_parse_size += parse_size; - - result = realloc(result, curr_parse_size + 1); - if (result == NULL) - { - perror("malloc"); - free(stat_html); - return NULL; - } - - /* Copy stat_html to result in correct position */ - strncpy(result + last_parse_size, stat_html, parse_size); - /* Cleanup */ - free(stat_html); - } - - result[curr_parse_size] = '\0'; - - if (ret_size) *ret_size = curr_parse_size; - - return result; +void status_interact(mastodont_t* api, char** data, size_t data_size) +{ + char* referer = getenv("HTTP_REFERER"); + printf("Location: %s\r\n\r\nRedirecting...", + referer ? referer : "/"); } void content_status(mastodont_t* api, char** data, size_t data_size) diff --git a/src/status.h b/src/status.h index 3f2dab0..6c26125 100644 --- a/src/status.h +++ b/src/status.h @@ -25,5 +25,6 @@ int try_interact_status(mastodont_t* api); char* construct_status(struct mstdnt_status* status, int* size); char* construct_statuses(struct mstdnt_status* statuses, size_t size, size_t* ret_size); void content_status(mastodont_t* api, char** data, size_t data_size); +void status_interact(mastodont_t* api, char** data, size_t data_size); #endif // STATUS_H diff --git a/src/string_helpers.c b/src/string_helpers.c new file mode 100644 index 0000000..cd7fa04 --- /dev/null +++ b/src/string_helpers.c @@ -0,0 +1,66 @@ +/* + * RatFE - Lightweight frontend for Pleroma + * Copyright (C) 2022 Nekobit + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +#include +#include +#include +#include "string_helpers.h" +#include "easprintf.h" + +char* construct_func_strings(char* (*func)(void*, size_t, int*), + void* strings, + size_t strings_len, + size_t* ret_size) +{ + char* res_html, *result = NULL; + int curr_parse_size = 0, last_parse_size, parse_size; + + if (strings_len <= 0) return NULL; + + for (size_t i = 0; i < strings_len; ++i) + { + res_html = func(strings, i, &parse_size); + + if (parse_size == -1) /* Malloc error */ + { + if (result) free(result); + return NULL; + } + last_parse_size = curr_parse_size; + curr_parse_size += parse_size; + + result = realloc(result, curr_parse_size + 1); + if (result == NULL) + { + perror("malloc"); + free(res_html); + return NULL; + } + + /* Copy res_html to result in correct position */ + strncpy(result + last_parse_size, res_html, parse_size); + /* Cleanup */ + free(res_html); + } + + result[curr_parse_size] = '\0'; + + if (ret_size) *ret_size = curr_parse_size; + + return result; +} diff --git a/src/string_helpers.h b/src/string_helpers.h new file mode 100644 index 0000000..6adcc0f --- /dev/null +++ b/src/string_helpers.h @@ -0,0 +1,38 @@ +/* + * RatFE - Lightweight frontend for Pleroma + * Copyright (C) 2022 Nekobit + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +#ifndef STRING_HELPERS_H +#define STRING_HELPERS_H + +/** + * Constructs a string based on a function + * + * @param func A function, with void* being the string, + * size_t being an index, and int* being the returned parse size + * @param strings The array of strings + * @param strings_len len of `strings` + * @param ret_size Set to the full result string size, useful for large + * results + * @return The result, this MUST be free'd when finished and checked for NULL. + */ +char* construct_func_strings(char* (*func)(void*, size_t, int*), + void* strings, + size_t strings_len, + size_t* ret_size); + +#endif // STRING_HELPERS_H diff --git a/static/lists.html b/static/lists.html index 480259b..6998240 100644 --- a/static/lists.html +++ b/static/lists.html @@ -1,6 +1,9 @@ -

Lists

+

Lists

+ +