New function timeline_link_header().

This commit is contained in:
default 2024-12-15 22:52:41 +01:00
parent e8ae4920f6
commit c6562fa39b
3 changed files with 35 additions and 3 deletions

View file

@ -279,6 +279,7 @@ void httpd_connection(FILE *f)
xs *payload = NULL; xs *payload = NULL;
xs *etag = NULL; xs *etag = NULL;
xs *last_modified = NULL; xs *last_modified = NULL;
xs *link = NULL;
int p_size = 0; int p_size = 0;
const char *p; const char *p;
int fcgi_id; int fcgi_id;
@ -326,7 +327,7 @@ void httpd_connection(FILE *f)
status = oauth_get_handler(req, q_path, &body, &b_size, &ctype); status = oauth_get_handler(req, q_path, &body, &b_size, &ctype);
if (status == 0) if (status == 0)
status = mastoapi_get_handler(req, q_path, &body, &b_size, &ctype); status = mastoapi_get_handler(req, q_path, &body, &b_size, &ctype, &link);
#endif /* NO_MASTODON_API */ #endif /* NO_MASTODON_API */
if (status == 0) if (status == 0)
@ -426,6 +427,8 @@ void httpd_connection(FILE *f)
headers = xs_dict_append(headers, "etag", etag); headers = xs_dict_append(headers, "etag", etag);
if (!xs_is_null(last_modified)) if (!xs_is_null(last_modified))
headers = xs_dict_append(headers, "last-modified", last_modified); headers = xs_dict_append(headers, "last-modified", last_modified);
if (!xs_is_null(link))
headers = xs_dict_append(headers, "Link", link);
/* if there are any additional headers, add them */ /* if there are any additional headers, add them */
const xs_dict *more_headers = xs_dict_get(srv_config, "http_headers"); const xs_dict *more_headers = xs_dict_get(srv_config, "http_headers");

View file

@ -1434,8 +1434,35 @@ xs_list *mastoapi_timeline(snac *user, const xs_dict *args, const char *index_fn
} }
xs_str *timeline_link_header(const char *endpoint, xs_list *timeline)
/* returns a Link header with paging information */
{
xs_str *s = NULL;
if (xs_list_len(timeline) == 0)
return NULL;
const xs_dict *first_st = xs_list_get(timeline, 0);
const xs_dict *last_st = xs_list_get(timeline, -1);
const char *first_id = xs_dict_get(first_st, "id");
const char *last_id = xs_dict_get(last_st, "id");
const char *host = xs_dict_get(srv_config, "host");
const char *protocol = xs_dict_get_def(srv_config, "protocol", "https");
s = xs_fmt(
"<%s:/" "/%s%s?max_id=%s>; rel=\"next\", "
"<%s:/" "/%s%s?since_id=%s>; rel=\"prev\"",
protocol, host, endpoint, last_id,
protocol, host, endpoint, first_id);
srv_debug(1, xs_fmt("timeline_link_header %s", s));
return s;
}
int mastoapi_get_handler(const xs_dict *req, const char *q_path, int mastoapi_get_handler(const xs_dict *req, const char *q_path,
char **body, int *b_size, char **ctype) char **body, int *b_size, char **ctype, xs_str **link)
{ {
(void)b_size; (void)b_size;
@ -1695,6 +1722,8 @@ int mastoapi_get_handler(const xs_dict *req, const char *q_path,
xs *ifn = user_index_fn(&snac1, "private"); xs *ifn = user_index_fn(&snac1, "private");
xs *out = mastoapi_timeline(&snac1, args, ifn); xs *out = mastoapi_timeline(&snac1, args, ifn);
*link = timeline_link_header(cmd, out);
*body = xs_json_dumps(out, 4); *body = xs_json_dumps(out, 4);
*ctype = "application/json"; *ctype = "application/json";
status = HTTP_STATUS_OK; status = HTTP_STATUS_OK;

2
snac.h
View file

@ -384,7 +384,7 @@ int oauth_post_handler(const xs_dict *req, const char *q_path,
const char *payload, int p_size, const char *payload, int p_size,
char **body, int *b_size, char **ctype); char **body, int *b_size, char **ctype);
int mastoapi_get_handler(const xs_dict *req, const char *q_path, int mastoapi_get_handler(const xs_dict *req, const char *q_path,
char **body, int *b_size, char **ctype); char **body, int *b_size, char **ctype, xs_str **link);
int mastoapi_post_handler(const xs_dict *req, const char *q_path, int mastoapi_post_handler(const xs_dict *req, const char *q_path,
const char *payload, int p_size, const char *payload, int p_size,
char **body, int *b_size, char **ctype); char **body, int *b_size, char **ctype);