From 0d8a040d9086cd7a9e13a7b070e425df9201f7e5 Mon Sep 17 00:00:00 2001 From: default Date: Wed, 24 May 2023 11:49:16 +0200 Subject: [PATCH] Added some experimental 'Question' posting code. --- activitypub.c | 20 +++++++++++++------ html.c | 4 +++- main.c | 53 ++++++++++++++++++++++++++++++++++----------------- snac.h | 2 +- 4 files changed, 54 insertions(+), 25 deletions(-) diff --git a/activitypub.c b/activitypub.c index a5e23de..82f55f3 100644 --- a/activitypub.c +++ b/activitypub.c @@ -678,14 +678,22 @@ d_char *msg_actor(snac *snac) } -d_char *msg_create(snac *snac, char *object) +xs_dict *msg_create(snac *snac, const xs_dict *object) /* creates a 'Create' message */ { - d_char *msg = msg_base(snac, "Create", "@object", snac->actor, "@now", object); + xs_dict *msg = msg_base(snac, "Create", "@object", snac->actor, "@now", object); + xs_val *v; - msg = xs_dict_append(msg, "attributedTo", xs_dict_get(object, "attributedTo")); - msg = xs_dict_append(msg, "to", xs_dict_get(object, "to")); - msg = xs_dict_append(msg, "cc", xs_dict_get(object, "cc")); + if ((v = xs_dict_get(object, "attributedTo"))) + msg = xs_dict_append(msg, "attributedTo", v); + + if ((v = xs_dict_get(object, "cc"))) + msg = xs_dict_append(msg, "cc", v); + + if ((v = xs_dict_get(object, "to"))) + msg = xs_dict_append(msg, "to", v); + else + msg = xs_dict_append(msg, "to", public_address); return msg; } @@ -951,7 +959,7 @@ xs_dict *msg_question(snac *user, const char *content, const xs_list *opts, int time_t t = time(NULL) + end_secs; xs *et = xs_str_utctime(t, "%Y-%m-%dT%H:%M:%SZ"); - msg = xs_dict_append(msg, "endTime", msg); + msg = xs_dict_append(msg, "endTime", et); return msg; } diff --git a/html.c b/html.c index 8d4adfb..518ac1f 100644 --- a/html.c +++ b/html.c @@ -852,7 +852,9 @@ xs_str *html_entry(snac *snac, xs_str *os, const xs_dict *msg, int local, #endif { - xs *c = sanitize(xs_dict_get(msg, "content")); + const char *content = xs_dict_get(msg, "content"); + + xs *c = sanitize(xs_is_null(content) ? "" : content); char *p, *v; /* do some tweaks to the content */ diff --git a/main.c b/main.c index b325717..32952f7 100644 --- a/main.c +++ b/main.c @@ -30,6 +30,7 @@ int usage(void) printf("note {basedir} {uid} {'text'} Sends a note to followers\n"); printf("resetpwd {basedir} {uid} Resets the password of a user\n"); printf("ping {basedir} {uid} {actor} Pings an actor\n"); +/* printf("question {basedir} {uid} 'opts' Generates a poll (;-separated opts)\n");*/ return 1; } @@ -63,7 +64,7 @@ int main(int argc, char *argv[]) if ((cmd = GET_ARGV()) == NULL) return usage(); - if (strcmp(cmd, "init") == 0) { + if (strcmp(cmd, "init") == 0) { /** **/ /* initialize the data storage */ /* ... */ basedir = GET_ARGV(); @@ -71,7 +72,7 @@ int main(int argc, char *argv[]) return snac_init(basedir); } - if (strcmp(cmd, "upgrade") == 0) { + if (strcmp(cmd, "upgrade") == 0) { /** **/ int ret; /* upgrade */ @@ -84,7 +85,7 @@ int main(int argc, char *argv[]) return ret; } - if (strcmp(cmd, "markdown") == 0) { + if (strcmp(cmd, "markdown") == 0) { /** **/ /* undocumented, for testing only */ xs *c = xs_readall(stdin); xs *fc = not_really_markdown(c, NULL); @@ -101,7 +102,7 @@ int main(int argc, char *argv[]) return 1; } - if (strcmp(cmd, "adduser") == 0) { + if (strcmp(cmd, "adduser") == 0) { /** **/ user = GET_ARGV(); return adduser(user); @@ -109,13 +110,13 @@ int main(int argc, char *argv[]) return 0; } - if (strcmp(cmd, "httpd") == 0) { + if (strcmp(cmd, "httpd") == 0) { /** **/ httpd(); srv_free(); return 0; } - if (strcmp(cmd, "purge") == 0) { + if (strcmp(cmd, "purge") == 0) { /** **/ purge_all(); return 0; } @@ -123,7 +124,7 @@ int main(int argc, char *argv[]) if ((user = GET_ARGV()) == NULL) return usage(); - if (strcmp(cmd, "webfinger") == 0) { + if (strcmp(cmd, "webfinger") == 0) { /** **/ xs *actor = NULL; xs *uid = NULL; int status; @@ -146,16 +147,16 @@ int main(int argc, char *argv[]) lastlog_write(&snac, "cmdline"); - if (strcmp(cmd, "resetpwd") == 0) { + if (strcmp(cmd, "resetpwd") == 0) { /** **/ return resetpwd(&snac); } - if (strcmp(cmd, "queue") == 0) { + if (strcmp(cmd, "queue") == 0) { /** **/ process_user_queue(&snac); return 0; } - if (strcmp(cmd, "timeline") == 0) { + if (strcmp(cmd, "timeline") == 0) { /** **/ #if 0 xs *list = local_list(&snac, XS_ALL); xs *body = html_timeline(&snac, list, 1); @@ -178,7 +179,7 @@ int main(int argc, char *argv[]) if ((url = GET_ARGV()) == NULL) return usage(); - if (strcmp(cmd, "announce") == 0) { + if (strcmp(cmd, "announce") == 0) { /** **/ xs *msg = msg_admiration(&snac, url, "Announce"); if (msg != NULL) { @@ -193,7 +194,7 @@ int main(int argc, char *argv[]) return 0; } - if (strcmp(cmd, "follow") == 0) { + if (strcmp(cmd, "follow") == 0) { /** **/ xs *msg = msg_follow(&snac, url); if (msg != NULL) { @@ -212,7 +213,7 @@ int main(int argc, char *argv[]) return 0; } - if (strcmp(cmd, "unfollow") == 0) { + if (strcmp(cmd, "unfollow") == 0) { /** **/ xs *object = NULL; if (valid_status(following_get(&snac, url, &object))) { @@ -230,7 +231,7 @@ int main(int argc, char *argv[]) return 0; } - if (strcmp(cmd, "ping") == 0) { + if (strcmp(cmd, "ping") == 0) { /** **/ xs *actor_o = NULL; if (valid_status(actor_request(&snac, url, &actor_o))) { @@ -251,7 +252,25 @@ int main(int argc, char *argv[]) return 0; } - if (strcmp(cmd, "request") == 0) { + if (strcmp(cmd, "question") == 0) { /** **/ + xs *opts = xs_split(url, ";"); + + xs *msg = msg_question(&snac, "Poll", opts, 0, 5 * 60); + xs *c_msg = msg_create(&snac, msg); + + if (dbglevel) { + xs *j = xs_json_dumps_pp(c_msg, 4); + printf("%s\n", j); + } + + enqueue_message(&snac, c_msg); + + timeline_add(&snac, xs_dict_get(msg, "id"), msg); + + return 0; + } + + if (strcmp(cmd, "request") == 0) { /** **/ int status; xs *data = NULL; @@ -267,7 +286,7 @@ int main(int argc, char *argv[]) return 0; } - if (strcmp(cmd, "actor") == 0) { + if (strcmp(cmd, "actor") == 0) { /** **/ int status; xs *data = NULL; @@ -283,7 +302,7 @@ int main(int argc, char *argv[]) return 0; } - if (strcmp(cmd, "note") == 0) { + if (strcmp(cmd, "note") == 0) { /** **/ xs *content = NULL; xs *msg = NULL; xs *c_msg = NULL; diff --git a/snac.h b/snac.h index bda95a3..d83eddb 100644 --- a/snac.h +++ b/snac.h @@ -193,7 +193,7 @@ int webfinger_get_handler(xs_dict *req, char *q_path, const char *default_avatar_base64(void); d_char *msg_admiration(snac *snac, char *object, char *type); -d_char *msg_create(snac *snac, char *object); +xs_dict *msg_create(snac *snac, const xs_dict *object); xs_dict *msg_follow(snac *snac, const char *actor); xs_dict *msg_note(snac *snac, const xs_str *content, const xs_val *rcpts,