Files
nostr_terminal/src/menu_write.c
T

178 lines
4.7 KiB
C

#include "tui.h"
#include "publish.h"
#include "editor.h"
#include "../resources/nostr_core_lib/cjson/cJSON.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
typedef struct {
char *content;
} NtEditorSpawnCtx;
static int nt_editor_spawn(void *user) {
NtEditorSpawnCtx *ctx = (NtEditorSpawnCtx *)user;
if (!ctx) {
return -1;
}
ctx->content = editor_launch(NULL);
return 0;
}
static cJSON *write_make_blog_tags(const char *d_tag_value, const char *title_opt) {
cJSON *tags = cJSON_CreateArray();
cJSON *d_tag;
if (!tags) {
return NULL;
}
d_tag = cJSON_CreateArray();
if (!d_tag) {
cJSON_Delete(tags);
return NULL;
}
cJSON_AddItemToArray(d_tag, cJSON_CreateString("d"));
cJSON_AddItemToArray(d_tag, cJSON_CreateString(d_tag_value ? d_tag_value : ""));
cJSON_AddItemToArray(tags, d_tag);
if (title_opt && title_opt[0] != '\0') {
cJSON *title_tag = cJSON_CreateArray();
if (title_tag) {
cJSON_AddItemToArray(title_tag, cJSON_CreateString("title"));
cJSON_AddItemToArray(title_tag, cJSON_CreateString(title_opt));
cJSON_AddItemToArray(tags, title_tag);
}
}
return tags;
}
void menu_write(void) {
static const TuiMenuItem WRITE_ITEMS[] = {
{NT_HK("T", "weet it"), 't'},
{NT_HK("B", "log post (kind 30023)"), 'b'},
{NT_HK("D", "iary entry (kind 30024)"), 'd'},
{"E" NT_HK("x", "it without saving"), 'x'},
};
TuiFrame frame = nt_frame("> Main Menu > Write");
TuiStatus status = nt_status();
TuiMenu menu = {WRITE_ITEMS, 4};
TuiMenuState menu_state = {0};
NtEditorSpawnCtx ctx = {0};
char *content;
int idx;
tuin_render_header(&frame);
nt_print_reset();
tuin_render_footer(&status);
nt_log("Launching external editor...");
if (nt_run_external_editor(nt_editor_spawn, &ctx) != 0) {
tuin_notice("Failed to launch external editor.");
return;
}
content = ctx.content;
if (!content) {
tuin_notice("No content created.");
return;
}
nt_print_reset();
nt_log("Content length: %d bytes", (int)strlen(content));
idx = tuin_menu_run(&frame, &menu, &status, &menu_state);
if (idx == 0) {
int posted;
if (tuin_confirm("Publish as tweet?") == 1) {
posted = publish_signed_event_with_report("Tweet", 1, content, NULL, 8000);
if (posted < 0) {
tuin_notice("Tweet publish failed.");
} else {
nt_log("Tweet published.");
}
}
} else if (idx == 1) {
char title[256];
char slug[256];
const char *d_val;
cJSON *tags;
int posted;
if (tuin_prompt("title", "", title, sizeof(title)) != 0) {
free(content);
return;
}
if (tuin_prompt("slug (d tag) [empty uses title]", "", slug, sizeof(slug)) != 0) {
free(content);
return;
}
if (tuin_confirm("Publish as blog post?") != 1) {
free(content);
return;
}
d_val = (slug[0] != '\0') ? slug : title;
tags = write_make_blog_tags(d_val, title);
if (!tags) {
tuin_notice("Failed to build blog tags.");
} else {
posted = publish_signed_event_with_report("Blog post", 30023, content, tags, 8000);
cJSON_Delete(tags);
if (posted < 0) {
tuin_notice("Blog publish failed.");
} else {
nt_log("Blog post published.");
}
}
} else if (idx == 2) {
char date_d[16];
time_t now = time(NULL);
struct tm tmv;
cJSON *tags;
int posted;
if (tuin_confirm("Publish as diary entry?") != 1) {
free(content);
return;
}
memset(&tmv, 0, sizeof(tmv));
if (!localtime_r(&now, &tmv)) {
free(content);
tuin_notice("Failed to get local date.");
return;
}
if (strftime(date_d, sizeof(date_d), "%Y%m%d", &tmv) == 0) {
free(content);
tuin_notice("Failed to format diary date.");
return;
}
tags = write_make_blog_tags(date_d, NULL);
if (!tags) {
tuin_notice("Failed to build diary tags.");
} else {
posted = publish_signed_event_with_report("Diary", 30024, content, tags, 8000);
cJSON_Delete(tags);
if (posted < 0) {
tuin_notice("Diary publish failed.");
} else {
nt_log("Diary entry published.");
}
}
} else {
nt_log("Discarded.");
}
free(content);
}