commit c6562fa

default  ·  2024-12-15 21:52:41 +0000 UTC
parent e8ae492
New function timeline_link_header().
3 files changed,  +35, -3
M snac.h
+4, -1
 1@@ -279,6 +279,7 @@ void httpd_connection(FILE *f)
 2     xs *payload  = NULL;
 3     xs *etag     = NULL;
 4     xs *last_modified = NULL;
 5+    xs *link     = NULL;
 6     int p_size   = 0;
 7     const char *p;
 8     int fcgi_id;
 9@@ -326,7 +327,7 @@ void httpd_connection(FILE *f)
10             status = oauth_get_handler(req, q_path, &body, &b_size, &ctype);
11 
12         if (status == 0)
13-            status = mastoapi_get_handler(req, q_path, &body, &b_size, &ctype);
14+            status = mastoapi_get_handler(req, q_path, &body, &b_size, &ctype, &link);
15 #endif /* NO_MASTODON_API */
16 
17         if (status == 0)
18@@ -426,6 +427,8 @@ void httpd_connection(FILE *f)
19         headers = xs_dict_append(headers, "etag", etag);
20     if (!xs_is_null(last_modified))
21         headers = xs_dict_append(headers, "last-modified", last_modified);
22+    if (!xs_is_null(link))
23+        headers = xs_dict_append(headers, "Link", link);
24 
25     /* if there are any additional headers, add them */
26     const xs_dict *more_headers = xs_dict_get(srv_config, "http_headers");
+30, -1
 1@@ -1434,8 +1434,35 @@ xs_list *mastoapi_timeline(snac *user, const xs_dict *args, const char *index_fn
 2 }
 3 
 4 
 5+xs_str *timeline_link_header(const char *endpoint, xs_list *timeline)
 6+/* returns a Link header with paging information */
 7+{
 8+    xs_str *s = NULL;
 9+
10+    if (xs_list_len(timeline) == 0)
11+        return NULL;
12+
13+    const xs_dict *first_st = xs_list_get(timeline, 0);
14+    const xs_dict *last_st  = xs_list_get(timeline, -1);
15+    const char *first_id    = xs_dict_get(first_st, "id");
16+    const char *last_id     = xs_dict_get(last_st, "id");
17+    const char *host        = xs_dict_get(srv_config, "host");
18+    const char *protocol    = xs_dict_get_def(srv_config, "protocol", "https");
19+
20+    s = xs_fmt(
21+        "<%s:/" "/%s%s?max_id=%s>; rel=\"next\", "
22+        "<%s:/" "/%s%s?since_id=%s>; rel=\"prev\"",
23+        protocol, host, endpoint, last_id,
24+        protocol, host, endpoint, first_id);
25+
26+    srv_debug(1, xs_fmt("timeline_link_header %s", s));
27+
28+    return s;
29+}
30+
31+
32 int mastoapi_get_handler(const xs_dict *req, const char *q_path,
33-                         char **body, int *b_size, char **ctype)
34+                         char **body, int *b_size, char **ctype, xs_str **link)
35 {
36     (void)b_size;
37 
38@@ -1695,6 +1722,8 @@ int mastoapi_get_handler(const xs_dict *req, const char *q_path,
39             xs *ifn = user_index_fn(&snac1, "private");
40             xs *out = mastoapi_timeline(&snac1, args, ifn);
41 
42+            *link = timeline_link_header(cmd, out);
43+
44             *body  = xs_json_dumps(out, 4);
45             *ctype = "application/json";
46             status = HTTP_STATUS_OK;
M snac.h
+1, -1
1@@ -384,7 +384,7 @@ int oauth_post_handler(const xs_dict *req, const char *q_path,
2                        const char *payload, int p_size,
3                        char **body, int *b_size, char **ctype);
4 int mastoapi_get_handler(const xs_dict *req, const char *q_path,
5-                         char **body, int *b_size, char **ctype);
6+                         char **body, int *b_size, char **ctype, xs_str **link);
7 int mastoapi_post_handler(const xs_dict *req, const char *q_path,
8                           const char *payload, int p_size,
9                           char **body, int *b_size, char **ctype);