Split out AP stuff

This commit is contained in:
jaina heartles 2022-04-20 23:26:08 -07:00
parent 266c18453a
commit ddd8b14355
3 changed files with 36 additions and 17 deletions

4
.gitignore vendored
View file

@ -1,2 +1,2 @@
/zig-out **/zig-out
/zig-cache **/zig-cache

21
src/db.zig Normal file
View file

@ -0,0 +1,21 @@
pub const Actor = struct {
id: u128,
display_name: ?[]const u8 = null,
handle: []const u8,
host: []const u8,
bio: ?[]const u8 = null,
};
const this_host = "localhost:8080";
const actor = Actor{
.id = 1234,
.handle = "testacct",
.host = this_host,
};
pub fn getActorById(id: u128) !?Actor {
if (id == actor.id) return actor;
return null;
}

View file

@ -1,11 +1,12 @@
const std = @import("std"); const std = @import("std");
const db = @import("./db.zig");
pub const io_mode = .evented; pub const io_mode = .evented;
const Reader = std.net.Stream.Reader; const Reader = std.net.Stream.Reader;
const Writer = std.net.Stream.Writer; const Writer = std.net.Stream.Writer;
const case_insensitive_utf8 = struct { const ciutf8 = struct {
const Hash = std.hash.Wyhash; const Hash = std.hash.Wyhash;
const View = std.unicode.Utf8View; const View = std.unicode.Utf8View;
const toLower = std.ascii.toLower; const toLower = std.ascii.toLower;
@ -76,11 +77,11 @@ const case_insensitive_utf8 = struct {
const HeaderMap = std.HashMap([]const u8, []const u8, struct { const HeaderMap = std.HashMap([]const u8, []const u8, struct {
pub fn eql(_: @This(), a: []const u8, b: []const u8) bool { pub fn eql(_: @This(), a: []const u8, b: []const u8) bool {
return case_insensitive_utf8.eql(a, b); return ciutf8.eql(a, b);
} }
pub fn hash(_: @This(), str: []const u8) u64 { pub fn hash(_: @This(), str: []const u8) u64 {
return case_insensitive_utf8.hash(str); return ciutf8.hash(str);
} }
}, std.hash_map.default_max_load_percentage); }, std.hash_map.default_max_load_percentage);
@ -431,7 +432,7 @@ const Route = struct {
const slice = path[segment_start..index]; const slice = path[segment_start..index];
const match = switch (seg) { const match = switch (seg) {
.literal => |str| case_insensitive_utf8.eql(slice, str), .literal => |str| ciutf8.eql(slice, str),
.param => true, .param => true,
}; };
@ -504,14 +505,8 @@ const routes = [_]Route{
const this_scheme = "http"; const this_scheme = "http";
const this_host = "localhost:8080"; const this_host = "localhost:8080";
const account = .{
.id = "abc123",
.handle = "testacct",
.host = this_host,
};
fn getUser(ctx: *Context) anyerror!void { fn getUser(ctx: *Context) anyerror!void {
const id = ctx.request.arg("id"); const id_str = ctx.request.arg("id");
const host = ctx.request.headers.get("host"); const host = ctx.request.headers.get("host");
if (host == null) { if (host == null) {
@ -519,8 +514,11 @@ fn getUser(ctx: *Context) anyerror!void {
return; return;
} }
if (!std.mem.eql(u8, account.id, id) or const id = try std.fmt.parseInt(u128, id_str, 10);
!std.mem.eql(u8, account.host, ctx.request.headers.get("host").?)) const actor = try db.getActorById(id);
if (actor == null or
!std.mem.eql(u8, actor.?.host, ctx.request.headers.get("host").?))
{ {
try ctx.response.statusOnly(404); try ctx.response.statusOnly(404);
return; return;
@ -530,8 +528,8 @@ fn getUser(ctx: *Context) anyerror!void {
var writer = try ctx.response.open(200); var writer = try ctx.response.open(200);
try writer.writeAll("{\"type\":\"Person\","); try writer.writeAll("{\"type\":\"Person\",");
try writer.print("\"id\":\"{s}://{s}/user/{s}\",", .{ this_scheme, this_host, id }); try writer.print("\"id\":\"{s}://{s}/user/{}\",", .{ this_scheme, this_host, id });
try writer.print("\"preferredUsername\":\"{s}\"", .{account.handle}); try writer.print("\"preferredUsername\":\"{s}\"", .{actor.?.handle});
try writer.writeAll("}"); try writer.writeAll("}");
} }