commit 82bcc4b

default  ·  2025-01-27 15:59:08 +0000 UTC
parent 6097ba5
Minor optimization in timeline retrieving.

Functions now receive an optional int *more, set to 1 if
there are more than the 'show' requested.
5 files changed,  +33, -24
M data.c
M html.c
M snac.h
+1, -1
1@@ -3081,7 +3081,7 @@ int activitypub_get_handler(const xs_dict *req, const char *q_path,
2         int cnt = xs_number_get(xs_dict_get_def(srv_config, "max_public_entries", "20"));
3 
4         /* get the public outbox or the pinned list */
5-        xs *elems = *p_path == 'o' ? timeline_simple_list(&snac, "public", 0, cnt) : pinned_list(&snac);
6+        xs *elems = *p_path == 'o' ? timeline_simple_list(&snac, "public", 0, cnt, NULL) : pinned_list(&snac);
7 
8         xs_list_foreach(elems, v) {
9             xs *i = NULL;
M data.c
+18, -6
 1@@ -1489,16 +1489,28 @@ xs_str *user_index_fn(snac *user, const char *idx_name)
 2 }
 3 
 4 
 5-xs_list *timeline_simple_list(snac *user, const char *idx_name, int skip, int show)
 6+xs_list *timeline_simple_list(snac *user, const char *idx_name, int skip, int show, int *more)
 7 /* returns a timeline (with all entries) */
 8 {
 9     xs *idx = user_index_fn(user, idx_name);
10 
11-    return index_list_desc(idx, skip, show);
12+    /* if a more flag is sent, request one more */
13+    xs_list *lst = index_list_desc(idx, skip, show + (more != NULL ? 1 : 0));
14+
15+    if (more != NULL) {
16+        if (xs_list_len(lst) > show) {
17+            *more = 1;
18+            lst = xs_list_del(lst, -1);
19+        }
20+        else
21+            *more = 0;
22+    }
23+
24+    return lst;
25 }
26 
27 
28-xs_list *timeline_list(snac *snac, const char *idx_name, int skip, int show)
29+xs_list *timeline_list(snac *snac, const char *idx_name, int skip, int show, int *more)
30 /* returns a timeline (only top level entries) */
31 {
32     int c_max;
33@@ -1510,7 +1522,7 @@ xs_list *timeline_list(snac *snac, const char *idx_name, int skip, int show)
34     if (show > c_max)
35         show = c_max;
36 
37-    xs *list = timeline_simple_list(snac, idx_name, skip, show);
38+    xs *list = timeline_simple_list(snac, idx_name, skip, show, more);
39 
40     return timeline_top_level(snac, list);
41 }
42@@ -2709,9 +2721,9 @@ xs_list *content_search(snac *user, const char *regex,
43     const char *md5s[3] = {0};
44     int c[3] = {0};
45 
46-    tls[0] = timeline_simple_list(user, "public", 0, XS_ALL);   /* public */
47+    tls[0] = timeline_simple_list(user, "public", 0, XS_ALL, NULL);   /* public */
48     tls[1] = timeline_instance_list(0, XS_ALL); /* instance */
49-    tls[2] = priv ? timeline_simple_list(user, "private", 0, XS_ALL) : xs_list_new(); /* private or none */
50+    tls[2] = priv ? timeline_simple_list(user, "private", 0, XS_ALL, NULL) : xs_list_new(); /* private or none */
51 
52     /* first positioning */
53     for (int n = 0; n < 3; n++)
M html.c
+11, -14
 1@@ -3314,21 +3314,17 @@ int html_get_handler(const xs_dict *req, const char *q_path,
 2         }
 3         else {
 4             xs *list = NULL;
 5-            xs *next = NULL;
 6+            int more = 0;
 7 
 8-            if (xs_is_true(xs_dict_get(srv_config, "strict_public_timelines"))) {
 9-                list = timeline_simple_list(&snac, "public", skip, show);
10-                next = timeline_simple_list(&snac, "public", skip + show, 1);
11-            }
12-            else {
13-                list = timeline_list(&snac, "public", skip, show);
14-                next = timeline_list(&snac, "public", skip + show, 1);
15-            }
16+            if (xs_is_true(xs_dict_get(srv_config, "strict_public_timelines")))
17+                list = timeline_simple_list(&snac, "public", skip, show, &more);
18+            else 
19+                list = timeline_list(&snac, "public", skip, show, &more);
20 
21             xs *pins = pinned_list(&snac);
22             pins = xs_list_cat(pins, list);
23 
24-            *body = html_timeline(&snac, pins, 1, skip, show, xs_list_len(next), NULL, "", 1, error);
25+            *body = html_timeline(&snac, pins, 1, skip, show, more, NULL, "", 1, error);
26 
27             *b_size = strlen(*body);
28             status  = HTTP_STATUS_OK;
29@@ -3490,13 +3486,14 @@ int html_get_handler(const xs_dict *req, const char *q_path,
30                                 xs_dict_get(req, "if-none-match"), etag);
31                 }
32                 else {
33+                    int more = 0;
34+
35                     snac_debug(&snac, 1, xs_fmt("building timeline"));
36 
37-                    xs *list = timeline_list(&snac, "private", skip, show);
38-                    xs *next = timeline_list(&snac, "private", skip + show, 1);
39+                    xs *list = timeline_list(&snac, "private", skip, show, &more);
40 
41                     *body = html_timeline(&snac, list, 0, skip, show,
42-                            xs_list_len(next), NULL, "/admin", 1, error);
43+                            more, NULL, "/admin", 1, error);
44 
45                     *b_size = strlen(*body);
46                     status  = HTTP_STATUS_OK;
47@@ -3702,7 +3699,7 @@ int html_get_handler(const xs_dict *req, const char *q_path,
48 
49         int cnt = xs_number_get(xs_dict_get_def(srv_config, "max_public_entries", "20"));
50 
51-        xs *elems = timeline_simple_list(&snac, "public", 0, cnt);
52+        xs *elems = timeline_simple_list(&snac, "public", 0, cnt, NULL);
53         xs *bio   = xs_dup(xs_dict_get(snac.config, "bio"));
54 
55         xs *rss_title = xs_fmt("%s (@%s@%s)",
+1, -1
1@@ -1676,7 +1676,7 @@ int mastoapi_get_handler(const xs_dict *req, const char *q_path,
2                 else
3                 if (strcmp(opt, "statuses") == 0) { /** **/
4                     /* the public list of posts of a user */
5-                    xs *timeline = timeline_simple_list(&snac2, "public", 0, 256);
6+                    xs *timeline = timeline_simple_list(&snac2, "public", 0, 256, NULL);
7                     xs_list *p   = timeline;
8                     const xs_str *v;
9 
M snac.h
+2, -2
 1@@ -157,8 +157,8 @@ int timeline_here(snac *snac, const char *md5);
 2 int timeline_get_by_md5(snac *snac, const char *md5, xs_dict **msg);
 3 int timeline_del(snac *snac, const char *id);
 4 xs_str *user_index_fn(snac *user, const char *idx_name);
 5-xs_list *timeline_simple_list(snac *user, const char *idx_name, int skip, int show);
 6-xs_list *timeline_list(snac *snac, const char *idx_name, int skip, int show);
 7+xs_list *timeline_simple_list(snac *user, const char *idx_name, int skip, int show, int *more);
 8+xs_list *timeline_list(snac *snac, const char *idx_name, int skip, int show, int *more);
 9 int timeline_add(snac *snac, const char *id, const xs_dict *o_msg);
10 int timeline_admire(snac *snac, const char *id, const char *admirer, int like);
11