X-Git-Url: https://gitweb.ps.run/chirp/blobdiff_plain/2ef30b70c3bc464defb933cb7a5b5ca88f827be0..a26fa9e137fb8693c595ca6a692181c8d4a402d7:/src/main.zig diff --git a/src/main.zig b/src/main.zig index bf68da0..d7eec6c 100644 --- a/src/main.zig +++ b/src/main.zig @@ -269,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 @@ -315,39 +315,45 @@ const Chirp = struct { var quotes = try quote_post.quotes.open(txn); try quotes.append(post_id); } + + return post_id; } fn post(env: lmdb.Env, user_id: UserId, text: []const u8) !void { - const txn = try env.txn(); - + 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(); + 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); + + 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 { - const txn = try env.txn(); - + 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 { @@ -434,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("
", .{}); +fn html_form(res: *http.Response, action: []const u8, inputs: anytype) !void { + try res.write("", .{action}); inline for (inputs) |input| { switch (@typeInfo(@TypeOf(input))) { @@ -496,7 +500,7 @@ fn write_header(res: *http.Response, logged_in: ?Login) !void { try res.write( \\Post
, .{}); - try html_form(res, "/logout", .{}, .{ + try html_form(res, "/logout", .{ \\type="submit" value="Logout" }); try res.write("

", .{}); @@ -507,17 +511,13 @@ fn write_header(res: *http.Response, logged_in: ?Login) !void { \\ \\ \\ - \\
+ \\
\\
\\ \\ \\ - \\
+ \\

, .{}); - try html_form(res, "/quit", .{}, .{ - \\type="submit" value="Quit" - }); - try res.write("

", .{}); } } fn write_start(res: *http.Response) !void { @@ -526,6 +526,7 @@ fn write_start(res: *http.Response) !void { \\ \\ \\ + \\ \\