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