From 78ec3b077b989bcae5950d75f4ee0cb03bd10030 Mon Sep 17 00:00:00 2001 From: default Date: Mon, 10 Oct 2022 09:03:07 +0200 Subject: [PATCH] Media can be attached to notes. The web interface limits this (by now) to only one attachment, given the URL. --- activitypub.c | 30 +++++++++++++++++++++++++++++- html.c | 17 +++++++++++++---- main.c | 2 +- snac.h | 2 +- 4 files changed, 44 insertions(+), 7 deletions(-) diff --git a/activitypub.c b/activitypub.c index 8771ee8..fb790d5 100644 --- a/activitypub.c +++ b/activitypub.c @@ -508,7 +508,7 @@ d_char *msg_follow(snac *snac, char *actor) } -d_char *msg_note(snac *snac, char *content, char *rcpts, char *in_reply_to) +d_char *msg_note(snac *snac, char *content, char *rcpts, char *in_reply_to, char *attach) /* creates a 'Note' message */ { xs *ntid = tid(0); @@ -520,6 +520,7 @@ d_char *msg_note(snac *snac, char *content, char *rcpts, char *in_reply_to) xs *cc = xs_list_new(); xs *irt = NULL; xs *tag = NULL; + xs *atls = NULL; d_char *msg = msg_base(snac, "Note", id, NULL, "@now", NULL); char *p, *v; @@ -561,6 +562,30 @@ d_char *msg_note(snac *snac, char *content, char *rcpts, char *in_reply_to) else irt = xs_val_new(XSTYPE_NULL); + /* create the attachment list, if there are any */ + if (!xs_is_null(attach) && *attach != '\0') { + xs *lsof1 = NULL; + + if (xs_type(attach) == XSTYPE_STRING) { + lsof1 = xs_list_append(xs_list_new(), attach); + attach = lsof1; + } + + atls = xs_list_new(); + while (xs_list_iter(&attach, &v)) { + xs *d = xs_dict_new(); + char *mime = xs_mime_by_ext(v); + + d = xs_dict_append(d, "mediaType", mime); + d = xs_dict_append(d, "url", v); + d = xs_dict_append(d, "name", ""); + d = xs_dict_append(d, "type", + xs_startswith(mime, "image/") ? "Image" : "Document"); + + atls = xs_list_append(atls, d); + } + } + if (tag == NULL) tag = xs_list_new(); @@ -594,6 +619,9 @@ d_char *msg_note(snac *snac, char *content, char *rcpts, char *in_reply_to) msg = xs_dict_append(msg, "inReplyTo", irt); msg = xs_dict_append(msg, "tag", tag); + if (atls != NULL) + msg = xs_dict_append(msg, "attachment", atls); + return msg; } diff --git a/html.c b/html.c index 167d685..a6b6f0e 100644 --- a/html.c +++ b/html.c @@ -195,6 +195,7 @@ d_char *html_top_controls(snac *snac, d_char *s) "rows=\"8\" wrap=\"virtual\" required=\"required\">\n" "\n" "\n" + " Image URL

" "

\n" "\n" @@ -204,12 +205,12 @@ d_char *html_top_controls(snac *snac, d_char *s) "

\n" "\n" " %s\n" - "

\n" + "

\n" "

\n" "\n" " %s\n" - "

\n" + "

\n" "

%s\n" @@ -222,7 +223,7 @@ d_char *html_top_controls(snac *snac, d_char *s) "

\n" "

%s:
\n" - "

\n" + "

\n" "

%s:
\n" "

\n" @@ -854,16 +855,24 @@ int html_post_handler(d_char *req, char *q_path, d_char *payload, int p_size, p_vars = xs_dict_get(req, "p_vars"); +#if 0 + { + xs *j1 = xs_json_dumps_pp(p_vars, 4); + printf("%s\n", j1); + } +#endif + if (p_path && strcmp(p_path, "admin/note") == 0) { /* post note */ char *content = xs_dict_get(p_vars, "content"); char *in_reply_to = xs_dict_get(p_vars, "in_reply_to"); + char *attach_url = xs_dict_get(p_vars, "attach_url"); if (content != NULL) { xs *msg = NULL; xs *c_msg = NULL; - msg = msg_note(&snac, content, NULL, in_reply_to); + msg = msg_note(&snac, content, NULL, in_reply_to, attach_url); c_msg = msg_create(&snac, msg); diff --git a/main.c b/main.c index 52eca27..188b92f 100644 --- a/main.c +++ b/main.c @@ -235,7 +235,7 @@ int main(int argc, char *argv[]) else content = xs_dup(url); - msg = msg_note(&snac, content, NULL, in_reply_to); + msg = msg_note(&snac, content, NULL, in_reply_to, NULL); c_msg = msg_create(&snac, msg); diff --git a/snac.h b/snac.h index b1b52c4..12bfbfa 100644 --- a/snac.h +++ b/snac.h @@ -111,7 +111,7 @@ int webfinger_get_handler(d_char *req, char *q_path, d_char *msg_admiration(snac *snac, char *object, char *type); d_char *msg_create(snac *snac, char *object); d_char *msg_follow(snac *snac, char *actor); -d_char *msg_note(snac *snac, char *content, char *rcpts, char *in_reply_to); +d_char *msg_note(snac *snac, char *content, char *rcpts, char *in_reply_to, char *attach); d_char *msg_undo(snac *snac, char *object); d_char *msg_delete(snac *snac, char *id); d_char *msg_actor(snac *snac);