Implement lists

FossilOrigin-Name: 6b814f02c65e5628f97d59ad29a8914a58aa8e720542b8dfd0171e98cf061b0b
This commit is contained in:
me@ow.nekobit.net 2022-02-24 02:59:13 +00:00
parent 81c12a35e6
commit 9f7ba4b13b
10 changed files with 185 additions and 43 deletions

11
dist/skel.css vendored
View file

@ -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;

View file

@ -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)

View file

@ -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);
}

View file

@ -21,6 +21,9 @@
#include <stddef.h>
#include <mastodont.h>
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

View file

@ -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 }
};

View file

@ -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)

View file

@ -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

66
src/string_helpers.c Normal file
View file

@ -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 <https://www.gnu.org/licenses/>.
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#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;
}

38
src/string_helpers.h Normal file
View file

@ -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 <https://www.gnu.org/licenses/>.
*/
#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

View file

@ -1,6 +1,9 @@
<h1 class="center">Lists</h1>
<h1 class="center-text">Lists</h1>
<ul class="large-list center">
%s
<li><a href="%s/lists/create" class="btn split">Create new list</a></li>
</ul>
<ul class="large-list center">
<li><a href="%s/lists/create" class="btn split center-text bold-text">Create new list</a></li>
</ul>