]> gitweb.ps.run Git - chirp/blobdiff - src/main.zig
start work on refactor
[chirp] / src / main.zig
index 29bedcfb4159e6ef7264ce47aed6ef25a02da73a..9fb872f6287e64b4870ccfe9e3f2eafaba3e1348 100644 (file)
@@ -6,6 +6,18 @@ const http = @import("http");
 // db {{{
 
 const Db = struct {
+    fn users(txn: lmdb.Txn) !UserList {
+        return try db.Db(UserId, User).init(txn, "users");
+    }
+    fn user_ids(txn: lmdb.Txn) !UsernameList {
+        return try db.Db(Username, UserId).init(txn, "user_ids");
+    }
+    fn sessions(txn: lmdb.Txn) !SessionList {
+        return try db.Db(SessionToken, UserId).init(txn, "sessions");
+    }
+    fn posts(txn: lmdb.Txn) !PostList {
+        return try db.Db(PostId, Post).init(txn, "posts");
+    }
     fn users(txn: lmdb.Txn) !db.Db(UserId, User) {
         return try db.Db(UserId, User).init(txn, "users");
     }
@@ -32,10 +44,10 @@ const User = struct {
     description: UserDescription,
     password_hash: PasswordHash,
 
-    posts: PostList,
+    posts: PostSet,
 
-    following: UserList,
-    followers: UserList,
+    following: UserSet,
+    followers: UserSet,
 
     post_lists: PostListList,
     feeds: UserListList,
@@ -52,19 +64,19 @@ const Post = struct {
     upvotes: u64 = 0,
     downvotes: u64 = 0,
     votes: VoteList,
-    comments: PostList,
-    quotes: PostList,
+    comments: PostSet,
+    quotes: PostSet,
 
     text: PostText,
 };
 
 const SavedPostList = struct {
     name: Name,
-    list: PostList,
+    list: PostSet,
 };
 const SavedUserList = struct {
     name: Name,
-    list: UserList,
+    list: UserSet,
 };
 
 const Vote = struct {
@@ -90,8 +102,11 @@ const PasswordHash = std.BoundedArray(u8, 128);
 const SessionToken = u64;
 const CookieValue = std.BoundedArray(u8, 128);
 const PostText = std.BoundedArray(u8, 1024);
-const PostList = db.Set(PostId);
-const UserList = db.Set(UserId);
+const PostSet = db.Set(PostId);
+const UserSet = db.Set(UserId);
+const PostList = db.SetList(PostId, Post);
+const UserList = db.SetList(UserId, User);
+const UsernameList = db.SetList(Username, UserId);
 const VoteList = db.SetList(UserId, Vote);
 const PostListList = db.List(SavedPostList);
 const UserListList = db.List(SavedUserList);
@@ -163,19 +178,18 @@ 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);
@@ -184,8 +198,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(),
@@ -220,9 +233,9 @@ const Chirp = struct {
                 .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),
-                .followers = try UserList.init(txn),
+                .posts = try PostSet.init(txn),
+                .following = try UserSet.init(txn),
+                .followers = try UserSet.init(txn),
                 .post_lists = try PostListList.init(txn),
                 .feeds = try UserListList.init(txn),
             });
@@ -268,7 +281,7 @@ const Chirp = struct {
         try sessions.del(session_token);
     }
 
-    fn append_post(env: lmdb.Env, user_id: UserId, post_list: PostList, parent_id: ?PostId, quote_id: ?PostId, text: []const u8) !PostId {
+    fn append_post(env: lmdb.Env, user_id: UserId, post_list: PostSet, parent_id: ?PostId, quote_id: ?PostId, text: []const u8) !PostId {
         var post_id: PostId = undefined;
 
         // TODO: do this in one commit
@@ -290,8 +303,8 @@ const Chirp = struct {
                 .user_id = user_id,
                 .time = std.time.timestamp(),
                 .votes = try VoteList.init(txn),
-                .comments = try PostList.init(txn),
-                .quotes = try PostList.init(txn),
+                .comments = try PostSet.init(txn),
+                .quotes = try PostSet.init(txn),
                 .text = decoded_text,
             });
         }
@@ -601,6 +614,10 @@ fn write_start(res: *http.Response) !void {
         \\  form {
         \\    display: inline-block;
         \\  }
+        \\  body {
+        \\    margin:40px auto;max-width:650px;line-height:1.6;font-size:18px;color:#444;padding:0 10px;
+        \\  }
+        \\  h1,h2,h3{line-height:1.2}
         \\</style>
         \\</head>
         \\<body>
@@ -716,7 +733,7 @@ fn write_post(res: *http.Response, txn: lmdb.Txn, logged_in: ?Login, post_id: Po
         try res.write("<br /><br />", .{});
         try html_form(res, "/comment", .{
             .{ "type=\"hidden\" value=\"{x}\" name=\"post_id\"", .{@intFromEnum(post.id)} },
-            "type=\"text\" name=\"text\" placeholder=\"Text\"",
+            .{ "textarea", "type=\"text\" name=\"text\" placeholder=\"Text\"", .{} },
             "type=\"submit\" value=\"Comment\"",
         });
         try res.write("<br />", .{});
@@ -827,14 +844,14 @@ fn write_profile(res: *http.Response, txn: lmdb.Txn, logged_in: ?Login, user: Us
 
     try res.write("<br />", .{});
 }
-fn write_posts(res: *http.Response, txn: lmdb.Txn, logged_in: ?Login, post_list: PostList, options: struct {
+fn write_posts(res: *http.Response, txn: lmdb.Txn, logged_in: ?Login, post_list: PostSet, options: struct {
     show_posts: bool,
     show_quotes: bool,
     show_comments: bool,
 }) !void {
     const posts_view = try post_list.open(txn);
 
-    var paginate = try Paginate(PostList).init(res, posts_view, Chirp.PostsPerPage);
+    var paginate = try Paginate(PostSet).init(res, posts_view, Chirp.PostsPerPage);
 
     while (paginate.next()) |post_id| {
         const posts = try Db.posts(txn);
@@ -850,7 +867,8 @@ fn write_posts(res: *http.Response, txn: lmdb.Txn, logged_in: ?Login, post_list:
 
     try paginate.write_navigation();
 }
-fn write_timeline(res: *http.Response, txn: lmdb.Txn, logged_in: ?Login, user_list: UserList) !void {
+fn write_timeline(res: *http.Response, txn: lmdb.Txn, logged_in: ?Login, user_list: UserSet) !void {
+    // TODO: paginate
     const users = try Db.users(txn);
     const posts = try Db.posts(txn);
 
@@ -858,6 +876,10 @@ fn write_timeline(res: *http.Response, txn: lmdb.Txn, logged_in: ?Login, user_li
     var prev_newest_post: ?Post = null;
 
     const following = try user_list.open(txn);
+    if (following.len() == 0) {
+        try res.write("Empty timeline (no users)", .{});
+        return;
+    }
 
     while (true) {
         var newest_post: ?Post = null;
@@ -894,6 +916,19 @@ fn write_timeline(res: *http.Response, txn: lmdb.Txn, logged_in: ?Login, user_li
         try res.write("<br />", .{});
     }
 }
+fn write_frontpage(res: *http.Response, txn: lmdb.Txn) !void {
+    const posts = try Db.posts(txn);
+    var counter: u64 = 0;
+    var it = try posts.reverse_iterator();
+    while (it.next()) |p| {
+        if (p.val.parent_id == null and p.val.quote_id == null) {
+            try write_post(res, txn, null, p.key, .{ .recurse = 1 });
+            counter += 1;
+        }
+
+        if (counter >= 10) break;
+    }
+}
 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);
@@ -1093,7 +1128,7 @@ const GET = struct {
 
             const following_view = try user.following.open(self.txn);
 
-            var paginate = try Paginate(UserList).init(self.res, following_view, Chirp.UsersPerPage);
+            var paginate = try Paginate(UserSet).init(self.res, following_view, Chirp.UsersPerPage);
 
             try self.res.write(
                 \\<h2><a href="/user/{s}">{s}</a> follows:</h2>
@@ -1121,7 +1156,7 @@ const GET = struct {
             const user = try users.get(user_id);
 
             const followers_view = try user.followers.open(self.txn);
-            var paginate = try Paginate(UserList).init(self.res, followers_view, Chirp.UsersPerPage);
+            var paginate = try Paginate(UserSet).init(self.res, followers_view, Chirp.UsersPerPage);
 
             try self.res.write(
                 \\<h2><a href="/user/{s}">{s}</a> followers:</h2>
@@ -1164,7 +1199,7 @@ 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\" autofocus",
+                .{ "textarea", "type=\"text\" name=\"text\" placeholder=\"Text\" autofocus", .{} },
                 "type=\"submit\" value=\"Quote\"",
             });
             try self.res.write("<br />", .{});
@@ -1177,8 +1212,8 @@ const GET = struct {
             .show_comments = false,
         });
     }
-    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 } }, .{
+    pub fn @"/list/"(self: Self, args: struct { list_id: PostSet.Base.Index }) !void {
+        try write_posts(self.res, self.txn, self.logged_in, PostSet{ .base = .{ .idx = args.list_id } }, .{
             .show_posts = true,
             .show_quotes = true,
             .show_comments = true,
@@ -1212,8 +1247,8 @@ const GET = struct {
             try self.res.write("not logged in", .{});
         }
     }
-    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 @"/feed/"(self: Self, args: struct { feed_id: UserSet.Base.Index }) !void {
+        try write_timeline(self.res, self.txn, self.logged_in, UserSet{ .base = .{ .idx = args.feed_id } });
     }
     pub fn @"/feeds"(self: Self) !void {
         if (self.logged_in) |login| {
@@ -1250,7 +1285,7 @@ const GET = struct {
 
             try html_form(self.res, "/post", .{
                 .{ "type=\"hidden\" name=\"referer\" value=\"{s}\"", .{referer} },
-                "type=\"text\" name=\"text\" placeholder=\"Text\" autofocus",
+                .{ "textarea", "type=\"text\" name=\"text\" placeholder=\"Text\" autofocus", .{} },
                 "type=\"submit\" value=\"Post\"",
             });
         } else {
@@ -1289,6 +1324,7 @@ const GET = struct {
         } else {
             // TODO: generic home
             try self.res.write("Homepage", .{});
+            // try write_frontpage(self.res, self.txn);
         }
     }
 };
@@ -1466,7 +1502,7 @@ const POST = struct {
             // TODO: decode name
 
             var txn = try self.env.txn();
-            const postlist = try PostList.init(txn);
+            const postlist = try PostSet.init(txn);
             try txn.commit();
 
             txn = try self.env.txn();
@@ -1475,9 +1511,9 @@ const POST = struct {
             try txn.commit();
         }
     }
-    pub fn @"/delete_list"(self: Self, args: struct { list_id: PostList.Base.Index }) !void {
+    pub fn @"/delete_list"(self: Self, args: struct { list_id: PostSet.Base.Index }) !void {
         if (self.logged_in) |login| {
-            var post_list: ?PostList = null;
+            var post_list: ?PostSet = null;
             {
                 const txn = try self.env.txn();
                 defer txn.commit() catch {};
@@ -1493,14 +1529,14 @@ const POST = struct {
             }
         }
     }
-    pub fn @"/list_add"(self: Self, args: struct { list_id: PostList.Base.Index, post_id: PostId }) !void {
+    pub fn @"/list_add"(self: Self, args: struct { list_id: PostSet.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{ .base = .{ .idx = args.list_id } };
+            const post_list = PostSet{ .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);
@@ -1515,7 +1551,7 @@ const POST = struct {
             const name = try Name.fromSlice(name_str);
 
             var txn = try self.env.txn();
-            const userlist = try UserList.init(txn);
+            const userlist = try UserSet.init(txn);
             try txn.commit();
 
             txn = try self.env.txn();
@@ -1524,9 +1560,9 @@ const POST = struct {
             try txn.commit();
         }
     }
-    pub fn @"/delete_feed"(self: Self, args: struct { list_id: UserList.Base.Index }) !void {
+    pub fn @"/delete_feed"(self: Self, args: struct { list_id: UserSet.Base.Index }) !void {
         if (self.logged_in) |login| {
-            var user_list: ?UserList = null;
+            var user_list: ?UserSet = null;
 
             {
                 const txn = try self.env.txn();
@@ -1543,14 +1579,14 @@ const POST = struct {
             }
         }
     }
-    pub fn @"/feed_add"(self: Self, args: struct { feed_id: UserList.Base.Index, user_id: UserId }) !void {
+    pub fn @"/feed_add"(self: Self, args: struct { feed_id: UserSet.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{ .base = .{ .idx = args.feed_id } };
+            const user_list = UserSet{ .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);