commit 3a1cae7

default  ·  2025-01-05 08:34:00 +0000 UTC
parent 17d7e00
Moved post language setting to msg_note(), where it really belongs.

On the way, posting from the Mastodon API sets the language accordingly.
5 files changed,  +26, -24
M html.c
M main.c
M snac.h
+17, -2
 1@@ -1422,7 +1422,8 @@ xs_dict *msg_follow(snac *snac, const char *q)
 2 
 3 
 4 xs_dict *msg_note(snac *snac, const xs_str *content, const xs_val *rcpts,
 5-                  const xs_str *in_reply_to, const xs_list *attach, int priv)
 6+                  const xs_str *in_reply_to, const xs_list *attach,
 7+                  int priv, const char *lang_str)
 8 /* creates a 'Note' message */
 9 {
10     xs *ntid = tid(0);
11@@ -1584,6 +1585,20 @@ xs_dict *msg_note(snac *snac, const xs_str *content, const xs_val *rcpts,
12     if (xs_list_len(atls))
13         msg = xs_dict_append(msg, "attachment", atls);
14 
15+    /* set language content map */
16+    if (xs_type(lang_str) == XSTYPE_STRING) {
17+        /* split at the first _ */
18+        xs *l0 = xs_split(lang_str, "_");
19+        const char *lang = xs_list_get(l0, 0);
20+
21+        if (xs_type(lang) == XSTYPE_STRING && strlen(lang) == 2) {
22+            /* a valid ISO language id */
23+            xs *cmap = xs_dict_new();
24+            cmap = xs_dict_set(cmap, lang, xs_dict_get(msg, "content"));
25+            msg = xs_dict_set(msg, "contentMap", cmap);
26+        }
27+    }
28+
29     return msg;
30 }
31 
32@@ -1625,7 +1640,7 @@ xs_dict *msg_question(snac *user, const char *content, xs_list *attach,
33                       const xs_list *opts, int multiple, int end_secs)
34 /* creates a Question message */
35 {
36-    xs_dict *msg = msg_note(user, content, NULL, NULL, attach, 0);
37+    xs_dict *msg = msg_note(user, content, NULL, NULL, attach, 0, NULL);
38     int max      = 8;
39     xs_set seen;
40 
M html.c
+3, -3
 1@@ -3515,7 +3515,7 @@ int html_get_handler(const xs_dict *req, const char *q_path,
 2             const char *b64 = xs_dict_get(q_vars, "content");
 3             int sz;
 4             xs *content = xs_base64_dec(b64, &sz);
 5-            xs *msg = msg_note(&snac, content, NULL, NULL, NULL, 0);
 6+            xs *msg = msg_note(&snac, content, NULL, NULL, NULL, 0, NULL);
 7             xs *c_msg = msg_create(&snac, msg);
 8 
 9             timeline_add(&snac, xs_dict_get(msg, "id"), msg);
10@@ -3661,7 +3661,7 @@ int html_post_handler(const xs_dict *req, const char *q_path,
11                 enqueue_close_question(&snac, xs_dict_get(msg, "id"), end_secs);
12             }
13             else
14-                msg = msg_note(&snac, content_2, to, in_reply_to, attach_list, priv);
15+                msg = msg_note(&snac, content_2, to, in_reply_to, attach_list, priv, NULL);
16 
17             if (sensitive != NULL) {
18                 msg = xs_dict_set(msg, "sensitive", xs_stock(XSTYPE_TRUE));
19@@ -4095,7 +4095,7 @@ int html_post_handler(const xs_dict *req, const char *q_path,
20         int c = 0;
21 
22         while (xs_list_next(ls, &v, &c)) {
23-            xs *msg = msg_note(&snac, "", actor, irt, NULL, 1);
24+            xs *msg = msg_note(&snac, "", actor, irt, NULL, 1, NULL);
25 
26             /* set the option */
27             msg = xs_dict_append(msg, "name", v);
M main.c
+1, -16
 1@@ -667,22 +667,7 @@ int main(int argc, char *argv[])
 2         else
 3             content = xs_dup(url);
 4 
 5-        msg = msg_note(&snac, content, NULL, NULL, attl, 0);
 6-
 7-        /* set a post language according the LANG environment variable */
 8-        const char *lang_env = getenv("LANG");
 9-        if (xs_type(lang_env) == XSTYPE_STRING) {
10-            /* split at the first _ */
11-            xs *l0 = xs_split(lang_env, "_");
12-            const char *lang = xs_list_get(l0, 0);
13-
14-            if (xs_type(lang) == XSTYPE_STRING && strlen(lang) == 2) {
15-                /* a valid ISO language id */
16-                xs *cmap = xs_dict_new();
17-                cmap = xs_dict_set(cmap, lang, xs_dict_get(msg, "content"));
18-                msg = xs_dict_set(msg, "contentMap", cmap);
19-            }
20-        }
21+        msg = msg_note(&snac, content, NULL, NULL, attl, 0, getenv("LANG"));
22 
23         c_msg = msg_create(&snac, msg);
24 
+3, -2
 1@@ -2600,6 +2600,7 @@ int mastoapi_post_handler(const xs_dict *req, const char *q_path,
 2             const char *visibility = xs_dict_get(args, "visibility");
 3             const char *summary    = xs_dict_get(args, "spoiler_text");
 4             const char *media_ids  = xs_dict_get(args, "media_ids");
 5+            const char *language   = xs_dict_get(args, "language");
 6 
 7             if (xs_is_null(media_ids))
 8                 media_ids = xs_dict_get(args, "media_ids[]");
 9@@ -2650,7 +2651,7 @@ int mastoapi_post_handler(const xs_dict *req, const char *q_path,
10 
11             /* prepare the message */
12             xs *msg = msg_note(&snac, content, NULL, irt, attach_list,
13-                        strcmp(visibility, "public") == 0 ? 0 : 1);
14+                        strcmp(visibility, "public") == 0 ? 0 : 1, language);
15 
16             if (!xs_is_null(summary) && *summary) {
17                 msg = xs_dict_set(msg, "sensitive", xs_stock(XSTYPE_TRUE));
18@@ -3000,7 +3001,7 @@ int mastoapi_post_handler(const xs_dict *req, const char *q_path,
19                                 if (o) {
20                                     const char *name = xs_dict_get(o, "name");
21 
22-                                    xs *msg = msg_note(&snac, "", atto, (char *)id, NULL, 1);
23+                                    xs *msg = msg_note(&snac, "", atto, (char *)id, NULL, 1, NULL);
24                                     msg = xs_dict_append(msg, "name", name);
25 
26                                     xs *c_msg = msg_create(&snac, msg);
M snac.h
+2, -1
 1@@ -314,7 +314,8 @@ xs_dict *msg_create(snac *snac, const xs_dict *object);
 2 xs_dict *msg_follow(snac *snac, const char *actor);
 3 
 4 xs_dict *msg_note(snac *snac, const xs_str *content, const xs_val *rcpts,
 5-                  const xs_str *in_reply_to, const xs_list *attach, int priv);
 6+                  const xs_str *in_reply_to, const xs_list *attach,
 7+                  int priv, const char *lang);
 8 
 9 xs_dict *msg_undo(snac *snac, const xs_val *object);
10 xs_dict *msg_delete(snac *snac, const char *id);