commit 3a1cae73265aebdbe678d94fbdc16edf3ccc643f
parent 17d7e0027abb48040ef989b1894bcd0bc28c9faa
Author: default <nobody@localhost>
Date: Sun, 5 Jan 2025 09:34:00 +0100
Moved post language setting to msg_note(), where it really belongs.
On the way, posting from the Mastodon API sets the language accordingly.
Diffstat:
5 files changed, 26 insertions(+), 24 deletions(-)
diff --git a/activitypub.c b/activitypub.c
@@ -1422,7 +1422,8 @@ xs_dict *msg_follow(snac *snac, const char *q)
xs_dict *msg_note(snac *snac, const xs_str *content, const xs_val *rcpts,
- const xs_str *in_reply_to, const xs_list *attach, int priv)
+ const xs_str *in_reply_to, const xs_list *attach,
+ int priv, const char *lang_str)
/* creates a 'Note' message */
{
xs *ntid = tid(0);
@@ -1584,6 +1585,20 @@ xs_dict *msg_note(snac *snac, const xs_str *content, const xs_val *rcpts,
if (xs_list_len(atls))
msg = xs_dict_append(msg, "attachment", atls);
+ /* set language content map */
+ if (xs_type(lang_str) == XSTYPE_STRING) {
+ /* split at the first _ */
+ xs *l0 = xs_split(lang_str, "_");
+ const char *lang = xs_list_get(l0, 0);
+
+ if (xs_type(lang) == XSTYPE_STRING && strlen(lang) == 2) {
+ /* a valid ISO language id */
+ xs *cmap = xs_dict_new();
+ cmap = xs_dict_set(cmap, lang, xs_dict_get(msg, "content"));
+ msg = xs_dict_set(msg, "contentMap", cmap);
+ }
+ }
+
return msg;
}
@@ -1625,7 +1640,7 @@ xs_dict *msg_question(snac *user, const char *content, xs_list *attach,
const xs_list *opts, int multiple, int end_secs)
/* creates a Question message */
{
- xs_dict *msg = msg_note(user, content, NULL, NULL, attach, 0);
+ xs_dict *msg = msg_note(user, content, NULL, NULL, attach, 0, NULL);
int max = 8;
xs_set seen;
diff --git a/html.c b/html.c
@@ -3515,7 +3515,7 @@ int html_get_handler(const xs_dict *req, const char *q_path,
const char *b64 = xs_dict_get(q_vars, "content");
int sz;
xs *content = xs_base64_dec(b64, &sz);
- xs *msg = msg_note(&snac, content, NULL, NULL, NULL, 0);
+ xs *msg = msg_note(&snac, content, NULL, NULL, NULL, 0, NULL);
xs *c_msg = msg_create(&snac, msg);
timeline_add(&snac, xs_dict_get(msg, "id"), msg);
@@ -3661,7 +3661,7 @@ int html_post_handler(const xs_dict *req, const char *q_path,
enqueue_close_question(&snac, xs_dict_get(msg, "id"), end_secs);
}
else
- msg = msg_note(&snac, content_2, to, in_reply_to, attach_list, priv);
+ msg = msg_note(&snac, content_2, to, in_reply_to, attach_list, priv, NULL);
if (sensitive != NULL) {
msg = xs_dict_set(msg, "sensitive", xs_stock(XSTYPE_TRUE));
@@ -4095,7 +4095,7 @@ int html_post_handler(const xs_dict *req, const char *q_path,
int c = 0;
while (xs_list_next(ls, &v, &c)) {
- xs *msg = msg_note(&snac, "", actor, irt, NULL, 1);
+ xs *msg = msg_note(&snac, "", actor, irt, NULL, 1, NULL);
/* set the option */
msg = xs_dict_append(msg, "name", v);
diff --git a/main.c b/main.c
@@ -667,22 +667,7 @@ int main(int argc, char *argv[])
else
content = xs_dup(url);
- msg = msg_note(&snac, content, NULL, NULL, attl, 0);
-
- /* set a post language according the LANG environment variable */
- const char *lang_env = getenv("LANG");
- if (xs_type(lang_env) == XSTYPE_STRING) {
- /* split at the first _ */
- xs *l0 = xs_split(lang_env, "_");
- const char *lang = xs_list_get(l0, 0);
-
- if (xs_type(lang) == XSTYPE_STRING && strlen(lang) == 2) {
- /* a valid ISO language id */
- xs *cmap = xs_dict_new();
- cmap = xs_dict_set(cmap, lang, xs_dict_get(msg, "content"));
- msg = xs_dict_set(msg, "contentMap", cmap);
- }
- }
+ msg = msg_note(&snac, content, NULL, NULL, attl, 0, getenv("LANG"));
c_msg = msg_create(&snac, msg);
diff --git a/mastoapi.c b/mastoapi.c
@@ -2600,6 +2600,7 @@ int mastoapi_post_handler(const xs_dict *req, const char *q_path,
const char *visibility = xs_dict_get(args, "visibility");
const char *summary = xs_dict_get(args, "spoiler_text");
const char *media_ids = xs_dict_get(args, "media_ids");
+ const char *language = xs_dict_get(args, "language");
if (xs_is_null(media_ids))
media_ids = xs_dict_get(args, "media_ids[]");
@@ -2650,7 +2651,7 @@ int mastoapi_post_handler(const xs_dict *req, const char *q_path,
/* prepare the message */
xs *msg = msg_note(&snac, content, NULL, irt, attach_list,
- strcmp(visibility, "public") == 0 ? 0 : 1);
+ strcmp(visibility, "public") == 0 ? 0 : 1, language);
if (!xs_is_null(summary) && *summary) {
msg = xs_dict_set(msg, "sensitive", xs_stock(XSTYPE_TRUE));
@@ -3000,7 +3001,7 @@ int mastoapi_post_handler(const xs_dict *req, const char *q_path,
if (o) {
const char *name = xs_dict_get(o, "name");
- xs *msg = msg_note(&snac, "", atto, (char *)id, NULL, 1);
+ xs *msg = msg_note(&snac, "", atto, (char *)id, NULL, 1, NULL);
msg = xs_dict_append(msg, "name", name);
xs *c_msg = msg_create(&snac, msg);
diff --git a/snac.h b/snac.h
@@ -314,7 +314,8 @@ 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,
- const xs_str *in_reply_to, const xs_list *attach, int priv);
+ const xs_str *in_reply_to, const xs_list *attach,
+ int priv, const char *lang);
xs_dict *msg_undo(snac *snac, const xs_val *object);
xs_dict *msg_delete(snac *snac, const char *id);