Convert image links in notes to attachments.

This commit is contained in:
default 2023-05-21 20:32:23 +02:00
parent 3cd73311be
commit 49362f5404
5 changed files with 28 additions and 11 deletions

View file

@ -646,7 +646,7 @@ d_char *msg_actor(snac *snac)
msg = xs_dict_set(msg, "preferredUsername", snac->uid); msg = xs_dict_set(msg, "preferredUsername", snac->uid);
msg = xs_dict_set(msg, "published", xs_dict_get(snac->config, "published")); 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); msg = xs_dict_set(msg, "summary", f_bio);
char *folders[] = { "inbox", "outbox", "followers", "following", NULL }; 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 */ /* format the content */
fc2 = not_really_markdown(content); fc2 = not_really_markdown(content, &atls);
if (in_reply_to != NULL && *in_reply_to) { if (in_reply_to != NULL && *in_reply_to) {
xs *p_msg = NULL; xs *p_msg = NULL;

View file

@ -3,6 +3,7 @@
#include "xs.h" #include "xs.h"
#include "xs_regex.h" #include "xs_regex.h"
#include "xs_mime.h"
#include "snac.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 */ /* formats a line */
{ {
xs_str *s = xs_str_new(NULL); xs_str *s = xs_str_new(NULL);
@ -73,8 +74,24 @@ static xs_str *format_line(const char *line)
else else
if (xs_startswith(v, "http")) { if (xs_startswith(v, "http")) {
xs *v2 = xs_strip_chars_i(xs_dup(v), "."); xs *v2 = xs_strip_chars_i(xs_dup(v), ".");
xs *s1 = xs_fmt("<a href=\"%s\" target=\"_blank\">%s</a>", 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("<a href=\"%s\" target=\"_blank\">%s</a>", v2, v);
s = xs_str_cat(s, s1);
}
} }
else else
s = xs_str_cat(s, v); 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 */ /* formats a content using some Markdown rules */
{ {
xs_str *s = xs_str_new(NULL); xs_str *s = xs_str_new(NULL);
@ -119,7 +136,7 @@ xs_str *not_really_markdown(const char *content)
if (in_pre) if (in_pre)
ss = xs_dup(v); ss = xs_dup(v);
else else
ss = xs_strip_i(format_line(v)); ss = xs_strip_i(format_line(v, attach));
if (xs_startswith(ss, ">")) { if (xs_startswith(ss, ">")) {
/* delete the > and subsequent spaces */ /* delete the > and subsequent spaces */

4
html.c
View file

@ -322,7 +322,7 @@ d_char *html_user_header(snac *snac, d_char *s, int local)
s = xs_str_cat(s, s1); s = xs_str_cat(s, s1);
if (local) { 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("<div class=\"p-note snac-top-user-bio\">%s</div>\n", bio); xs *s1 = xs_fmt("<div class=\"p-note snac-top-user-bio\">%s</div>\n", bio);
s = xs_str_cat(s, s1); 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 **/ if (strcmp(p_path, ".rss") == 0) { /** public timeline in RSS format **/
d_char *rss; d_char *rss;
xs *elems = timeline_simple_list(&snac, "public", 0, 20); 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; char *p, *v;
/* escape tags */ /* escape tags */

2
main.c
View file

@ -87,7 +87,7 @@ int main(int argc, char *argv[])
if (strcmp(cmd, "markdown") == 0) { if (strcmp(cmd, "markdown") == 0) {
/* undocumented, for testing only */ /* undocumented, for testing only */
xs *c = xs_readall(stdin); xs *c = xs_readall(stdin);
xs *fc = not_really_markdown(c); xs *fc = not_really_markdown(c, NULL);
printf("<html>\n%s\n</html>\n", fc); printf("<html>\n%s\n</html>\n", fc);
return 0; return 0;

2
snac.h
View file

@ -228,7 +228,7 @@ int activitypub_post_handler(const xs_dict *req, const char *q_path,
char *payload, int p_size, char *payload, int p_size,
char **body, int *b_size, char **ctype); 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); xs_str *sanitize(const char *content);
int html_get_handler(const xs_dict *req, const char *q_path, int html_get_handler(const xs_dict *req, const char *q_path,