summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authordefault <nobody@localhost>2023-05-21 20:32:23 +0200
committerdefault <nobody@localhost>2023-05-21 20:32:23 +0200
commit49362f54049a357e52cd6c57d2aa20d33add3307 (patch)
tree0417bac6b8157ce7be07be86b7eb6915bb24cdf7
parent3cd73311bece7783bbcc3774f5fd54c520eb88ed (diff)
Convert image links in notes to attachments.
-rw-r--r--activitypub.c4
-rw-r--r--format.c27
-rw-r--r--html.c4
-rw-r--r--main.c2
-rw-r--r--snac.h2
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("<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
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("<div class=\"p-note snac-top-user-bio\">%s</div>\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("<html>\n%s\n</html>\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,