378 lines
12 KiB
C
378 lines
12 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 <string.h>
|
|
#include <stdint.h>
|
|
#include <stdlib.h>
|
|
#include "string.h"
|
|
#include "helpers.h"
|
|
#include "notifications.h"
|
|
#include "base_page.h"
|
|
#include "string_helpers.h"
|
|
#include "easprintf.h"
|
|
#include "navigation.h"
|
|
#include "http.h"
|
|
#include "status.h"
|
|
#include "error.h"
|
|
#include "emoji.h"
|
|
#include "account.h"
|
|
#include "../config.h"
|
|
|
|
// Pages
|
|
#include "../static/notifications_page.ctmpl"
|
|
#include "../static/notifications.ctmpl"
|
|
#include "../static/notification_action.ctmpl"
|
|
#include "../static/notification.ctmpl"
|
|
#include "../static/notification_compact.ctmpl"
|
|
#include "../static/like_svg.ctmpl"
|
|
#include "../static/repeat_svg.ctmpl"
|
|
#include "../static/notifications_embed.ctmpl"
|
|
|
|
struct notification_args
|
|
{
|
|
struct session* ssn;
|
|
mastodont_t* api;
|
|
struct mstdnt_notification* notifs;
|
|
};
|
|
|
|
char* construct_notification(struct session* ssn,
|
|
mastodont_t* api,
|
|
struct mstdnt_notification* notif,
|
|
size_t* size)
|
|
{
|
|
char* notif_html;
|
|
|
|
if (notif->status)
|
|
{
|
|
// Construct status with notification_info
|
|
notif_html = construct_status(ssn, api, notif->status, size, notif, NULL, 0);
|
|
}
|
|
else {
|
|
notif_html = construct_notification_action(notif, size);
|
|
}
|
|
|
|
return notif_html;
|
|
}
|
|
|
|
char* construct_notification_action(struct mstdnt_notification* notif, size_t* size)
|
|
{
|
|
char* res;
|
|
char* serialized_display_name = sanitize_html(notif->account->display_name);
|
|
char* display_name = emojify(serialized_display_name,
|
|
notif->account->emojis,
|
|
notif->account->emojis_len);
|
|
struct notification_action_template tdata = {
|
|
.avatar = notif->account->avatar,
|
|
.acct = notif->account->acct,
|
|
.display_name = display_name,
|
|
.prefix = config_url_prefix,
|
|
.action = notification_type_compact_str(notif->type),
|
|
.notif_svg = notification_type_svg(notif->type)
|
|
};
|
|
res = tmpl_gen_notification_action(&tdata, size);
|
|
/* // Cleanup */
|
|
if (display_name != notif->account->display_name &&
|
|
display_name != serialized_display_name)
|
|
free(display_name);
|
|
if (serialized_display_name != notif->account->display_name)
|
|
free(serialized_display_name);
|
|
return res;
|
|
}
|
|
|
|
char* construct_notification_compact(struct session* ssn,
|
|
mastodont_t* api,
|
|
struct mstdnt_notification* notif,
|
|
size_t* size)
|
|
{
|
|
char* notif_html;
|
|
char* status_format = NULL;
|
|
char* notif_stats = NULL;
|
|
|
|
const char* type_str = notification_type_compact_str(notif->type);
|
|
const char* type_svg = notification_type_svg(notif->type);
|
|
|
|
if (notif->status)
|
|
{
|
|
if (notif->type == MSTDNT_NOTIFICATION_MENTION)
|
|
notif_stats = construct_interaction_buttons(ssn, notif->status, NULL,
|
|
STATUS_NO_LIKEBOOST | STATUS_NO_DOPAMEME);
|
|
status_format = reformat_status(ssn,
|
|
notif->status->content,
|
|
notif->status->emojis,
|
|
notif->status->emojis_len);
|
|
}
|
|
|
|
char* serialized_display_name = sanitize_html(notif->account->display_name);
|
|
char* display_name = emojify(serialized_display_name,
|
|
notif->account->emojis,
|
|
notif->account->emojis_len);
|
|
struct notification_compact_template tdata = {
|
|
.avatar = notif->account->avatar,
|
|
.has_icon = strlen(type_svg) == 0 ? "" : "-with-icon",
|
|
.acct = notif->account->acct,
|
|
.display_name = display_name,
|
|
.action = type_str,
|
|
.notif_svg = type_svg,
|
|
.is_status = (notif->type == MSTDNT_NOTIFICATION_STATUS ||
|
|
notif->type == MSTDNT_NOTIFICATION_MENTION ? "is-mention" : NULL),
|
|
/* Might show follower address */
|
|
.content = (notif->type == MSTDNT_NOTIFICATION_FOLLOW ?
|
|
notif->account->acct : status_format),
|
|
.stats = notif_stats
|
|
};
|
|
|
|
notif_html = tmpl_gen_notification_compact(&tdata, size);
|
|
|
|
if (status_format &&
|
|
status_format != notif->status->content) free(status_format);
|
|
if (notif_stats) free(notif_stats);
|
|
if (serialized_display_name != notif->account->display_name)
|
|
free(serialized_display_name);
|
|
if (display_name != notif->account->display_name &&
|
|
display_name != serialized_display_name)
|
|
free(display_name);
|
|
return notif_html;
|
|
}
|
|
|
|
static char* construct_notification_voidwrap(void* passed, size_t index, size_t* res)
|
|
{
|
|
struct notification_args* args = passed;
|
|
return construct_notification(args->ssn, args->api, args->notifs + index, res);
|
|
}
|
|
|
|
static char* construct_notification_compact_voidwrap(void* passed, size_t index, size_t* res)
|
|
{
|
|
struct notification_args* args = passed;
|
|
return construct_notification_compact(args->ssn, args->api, args->notifs + index, res);
|
|
}
|
|
|
|
char* construct_notifications(struct session* ssn,
|
|
mastodont_t* api,
|
|
struct mstdnt_notification* notifs,
|
|
size_t size,
|
|
size_t* ret_size)
|
|
{
|
|
struct notification_args args = {
|
|
.ssn = ssn,
|
|
.api = api,
|
|
.notifs = notifs
|
|
};
|
|
return construct_func_strings(construct_notification_voidwrap, &args, size, ret_size);
|
|
}
|
|
|
|
char* construct_notifications_compact(struct session* ssn,
|
|
mastodont_t* api,
|
|
struct mstdnt_notification* notifs,
|
|
size_t size,
|
|
size_t* ret_size)
|
|
{
|
|
struct notification_args args = {
|
|
.ssn = ssn,
|
|
.api = api,
|
|
.notifs = notifs
|
|
};
|
|
return construct_func_strings(construct_notification_compact_voidwrap,
|
|
&args,
|
|
size,
|
|
ret_size);
|
|
}
|
|
|
|
void content_notifications(PATH_ARGS)
|
|
{
|
|
struct mstdnt_args m_args;
|
|
set_mstdnt_args(&m_args, ssn);
|
|
struct mstdnt_storage storage = { 0 };
|
|
struct mstdnt_notification* notifs = NULL;
|
|
size_t notifs_len = 0;
|
|
|
|
struct mstdnt_notifications_args args = {
|
|
.exclude_types = 0,
|
|
.account_id = NULL,
|
|
.exclude_visibilities = 0,
|
|
.include_types = 0,
|
|
.with_muted = 1,
|
|
.max_id = keystr(ssn->post.max_id),
|
|
.min_id = keystr(ssn->post.min_id),
|
|
.since_id = NULL,
|
|
.offset = 0,
|
|
.limit = 20,
|
|
};
|
|
|
|
if (keystr(ssn->cookies.logged_in))
|
|
mastodont_get_notifications(api, &m_args, &args, &storage, ¬ifs, ¬ifs_len);
|
|
|
|
PERL_STACK_INIT;
|
|
HV* session_hv = perlify_session(ssn);
|
|
XPUSHs(newRV_noinc((SV*)session_hv));
|
|
XPUSHs(newRV_noinc((SV*)template_files));
|
|
if (notifs)
|
|
XPUSHs(newRV_noinc((SV*)perlify_notifications(notifs, notifs_len)));
|
|
|
|
// ARGS
|
|
PERL_STACK_SCALAR_CALL("notifications::content_notifications");
|
|
|
|
// Duplicate so we can free the TMPs
|
|
char* dup = PERL_GET_STACK_EXIT;
|
|
|
|
struct base_page b = {
|
|
.category = BASE_CAT_NOTIFICATIONS,
|
|
.content = dup,
|
|
.session = session_hv,
|
|
.sidebar_left = NULL
|
|
};
|
|
|
|
// Output
|
|
render_base_page(&b, req, ssn, api);
|
|
mastodont_storage_cleanup(&storage);
|
|
Safefree(dup);
|
|
}
|
|
|
|
void content_notifications_compact(PATH_ARGS)
|
|
{
|
|
struct mstdnt_args m_args;
|
|
set_mstdnt_args(&m_args, ssn);
|
|
char* page;
|
|
struct mstdnt_storage storage = { 0 };
|
|
struct mstdnt_notification* notifs = NULL;
|
|
size_t notifs_len = 0;
|
|
|
|
if (keystr(ssn->cookies.logged_in))
|
|
{
|
|
struct mstdnt_notifications_args args = {
|
|
.exclude_types = 0,
|
|
.account_id = NULL,
|
|
.exclude_visibilities = 0,
|
|
.include_types = 0,
|
|
.with_muted = 1,
|
|
.max_id = keystr(ssn->post.max_id),
|
|
.min_id = keystr(ssn->post.min_id),
|
|
.since_id = NULL,
|
|
.offset = 0,
|
|
.limit = 20,
|
|
};
|
|
|
|
mastodont_get_notifications(api, &m_args, &args, &storage, ¬ifs, ¬ifs_len);
|
|
}
|
|
|
|
PERL_STACK_INIT;
|
|
HV* session_hv = perlify_session(ssn);
|
|
XPUSHs(newRV_noinc((SV*)session_hv));
|
|
XPUSHs(newRV_noinc((SV*)template_files));
|
|
if (notifs)
|
|
XPUSHs(newRV_noinc((SV*)perlify_notifications(notifs, notifs_len)));
|
|
|
|
PERL_STACK_SCALAR_CALL("notifications::embed_notifications");
|
|
|
|
page = PERL_GET_STACK_EXIT;
|
|
|
|
send_result(req, NULL, NULL, page, 0);
|
|
|
|
mastodont_storage_cleanup(&storage);
|
|
mstdnt_cleanup_notifications(notifs, notifs_len);
|
|
}
|
|
|
|
void content_notifications_clear(PATH_ARGS)
|
|
{
|
|
char* referer = GET_ENV("HTTP_REFERER", req);
|
|
struct mstdnt_args m_args;
|
|
set_mstdnt_args(&m_args, ssn);
|
|
struct mstdnt_storage storage = { 0 };
|
|
|
|
if (data)
|
|
{
|
|
mastodont_notification_dismiss(api, &m_args, &storage, data[0]);
|
|
}
|
|
else {
|
|
mastodont_notifications_clear(api, &m_args, &storage);
|
|
}
|
|
|
|
mastodont_storage_cleanup(&storage);
|
|
redirect(req, REDIRECT_303, referer);
|
|
}
|
|
|
|
void content_notifications_read(PATH_ARGS)
|
|
{
|
|
char* referer = GET_ENV("HTTP_REFERER", req);
|
|
struct mstdnt_args m_args;
|
|
set_mstdnt_args(&m_args, ssn);
|
|
struct mstdnt_storage storage = { 0 };
|
|
|
|
if (data)
|
|
{
|
|
struct mstdnt_notifications_args args = { .id = data[0] };
|
|
mastodont_notifications_read(api, &m_args, &args, &storage, NULL);
|
|
}
|
|
else {
|
|
struct mstdnt_notifications_args args = { .max_id = keystr(ssn->post.max_id) };
|
|
mastodont_notifications_read(api, &m_args, &args, &storage, NULL);
|
|
}
|
|
|
|
mastodont_storage_cleanup(&storage);
|
|
redirect(req, REDIRECT_303, referer);
|
|
}
|
|
|
|
// Converts it into a perl struct
|
|
static HV* perlify_notification_pleroma(struct mstdnt_notification_pleroma* notif)
|
|
{
|
|
if (!notif) return NULL;
|
|
HV* notif_pl_hv = newHV();
|
|
|
|
hvstores_int(notif_pl_hv, "is_muted", notif->is_muted);
|
|
hvstores_int(notif_pl_hv, "is_seen", notif->is_seen);
|
|
|
|
return notif_pl_hv;
|
|
}
|
|
|
|
|
|
// 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_int(notif_hv, "created_at", notif->created_at);
|
|
hvstores_str(notif_hv, "emoji", notif->emoji);
|
|
hvstores_str(notif_hv, "type", mstdnt_notification_t_to_str(notif->type));
|
|
hvstores_ref(notif_hv, "account", perlify_account(notif->account));
|
|
hvstores_ref(notif_hv, "pleroma", perlify_notification_pleroma(notif->pleroma));
|
|
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);
|
|
}
|