X-Git-Url: https://gitweb.ps.run/chirp/blobdiff_plain/f3f8c8b971b7b8dcb2f391ab61dfd1f03da8da9e..d4496703a509fb5ad5352787ae277ebe204195c8:/src/main.zig diff --git a/src/main.zig b/src/main.zig index f8113dc..0b22dbb 100644 --- a/src/main.zig +++ b/src/main.zig @@ -33,6 +33,9 @@ const User = struct { posts: PostList, following: UserList, followers: UserList, + + post_lists: PostListList, + feeds: UserListList, }; const Post = struct { @@ -52,6 +55,15 @@ const Post = struct { text: PostText, }; +const SavedPostList = struct { + name: Name, + list: PostList, +}; +const SavedUserList = struct { + name: Name, + list: UserList, +}; + const Vote = struct { const Kind = enum { Up, Down }; @@ -64,10 +76,11 @@ const Login = struct { user: User, session_token: SessionToken, }; +const Name = std.BoundedArray(u8, 32); const UserId = enum(u64) { _ }; const PostId = enum(u64) { _ }; const Timestamp = i64; -const Username = std.BoundedArray(u8, 16); +const Username = std.BoundedArray(u8, 32); const DisplayName = std.BoundedArray(u8, 64); const PasswordHash = std.BoundedArray(u8, 128); const SessionToken = u64; @@ -76,6 +89,8 @@ const PostText = std.BoundedArray(u8, 1024); const PostList = db.Set(PostId); const UserList = db.Set(UserId); const VoteList = db.SetList(UserId, Vote); +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)); @@ -123,8 +138,8 @@ fn reencode(text: []const u8) !PostText { return result; } -fn decode(text: []const u8) !std.BoundedArray(u8, 32) { - var result = try std.BoundedArray(u8, 32).init(0); +fn decode(text: []const u8) !std.BoundedArray(u8, 1024) { + var result = try std.BoundedArray(u8, 1024).init(0); const max_len = @min(text.len, 1024); // TODO: PostText length @@ -184,12 +199,14 @@ const Chirp = struct { } } - pub fn register_user(env: *lmdb.Env, username: []const u8, password: []const u8) !bool { + pub fn register_user(env: lmdb.Env, username: []const u8, password: []const u8) !bool { const username_array = try Username.fromSlice(username); const display_name = try DisplayName.fromSlice(username); const txn = try env.txn(); - defer txn.commit() catch {}; + defer txn.commit() catch |err| { + std.debug.print("error registering user: {}\n", .{err}); + }; const users = try Db.users(txn); const user_ids = try Db.user_ids(txn); @@ -207,6 +224,8 @@ const Chirp = struct { .posts = try PostList.init(txn), .following = try UserList.init(txn), .followers = try UserList.init(txn), + .post_lists = try PostListList.init(txn), + .feeds = try UserListList.init(txn), }); try user_ids.put(username_array, user_id); @@ -216,7 +235,7 @@ const Chirp = struct { } pub fn login_user( - env: *lmdb.Env, + env: lmdb.Env, username: []const u8, password: []const u8, ) !SessionToken { @@ -242,7 +261,7 @@ const Chirp = struct { } } - fn logout_user(env: *lmdb.Env, session_token: SessionToken) !void { + fn logout_user(env: lmdb.Env, session_token: SessionToken) !void { const txn = try env.txn(); defer txn.commit() catch {}; @@ -250,7 +269,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) !void { + fn append_post(env: lmdb.Env, user_id: UserId, post_list: PostList, parent_id: ?PostId, quote_id: ?PostId, text: []const u8) !PostId { var post_id: PostId = undefined; // TODO: do this in one commit @@ -284,7 +303,7 @@ const Chirp = struct { defer txn.commit() catch {}; var posts_view = try post_list.open(txn); - try posts_view.append(post_id, {}); + try posts_view.append(post_id); } if (quote_id != null) { @@ -294,44 +313,50 @@ const Chirp = struct { const posts = try Db.posts(txn); const quote_post = try posts.get(quote_id.?); var quotes = try quote_post.quotes.open(txn); - try quotes.append(post_id, {}); + try quotes.append(post_id); } - } - fn post(env: *lmdb.Env, user_id: UserId, text: []const u8) !void { - const txn = try env.txn(); + return post_id; + } + fn post(env: lmdb.Env, user_id: UserId, text: []const u8) !void { + var txn = try env.txn(); const users = try Db.users(txn); const user = try users.get(user_id); - txn.abort(); - try append_post(env, user_id, user.posts, null, null, text); + const post_id = try append_post(env, user_id, user.posts, null, null, text); + _ = post_id; } - fn comment(env: *lmdb.Env, user_id: UserId, parent_post_id: PostId, text: []const u8) !void { - const txn = try env.txn(); + 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 user = try users.get(user_id); const posts = try Db.posts(txn); const parent_post = try posts.get(parent_post_id); - txn.abort(); - try append_post(env, user_id, parent_post.comments, parent_post_id, null, text); - } + const post_id = try append_post(env, user_id, parent_post.comments, parent_post_id, null, text); - fn quote(env: *lmdb.Env, user_id: UserId, quote_post_id: PostId, text: []const u8) !void { - const txn = try env.txn(); + txn = try env.txn(); + var replies_view = try user.posts.open(txn); + try replies_view.append(post_id); + try txn.commit(); + } + 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 user = try users.get(user_id); - txn.abort(); - try append_post(env, user_id, user.posts, null, quote_post_id, text); + const post_id = try append_post(env, user_id, user.posts, null, quote_post_id, text); + _ = post_id; } - fn vote(env: *lmdb.Env, post_id: PostId, user_id: UserId, kind: Vote.Kind) !void { + fn vote(env: lmdb.Env, post_id: PostId, user_id: UserId, kind: Vote.Kind) !void { const txn = try env.txn(); defer txn.commit() catch {}; @@ -371,7 +396,7 @@ const Chirp = struct { } } - fn follow(env: *lmdb.Env, user_id: UserId, user_id_to_follow: UserId) !void { + fn follow(env: lmdb.Env, user_id: UserId, user_id_to_follow: UserId) !void { const txn = try env.txn(); defer txn.commit() catch {}; @@ -387,14 +412,14 @@ const Chirp = struct { try user_following.del(user_id_to_follow); try user_to_follow_followers.del(user_id); } else if (!(user_following.has(user_id_to_follow) catch true) and !(user_to_follow_followers.has(user_id) catch true)) { - try user_following.append(user_id_to_follow, {}); - try user_to_follow_followers.append(user_id, {}); + try user_following.append(user_id_to_follow); + try user_to_follow_followers.append(user_id); } else { std.debug.print("Something went wrong when trying to unfollow\n", .{}); } } - fn get_session_user_id(env: *lmdb.Env, session_token: SessionToken) !UserId { + fn get_session_user_id(env: lmdb.Env, session_token: SessionToken) !UserId { const txn = try env.txn(); defer txn.abort(); @@ -403,7 +428,7 @@ const Chirp = struct { return try sessions.get(session_token); } - fn get_user(env: *lmdb.Env, user_id: UserId) !User { + fn get_user(env: lmdb.Env, user_id: UserId) !User { const txn = try env.txn(); defer txn.abort(); @@ -415,10 +440,8 @@ const Chirp = struct { // }}} // html {{{ -fn html_form(res: *http.Response, comptime fmt_action: []const u8, args_action: anytype, inputs: anytype) !void { - try res.write("
+ \\