Combine pagination code
This commit is contained in:
parent
c49f1487a7
commit
568c6cd6b6
3 changed files with 44 additions and 82 deletions
|
@ -263,3 +263,42 @@ const json_options = if (builtin.mode == .Debug)
|
||||||
},
|
},
|
||||||
.string = .{ .String = .{} },
|
.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},
|
||||||
|
);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
|
@ -2,6 +2,7 @@ const std = @import("std");
|
||||||
const api = @import("api");
|
const api = @import("api");
|
||||||
const util = @import("util");
|
const util = @import("util");
|
||||||
const query_utils = @import("../query.zig");
|
const query_utils = @import("../query.zig");
|
||||||
|
const controller_utils = @import("../controllers.zig").helpers;
|
||||||
|
|
||||||
const QueryArgs = api.CommunityQueryArgs;
|
const QueryArgs = api.CommunityQueryArgs;
|
||||||
const Uuid = util.Uuid;
|
const Uuid = util.Uuid;
|
||||||
|
@ -31,39 +32,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);
|
||||||
|
|
||||||
var link = std.ArrayList(u8).init(req.allocator);
|
try controller_utils.paginate(srv.community, path, results, res, 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);
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
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},
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
const std = @import("std");
|
const std = @import("std");
|
||||||
const api = @import("api");
|
const api = @import("api");
|
||||||
const query_utils = @import("../query.zig");
|
const query_utils = @import("../query.zig");
|
||||||
|
const controller_utils = @import("../controllers.zig").helpers;
|
||||||
|
|
||||||
pub const global = struct {
|
pub const global = struct {
|
||||||
pub const method = .GET;
|
pub const method = .GET;
|
||||||
|
@ -10,18 +11,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);
|
||||||
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);
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -33,41 +23,6 @@ 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);
|
||||||
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);
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
// 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},
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
Loading…
Reference in a new issue