Use relative links on pagination

This commit is contained in:
jaina heartles 2022-11-27 01:59:37 -08:00
parent b2c69c2df8
commit 8aa4f900f6
4 changed files with 23 additions and 15 deletions

View file

@ -242,14 +242,14 @@ const json_options = if (builtin.mode == .Debug)
};
pub const helpers = struct {
pub fn paginate(community: api.Community, path: []const u8, results: anytype, res: *Response, alloc: std.mem.Allocator) !void {
pub fn paginate(results: anytype, res: *Response, alloc: std.mem.Allocator) !void {
var link = std.ArrayList(u8).init(alloc);
const link_writer = link.writer();
defer link.deinit();
try writeLink(link_writer, community, path, results.next_page, "next");
try writeLink(link_writer, null, "", results.next_page, "next");
try link_writer.writeByte(',');
try writeLink(link_writer, community, path, results.prev_page, "prev");
try writeLink(link_writer, null, "", results.prev_page, "prev");
try res.headers.put("Link", link.items);
@ -258,16 +258,24 @@ pub const helpers = struct {
fn writeLink(
writer: anytype,
community: api.Community,
community: ?api.Community,
path: []const u8,
params: anytype,
rel: []const u8,
) !void {
if (community) |c| {
try std.fmt.format(
writer,
"<{s}://{s}/{s}?{}>; rel=\"{s}\"",
.{ @tagName(c.scheme), c.host, path, http.queryStringify(params), rel },
);
} else {
try std.fmt.format(
writer,
"<{s}?{}>; rel=\"{s}\"",
.{ path, http.queryStringify(params), rel },
);
}
// TODO: percent-encode
try std.fmt.format(
writer,
"<{s}://{s}/{s}?{}>; rel=\"{s}\"",
.{ @tagName(community.scheme), community.host, path, http.queryStringify(params), rel },
);
}
};

View file

@ -27,6 +27,6 @@ pub const query = struct {
pub fn handler(req: anytype, res: anytype, srv: anytype) !void {
const results = try srv.queryCommunities(req.query);
try controller_utils.paginate(srv.community, path, results, res, req.allocator);
try controller_utils.paginate(results, res, req.allocator);
}
};

View file

@ -10,7 +10,7 @@ pub const global = struct {
pub fn handler(req: anytype, res: anytype, srv: anytype) !void {
const results = try srv.globalTimeline(req.query);
try controller_utils.paginate(srv.community, path, results, res, req.allocator);
try controller_utils.paginate(results, res, req.allocator);
}
};
@ -22,7 +22,7 @@ pub const local = struct {
pub fn handler(req: anytype, res: anytype, srv: anytype) !void {
const results = try srv.localTimeline(req.query);
try controller_utils.paginate(srv.community, path, results, res, req.allocator);
try controller_utils.paginate(results, res, req.allocator);
}
};
@ -34,6 +34,6 @@ pub const home = struct {
pub fn handler(req: anytype, res: anytype, srv: anytype) !void {
const results = try srv.homeTimeline(req.query);
try controller_utils.paginate(srv.community, path, results, res, req.allocator);
try controller_utils.paginate(results, res, req.allocator);
}
};

View file

@ -47,7 +47,7 @@ pub const query_followers = struct {
pub fn handler(req: anytype, res: anytype, srv: anytype) !void {
const results = try srv.queryFollowers(req.args.id, req.query);
try controller_utils.paginate(srv.community, path, results, res, req.allocator);
try controller_utils.paginate(results, res, req.allocator);
}
};
@ -64,6 +64,6 @@ pub const query_following = struct {
pub fn handler(req: anytype, res: anytype, srv: anytype) !void {
const results = try srv.queryFollowing(req.args.id, req.query);
try controller_utils.paginate(srv.community, path, results, res, req.allocator);
try controller_utils.paginate(results, res, req.allocator);
}
};