From 49362f54049a357e52cd6c57d2aa20d33add3307 Mon Sep 17 00:00:00 2001 From: default Date: Sun, 21 May 2023 20:32:23 +0200 Subject: [PATCH] Convert image links in notes to attachments. --- activitypub.c | 4 ++-- format.c | 27 ++++++++++++++++++++++----- html.c | 4 ++-- main.c | 2 +- snac.h | 2 +- 5 files changed, 28 insertions(+), 11 deletions(-) diff --git a/activitypub.c b/activitypub.c index 18ed25c..406aee8 100644 --- a/activitypub.c +++ b/activitypub.c @@ -646,7 +646,7 @@ d_char *msg_actor(snac *snac) msg = xs_dict_set(msg, "preferredUsername", snac->uid); msg = xs_dict_set(msg, "published", xs_dict_get(snac->config, "published")); - f_bio = not_really_markdown(xs_dict_get(snac->config, "bio")); + f_bio = not_really_markdown(xs_dict_get(snac->config, "bio"), NULL); msg = xs_dict_set(msg, "summary", f_bio); char *folders[] = { "inbox", "outbox", "followers", "following", NULL }; @@ -789,7 +789,7 @@ xs_dict *msg_note(snac *snac, const xs_str *content, const xs_val *rcpts, } /* format the content */ - fc2 = not_really_markdown(content); + fc2 = not_really_markdown(content, &atls); if (in_reply_to != NULL && *in_reply_to) { xs *p_msg = NULL; diff --git a/format.c b/format.c index 14508b9..5eec404 100644 --- a/format.c +++ b/format.c @@ -3,6 +3,7 @@ #include "xs.h" #include "xs_regex.h" +#include "xs_mime.h" #include "snac.h" @@ -38,7 +39,7 @@ struct { }; -static xs_str *format_line(const char *line) +static xs_str *format_line(const char *line, xs_list **attach) /* formats a line */ { xs_str *s = xs_str_new(NULL); @@ -73,8 +74,24 @@ static xs_str *format_line(const char *line) else if (xs_startswith(v, "http")) { xs *v2 = xs_strip_chars_i(xs_dup(v), "."); - xs *s1 = xs_fmt("%s", v2, v); - s = xs_str_cat(s, s1); + + const char *mime = xs_mime_by_ext(v2); + + if (attach != NULL && xs_startswith(mime, "image/")) { + /* if it's a link to an image, insert it as an attachment */ + xs *d = xs_dict_new(); + + d = xs_dict_append(d, "mediaType", mime); + d = xs_dict_append(d, "url", v2); + d = xs_dict_append(d, "name", ""); + d = xs_dict_append(d, "type", "Image"); + + *attach = xs_list_append(*attach, d); + } + else { + xs *s1 = xs_fmt("%s", v2, v); + s = xs_str_cat(s, s1); + } } else s = xs_str_cat(s, v); @@ -90,7 +107,7 @@ static xs_str *format_line(const char *line) } -xs_str *not_really_markdown(const char *content) +xs_str *not_really_markdown(const char *content, xs_list **attach) /* formats a content using some Markdown rules */ { xs_str *s = xs_str_new(NULL); @@ -119,7 +136,7 @@ xs_str *not_really_markdown(const char *content) if (in_pre) ss = xs_dup(v); else - ss = xs_strip_i(format_line(v)); + ss = xs_strip_i(format_line(v, attach)); if (xs_startswith(ss, ">")) { /* delete the > and subsequent spaces */ diff --git a/html.c b/html.c index e2760ca..6b9de20 100644 --- a/html.c +++ b/html.c @@ -322,7 +322,7 @@ d_char *html_user_header(snac *snac, d_char *s, int local) s = xs_str_cat(s, s1); if (local) { - xs *bio = not_really_markdown(xs_dict_get(snac->config, "bio")); + xs *bio = not_really_markdown(xs_dict_get(snac->config, "bio"), NULL); xs *s1 = xs_fmt("
%s
\n", bio); s = xs_str_cat(s, s1); @@ -1467,7 +1467,7 @@ int html_get_handler(const xs_dict *req, const char *q_path, if (strcmp(p_path, ".rss") == 0) { /** public timeline in RSS format **/ d_char *rss; xs *elems = timeline_simple_list(&snac, "public", 0, 20); - xs *bio = not_really_markdown(xs_dict_get(snac.config, "bio")); + xs *bio = not_really_markdown(xs_dict_get(snac.config, "bio"), NULL); char *p, *v; /* escape tags */ diff --git a/main.c b/main.c index 1462c64..b325717 100644 --- a/main.c +++ b/main.c @@ -87,7 +87,7 @@ int main(int argc, char *argv[]) if (strcmp(cmd, "markdown") == 0) { /* undocumented, for testing only */ xs *c = xs_readall(stdin); - xs *fc = not_really_markdown(c); + xs *fc = not_really_markdown(c, NULL); printf("\n%s\n\n", fc); return 0; diff --git a/snac.h b/snac.h index 5ee86b4..441fa7e 100644 --- a/snac.h +++ b/snac.h @@ -228,7 +228,7 @@ int activitypub_post_handler(const xs_dict *req, const char *q_path, char *payload, int p_size, char **body, int *b_size, char **ctype); -xs_str *not_really_markdown(const char *content); +xs_str *not_really_markdown(const char *content, xs_list **attach); xs_str *sanitize(const char *content); int html_get_handler(const xs_dict *req, const char *q_path,