Use relative links on pagination
This commit is contained in:
parent
b2c69c2df8
commit
8aa4f900f6
4 changed files with 23 additions and 15 deletions
|
@ -242,14 +242,14 @@ const json_options = if (builtin.mode == .Debug)
|
||||||
};
|
};
|
||||||
|
|
||||||
pub const helpers = struct {
|
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);
|
var link = std.ArrayList(u8).init(alloc);
|
||||||
const link_writer = link.writer();
|
const link_writer = link.writer();
|
||||||
defer link.deinit();
|
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 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);
|
try res.headers.put("Link", link.items);
|
||||||
|
|
||||||
|
@ -258,16 +258,24 @@ pub const helpers = struct {
|
||||||
|
|
||||||
fn writeLink(
|
fn writeLink(
|
||||||
writer: anytype,
|
writer: anytype,
|
||||||
community: api.Community,
|
community: ?api.Community,
|
||||||
path: []const u8,
|
path: []const u8,
|
||||||
params: anytype,
|
params: anytype,
|
||||||
rel: []const u8,
|
rel: []const u8,
|
||||||
) !void {
|
) !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
|
// TODO: percent-encode
|
||||||
try std.fmt.format(
|
|
||||||
writer,
|
|
||||||
"<{s}://{s}/{s}?{}>; rel=\"{s}\"",
|
|
||||||
.{ @tagName(community.scheme), community.host, path, http.queryStringify(params), rel },
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
|
@ -27,6 +27,6 @@ pub const query = struct {
|
||||||
pub fn handler(req: anytype, res: anytype, srv: anytype) !void {
|
pub fn handler(req: anytype, res: anytype, srv: anytype) !void {
|
||||||
const results = try srv.queryCommunities(req.query);
|
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);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
|
@ -10,7 +10,7 @@ pub const global = struct {
|
||||||
|
|
||||||
pub fn handler(req: anytype, res: anytype, srv: anytype) !void {
|
pub fn handler(req: anytype, res: anytype, srv: anytype) !void {
|
||||||
const results = try srv.globalTimeline(req.query);
|
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 {
|
pub fn handler(req: anytype, res: anytype, srv: anytype) !void {
|
||||||
const results = try srv.localTimeline(req.query);
|
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 {
|
pub fn handler(req: anytype, res: anytype, srv: anytype) !void {
|
||||||
const results = try srv.homeTimeline(req.query);
|
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);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
|
@ -47,7 +47,7 @@ pub const query_followers = struct {
|
||||||
pub fn handler(req: anytype, res: anytype, srv: anytype) !void {
|
pub fn handler(req: anytype, res: anytype, srv: anytype) !void {
|
||||||
const results = try srv.queryFollowers(req.args.id, req.query);
|
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 {
|
pub fn handler(req: anytype, res: anytype, srv: anytype) !void {
|
||||||
const results = try srv.queryFollowing(req.args.id, req.query);
|
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);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
Loading…
Reference in a new issue