Browse Source

Convert image links in notes to attachments.

default 1 year ago
parent
commit
49362f5404
5 changed files with 28 additions and 11 deletions
  1. 2 2
      activitypub.c
  2. 22 5
      format.c
  3. 2 2
      html.c
  4. 1 1
      main.c
  5. 1 1
      snac.h

+ 2 - 2
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;

+ 22 - 5
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 */

+ 2 - 2
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 */

+ 1 - 1
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;

+ 1 - 1
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,