fediglam/src/main/controllers/timelines.zig

74 lines
2 KiB
Zig
Raw Normal View History

2022-11-14 07:00:20 +00:00
const std = @import("std");
const api = @import("api");
const query_utils = @import("../query.zig");
2022-11-12 12:39:49 +00:00
pub const global = struct {
pub const method = .GET;
pub const path = "/timelines/global";
2022-11-14 07:00:20 +00:00
pub const Query = api.TimelineArgs;
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();
2022-11-12 12:39:49 +00:00
2022-11-14 07:00:20 +00:00
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);
2022-11-12 12:39:49 +00:00
}
};
2022-11-12 13:23:55 +00:00
pub const local = struct {
pub const method = .GET;
pub const path = "/timelines/local";
2022-11-14 07:00:20 +00:00
pub const Query = api.TimelineArgs;
pub fn handler(req: anytype, res: anytype, srv: anytype) !void {
const results = try srv.localTimeline(req.query);
2022-11-12 13:23:55 +00:00
2022-11-14 07:00:20 +00:00
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);
2022-11-12 13:23:55 +00:00
}
};
2022-11-14 07:00:20 +00:00
// 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},
);
}