]> gitweb.ps.run Git - chirp/blobdiff - src/main.zig
update hash buffer
[chirp] / src / main.zig
index 2fa757c435535fdb04d5b09a07680938de4dc6bb..d35221e98eb95da47fd0a60868801cfe4b8c5ed7 100644 (file)
@@ -29,9 +29,11 @@ const User = struct {
     id: UserId,
     name: Username,
     display_name: DisplayName,
+    description: UserDescription,
     password_hash: PasswordHash,
+
     posts: PostList,
-    replies: PostList,
+
     following: UserList,
     followers: UserList,
 
@@ -83,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);
@@ -94,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
 
@@ -108,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 &#x
-            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(), "<br />", .{});
+            } 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{x};", .{escaped_value});
             }
 
-            try std.fmt.format(result.writer(), "&#x{s};", .{text[idx + 1 .. idx + 3]});
             idx += 2;
         } else {
             try result.append(c);
@@ -167,18 +161,20 @@ fn decode(text: []const u8) !std.BoundedArray(u8, 1024) {
 }
 
 const Chirp = struct {
+    const PostsPerPage = 10;
+    const UsersPerPage = 10;
+    var HashBuffer = std.mem.zeroes([1024 * 1024 * 50]u8);
+
     pub fn hash_password(password: []const u8) !PasswordHash {
         var hash_buffer = try PasswordHash.init(128);
 
         // TODO: choose buffer size
-        // TODO: dont allocate on stack, maybe zero memory?
-        var buffer: [1024 * 10]u8 = undefined;
-        var alloc = std.heap.FixedBufferAllocator.init(&buffer);
+        var alloc = std.heap.FixedBufferAllocator.init(&HashBuffer);
 
         // TODO: choose limits
         const result = try std.crypto.pwhash.argon2.strHash(password, .{
             .allocator = alloc.allocator(),
-            .params = std.crypto.pwhash.argon2.Params.fromLimits(1000, 1024),
+            .params = std.crypto.pwhash.argon2.Params.owasp_2id,
         }, hash_buffer.slice());
 
         try hash_buffer.resize(result.len);
@@ -187,8 +183,7 @@ const Chirp = struct {
     }
 
     pub fn verify_password(password: []const u8, hash: PasswordHash) bool {
-        var buffer: [1024 * 10]u8 = undefined;
-        var alloc = std.heap.FixedBufferAllocator.init(&buffer);
+        var alloc = std.heap.FixedBufferAllocator.init(&HashBuffer);
 
         if (std.crypto.pwhash.argon2.strVerify(hash.constSlice(), password, .{
             .allocator = alloc.allocator(),
@@ -221,9 +216,9 @@ 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),
-                .replies = try PostList.init(txn),
                 .following = try UserList.init(txn),
                 .followers = try UserList.init(txn),
                 .post_lists = try PostListList.init(txn),
@@ -285,7 +280,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,
@@ -328,11 +323,7 @@ const Chirp = struct {
         txn.abort();
 
         const post_id = try append_post(env, user_id, user.posts, null, null, text);
-
-        txn = try env.txn();
-        var replies_view = try user.replies.open(txn);
-        try replies_view.append(post_id);
-        try txn.commit();
+        _ = post_id;
     }
 
     fn comment(env: lmdb.Env, user_id: UserId, parent_post_id: PostId, text: []const u8) !void {
@@ -347,7 +338,7 @@ const Chirp = struct {
         const post_id = try append_post(env, user_id, parent_post.comments, parent_post_id, null, text);
 
         txn = try env.txn();
-        var replies_view = try user.replies.open(txn);
+        var replies_view = try user.posts.open(txn);
         try replies_view.append(post_id);
         try txn.commit();
     }
@@ -359,11 +350,7 @@ const Chirp = struct {
         txn.abort();
 
         const post_id = try append_post(env, user_id, user.posts, null, quote_post_id, text);
-
-        txn = try env.txn();
-        var replies_view = try user.replies.open(txn);
-        try replies_view.append(post_id);
-        try txn.commit();
+        _ = post_id;
     }
 
     fn vote(env: lmdb.Env, post_id: PostId, user_id: UserId, kind: Vote.Kind) !void {
@@ -450,15 +437,86 @@ const Chirp = struct {
 // }}}
 
 // html {{{
+pub fn Paginate(comptime T: type) type {
+    return struct {
+        const Self = @This();
+
+        const IterateResult = T.Base.View.Iterator.Result;
+
+        res: *http.Response,
+        view: T.View,
+        per_page: u64,
+
+        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.Base.Key, starting_at_str, 16);
+            }
+
+            return .{
+                .res = res,
+                .view = view,
+                .per_page = per_page,
+                .it = it,
+                .starting_idx = it.idx,
+            };
+        }
+        pub fn next(self: *Self) ?IterateResult {
+            if (self.it.next()) |kv| {
+                if (self.count < self.per_page) {
+                    self.count += 1;
+                    return kv;
+                }
+            }
+            return null;
+        }
+        pub fn write_navigation(self: *Self) !void {
+            const next_idx = self.it.next();
+
+            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.?;
+
+                var count: u64 = 0;
+                while (prev_it.next()) |kv| {
+                    oldest_idx = kv.key;
+
+                    if (count > self.per_page) {
+                        break;
+                    } else {
+                        count += 1;
+                    }
+                }
+
+                try self.res.write("<a href=\"{s}?starting_at={x}\">Prev</a> ", .{ self.res.req.target, @intFromEnum(oldest_idx) });
+            }
+
+            if (next_idx) |kv| {
+                try self.res.write("<a href=\"{s}?starting_at={x}\">Next</a>", .{ self.res.req.target, @intFromEnum(kv.key) });
+            }
+        }
+    };
+}
 fn html_form(res: *http.Response, action: []const u8, inputs: anytype) !void {
     try res.write("<form action=\"{s}\" method=\"post\">", .{action});
 
     inline for (inputs) |input| {
         switch (@typeInfo(@TypeOf(input))) {
-            .Struct => {
-                try res.write("<input ", .{});
-                try res.write(input[0], input[1]);
-                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("<input ", .{});
+                    try res.write(input[0], input[1]);
+                    try res.write(" />", .{});
+                }
             },
             else => {
                 try res.write("<input ", .{});
@@ -536,6 +594,7 @@ fn write_start(res: *http.Response) !void {
         \\<html>
         \\<head>
         \\<link rel="icon" href="data:image/svg+xml,<svg xmlns=%22http://www.w3.org/2000/svg%22 viewBox=%220 0 100 100%22><text y=%22.9em%22 font-size=%2290%22>&#128035;</text></svg>">
+        \\<meta name="viewport" content="width=device-width, initial-scale=1.0" />
         \\<style>
         \\  form {
         \\    display: inline-block;
@@ -558,9 +617,9 @@ fn write_post(res: *http.Response, txn: lmdb.Txn, logged_in: ?Login, post_id: Po
     const user = try users.get(post.user_id);
 
     try res.write(
-        \\<div>
+        \\<div id="{x}">
         \\<span><a href="/user/{s}">{s}</a>
-    , .{ user.name.constSlice(), user.display_name.constSlice() });
+    , .{ @intFromEnum(post_id), user.name.constSlice(), user.display_name.constSlice() });
     if (post.parent_id) |id| {
         try res.write(" <a href=\"/post/{x}\">..</a>", .{@intFromEnum(id)});
     }
@@ -569,6 +628,17 @@ fn write_post(res: *http.Response, txn: lmdb.Txn, logged_in: ?Login, post_id: Po
         \\<span>{s}</span><br />
     , .{ time_str(post.time).constSlice(), post.text.constSlice() });
 
+    if (logged_in != null and post.user_id == logged_in.?.user.id) {
+        // Votes
+        try res.write(
+            \\<small>
+            \\<a href="/upvotes/{0x}">{1} Upvotes</a>
+            \\<a href="/downvotes/{0x}">{2} Downvotes</a>
+            \\</small>
+            \\<br />
+        , .{ @intFromEnum(post_id), post.upvotes, post.downvotes });
+    }
+
     if (post.quote_id) |quote_id| {
         try res.write("<div style=\"border: 1px solid black;\">", .{});
         if (options.recurse > 0) {
@@ -616,7 +686,7 @@ fn write_post(res: *http.Response, txn: lmdb.Txn, logged_in: ?Login, post_id: Po
 
     // Quote
     try res.write(
-        \\<a href="/quotes/{x}">&#x1F501; {}</a> 
+        \\<a href="/quoted/{x}">&#x1F501; {}</a> 
     , .{ @intFromEnum(post.id), quotes_view.len() });
 
     // Save to List
@@ -628,12 +698,13 @@ fn write_post(res: *http.Response, txn: lmdb.Txn, logged_in: ?Login, post_id: Po
         // TODO: mark lists that already contain post
         while (it.next()) |kv| {
             const name = kv.val.name;
-            const id = kv.val.list.idx.?;
-            try res.write("<option value=\"{x}\">{s}</option>", .{ id, name.constSlice() });
+            const id = kv.val.list.base.idx.?;
+            const list_view = try kv.val.list.open(txn);
+            try res.write("<option value=\"{x}\">{s}{s}</option>", .{ id, name.constSlice(), if (list_view.has(post_id) catch false) " *" else "" });
         }
         try res.write("</select>", .{});
-        try res.write("<input type=\"hidden\" name=\"post_id\" value=\"{x}\"></input>", .{@intFromEnum(post_id)});
-        try res.write("<input type=\"submit\" value=\"Save\"></input>", .{});
+        try res.write("<input type=\"hidden\" name=\"post_id\" value=\"{x}\" />", .{@intFromEnum(post_id)});
+        try res.write("<input type=\"submit\" value=\"Save\" />", .{});
         try res.write("</form>", .{});
     }
 
@@ -659,7 +730,7 @@ fn write_post(res: *http.Response, txn: lmdb.Txn, logged_in: ?Login, post_id: Po
         var it = comments_view.iterator();
         var count: u8 = 0;
         while (it.next()) |comment_id| {
-            try write_post(res, txn, logged_in, comment_id, .{ .recurse = options.recurse - 1 });
+            try write_post(res, txn, logged_in, comment_id.key, .{ .recurse = options.recurse - 1 });
             try res.write("<br />", .{});
             if (options.recurse == 1) {
                 count += 1;
@@ -691,12 +762,12 @@ fn write_profile(res: *http.Response, txn: lmdb.Txn, logged_in: ?Login, user: Us
         // follow/unfollow
         if (try followers.has(login.user.id)) {
             try html_form(res, "/follow", .{
-                .{ "type=\"hidden\" name=\"user.id\" value=\"{x}\"", .{@intFromEnum(user.id)} },
+                .{ "type=\"hidden\" name=\"user_id\" value=\"{x}\"", .{@intFromEnum(user.id)} },
                 \\type="submit" value="Unfollow"
             });
         } else {
             try html_form(res, "/follow", .{
-                .{ "type=\"hidden\" name=\"user.id\" value=\"{x}\"", .{@intFromEnum(user.id)} },
+                .{ "type=\"hidden\" name=\"user_id\" value=\"{x}\"", .{@intFromEnum(user.id)} },
                 \\type="submit" value="Follow"
             });
         }
@@ -708,12 +779,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.?;
-            try res.write("<option value=\"{x}\">{s}</option>", .{ id, name.constSlice() });
+            const id = kv.val.list.base.idx.?;
+            const list_view = try kv.val.list.open(txn);
+            try res.write("<option value=\"{x}\">{s}{s}</option>", .{ id, name.constSlice(), if (list_view.has(user.id) catch false) " *" else "" });
         }
         try res.write("</select>", .{});
-        try res.write("<input type=\"hidden\" name=\"user.id\" value=\"{x}\"></input>", .{@intFromEnum(user.id)});
-        try res.write("<input type=\"submit\" value=\"Add to feed\"></input>", .{});
+        try res.write("<input type=\"hidden\" name=\"user_id\" value=\"{x}\" />", .{@intFromEnum(user.id)});
+        try res.write("<input type=\"submit\" value=\"Add to feed\" />", .{});
         try res.write("</form>", .{});
     }
     try res.write(
@@ -725,7 +797,11 @@ fn write_profile(res: *http.Response, txn: lmdb.Txn, logged_in: ?Login, user: Us
         user.name.constSlice(), followers.len(),
     });
 
-    try res.write("<a href=\"/replies/{s}\">Replies</a><br />", .{
+    try res.write(
+        \\<a href="/all/{0s}">All Posts</a>
+        \\ <a href="/comments/{0s}">Comments</a>
+        \\ <a href="/quotes/{0s}">Quotes</a><br />
+    , .{
         user.name.constSlice(),
     });
 
@@ -738,16 +814,39 @@ fn write_profile(res: *http.Response, txn: lmdb.Txn, logged_in: ?Login, user: Us
         , .{});
     }
 
+    if (user.description.len > 0) {
+        try res.write(
+            \\<div style="padding-left: 5px; border-left: 1px solid grey;">
+            // \\&#x00AB; {s} &#x00BB;
+            \\<i>{s}</i>
+            \\</div>
+        , .{user.description.constSlice()});
+    }
+
     try res.write("<br />", .{});
 }
-fn write_posts(res: *http.Response, txn: lmdb.Txn, logged_in: ?Login, post_list: PostList) !void {
+fn write_posts(res: *http.Response, txn: lmdb.Txn, logged_in: ?Login, post_list: PostList, options: struct {
+    show_posts: bool,
+    show_quotes: bool,
+    show_comments: bool,
+}) !void {
     const posts_view = try post_list.open(txn);
 
-    var it = posts_view.reverse_iterator();
-    while (it.next()) |post_id| {
-        try write_post(res, txn, logged_in, post_id, .{ .recurse = 1 });
-        try res.write("<br />", .{});
+    var paginate = try Paginate(PostList).init(res, posts_view, Chirp.PostsPerPage);
+
+    while (paginate.next()) |post_id| {
+        const posts = try Db.posts(txn);
+        const post = try posts.get(post_id.key);
+        if ((options.show_posts and (post.parent_id == null and post.quote_id == null)) or
+            (options.show_quotes and (post.quote_id != null)) or
+            (options.show_comments and (post.parent_id != null)))
+        {
+            try write_post(res, txn, logged_in, post_id.key, .{ .recurse = 1 });
+            try res.write("<br />", .{});
+        }
     }
+
+    try paginate.write_navigation();
 }
 fn write_timeline(res: *http.Response, txn: lmdb.Txn, logged_in: ?Login, user_list: UserList) !void {
     const users = try Db.users(txn);
@@ -763,7 +862,7 @@ fn write_timeline(res: *http.Response, txn: lmdb.Txn, logged_in: ?Login, user_li
 
         var following_it = following.iterator();
         while (following_it.next()) |following_id| {
-            const followed_user = try users.get(following_id);
+            const followed_user = try users.get(following_id.key);
             const followed_posts = try followed_user.posts.open(txn);
 
             if (followed_posts.len() == 0) {
@@ -772,7 +871,7 @@ fn write_timeline(res: *http.Response, txn: lmdb.Txn, logged_in: ?Login, user_li
 
             var followed_posts_it = followed_posts.reverse_iterator();
             while (followed_posts_it.next()) |followed_post_id| {
-                const p = try posts.get(followed_post_id);
+                const p = try posts.get(followed_post_id.key);
 
                 if ((prev_newest_post == null or p.time < prev_newest_post.?.time) and (newest_post == null or newest_post.?.time < p.time)) {
                     newest_post = p;
@@ -793,6 +892,35 @@ fn write_timeline(res: *http.Response, txn: lmdb.Txn, logged_in: ?Login, user_li
         try res.write("<br />", .{});
     }
 }
+fn write_user(res: *http.Response, txn: lmdb.Txn, user_id: UserId) !void {
+    const users = try Db.users(txn);
+    const user = try users.get(user_id);
+    try res.write(
+        \\<a href="/user/{s}">{s}</a>
+    , .{ user.name.constSlice(), user.display_name.constSlice() });
+}
+fn write_votes(res: *http.Response, txn: lmdb.Txn, votes: VoteList, options: struct {
+    show_upvotes: bool = true,
+    show_downvotes: bool = true,
+}) !void {
+    const votes_view = try votes.open(txn);
+
+    var paginate = try Paginate(VoteList).init(res, votes_view, Chirp.UsersPerPage);
+
+    while (paginate.next()) |kv| {
+        const user_id = kv.key;
+        const vote = kv.val;
+
+        if ((options.show_upvotes and vote.kind == .Up) or
+            (options.show_downvotes and vote.kind == .Down))
+        {
+            try write_user(res, txn, user_id);
+            try res.write(" <small>{s}</small><br />", .{time_str(vote.time).constSlice()});
+        }
+    }
+
+    try paginate.write_navigation();
+}
 fn check_login(env: lmdb.Env, req: http.Request, res: *http.Response) !?Login {
     var result: ?Login = null;
 
@@ -839,20 +967,20 @@ const GET = struct {
 
     fn handle(self: Self) !bool {
         const ti = @typeInfo(Self);
-        inline for (ti.Struct.decls) |f_decl| {
+        inline for (ti.@"struct".decls) |f_decl| {
             const has_arg = f_decl.name.len > 1 and f_decl.name[f_decl.name.len - 1] == '/';
             const match = if (has_arg) std.mem.startsWith(u8, self.req.target, f_decl.name) else std.mem.eql(u8, self.req.target, f_decl.name);
 
             if (match) {
                 const f = @field(Self, f_decl.name);
                 const fi = @typeInfo(@TypeOf(f));
-                if (fi.Fn.params.len == 1) {
+                if (fi.@"fn".params.len == 1) {
                     try @call(.auto, f, .{self});
                 } else {
-                    const arg_type = fi.Fn.params[1].type.?;
+                    const arg_type = fi.@"fn".params[1].type.?;
                     const arg_info = @typeInfo(arg_type);
                     var arg: arg_type = undefined;
-                    const field = arg_info.Struct.fields[0];
+                    const field = arg_info.@"struct".fields[0];
                     if (self.req.target.len <= f_decl.name.len) {
                         return error.NoArgProvided;
                     }
@@ -860,10 +988,10 @@ const GET = struct {
                     const field_ti = @typeInfo(field.type);
                     switch (field_ti) {
                         // TODO: maybe handle BoundedArray?
-                        .Int => {
+                        .int => {
                             @field(arg, field.name) = try std.fmt.parseUnsigned(field.type, str, 16);
                         },
-                        .Enum => {
+                        .@"enum" => {
                             @field(arg, field.name) = try parse_enum(field.type, str, 16);
                         },
                         else => {
@@ -887,14 +1015,18 @@ const GET = struct {
 
             try write_profile(self.res, self.txn, self.logged_in, user);
 
-            try write_posts(self.res, self.txn, self.logged_in, user.posts);
+            try write_posts(self.res, self.txn, self.logged_in, user.posts, .{
+                .show_posts = true,
+                .show_quotes = false,
+                .show_comments = false,
+            });
         } else |err| {
             try self.res.write(
                 \\<p>User not found [{}]</p>
             , .{err});
         }
     }
-    pub fn @"/replies/"(self: Self, args: struct { username: []const u8 }) !void {
+    pub fn @"/comments/"(self: Self, args: struct { username: []const u8 }) !void {
         const user_ids = try Db.user_ids(self.txn);
         if (user_ids.get(try Username.fromSlice(args.username))) |user_id| {
             const users = try Db.users(self.txn);
@@ -902,7 +1034,49 @@ const GET = struct {
 
             try write_profile(self.res, self.txn, self.logged_in, user);
 
-            try write_posts(self.res, self.txn, self.logged_in, user.replies);
+            try write_posts(self.res, self.txn, self.logged_in, user.posts, .{
+                .show_posts = false,
+                .show_quotes = false,
+                .show_comments = true,
+            });
+        } else |err| {
+            try self.res.write(
+                \\<p>User not found [{}]</p>
+            , .{err});
+        }
+    }
+    pub fn @"/quotes/"(self: Self, args: struct { username: []const u8 }) !void {
+        const user_ids = try Db.user_ids(self.txn);
+        if (user_ids.get(try Username.fromSlice(args.username))) |user_id| {
+            const users = try Db.users(self.txn);
+            const user = try users.get(user_id);
+
+            try write_profile(self.res, self.txn, self.logged_in, user);
+
+            try write_posts(self.res, self.txn, self.logged_in, user.posts, .{
+                .show_posts = false,
+                .show_quotes = true,
+                .show_comments = false,
+            });
+        } else |err| {
+            try self.res.write(
+                \\<p>User not found [{}]</p>
+            , .{err});
+        }
+    }
+    pub fn @"/all/"(self: Self, args: struct { username: []const u8 }) !void {
+        const user_ids = try Db.user_ids(self.txn);
+        if (user_ids.get(try Username.fromSlice(args.username))) |user_id| {
+            const users = try Db.users(self.txn);
+            const user = try users.get(user_id);
+
+            try write_profile(self.res, self.txn, self.logged_in, user);
+
+            try write_posts(self.res, self.txn, self.logged_in, user.posts, .{
+                .show_posts = true,
+                .show_quotes = true,
+                .show_comments = true,
+            });
         } else |err| {
             try self.res.write(
                 \\<p>User not found [{}]</p>
@@ -915,20 +1089,23 @@ const GET = struct {
             const users = try Db.users(self.txn);
             const user = try users.get(user_id);
 
-            const following = try user.following.open(self.txn);
-            var it = following.iterator();
+            const following_view = try user.following.open(self.txn);
+
+            var paginate = try Paginate(UserList).init(self.res, following_view, Chirp.UsersPerPage);
 
             try self.res.write(
                 \\<h2><a href="/user/{s}">{s}</a> follows:</h2>
             , .{ user.name.constSlice(), user.display_name.constSlice() });
 
-            while (it.next()) |following_id| {
-                const following_user = try users.get(following_id);
+            while (paginate.next()) |following_id| {
+                const following_user = try users.get(following_id.key);
 
                 try self.res.write(
                     \\<a href="/user/{s}">{s}</a><br />
                 , .{ following_user.name.constSlice(), following_user.display_name.constSlice() });
             }
+
+            try paginate.write_navigation();
         } else |err| {
             try self.res.write(
                 \\<p>User not found [{}]</p>
@@ -941,20 +1118,22 @@ const GET = struct {
             const users = try Db.users(self.txn);
             const user = try users.get(user_id);
 
-            const followers = try user.followers.open(self.txn);
-            var it = followers.iterator();
+            const followers_view = try user.followers.open(self.txn);
+            var paginate = try Paginate(UserList).init(self.res, followers_view, Chirp.UsersPerPage);
 
             try self.res.write(
                 \\<h2><a href="/user/{s}">{s}</a> followers:</h2>
             , .{ user.name.constSlice(), user.display_name.constSlice() });
 
-            while (it.next()) |follower_id| {
-                const follower_user = try users.get(follower_id);
+            while (paginate.next()) |follower_id| {
+                const follower_user = try users.get(follower_id.key);
 
                 try self.res.write(
                     \\<a href="/user/{s}">{s}</a><br />
                 , .{ follower_user.name.constSlice(), follower_user.display_name.constSlice() });
             }
+
+            try paginate.write_navigation();
         } else |err| {
             try self.res.write(
                 \\<p>User not found [{}]</p>
@@ -967,7 +1146,13 @@ const GET = struct {
             .show_comment_field = true,
         });
     }
-    pub fn @"/quotes/"(self: Self, args: struct { post_id: PostId }) !void {
+    pub fn @"/upvotes/"(self: Self, args: struct { post_id: PostId }) !void {
+        const posts = try Db.posts(self.txn);
+        const post = try posts.get(args.post_id);
+        try self.res.write("{} upvotes:<br />", .{post.upvotes});
+        try write_votes(self.res, self.txn, post.votes, .{});
+    }
+    pub fn @"/quoted/"(self: Self, args: struct { post_id: PostId }) !void {
         const posts = try Db.posts(self.txn);
         const post = try posts.get(args.post_id);
 
@@ -977,21 +1162,25 @@ const GET = struct {
             try html_form(self.res, "/quote", .{
                 .{ "type=\"hidden\" name=\"referer\" value=\"{s}\"", .{referer} },
                 .{ "type=\"hidden\" name=\"post_id\" value=\"{x}\"", .{@intFromEnum(post.id)} },
-                "type=\"text\" name=\"text\" placeholder=\"Text\"",
+                "type=\"text\" name=\"text\" placeholder=\"Text\" autofocus",
                 "type=\"submit\" value=\"Quote\"",
             });
             try self.res.write("<br />", .{});
         }
 
-        const quotes_view = try post.quotes.open(self.txn);
-        var it = quotes_view.iterator();
-        while (it.next()) |quote_id| {
-            try write_post(self.res, self.txn, self.logged_in, quote_id, .{ .recurse = 1 });
-            try self.res.write("<br />", .{});
-        }
+        // TODO: show all bc this only contains quotes?
+        try write_posts(self.res, self.txn, self.logged_in, post.quotes, .{
+            .show_posts = false,
+            .show_quotes = true,
+            .show_comments = false,
+        });
     }
-    pub fn @"/list/"(self: Self, args: struct { list_id: PostList.Index }) !void {
-        try write_posts(self.res, self.txn, self.logged_in, PostList{ .idx = args.list_id });
+    pub fn @"/list/"(self: Self, args: struct { list_id: PostList.Base.Index }) !void {
+        try write_posts(self.res, self.txn, self.logged_in, PostList{ .base = .{ .idx = args.list_id } }, .{
+            .show_posts = true,
+            .show_quotes = true,
+            .show_comments = true,
+        });
     }
     pub fn @"/lists"(self: Self) !void {
         if (self.logged_in) |login| {
@@ -1009,8 +1198,8 @@ const GET = struct {
                 const name = kv.val.name;
                 const post_list = kv.val.list;
                 try self.res.write(
-                    \\<a href="/list/{x}">{s}</a>
-                , .{ post_list.idx.?, name.constSlice() });
+                    \\<a href="/list/{x}">{s}</a> 
+                , .{ post_list.base.idx.?, name.constSlice() });
                 try html_form(self.res, "/delete_list", .{
                     .{ "type=\"hidden\" name=\"list_id\" value=\"{x}\"", .{kv.key} },
                     "type=\"submit\" value=\"Delete\"",
@@ -1021,8 +1210,8 @@ const GET = struct {
             try self.res.write("not logged in", .{});
         }
     }
-    pub fn @"/feed/"(self: Self, args: struct { feed_id: UserList.Index }) !void {
-        try write_timeline(self.res, self.txn, self.logged_in, UserList{ .idx = args.feed_id });
+    pub fn @"/feed/"(self: Self, args: struct { feed_id: UserList.Base.Index }) !void {
+        try write_timeline(self.res, self.txn, self.logged_in, UserList{ .base = .{ .idx = args.feed_id } });
     }
     pub fn @"/feeds"(self: Self) !void {
         if (self.logged_in) |login| {
@@ -1040,8 +1229,8 @@ const GET = struct {
                 const name = kv.val.name;
                 const user_list = kv.val.list;
                 try self.res.write(
-                    \\<a href="/feed/{x}">{s}</a>
-                , .{ user_list.idx.?, name.constSlice() });
+                    \\<a href="/feed/{x}">{s}</a> 
+                , .{ user_list.base.idx.?, name.constSlice() });
                 try html_form(self.res, "/delete_feed", .{
                     .{ "type=\"hidden\" name=\"list_id\" value=\"{x}\"", .{kv.key} },
                     "type=\"submit\" value=\"Delete\"",
@@ -1059,7 +1248,7 @@ const GET = struct {
 
             try html_form(self.res, "/post", .{
                 .{ "type=\"hidden\" name=\"referer\" value=\"{s}\"", .{referer} },
-                "type=\"text\" name=\"text\"",
+                "type=\"text\" name=\"text\" placeholder=\"Text\" autofocus",
                 "type=\"submit\" value=\"Post\"",
             });
         } else {
@@ -1078,6 +1267,11 @@ const GET = struct {
                 .{ "type=\"text\" name=\"display_name\" placeholder=\"{s}\"", .{login.user.display_name.constSlice()} },
                 "type=\"submit\" value=\"Change\"",
             });
+            try self.res.write("<br />Description: ", .{});
+            try html_form(self.res, "/set_description", .{
+                .{ "textarea", "type=\"text\" name=\"description\" placeholder=\"{s}\"", .{login.user.description.constSlice()} },
+                "type=\"submit\" value=\"Change\"",
+            });
             try self.res.write("<br />Password: ", .{});
             try html_form(self.res, "/set_password", .{
                 "type=\"text\" name=\"password\"",
@@ -1109,24 +1303,24 @@ const POST = struct {
 
     pub fn handle(self: Self) !bool {
         const ti = @typeInfo(Self);
-        inline for (ti.Struct.decls) |f_decl| {
+        inline for (ti.@"struct".decls) |f_decl| {
             if (std.mem.eql(u8, f_decl.name, self.req.target)) {
                 const f = @field(Self, f_decl.name);
                 const fi = @typeInfo(@TypeOf(f));
-                if (fi.Fn.params.len == 1) {
+                if (fi.@"fn".params.len == 1) {
                     _ = try @call(.auto, f, .{self});
                 } else {
-                    const args_type = fi.Fn.params[fi.Fn.params.len - 1].type.?;
+                    const args_type = fi.@"fn".params[fi.@"fn".params.len - 1].type.?;
                     const argsi = @typeInfo(args_type);
                     var args: args_type = undefined;
-                    inline for (argsi.Struct.fields) |field| {
+                    inline for (argsi.@"struct".fields) |field| {
                         const str = self.req.get_value(field.name) orelse return error.ArgNotFound;
                         const field_ti = @typeInfo(field.type);
                         switch (field_ti) {
-                            .Int => {
+                            .int => {
                                 @field(args, field.name) = try std.fmt.parseUnsigned(field.type, str, 16);
                             },
-                            .Enum => {
+                            .@"enum" => {
                                 @field(args, field.name) = try parse_enum(field.type, str, 16);
                             },
                             else => {
@@ -1201,6 +1395,18 @@ const POST = struct {
         user.display_name = display_name;
         try users.put(login.user.id, user);
     }
+    pub fn @"/set_description"(self: Self, args: struct { description: []const u8 }) !void {
+        const login = self.logged_in orelse return error.NotLoggedIn;
+        const description = try reencode(UserDescription, args.description);
+
+        const txn = try self.env.txn();
+        defer txn.commit() catch {};
+
+        const users = try Db.users(txn);
+        var user = login.user;
+        user.description = description;
+        try users.put(login.user.id, user);
+    }
     pub fn @"/set_password"(self: Self, args: struct { password: []const u8 }) !void {
         const login = self.logged_in orelse return error.NotLoggedIn;
 
@@ -1267,7 +1473,7 @@ const POST = struct {
             try txn.commit();
         }
     }
-    pub fn @"/delete_list"(self: Self, args: struct { list_id: PostList.Index }) !void {
+    pub fn @"/delete_list"(self: Self, args: struct { list_id: PostList.Base.Index }) !void {
         if (self.logged_in) |login| {
             var post_list: ?PostList = null;
             {
@@ -1285,14 +1491,14 @@ const POST = struct {
             }
         }
     }
-    pub fn @"/list_add"(self: Self, args: struct { list_id: PostList.Index, post_id: PostId }) !void {
+    pub fn @"/list_add"(self: Self, args: struct { list_id: PostList.Base.Index, post_id: PostId }) !void {
         if (self.logged_in) |login| {
             _ = login;
 
             const txn = try self.env.txn();
             defer txn.commit() catch {};
 
-            const post_list = PostList{ .idx = args.list_id };
+            const post_list = PostList{ .base = .{ .idx = args.list_id } };
             var post_list_view = try post_list.open(txn);
             if (try post_list_view.has(args.post_id)) {
                 try post_list_view.del(args.post_id);
@@ -1316,7 +1522,7 @@ const POST = struct {
             try txn.commit();
         }
     }
-    pub fn @"/delete_feed"(self: Self, args: struct { list_id: UserList.Index }) !void {
+    pub fn @"/delete_feed"(self: Self, args: struct { list_id: UserList.Base.Index }) !void {
         if (self.logged_in) |login| {
             var user_list: ?UserList = null;
 
@@ -1335,14 +1541,14 @@ const POST = struct {
             }
         }
     }
-    pub fn @"/feed_add"(self: Self, args: struct { feed_id: UserList.Index, user_id: UserId }) !void {
+    pub fn @"/feed_add"(self: Self, args: struct { feed_id: UserList.Base.Index, user_id: UserId }) !void {
         if (self.logged_in) |login| {
             _ = login;
 
             const txn = try self.env.txn();
             defer txn.commit() catch {};
 
-            const user_list = UserList{ .idx = args.feed_id };
+            const user_list = UserList{ .base = .{ .idx = args.feed_id } };
             var user_list_view = try user_list.open(txn);
             if (try user_list_view.has(args.user_id)) {
                 try user_list_view.del(args.user_id);
@@ -1473,9 +1679,10 @@ pub fn main() !void {
         server.wait();
         while (true) {
             const req = (server.next_request(&req_buffer) catch break) orelse break;
-            handle_request(env, req) catch {
-                try handle_error(env, req);
-            };
+            // handle_request(env, req) catch {
+            //     try handle_error(env, req);
+            // };
+            try handle_request(env, req);
         }
     }
     // const ThreadCount = 1;
@@ -1493,7 +1700,7 @@ pub fn main() !void {
 
 fn handle_error(env: lmdb.Env, req: http.Request) !void {
     _ = env;
-    var res = http.Response.init(req.fd, &res_head_buffer, &res_body_buffer);
+    var res = http.Response.init(req, &res_head_buffer, &res_body_buffer);
     try write_start(&res);
     try res.write("Oops, something went terribly wrong there D:", .{});
     try write_end(&res);
@@ -1504,7 +1711,7 @@ fn handle_request(env: lmdb.Env, req: http.Request) !void {
     // std.debug.print("[{}]: {s}\n", .{ req.method, req.head.? });
 
     // reponse
-    var res = http.Response.init(req.fd, &res_head_buffer, &res_body_buffer);
+    var res = http.Response.init(req, &res_head_buffer, &res_body_buffer);
 
     // check session token
     const logged_in: ?Login = try check_login(env, req, &res);