From d41f1e404a738cd6ac5df9d80af93376b0fb2426 Mon Sep 17 00:00:00 2001 From: "me@ow.nekobit.net" Date: Tue, 29 Mar 2022 13:03:02 +0000 Subject: [PATCH] Notifications FossilOrigin-Name: ce21a66f8c12ceea330a9dfa87ad5bfbd1241feced93851afe10a7c630fe5ef6 --- dist/treebird20.css | 29 +++++++++++++++++ src/notifications.c | 59 ++++++++++++++++++++++++++-------- src/notifications.h | 4 +-- src/status.c | 18 +++++++++-- src/status.h | 2 +- src/string_helpers.c | 8 +++-- src/type_string.c | 33 +++++++++++++++++++ src/type_string.h | 26 +++++++++++++++ static/notification.html | 8 ++++- static/notifications.html | 2 +- static/notifications_page.html | 4 ++- static/status.html | 1 + 12 files changed, 169 insertions(+), 25 deletions(-) create mode 100644 src/type_string.c create mode 100644 src/type_string.h diff --git a/dist/treebird20.css b/dist/treebird20.css index c8a855a..d1ea772 100644 --- a/dist/treebird20.css +++ b/dist/treebird20.css @@ -257,6 +257,24 @@ ul li:first-child a.sidebarbtn font-size: 10px; } +.notification-info +{ + margin-top: 7px; +} + +.notification-info img +{ + margin-left: 36px; + width: 24px; +} + +.notification-info .notification-user +{ + display: inline; + position: relative; + top: -8px; +} + /*************************** * Statuses * **************************/ @@ -268,6 +286,11 @@ ul li:first-child a.sidebarbtn border-spacing: 0px; } +.notification-info + .status +{ + padding-top: 0; +} + .status .profile-picture { display: inline-block; @@ -692,6 +715,7 @@ ul li:first-child a.sidebarbtn display: block; padding: 0; margin-top: 8px; + padding-left: 0px !important; margin-bottom: 6px; } @@ -703,6 +727,11 @@ ul li:first-child a.sidebarbtn margin: 0 4px; } +.emoji +{ + font-family: Emoji; +} + /************************************************* * LISTS * *************************************************/ diff --git a/src/notifications.c b/src/notifications.c index 8e5ae03..4abbedd 100644 --- a/src/notifications.c +++ b/src/notifications.c @@ -22,6 +22,7 @@ #include "base_page.h" #include "string_helpers.h" #include "easprintf.h" +#include "status.h" // Pages #include "../static/notifications_page.chtml" @@ -32,22 +33,21 @@ char* construct_notification(struct mstdnt_notification* notif, int* size) { char* notif_html; + int s = 0; + + if (notif->status) + { + // Construct status with notification_info + notif_html = construct_status(notif->status, &s, notif); + } + else { + notif_html = NULL; + } + + if (size) *size = s; return notif_html; } -const char* notification_type_str(mstdnt_notification_t type) -{ - switch (type) - { - case MSTDNT_NOTIFICATION_FOLLOW: return L10N[L10N_EN_US][L10N_NOTIF_COMPACT_FOLLOW]; - case MSTDNT_NOTIFICATION_FOLLOW_REQUEST: return L10N[L10N_EN_US][L10N_NOTIF_COMPACT_FOLLOW_REQUEST]; - case MSTDNT_NOTIFICATION_REBLOG: return L10N[L10N_EN_US][L10N_NOTIF_COMPACT_REPEATED]; - case MSTDNT_NOTIFICATION_FAVOURITE: return L10N[L10N_EN_US][L10N_NOTIF_COMPACT_LIKED]; - case MSTDNT_NOTIFICATION_POLL: return L10N[L10N_EN_US][L10N_NOTIF_COMPACT_POLL]; - case MSTDNT_NOTIFICATION_EMOJI_REACT: return L10N[L10N_EN_US][L10N_NOTIF_COMPACT_REACTED_WITH]; - default: return ""; - } -} char* construct_notification_compact(struct mstdnt_notification* notif, int* size) { @@ -109,13 +109,44 @@ char* construct_notifications_compact(struct mstdnt_notification* notifs, void content_notifications(mastodont_t* api, char** data, size_t data_size) { + char* page, *notif_html = NULL; + struct mstdnt_storage storage; + struct mstdnt_notification* notifs; + size_t notifs_len; + + if (cookies.logged_in) + { + 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 = 20, + }; + + if (mastodont_get_notifications(api, &args, &storage, ¬ifs, ¬ifs_len) == 0) + notif_html = construct_notifications(notifs, notifs_len, NULL); + + mstdnt_cleanup_notifications(notifs, notifs_len); + } + + easprintf(&page, data_notifications_page_html, + notif_html ? notif_html : "Not logged in"); + struct base_page b = { .locale = L10N_EN_US, - .content = data_notifications_page_html, + .content = page, .sidebar_left = NULL }; // Output render_base_page(&b, api); + if (notif_html) free(notif_html); + if (page) free(page); } diff --git a/src/notifications.h b/src/notifications.h index 1eaee88..dcfd742 100644 --- a/src/notifications.h +++ b/src/notifications.h @@ -19,8 +19,8 @@ #ifndef NOTIFICATIONS_H #define NOTIFICATIONS_H #include - -const char* notification_type_str(mstdnt_notification_t type); +#include "cookie.h" +#include "type_string.h" char* construct_notification(struct mstdnt_notification* notif, int* size); char* construct_notification_compact(struct mstdnt_notification* notif, int* size); diff --git a/src/status.c b/src/status.c index c41b6c3..5f8cf87 100644 --- a/src/status.c +++ b/src/status.c @@ -28,9 +28,11 @@ #include "attachments.h" #include "emoji_reaction.h" #include "../config.h" +#include "type_string.h" // Pages #include "../static/status.chtml" +#include "../static/notification.chtml" #define NUM_STR "%u" @@ -97,7 +99,9 @@ int try_interact_status(mastodont_t* api, char* id) return 0; } -char* construct_status(struct mstdnt_status* status, int* size) +char* construct_status(struct mstdnt_status* status, + int* size, + struct mstdnt_notification* notif) { char* stat_html; @@ -107,6 +111,7 @@ char* construct_status(struct mstdnt_status* status, int* size) char* favourites_count = NULL; char* attachments = NULL; char* emoji_reactions = NULL; + char* notif_info = NULL; if (status->replies_count) easprintf(&reply_count, NUM_STR, status->replies_count); if (status->reblogs_count) @@ -117,9 +122,15 @@ char* construct_status(struct mstdnt_status* status, int* size) attachments = construct_attachments(status->sensitive, status->media_attachments, status->media_attachments_len, NULL); if (status->pleroma.emoji_reactions_len) emoji_reactions = construct_emoji_reactions(status->pleroma.emoji_reactions, status->pleroma.emoji_reactions_len, NULL); + if (notif && notif->type != MSTDNT_NOTIFICATION_MENTION) + easprintf(¬if_info, data_notification_html, + notif->account->avatar, + notif->account->display_name, + notification_type_str(notif->type)); size_t s = easprintf(&stat_html, data_status_html, + notif_info ? notif_info : "", status->account.avatar, status->account.display_name, /* Username */ config_url_prefix, @@ -151,12 +162,13 @@ char* construct_status(struct mstdnt_status* status, int* size) if (favourites_count) free(favourites_count); if (attachments) free(attachments); if (emoji_reactions) free(emoji_reactions); + if (notif) free(notif_info); return stat_html; } static char* construct_status_voidwrap(void* passed, size_t index, int* res) { - return construct_status((struct mstdnt_status*)passed + index, res); + return construct_status((struct mstdnt_status*)passed + index, res, NULL); } char* construct_statuses(struct mstdnt_status* statuses, size_t size, size_t* ret_size) @@ -210,7 +222,7 @@ void content_status(mastodont_t* api, char** data, size_t data_size, int is_repl before_html = construct_statuses(statuses_before, stat_before_len, NULL); // Current status - stat_html = construct_status(&status, NULL); + stat_html = construct_status(&status, NULL, NULL); if (is_reply) { stat_reply = reply_status(data[0], diff --git a/src/status.h b/src/status.h index fd90029..cc55d44 100644 --- a/src/status.h +++ b/src/status.h @@ -28,7 +28,7 @@ void content_status_create(mastodont_t* api, char** data, size_t data_size); char* construct_post_box(char* reply_id, char* default_content, int* size); -char* construct_status(struct mstdnt_status* status, int* size); +char* construct_status(struct mstdnt_status* status, int* size, struct mstdnt_notification* notif); char* construct_statuses(struct mstdnt_status* statuses, size_t size, size_t* ret_size); // Status frontends diff --git a/src/string_helpers.c b/src/string_helpers.c index cacec66..c816e89 100644 --- a/src/string_helpers.c +++ b/src/string_helpers.c @@ -35,6 +35,10 @@ char* construct_func_strings(char* (*func)(void*, size_t, int*), for (size_t i = 0; i < strings_len; ++i) { res_html = func(strings, i, &parse_size); + + // Don't bother if it returns null + // Null is equivalent to "" + if (!res_html) continue; if (parse_size == -1) /* Malloc error */ { @@ -52,9 +56,9 @@ char* construct_func_strings(char* (*func)(void*, size_t, int*), return NULL; } - /* Copy res_html to result in correct position */ + // Copy res_html to result in correct position strncpy(result + last_parse_size, res_html, parse_size); - /* Cleanup */ + // Cleanup free(res_html); } diff --git a/src/type_string.c b/src/type_string.c new file mode 100644 index 0000000..53fa4ed --- /dev/null +++ b/src/type_string.c @@ -0,0 +1,33 @@ +/* + * 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 . + */ + +#include "type_string.h" + +const char* notification_type_str(mstdnt_notification_t type) +{ + switch (type) + { + case MSTDNT_NOTIFICATION_FOLLOW: return L10N[L10N_EN_US][L10N_NOTIF_COMPACT_FOLLOW]; + case MSTDNT_NOTIFICATION_FOLLOW_REQUEST: return L10N[L10N_EN_US][L10N_NOTIF_COMPACT_FOLLOW_REQUEST]; + case MSTDNT_NOTIFICATION_REBLOG: return L10N[L10N_EN_US][L10N_NOTIF_COMPACT_REPEATED]; + case MSTDNT_NOTIFICATION_FAVOURITE: return L10N[L10N_EN_US][L10N_NOTIF_COMPACT_LIKED]; + case MSTDNT_NOTIFICATION_POLL: return L10N[L10N_EN_US][L10N_NOTIF_COMPACT_POLL]; + case MSTDNT_NOTIFICATION_EMOJI_REACT: return L10N[L10N_EN_US][L10N_NOTIF_COMPACT_REACTED_WITH]; + default: return ""; + } +} diff --git a/src/type_string.h b/src/type_string.h new file mode 100644 index 0000000..b10777d --- /dev/null +++ b/src/type_string.h @@ -0,0 +1,26 @@ +/* + * 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 . + */ + +#ifndef TYPE_STRING_H +#define TYPE_STRING_H +#include +#include "l10n.h" + +const char* notification_type_str(mstdnt_notification_t type); + +#endif // TYPE_STRING_H diff --git a/static/notification.html b/static/notification.html index 07552d0..683cfcd 100644 --- a/static/notification.html +++ b/static/notification.html @@ -1 +1,7 @@ -notif +
+ +
+ %s + %s +
+
diff --git a/static/notifications.html b/static/notifications.html index 43e63ba..2963929 100644 --- a/static/notifications.html +++ b/static/notifications.html @@ -1,3 +1,3 @@
- + %s
diff --git a/static/notifications_page.html b/static/notifications_page.html index 8906585..5cb4e44 100644 --- a/static/notifications_page.html +++ b/static/notifications_page.html @@ -1,4 +1,6 @@

Notifications

- %s +
+ %s +
diff --git a/static/status.html b/static/status.html index fcb5d64..9fb9244 100644 --- a/static/status.html +++ b/static/status.html @@ -1,4 +1,5 @@ + %s