treebird/src/base_page.c
nekobit b677ba4b52 Create sidebar
FossilOrigin-Name: 867d2d317849fbb7cba4005a76dce17822afbd603e8205e2da705381299d40bc
2022-05-16 03:30:37 +00:00

172 lines
6.1 KiB
C

/*
* Treebird - 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 <fcgi_stdio.h>
#include <string.h>
#include <stdlib.h>
#include "base_page.h"
#include "easprintf.h"
#include "cookie.h"
#include "notifications.h"
#include "string_helpers.h"
#include "../config.h"
#include "local_config_set.h"
#include "account.h"
// Files
#include "../static/index.chtml"
#include "../static/quick_login.chtml"
#define BODY_STYLE "style=\"background:url('%s');\""
void render_base_page(struct base_page* page, struct session* ssn, mastodont_t* api)
{
char* cookie = getenv("HTTP_COOKIE");
enum l10n_locale locale = page->locale;
char* login_string = "<a href=\"login\" id=\"login-header\">Login / Register</a>";
char* background_url_css = NULL;
// Sidebar
char* sidebar_str,
* main_sidebar_str = NULL,
* account_sidebar_str = NULL;
// Mastodont, used for notifications sidebar
struct mstdnt_account acct = { 0 };
struct mstdnt_storage storage = { 0 };
struct mstdnt_notification* notifs = NULL;
size_t notifs_len = 0;
read_config(ssn);
if (ssn->config.logged_in)
login_string = "";
if (ssn->config.background_url)
{
easprintf(&background_url_css, BODY_STYLE, ssn->config.background_url);
}
// If user is logged in
if (ssn->cookies.logged_in && ssn->cookies.access_token)
{
if (mastodont_verify_credentials(api, &acct, &storage) == 0)
{
account_sidebar_str = construct_account_sidebar(&acct, NULL);
}
mstdnt_cleanup_account(&acct);
mastodont_storage_cleanup(&storage); // reuse it later
// Get / Show notifications on sidebar
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, &args, &storage, &notifs, &notifs_len) == 0)
{
main_sidebar_str = construct_notifications_compact(ssn, api, notifs, notifs_len, NULL);
}
mstdnt_cleanup_notifications(notifs, notifs_len);
mastodont_storage_cleanup(&storage);
}
else {
// Construct small login page
easprintf(&main_sidebar_str, data_quick_login_html,
config_url_prefix,
L10N[L10N_EN_US][L10N_USERNAME],
L10N[L10N_EN_US][L10N_PASSWORD],
L10N[L10N_EN_US][L10N_LOGIN_BTN]);
}
// Combine into sidebar
easprintf(&sidebar_str, "%s%s",
account_sidebar_str ? account_sidebar_str : "",
main_sidebar_str ? main_sidebar_str : "");
char* data;
int len = easprintf(&data, data_index_html,
L10N[locale][L10N_APP_NAME],
ssn->config.theme,
ssn->config.themeclr ? "-dark" : "",
background_url_css ? background_url_css : "",
config_url_prefix,
L10N[locale][L10N_APP_NAME],
login_string,
config_url_prefix,
L10N[locale][L10N_SEARCH_PLACEHOLDER],
L10N[locale][L10N_SEARCH_BUTTON],
CAT_TEXT(page->category, BASE_CAT_HOME),
config_url_prefix,
L10N[locale][L10N_HOME],
CAT_TEXT(page->category, BASE_CAT_LOCAL),
config_url_prefix,
L10N[locale][L10N_LOCAL],
CAT_TEXT(page->category, BASE_CAT_FEDERATED),
config_url_prefix,
L10N[locale][L10N_FEDERATED],
CAT_TEXT(page->category, BASE_CAT_NOTIFICATIONS),
config_url_prefix,
L10N[locale][L10N_NOTIFICATIONS],
CAT_TEXT(page->category, BASE_CAT_LISTS),
config_url_prefix,
L10N[locale][L10N_LISTS],
CAT_TEXT(page->category, BASE_CAT_FAVOURITES),
config_url_prefix,
L10N[locale][L10N_FAVOURITES],
CAT_TEXT(page->category, BASE_CAT_BOOKMARKS),
config_url_prefix,
L10N[locale][L10N_BOOKMARKS],
CAT_TEXT(page->category, BASE_CAT_DIRECT),
config_url_prefix,
L10N[locale][L10N_DIRECT],
CAT_TEXT(page->category, BASE_CAT_CONFIG),
config_url_prefix,
L10N[locale][L10N_CONFIG],
page->sidebar_left ?
page->sidebar_left : "",
page->content,
sidebar_str ? sidebar_str : "");
if (!data)
{
perror("malloc");
goto cleanup;
}
fputs("Content-type: text/html\r\n", stdout);
printf("Content-Length: %d\r\n\r\n", len + 1);
puts(data);
// Cleanup
/* cleanup_all: */
free(data);
cleanup:
if (sidebar_str) free(sidebar_str);
if (main_sidebar_str) free(main_sidebar_str);
if (account_sidebar_str) free(account_sidebar_str);
if (background_url_css) free(background_url_css);
}