X-Git-Url: https://gitweb.ps.run/chirp/blobdiff_plain/e439c64e7489a392a0debce2dbf4a87ee05e367c..c2ae0de6287635110bbcf3478c3d8935d38d02c6:/src/main.zig
diff --git a/src/main.zig b/src/main.zig
index 2ccb16b..29bedcf 100644
--- a/src/main.zig
+++ b/src/main.zig
@@ -29,8 +29,11 @@ const User = struct {
id: UserId,
name: Username,
display_name: DisplayName,
+ description: UserDescription,
password_hash: PasswordHash,
+
posts: PostList,
+
following: UserList,
followers: UserList,
@@ -82,6 +85,7 @@ const PostId = enum(u64) { _ };
const Timestamp = i64;
const Username = std.BoundedArray(u8, 32);
const DisplayName = std.BoundedArray(u8, 64);
+const UserDescription = std.BoundedArray(u8, 1024);
const PasswordHash = std.BoundedArray(u8, 128);
const SessionToken = u64;
const CookieValue = std.BoundedArray(u8, 128);
@@ -93,12 +97,12 @@ const PostListList = db.List(SavedPostList);
const UserListList = db.List(SavedUserList);
fn parse_enum(comptime E: type, buf: []const u8, base: u8) !E {
- return @enumFromInt(try std.fmt.parseUnsigned(@typeInfo(E).Enum.tag_type, buf, base));
+ return @enumFromInt(try std.fmt.parseUnsigned(@typeInfo(E).@"enum".tag_type, buf, base));
}
// https://developer.mozilla.org/en-US/docs/Glossary/Percent-encoding
-fn reencode(text: []const u8) !PostText {
- var result = try PostText.init(0);
+fn reencode(comptime T: type, text: []const u8) !T {
+ var result = try T.init(0);
const len = @min(text.len, 1024); // TODO: PostText length
@@ -107,28 +111,19 @@ fn reencode(text: []const u8) !PostText {
const c = text[idx];
if (c == '+') {
try result.append(' ');
- } else if (c == '%') {
- // special case of ...
- // assume only , no
- if (idx + 6 < text.len and std.mem.eql(u8, text[idx .. idx + 6], "%26%23")) {
- const num_start = idx + 6;
- var num_end = num_start;
- while (num_end < text.len and std.ascii.isDigit(text[num_end])) {
- num_end += 1;
- }
+ } else if (c == '%' and idx + 2 < text.len) {
+ const allow = &[_]u8{ 0x26, 0x23, 0x3b, 0x0a };
- if (num_end + 2 < text.len and
- text[num_end] == '%' and
- text[num_end + 1] == '3' and
- std.ascii.toLower(text[num_end + 2]) == 'b')
- {
- try std.fmt.format(result.writer(), "{s};", .{text[num_start..num_end]});
- idx = num_end + 2;
- continue;
- }
+ const escaped_value = std.fmt.parseUnsigned(u8, text[idx + 1 .. idx + 3], 16) catch continue;
+
+ if (escaped_value == 0x0d) {
+ try std.fmt.format(result.writer(), "
", .{});
+ } else if (std.mem.indexOfScalar(u8, allow, escaped_value) != null) {
+ try std.fmt.format(result.writer(), "{c}", .{escaped_value});
+ } else {
+ try std.fmt.format(result.writer(), "{x};", .{escaped_value});
}
- try std.fmt.format(result.writer(), "{s};", .{text[idx + 1 .. idx + 3]});
idx += 2;
} else {
try result.append(c);
@@ -223,6 +218,7 @@ const Chirp = struct {
.id = user_id,
.name = username_array,
.display_name = display_name,
+ .description = try UserDescription.init(0),
.password_hash = try hash_password(password),
.posts = try PostList.init(txn),
.following = try UserList.init(txn),
@@ -286,7 +282,7 @@ const Chirp = struct {
const posts = try Db.posts(txn);
post_id = try db.Prng.gen(posts.dbi, PostId);
- const decoded_text = try reencode(text);
+ const decoded_text = try reencode(PostText, text);
try posts.put(post_id, Post{
.id = post_id,
.parent_id = parent_id,
@@ -447,24 +443,20 @@ pub fn Paginate(comptime T: type) type {
return struct {
const Self = @This();
- const IterateResult = @typeInfo(@TypeOf(T.View.Iterator.next)).Fn.return_type.?;
+ const IterateResult = T.Base.View.Iterator.Result;
res: *http.Response,
view: T.View,
per_page: u64,
- it: T.View.Iterator,
- starting_idx: ?T.Key,
+ it: T.Base.View.Iterator,
+ starting_idx: ?T.Base.Key,
count: u64 = 0,
pub fn init(res: *http.Response, view: T.View, per_page: u64) !Self {
var it = view.reverse_iterator();
if (res.req.get_param("starting_at")) |starting_at_str| {
- it.idx = try parse_enum(T.Key, starting_at_str, 16);
- }
-
- if (it.idx == null) {
- return error.InvalidIterator;
+ it.idx = try parse_enum(T.Base.Key, starting_at_str, 16);
}
return .{
@@ -472,10 +464,10 @@ pub fn Paginate(comptime T: type) type {
.view = view,
.per_page = per_page,
.it = it,
- .starting_idx = it.idx.?,
+ .starting_idx = it.idx,
};
}
- pub fn next(self: *Self) IterateResult {
+ pub fn next(self: *Self) ?IterateResult {
if (self.it.next()) |kv| {
if (self.count < self.per_page) {
self.count += 1;
@@ -487,7 +479,7 @@ pub fn Paginate(comptime T: type) type {
pub fn write_navigation(self: *Self) !void {
const next_idx = self.it.next();
- if (self.view.head.last != self.starting_idx) {
+ if (self.view.base.head.last != self.starting_idx) {
var prev_it = self.view.iterator();
prev_it.idx = self.starting_idx.?;
var oldest_idx = self.starting_idx.?;
@@ -517,10 +509,16 @@ fn html_form(res: *http.Response, action: []const u8, inputs: anytype) !void {
inline for (inputs) |input| {
switch (@typeInfo(@TypeOf(input))) {
- .Struct => {
- try res.write("", .{});
+ .@"struct" => |s| {
+ if (s.fields.len == 3) {
+ try res.write("<{s} ", .{input[0]});
+ try res.write(input[1], input[2]);
+ try res.write(">{s}>", .{input[0]});
+ } else {
+ try res.write("", .{});
+ }
},
else => {
try res.write("{s}{s}", .{ id, name.constSlice(), if (list_view.has(post_id) catch false) " *" else "" });
}
try res.write("", .{});
- try res.write("", .{@intFromEnum(post_id)});
- try res.write("", .{});
+ try res.write("", .{@intFromEnum(post_id)});
+ try res.write("", .{});
try res.write("", .{});
}
@@ -783,13 +781,13 @@ fn write_profile(res: *http.Response, txn: lmdb.Txn, logged_in: ?Login, user: Us
var it = feeds_view.iterator();
while (it.next()) |kv| {
const name = kv.val.name;
- const id = kv.val.list.idx.?;
+ const id = kv.val.list.base.idx.?;
const list_view = try kv.val.list.open(txn);
try res.write("", .{ id, name.constSlice(), if (list_view.has(user.id) catch false) " *" else "" });
}
try res.write("", .{});
- try res.write("", .{@intFromEnum(user.id)});
- try res.write("", .{});
+ try res.write("", .{@intFromEnum(user.id)});
+ try res.write("", .{});
try res.write("", .{});
}
try res.write(
@@ -818,6 +816,15 @@ fn write_profile(res: *http.Response, txn: lmdb.Txn, logged_in: ?Login, user: Us
, .{});
}
+ if (user.description.len > 0) {
+ try res.write(
+ \\