Combine pagination code

This commit is contained in:
jaina heartles 2022-11-14 00:25:52 -08:00
parent c49f1487a7
commit 568c6cd6b6
3 changed files with 44 additions and 82 deletions

View file

@ -263,3 +263,42 @@ const json_options = if (builtin.mode == .Debug)
},
.string = .{ .String = .{} },
};
pub const helpers = struct {
pub fn paginate(community: api.Community, path: []const u8, 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 link_writer.writeByte(',');
try writeLink(link_writer, community, path, results.prev_page, "prev");
try res.headers.put("Link", link.items);
try res.json(.ok, results.items);
}
fn writeLink(
writer: anytype,
community: api.Community,
path: []const u8,
params: anytype,
rel: []const u8,
) !void {
// TODO: percent-encode
try std.fmt.format(
writer,
"<{s}://{s}/{s}?",
.{ @tagName(community.scheme), community.host, path },
);
try query_utils.formatQuery(params, writer);
try std.fmt.format(
writer,
">; rel=\"{s}\"",
.{rel},
);
}
};

View file

@ -2,6 +2,7 @@ const std = @import("std");
const api = @import("api");
const util = @import("util");
const query_utils = @import("../query.zig");
const controller_utils = @import("../controllers.zig").helpers;
const QueryArgs = api.CommunityQueryArgs;
const Uuid = util.Uuid;
@ -31,39 +32,6 @@ pub const query = struct {
pub fn handler(req: anytype, res: anytype, srv: anytype) !void {
const results = try srv.queryCommunities(req.query);
var link = std.ArrayList(u8).init(req.allocator);
const link_writer = link.writer();
defer link.deinit();
try writeLink(link_writer, srv.community, path, results.next_page, "next");
try link_writer.writeByte(',');
try writeLink(link_writer, srv.community, path, results.prev_page, "prev");
try res.headers.put("Link", link.items);
try res.json(.ok, results.items);
try controller_utils.paginate(srv.community, path, results, res, req.allocator);
}
};
fn writeLink(
writer: anytype,
community: api.Community,
path: []const u8,
params: anytype,
rel: []const u8,
) !void {
// TODO: percent-encode
try std.fmt.format(
writer,
"<{s}://{s}/{s}?",
.{ @tagName(community.scheme), community.host, path },
);
try query_utils.formatQuery(params, writer);
try std.fmt.format(
writer,
">; rel=\"{s}\"",
.{rel},
);
}

View file

@ -1,6 +1,7 @@
const std = @import("std");
const api = @import("api");
const query_utils = @import("../query.zig");
const controller_utils = @import("../controllers.zig").helpers;
pub const global = struct {
pub const method = .GET;
@ -10,18 +11,7 @@ pub const global = struct {
pub fn handler(req: anytype, res: anytype, srv: anytype) !void {
const results = try srv.globalTimeline(req.query);
var link = std.ArrayList(u8).init(req.allocator);
const link_writer = link.writer();
defer link.deinit();
try writeLink(link_writer, srv.community, path, results.next_page, "next");
try link_writer.writeByte(',');
try writeLink(link_writer, srv.community, path, results.prev_page, "prev");
try res.headers.put("Link", link.items);
try res.json(.ok, results.items);
try controller_utils.paginate(srv.community, path, results, res, req.allocator);
}
};
@ -33,41 +23,6 @@ pub const local = struct {
pub fn handler(req: anytype, res: anytype, srv: anytype) !void {
const results = try srv.localTimeline(req.query);
var link = std.ArrayList(u8).init(req.allocator);
const link_writer = link.writer();
defer link.deinit();
try writeLink(link_writer, srv.community, path, results.next_page, "next");
try link_writer.writeByte(',');
try writeLink(link_writer, srv.community, path, results.prev_page, "prev");
try res.headers.put("Link", link.items);
try res.json(.ok, results.items);
try controller_utils.paginate(srv.community, path, results, res, req.allocator);
}
};
// TOOD: unify with communities.zig
fn writeLink(
writer: anytype,
community: api.Community,
path: []const u8,
params: anytype,
rel: []const u8,
) !void {
// TODO: percent-encode
try std.fmt.format(
writer,
"<{s}://{s}/{s}?",
.{ @tagName(community.scheme), community.host, path },
);
try query_utils.formatQuery(params, writer);
try std.fmt.format(
writer,
">; rel=\"{s}\"",
.{rel},
);
}