]> gitweb.ps.run Git - chirp/commitdiff
changes
authorpatrick-scho <patrick.schoenberger@posteo.de>
Fri, 9 May 2025 17:22:47 +0000 (19:22 +0200)
committerpatrick-scho <patrick.schoenberger@posteo.de>
Fri, 9 May 2025 17:22:47 +0000 (19:22 +0200)
src/main.zig

index 9fb872f6287e64b4870ccfe9e3f2eafaba3e1348..24bdc51a2cbee1f424e54154735844248ddd8e1a 100644 (file)
@@ -6,30 +6,34 @@ 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");
-    }
-    fn user_ids(txn: lmdb.Txn) !db.Db(Username, UserId) {
-        return try db.Db(Username, UserId).init(txn, "user_ids");
-    }
-    fn sessions(txn: lmdb.Txn) !db.Db(SessionToken, UserId) {
-        return try db.Db(SessionToken, UserId).init(txn, "sessions");
-    }
-    fn posts(txn: lmdb.Txn) !db.Db(PostId, Post) {
-        return try db.Db(PostId, Post).init(txn, "posts");
+    pub fn init(env: lmdb.Env) !void {
+        const txn = try env.txn();
+        const dbi = try txn.dbi(null);
+        if (try dbi.has(1001)) {
+            users = try dbi.get(1001, @TypeOf(users));
+        } else {
+            users = try @TypeOf(users).init(txn);
+        }
+        if (try dbi.has(1002)) {
+            user_ids = try dbi.get(1002, @TypeOf(user_ids));
+        } else {
+            user_ids = try @TypeOf(user_ids).init(txn);
+        }
+        if (try dbi.has(1003)) {
+            sessions = try dbi.get(1003, @TypeOf(sessions));
+        } else {
+            sessions = try @TypeOf(sessions).init(txn);
+        }
+        if (try dbi.has(1004)) {
+            posts = try dbi.get(1004, @TypeOf(posts));
+        } else {
+            posts = try @TypeOf(posts).init(txn);
+        }
     }
+    var users: UserList = undefined;
+    var user_ids: UsernameList = undefined;
+    var sessions: SessionList = undefined;
+    var posts: PostList = undefined;
 };
 
 // }}}
@@ -107,6 +111,7 @@ const UserSet = db.Set(UserId);
 const PostList = db.SetList(PostId, Post);
 const UserList = db.SetList(UserId, User);
 const UsernameList = db.SetList(Username, UserId);
+const SessionList = db.SetList(SessionToken, UserId);
 const VoteList = db.SetList(UserId, Vote);
 const PostListList = db.List(SavedPostList);
 const UserListList = db.List(SavedUserList);
@@ -219,15 +224,15 @@ const Chirp = struct {
             std.debug.print("error registering user: {}\n", .{err});
         };
 
-        const users = try Db.users(txn);
-        const user_ids = try Db.user_ids(txn);
+        var users = try Db.users.open(txn);
+        var user_ids = try Db.user_ids.open(txn);
 
         if (try user_ids.has(username_array)) {
             return false;
         } else {
-            const user_id = try db.Prng.gen(users.dbi, UserId);
+            const user_id = try db.Prng.gen(users.base.dbi, UserId);
 
-            try users.put(user_id, User{
+            try users.append(user_id, User{
                 .id = user_id,
                 .name = username_array,
                 .display_name = display_name,
@@ -240,7 +245,7 @@ const Chirp = struct {
                 .feeds = try UserListList.init(txn),
             });
 
-            try user_ids.put(username_array, user_id);
+            try user_ids.append(username_array, user_id);
 
             return true;
         }
@@ -256,17 +261,17 @@ const Chirp = struct {
         const txn = try env.txn();
         defer txn.commit() catch {};
 
-        const user_ids = try Db.user_ids(txn);
+        const user_ids = try Db.user_ids.open(txn);
         const user_id = try user_ids.get(username_array);
         std.debug.print("user logging in, id: {}\n", .{user_id});
 
-        const users = try Db.users(txn);
+        const users = try Db.users.open(txn);
         const user = try users.get(user_id);
 
         if (verify_password(password, user.password_hash)) {
-            const sessions = try Db.sessions(txn);
-            const session_token = try db.Prng.gen(sessions.dbi, SessionToken);
-            try sessions.put(session_token, user_id);
+            var sessions = try Db.sessions.open(txn);
+            const session_token = try db.Prng.gen(sessions.base.dbi, SessionToken);
+            try sessions.append(session_token, user_id);
             return session_token;
         } else {
             return error.IncorrectPassword;
@@ -277,7 +282,7 @@ const Chirp = struct {
         const txn = try env.txn();
         defer txn.commit() catch {};
 
-        const sessions = try Db.sessions(txn);
+        var sessions = try Db.sessions.open(txn);
         try sessions.del(session_token);
     }
 
@@ -292,7 +297,7 @@ const Chirp = struct {
             txn = try env.txn();
             defer txn.commit() catch {};
 
-            const posts = try Db.posts(txn);
+            const posts = try Db.posts.open(txn);
             post_id = try db.Prng.gen(posts.dbi, PostId);
 
             const decoded_text = try reencode(PostText, text);
@@ -322,7 +327,7 @@ const Chirp = struct {
             txn = try env.txn();
             defer txn.commit() catch {};
 
-            const posts = try Db.posts(txn);
+            const posts = try Db.posts.open(txn);
             const quote_post = try posts.get(quote_id.?);
             var quotes = try quote_post.quotes.open(txn);
             try quotes.append(post_id);
@@ -333,7 +338,7 @@ const Chirp = struct {
 
     fn post(env: lmdb.Env, user_id: UserId, text: []const u8) !void {
         var txn = try env.txn();
-        const users = try Db.users(txn);
+        const users = try Db.users.open(txn);
         const user = try users.get(user_id);
         txn.abort();
 
@@ -343,10 +348,10 @@ const Chirp = struct {
 
     fn comment(env: lmdb.Env, user_id: UserId, parent_post_id: PostId, text: []const u8) !void {
         var txn = try env.txn();
-        const users = try Db.users(txn);
+        const users = try Db.users.open(txn);
         const user = try users.get(user_id);
 
-        const posts = try Db.posts(txn);
+        const posts = try Db.posts.open(txn);
         const parent_post = try posts.get(parent_post_id);
         txn.abort();
 
@@ -360,7 +365,7 @@ const Chirp = struct {
 
     fn quote(env: lmdb.Env, user_id: UserId, quote_post_id: PostId, text: []const u8) !void {
         var txn = try env.txn();
-        const users = try Db.users(txn);
+        const users = try Db.users.open(txn);
         const user = try users.get(user_id);
         txn.abort();
 
@@ -372,7 +377,7 @@ const Chirp = struct {
         const txn = try env.txn();
         defer txn.commit() catch {};
 
-        const posts = try Db.posts(txn);
+        const posts = try Db.posts.open(txn);
 
         var p = try posts.get(post_id);
         var votes_view = try p.votes.open(txn);
@@ -412,7 +417,7 @@ const Chirp = struct {
         const txn = try env.txn();
         defer txn.commit() catch {};
 
-        const users = try Db.users(txn);
+        const users = try Db.users.open(txn);
 
         const user = try users.get(user_id);
         const user_to_follow = try users.get(user_id_to_follow);
@@ -435,7 +440,7 @@ const Chirp = struct {
         const txn = try env.txn();
         defer txn.abort();
 
-        const sessions = try Db.sessions(txn);
+        const sessions = try Db.sessions.open(txn);
 
         return try sessions.get(session_token);
     }
@@ -444,7 +449,7 @@ const Chirp = struct {
         const txn = try env.txn();
         defer txn.abort();
 
-        const users = try Db.users(txn);
+        const users = try Db.users.open(txn);
         return try users.get(user_id);
     }
 };
@@ -627,12 +632,12 @@ fn write_end(res: *http.Response) !void {
     try res.write("</body></html>", .{});
 }
 fn write_post(res: *http.Response, txn: lmdb.Txn, logged_in: ?Login, post_id: PostId, options: struct { recurse: u8 = 0, show_comment_field: bool = false }) !void {
-    const posts = try Db.posts(txn);
+    const posts = try Db.posts.open(txn);
     const post = posts.get(post_id) catch {
         res.redirect("/") catch {};
         return;
     };
-    const users = try Db.users(txn);
+    const users = try Db.users.open(txn);
     const user = try users.get(post.user_id);
 
     try res.write(
@@ -854,7 +859,7 @@ fn write_posts(res: *http.Response, txn: lmdb.Txn, logged_in: ?Login, post_list:
     var paginate = try Paginate(PostSet).init(res, posts_view, Chirp.PostsPerPage);
 
     while (paginate.next()) |post_id| {
-        const posts = try Db.posts(txn);
+        const posts = try Db.posts.open(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
@@ -869,8 +874,8 @@ fn write_posts(res: *http.Response, txn: lmdb.Txn, logged_in: ?Login, post_list:
 }
 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);
+    const users = try Db.users.open(txn);
+    const posts = try Db.posts.open(txn);
 
     var newest_post_ids = try std.BoundedArray(PostId, 10).init(0); // TODO: TimelinePostsCount
     var prev_newest_post: ?Post = null;
@@ -917,7 +922,7 @@ fn write_timeline(res: *http.Response, txn: lmdb.Txn, logged_in: ?Login, user_li
     }
 }
 fn write_frontpage(res: *http.Response, txn: lmdb.Txn) !void {
-    const posts = try Db.posts(txn);
+    const posts = try Db.posts.open(txn);
     var counter: u64 = 0;
     var it = try posts.reverse_iterator();
     while (it.next()) |p| {
@@ -930,7 +935,7 @@ fn write_frontpage(res: *http.Response, txn: lmdb.Txn) !void {
     }
 }
 fn write_user(res: *http.Response, txn: lmdb.Txn, user_id: UserId) !void {
-    const users = try Db.users(txn);
+    const users = try Db.users.open(txn);
     const user = try users.get(user_id);
     try res.write(
         \\<a href="/user/{s}">{s}</a>
@@ -970,7 +975,7 @@ fn check_login(env: lmdb.Env, req: http.Request, res: *http.Response) !?Login {
             if (Chirp.get_session_user_id(env, session_token) catch null) |user_id| {
                 const txn = try env.txn();
                 defer txn.abort();
-                const users = try Db.users(txn);
+                const users = try Db.users.open(txn);
 
                 result = .{
                     .user = try users.get(user_id),
@@ -1045,9 +1050,9 @@ const GET = struct {
     }
 
     pub fn @"/user/"(self: Self, args: struct { username: []const u8 }) !void {
-        const user_ids = try Db.user_ids(self.txn);
+        const user_ids = try Db.user_ids.open(self.txn);
         if (user_ids.get(try Username.fromSlice(args.username))) |user_id| {
-            const users = try Db.users(self.txn);
+            const users = try Db.users.open(self.txn);
             const user = try users.get(user_id);
 
             try write_profile(self.res, self.txn, self.logged_in, user);
@@ -1064,9 +1069,9 @@ const GET = struct {
         }
     }
     pub fn @"/comments/"(self: Self, args: struct { username: []const u8 }) !void {
-        const user_ids = try Db.user_ids(self.txn);
+        const user_ids = try Db.user_ids.open(self.txn);
         if (user_ids.get(try Username.fromSlice(args.username))) |user_id| {
-            const users = try Db.users(self.txn);
+            const users = try Db.users.open(self.txn);
             const user = try users.get(user_id);
 
             try write_profile(self.res, self.txn, self.logged_in, user);
@@ -1083,9 +1088,9 @@ const GET = struct {
         }
     }
     pub fn @"/quotes/"(self: Self, args: struct { username: []const u8 }) !void {
-        const user_ids = try Db.user_ids(self.txn);
+        const user_ids = try Db.user_ids.open(self.txn);
         if (user_ids.get(try Username.fromSlice(args.username))) |user_id| {
-            const users = try Db.users(self.txn);
+            const users = try Db.users.open(self.txn);
             const user = try users.get(user_id);
 
             try write_profile(self.res, self.txn, self.logged_in, user);
@@ -1102,9 +1107,9 @@ const GET = struct {
         }
     }
     pub fn @"/all/"(self: Self, args: struct { username: []const u8 }) !void {
-        const user_ids = try Db.user_ids(self.txn);
+        const user_ids = try Db.user_ids.open(self.txn);
         if (user_ids.get(try Username.fromSlice(args.username))) |user_id| {
-            const users = try Db.users(self.txn);
+            const users = try Db.users.open(self.txn);
             const user = try users.get(user_id);
 
             try write_profile(self.res, self.txn, self.logged_in, user);
@@ -1121,9 +1126,9 @@ const GET = struct {
         }
     }
     pub fn @"/following/"(self: Self, args: struct { username: []const u8 }) !void {
-        const user_ids = try Db.user_ids(self.txn);
+        const user_ids = try Db.user_ids.open(self.txn);
         if (user_ids.get(try Username.fromSlice(args.username))) |user_id| {
-            const users = try Db.users(self.txn);
+            const users = try Db.users.open(self.txn);
             const user = try users.get(user_id);
 
             const following_view = try user.following.open(self.txn);
@@ -1150,9 +1155,9 @@ const GET = struct {
         }
     }
     pub fn @"/followers/"(self: Self, args: struct { username: []const u8 }) !void {
-        const user_ids = try Db.user_ids(self.txn);
+        const user_ids = try Db.user_ids.open(self.txn);
         if (user_ids.get(try Username.fromSlice(args.username))) |user_id| {
-            const users = try Db.users(self.txn);
+            const users = try Db.users.open(self.txn);
             const user = try users.get(user_id);
 
             const followers_view = try user.followers.open(self.txn);
@@ -1184,13 +1189,13 @@ const GET = struct {
         });
     }
     pub fn @"/upvotes/"(self: Self, args: struct { post_id: PostId }) !void {
-        const posts = try Db.posts(self.txn);
+        const posts = try Db.posts.open(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 posts = try Db.posts.open(self.txn);
         const post = try posts.get(args.post_id);
 
         const referer = if (self.req.get_header("Referer")) |ref| ref else self.req.target;
@@ -1409,13 +1414,13 @@ const POST = struct {
         const txn = try self.env.txn();
         defer txn.commit() catch {};
 
-        const user_ids = try Db.user_ids(txn);
+        var user_ids = try Db.user_ids.open(txn);
 
         if (!try user_ids.has(username)) {
             try user_ids.del(login.user.name);
             try user_ids.put(username, login.user.id);
 
-            const users = try Db.users(txn);
+            const users = try Db.users.open(txn);
             var user = login.user;
             user.name = username;
             try users.put(login.user.id, user);
@@ -1428,7 +1433,7 @@ const POST = struct {
         const txn = try self.env.txn();
         defer txn.commit() catch {};
 
-        const users = try Db.users(txn);
+        const users = try Db.users.open(txn);
         var user = login.user;
         user.display_name = display_name;
         try users.put(login.user.id, user);
@@ -1440,7 +1445,7 @@ const POST = struct {
         const txn = try self.env.txn();
         defer txn.commit() catch {};
 
-        const users = try Db.users(txn);
+        const users = try Db.users.open(txn);
         var user = login.user;
         user.description = description;
         try users.put(login.user.id, user);
@@ -1451,7 +1456,7 @@ const POST = struct {
         const txn = try self.env.txn();
         defer txn.commit() catch {};
 
-        const users = try Db.users(txn);
+        const users = try Db.users.open(txn);
         var user = login.user;
         user.password_hash = try Chirp.hash_password(args.password);
         try users.put(login.user.id, user);
@@ -1635,8 +1640,8 @@ fn list_users(env: lmdb.Env) !void {
     const txn = try env.txn();
     defer txn.abort();
 
-    const users = try Db.users(txn);
-    var it = try users.iterator();
+    const users = try Db.users.open(txn);
+    var it = users.iterator();
 
     while (it.next()) |kv| {
         const key = kv.key;
@@ -1648,8 +1653,8 @@ fn list_user_ids(env: lmdb.Env) !void {
     const txn = try env.txn();
     defer txn.abort();
 
-    const user_ids = try Db.user_ids(txn);
-    var it = try user_ids.iterator();
+    const user_ids = try Db.user_ids.open(txn);
+    var it = user_ids.iterator();
 
     while (it.next()) |kv| {
         const key = kv.key;
@@ -1662,8 +1667,8 @@ fn list_sessions(env: lmdb.Env) !void {
     const txn = try env.txn();
     defer txn.abort();
 
-    const sessions = try Db.sessions(txn);
-    var it = try sessions.iterator();
+    const sessions = try Db.sessions.open(txn);
+    var it = sessions.iterator();
 
     while (it.next()) |kv| {
         const key = kv.key;
@@ -1676,8 +1681,8 @@ fn list_posts(env: lmdb.Env) !void {
     const txn = try env.txn();
     defer txn.abort();
 
-    const posts = try Db.posts(txn);
-    var it = try posts.iterator();
+    const posts = try Db.posts.open(txn);
+    var it = posts.iterator();
 
     while (it.next()) |kv| {
         const key = kv.key;
@@ -1704,6 +1709,8 @@ pub fn main() !void {
     var env = try lmdb.Env.open("db", 1024 * 1024 * 10);
     defer env.close();
 
+    try Db.init(env);
+
     std.debug.print("Users:\n", .{});
     try list_users(env);
     std.debug.print("User IDs:\n", .{});