X-Git-Url: https://gitweb.ps.run/chirp/blobdiff_plain/8c4068986ff26fe12e46d6ba3208e279678ce524..76968d86e7b0d55c44fb9c2a3bd5352f3c46a54e:/src/main.zig diff --git a/src/main.zig b/src/main.zig index 65dab07..98da131 100644 --- a/src/main.zig +++ b/src/main.zig @@ -28,12 +28,20 @@ const User = struct { // TODO: choose sizes id: UserId, name: Username, + display_name: DisplayName, password_hash: PasswordHash, posts: PostList, + following: UserList, + followers: UserList, + + post_lists: PostListList, + feeds: UserListList, }; const Post = struct { id: PostId, + parent_id: ?PostId, + quote_id: ?PostId, user_id: UserId, time: Timestamp, @@ -42,11 +50,20 @@ const Post = struct { downvotes: u64 = 0, votes: VoteList, comments: PostList, - // quote posts + quotes: PostList, text: PostText, }; +const SavedPostList = struct { + name: Name, + list: PostList, +}; +const SavedUserList = struct { + name: Name, + list: UserList, +}; + const Vote = struct { const Kind = enum { Up, Down }; @@ -57,20 +74,96 @@ const Vote = struct { const Id = u64; const Login = struct { user: User, - user_id: UserId, 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; const CookieValue = std.BoundedArray(u8, 128); const PostText = std.BoundedArray(u8, 1024); -const PostList = db.SetList(PostId, void); -const UserList = db.SetList(UserId, User); +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)); +} + +// https://developer.mozilla.org/en-US/docs/Glossary/Percent-encoding +fn reencode(text: []const u8) !PostText { + var result = try PostText.init(0); + + const len = @min(text.len, 1024); // TODO: PostText length + + var idx: usize = 0; + while (idx < len) : (idx += 1) { + 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; + } + + 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; + } + } + + try std.fmt.format(result.writer(), "&#x{s};", .{text[idx + 1 .. idx + 3]}); + idx += 2; + } else { + try result.append(c); + } + } + + return result; +} + +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 + + var idx: usize = 0; + var len: usize = 0; + while (len < max_len and idx < text.len) : ({ + idx += 1; + len += 1; + }) { + const c = text[idx]; + if (c == '+') { + try result.append(' '); + } else if (c == '%') { + if (idx + 2 < text.len) { + try std.fmt.format(result.writer(), "{c}", .{try std.fmt.parseUnsigned(u8, text[idx + 1 .. idx + 3], 16)}); + } + idx += 2; + } else { + try result.append(c); + } + } + + return result; +} const Chirp = struct { pub fn hash_password(password: []const u8) !PasswordHash { @@ -106,11 +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); @@ -119,13 +215,17 @@ const Chirp = struct { return false; } else { const user_id = try db.Prng.gen(users.dbi, UserId); - const posts = try Db.posts(txn); try users.put(user_id, User{ .id = user_id, .name = username_array, + .display_name = display_name, .password_hash = try hash_password(password), - .posts = try PostList.init(posts.dbi), + .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); @@ -135,7 +235,7 @@ const Chirp = struct { } pub fn login_user( - env: *lmdb.Env, + env: lmdb.Env, username: []const u8, password: []const u8, ) !SessionToken { @@ -161,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 {}; @@ -169,7 +269,7 @@ const Chirp = struct { try sessions.del(session_token); } - fn post(env: *lmdb.Env, user_id: UserId, 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 @@ -182,14 +282,18 @@ const Chirp = struct { const posts = try Db.posts(txn); post_id = try db.Prng.gen(posts.dbi, PostId); - const votes = try txn.dbi("votes"); + + const decoded_text = try reencode(text); try posts.put(post_id, Post{ .id = post_id, + .parent_id = parent_id, + .quote_id = quote_id, .user_id = user_id, .time = std.time.timestamp(), - .votes = try VoteList.init(votes), - .comments = try PostList.init(posts.dbi), - .text = try PostText.fromSlice(text), + .votes = try VoteList.init(txn), + .comments = try PostList.init(txn), + .quotes = try PostList.init(txn), + .text = decoded_text, }); } @@ -198,79 +302,124 @@ const Chirp = struct { txn = try env.txn(); defer txn.commit() catch {}; - const users = try Db.users(txn); - var user = try users.get(user_id); + var posts_view = try post_list.open(txn); + try posts_view.append(post_id); + } + + if (quote_id != null) { + txn = try env.txn(); + defer txn.commit() catch {}; const posts = try Db.posts(txn); - var posts_view = try user.posts.open(posts.dbi); - try posts_view.append(post_id, {}); + const quote_post = try posts.get(quote_id.?); + 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 { + var txn = try env.txn(); + const users = try Db.users(txn); + const user = try users.get(user_id); + txn.abort(); + + 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 { + 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(); + + 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 vote(env: *lmdb.Env, post_id: PostId, user_id: UserId, kind: Vote.Kind) !void { + 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(); + + 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 { const txn = try env.txn(); defer txn.commit() catch {}; const posts = try Db.posts(txn); - const votes = try txn.dbi("votes"); var p = try posts.get(post_id); - var votes_view = try p.votes.open(votes); + var votes_view = try p.votes.open(txn); + + var add_vote = true; if (try votes_view.has(user_id)) { const old_vote = try votes_view.get(user_id); - if (old_vote.kind == kind) { - return; - } else { - try votes_view.del(user_id); + add_vote = old_vote.kind != kind; - if (old_vote.kind == .Up) { - p.upvotes -= 1; - } else { - p.downvotes -= 1; - } - try posts.put(post_id, p); + try votes_view.del(user_id); + + switch (old_vote.kind) { + .Up => p.upvotes -= 1, + .Down => p.downvotes -= 1, } + try posts.put(post_id, p); } - try votes_view.append(user_id, Vote{ - .kind = kind, - .time = std.time.timestamp(), - }); - if (kind == .Up) { - p.upvotes += 1; - } else { - p.downvotes += 1; + if (add_vote) { + try votes_view.append(user_id, Vote{ + .kind = kind, + .time = std.time.timestamp(), + }); + + if (kind == .Up) { + p.upvotes += 1; + } else { + p.downvotes += 1; + } + try posts.put(post_id, p); } - try posts.put(post_id, p); } - fn unvote(env: *lmdb.Env, post_id: PostId, user_id: 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 {}; - const posts = try Db.posts(txn); - const votes = try txn.dbi("votes"); - - var p = try posts.get(post_id); - var votes_view = try p.votes.open(votes); + const users = try Db.users(txn); - if (try votes_view.has(user_id)) { - const v = try votes_view.get(user_id); + const user = try users.get(user_id); + const user_to_follow = try users.get(user_id_to_follow); - if (v.kind == .Up) { - p.upvotes -= 1; - } else { - p.downvotes -= 1; - } - try posts.put(post_id, p); + var user_following = try user.following.open(txn); + var user_to_follow_followers = try user_to_follow.followers.open(txn); - try votes_view.del(user_id); + if ((user_following.has(user_id_to_follow) catch false) and (user_to_follow_followers.has(user_id) catch false)) { + 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); + } 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(); @@ -279,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(); @@ -291,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))) { @@ -316,84 +463,986 @@ fn html_form(res: *http.Response, comptime fmt_action: []const u8, args_action: // }}} // write {{{ +const TimeStr = std.BoundedArray(u8, 256); + +// http://howardhinnant.github.io/date_algorithms.html +fn time_str(_t: i64) TimeStr { + const t: u64 = @intCast(_t); + var result = TimeStr.init(0) catch unreachable; + + const nD = @divFloor(t, std.time.s_per_day); + const z: u64 = nD + 719468; + const era: u64 = (if (z >= 0) z else z - 146096) / 146097; + const doe: u64 = z - era * 146097; // [0, 146096] + const yoe: u64 = (doe - doe / 1460 + doe / 36524 - doe / 146096) / 365; // [0, 399] + const Y: u64 = yoe + era * 400; + const doy: u64 = doe - (365 * yoe + yoe / 4 - yoe / 100); // [0, 365] + const mp: u64 = (5 * doy + 2) / 153; // [0, 11] + const D: u64 = doy - (153 * mp + 2) / 5 + 1; // [1, 31] + const M: u64 = if (mp < 10) mp + 3 else mp - 9; + + const h: u64 = @divFloor(t - nD * std.time.s_per_day, std.time.s_per_hour); + const m: u64 = @divFloor(t - nD * std.time.s_per_day - h * std.time.s_per_hour, std.time.s_per_min); + const s: u64 = t - nD * std.time.s_per_day - h * std.time.s_per_hour - m * std.time.s_per_min; + + std.fmt.format(result.writer(), "", .{ Y, M, D, h, m, s, t * 1000 }) catch unreachable; + + return result; +} fn write_header(res: *http.Response, logged_in: ?Login) !void { if (logged_in) |login| { try res.write( - \\Home
+ \\Home
+ , .{}); + try res.write( + \\Profile
, .{login.user.name.constSlice()}); - try html_form(res, "/logout", .{}, .{ + try res.write( + \\Post
+ , .{}); + try html_form(res, "/logout", .{ \\type="submit" value="Logout" }); - try html_form(res, "/quit", .{}, .{ - \\type="submit" value="Quit" - }); - try res.write("
", .{}); - try html_form(res, "/post", .{}, .{ - \\type="text" name="text" - , - \\type="submit" value="Post" - }); + try res.write("

", .{}); } else { try res.write( \\Home
- \\Register - \\Login
+ \\ + \\ + \\ + \\ + \\

+ \\
+ \\ + \\ + \\ + \\


, .{}); - try html_form(res, "/quit", .{}, .{ - \\type="submit" value="Quit" - }); } } -fn write_posts(res: *http.Response, txn: lmdb.Txn, user: User, login: ?Login) !void { - const votes_dbi = try txn.dbi("votes"); +fn write_start(res: *http.Response) !void { + try res.write( + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + , .{}); +} +fn write_end(res: *http.Response) !void { + try res.write("", .{}); +} +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_view = try user.posts.open(posts.dbi); + const post = posts.get(post_id) catch { + res.redirect("/") catch {}; + return; + }; + const users = try Db.users(txn); + const user = try users.get(post.user_id); + + try res.write( + \\
+ \\{s} + , .{ user.name.constSlice(), user.display_name.constSlice() }); + if (post.parent_id) |id| { + try res.write(" ..", .{@intFromEnum(id)}); + } + try res.write( + \\ {s}
+ \\{s}
+ , .{ time_str(post.time).constSlice(), post.text.constSlice() }); + + if (post.quote_id) |quote_id| { + try res.write("
", .{}); + if (options.recurse > 0) { + try write_post(res, txn, logged_in, quote_id, .{ .recurse = options.recurse - 1 }); + } else { + try res.write("...", .{@intFromEnum(post_id)}); + } + try res.write("
", .{}); + } - var it = posts_view.iterator(); - while (it.next()) |kv| { - const post_id = kv.key; - const post = try posts.get(post_id); + const comments_view = try post.comments.open(txn); + const quotes_view = try post.quotes.open(txn); + const votes_view = try post.votes.open(txn); + + // Votes + const vote: ?Vote = if (logged_in != null and try votes_view.has(logged_in.?.user.id)) try votes_view.get(logged_in.?.user.id) else null; + + if (vote != null and vote.?.kind == .Up) { + try html_form(res, "/upvote", .{ + .{ "type=\"hidden\" value=\"{x}\" name=\"post_id\"", .{@intFromEnum(post.id)} }, + .{ "type=\"submit\" value=\"⬆ {}\"", .{post.upvotes} }, + }); + } else { + try html_form(res, "/upvote", .{ + .{ "type=\"hidden\" value=\"{x}\" name=\"post_id\"", .{@intFromEnum(post.id)} }, + .{ "type=\"submit\" value=\"⇧ {}\"", .{post.upvotes} }, + }); + } + if (vote != null and vote.?.kind == .Down) { + try html_form(res, "/downvote", .{ + .{ "type=\"hidden\" value=\"{x}\" name=\"post_id\"", .{@intFromEnum(post.id)} }, + .{ "type=\"submit\" value=\"⬇ {}\"", .{post.downvotes} }, + }); + } else { + try html_form(res, "/downvote", .{ + .{ "type=\"hidden\" value=\"{x}\" name=\"post_id\"", .{@intFromEnum(post.id)} }, + .{ "type=\"submit\" value=\"⇩ {}\"", .{post.downvotes} }, + }); + } + + // Comment Count + try res.write( + \\💭 {} + , .{ @intFromEnum(post.id), comments_view.len() }); + + // Quote + try res.write( + \\🔁 {} + , .{ @intFromEnum(post.id), quotes_view.len() }); + + // Save to List + if (logged_in) |login| { + const lists_view = try login.user.post_lists.open(txn); + try res.write("
", .{}); + try res.write("", .{}); + try res.write("", .{@intFromEnum(post_id)}); + try res.write("", .{}); + try res.write("
", .{}); + } + + // Comment field + // TODO: maybe always show comment field and prompt for login + if (options.show_comment_field and logged_in != null) { + try res.write("

", .{}); + try html_form(res, "/comment", .{ + .{ "type=\"hidden\" value=\"{x}\" name=\"post_id\"", .{@intFromEnum(post.id)} }, + "type=\"text\" name=\"text\" placeholder=\"Text\"", + "type=\"submit\" value=\"Comment\"", + }); + try res.write("
", .{}); + } + // Comments + if (options.recurse > 0 and comments_view.len() > 0) { try res.write( - \\
- \\

{s}

- , .{post.text.constSlice()}); + \\ + \\Comments + , .{if (options.recurse > 1) " open" else ""}); + try res.write("
", .{}); + 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 res.write("
", .{}); + if (options.recurse == 1) { + count += 1; + if (count >= 3) break; + } + } + try res.write( + \\
+ \\ + , .{}); + } - const votes_view = try post.votes.open(votes_dbi); - const comments_view = try post.comments.open(posts.dbi); + try res.write( + \\
+ , .{}); +} +fn write_profile(res: *http.Response, txn: lmdb.Txn, logged_in: ?Login, user: User) !void { + const following = try user.following.open(txn); + const followers = try user.followers.open(txn); + + try res.write( + \\

{s}

+ , .{ + user.name.constSlice(), user.display_name.constSlice(), + }); + if (logged_in != null and user.id != logged_in.?.user.id) { + const login = logged_in.?; + + // 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="submit" value="Unfollow" + }); + } else { + try html_form(res, "/follow", .{ + .{ "type=\"hidden\" name=\"user.id\" value=\"{x}\"", .{@intFromEnum(user.id)} }, + \\type="submit" value="Follow" + }); + } - var has_voted: ?Vote.Kind = null; + // add to feed + const feeds_view = try login.user.feeds.open(txn); + try res.write("
", .{}); + try res.write("", .{}); + try res.write("", .{@intFromEnum(user.id)}); + try res.write("", .{}); + try res.write("
", .{}); + } + try res.write( + \\ {} following + \\ {} followers + \\
+ , .{ + user.name.constSlice(), following.len(), + user.name.constSlice(), followers.len(), + }); + + try res.write( + \\All Posts + \\ Comments + \\ Quotes
+ , .{ + user.name.constSlice(), + }); + + if (logged_in != null and user.id == logged_in.?.user.id) { + try res.write( + \\Lists + \\Feeds + \\Edit
+ \\
+ , .{}); + } - if (login != null and try votes_view.has(login.?.user_id)) { - const vote = try votes_view.get(login.?.user_id); + try res.write("
", .{}); +} +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| { + const posts = try Db.posts(txn); + const post = try posts.get(post_id); + 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, .{ .recurse = 1 }); + try res.write("
", .{}); + } + } +} +fn write_timeline(res: *http.Response, txn: lmdb.Txn, logged_in: ?Login, user_list: UserList) !void { + const users = try Db.users(txn); + const posts = try Db.posts(txn); + + var newest_post_ids = try std.BoundedArray(PostId, 10).init(0); // TODO: TimelinePostsCount + var prev_newest_post: ?Post = null; + + const following = try user_list.open(txn); + + while (true) { + var newest_post: ?Post = null; + + var following_it = following.iterator(); + while (following_it.next()) |following_id| { + const followed_user = try users.get(following_id); + const followed_posts = try followed_user.posts.open(txn); + + if (followed_posts.len() == 0) { + continue; + } + + var followed_posts_it = followed_posts.reverse_iterator(); + while (followed_posts_it.next()) |followed_post_id| { + const p = try posts.get(followed_post_id); + + 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; + break; + } + } + } + if (newest_post) |post| { + newest_post_ids.append(post.id) catch break; + prev_newest_post = post; + } else { + break; + } + } + + for (newest_post_ids.constSlice()) |post_id| { + try write_post(res, txn, logged_in, post_id, .{ .recurse = 1 }); + try res.write("
", .{}); + } +} +fn check_login(env: lmdb.Env, req: http.Request, res: *http.Response) !?Login { + var result: ?Login = null; + + if (req.get_cookie("session_token")) |session_token_str| { + var remove_session_token = true; + + if (std.fmt.parseUnsigned(SessionToken, session_token_str, 16) catch null) |session_token| { + // const session_token = try std.fmt.parseUnsigned(SessionToken, session_token_str, 10); + // const session_token = std.mem.bytesToValue(SessionToken, session_token_str); + 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); + + result = .{ + .user = try users.get(user_id), + .session_token = session_token, + }; + + remove_session_token = false; + } + } + + if (remove_session_token) { + try res.add_header( + "Set-Cookie", + .{"session_token=deleted; Expires=Thu, 01 Jan 1970 00:00:00 GMT"}, + ); + } + } + + return result; +} +// }}} + +// GET {{{ +const GET = struct { + const Self = @This(); + + txn: lmdb.Txn, + req: http.Request, + res: *http.Response, + logged_in: ?Login, + + fn handle(self: Self) !bool { + const ti = @typeInfo(Self); + 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) { + try @call(.auto, f, .{self}); + } else { + 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]; + if (self.req.target.len <= f_decl.name.len) { + return error.NoArgProvided; + } + const str = self.req.target[f_decl.name.len..self.req.target.len]; + const field_ti = @typeInfo(field.type); + switch (field_ti) { + // TODO: maybe handle BoundedArray? + .Int => { + @field(arg, field.name) = try std.fmt.parseUnsigned(field.type, str, 16); + }, + .Enum => { + @field(arg, field.name) = try parse_enum(field.type, str, 16); + }, + else => { + @field(arg, field.name) = str; + }, + } - has_voted = vote.kind; + try @call(.auto, f, .{ self, arg }); + } + return true; + } } + return false; + } - if (has_voted != null and has_voted.? == .Up) { - try html_form(res, "/unupvote/{}", .{@intFromEnum(post_id)}, .{ - .{ "type=\"submit\" value=\"⬆ {}\"", .{post.upvotes} }, + pub fn @"/user/"(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 = false, + .show_comments = false, + }); + } else |err| { + try self.res.write( + \\

User not found [{}]

+ , .{err}); + } + } + 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); + 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 = false, + .show_comments = true, }); + } else |err| { + try self.res.write( + \\

User not found [{}]

+ , .{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( + \\

User not found [{}]

+ , .{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( + \\

User not found [{}]

+ , .{err}); + } + } + pub fn @"/following/"(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); + + const following = try user.following.open(self.txn); + var it = following.iterator(); + + try self.res.write( + \\

{s} follows:

+ , .{ user.name.constSlice(), user.display_name.constSlice() }); + + while (it.next()) |following_id| { + const following_user = try users.get(following_id); + + try self.res.write( + \\{s}
+ , .{ following_user.name.constSlice(), following_user.display_name.constSlice() }); + } + } else |err| { + try self.res.write( + \\

User not found [{}]

+ , .{err}); + } + } + pub fn @"/followers/"(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); + + const followers = try user.followers.open(self.txn); + var it = followers.iterator(); + + try self.res.write( + \\

{s} followers:

+ , .{ user.name.constSlice(), user.display_name.constSlice() }); + + while (it.next()) |follower_id| { + const follower_user = try users.get(follower_id); + + try self.res.write( + \\{s}
+ , .{ follower_user.name.constSlice(), follower_user.display_name.constSlice() }); + } + } else |err| { + try self.res.write( + \\

User not found [{}]

+ , .{err}); + } + } + pub fn @"/post/"(self: Self, args: struct { post_id: PostId }) !void { + try write_post(self.res, self.txn, self.logged_in, args.post_id, .{ + .recurse = 3, // TODO: factor out + .show_comment_field = true, + }); + } + 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); + + const referer = if (self.req.get_header("Referer")) |ref| ref else self.req.target; + + if (self.logged_in != null) { + 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=\"submit\" value=\"Quote\"", + }); + try self.res.write("
", .{}); + } + + 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("
", .{}); + } + } + 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 }, .{ + .show_posts = true, + .show_quotes = true, + .show_comments = true, + }); + } + pub fn @"/lists"(self: Self) !void { + if (self.logged_in) |login| { + const post_lists_view = try login.user.post_lists.open(self.txn); + + try html_form(self.res, "/create_list", .{ + "type=\"text\" name=\"name\"", + "type=\"submit\" value=\"Add\"", + }); + + try self.res.write("

", .{}); + + var it = post_lists_view.iterator(); + while (it.next()) |kv| { + const name = kv.val.name; + const post_list = kv.val.list; + try self.res.write( + \\{s} + , .{ post_list.idx.?, name.constSlice() }); + try html_form(self.res, "/delete_list", .{ + .{ "type=\"hidden\" name=\"list_id\" value=\"{x}\"", .{kv.key} }, + "type=\"submit\" value=\"Delete\"", + }); + try self.res.write("
", .{}); + } } else { - try html_form(res, "/upvote/{}", .{@intFromEnum(post_id)}, .{ - .{ "type=\"submit\" value=\"⬆ {}\"", .{post.upvotes} }, + 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 @"/feeds"(self: Self) !void { + if (self.logged_in) |login| { + const feeds_view = try login.user.feeds.open(self.txn); + + try html_form(self.res, "/create_feed", .{ + "type=\"text\" name=\"name\"", + "type=\"submit\" value=\"Add\"", }); + + try self.res.write("

", .{}); + + var it = feeds_view.iterator(); + while (it.next()) |kv| { + const name = kv.val.name; + const user_list = kv.val.list; + try self.res.write( + \\{s} + , .{ user_list.idx.?, name.constSlice() }); + try html_form(self.res, "/delete_feed", .{ + .{ "type=\"hidden\" name=\"list_id\" value=\"{x}\"", .{kv.key} }, + "type=\"submit\" value=\"Delete\"", + }); + try self.res.write("
", .{}); + } + } else { + try self.res.write("not logged in", .{}); } - if (has_voted != null and has_voted.? == .Down) { - try html_form(res, "/undownvote/{}", .{@intFromEnum(post_id)}, .{ - .{ "type=\"submit\" value=\"⬇ {}\"", .{post.downvotes} }, + } + pub fn @"/post"(self: Self) !void { + if (self.logged_in) |login| { + _ = login; + const referer = if (self.req.get_header("Referer")) |ref| ref else self.req.target; + + try html_form(self.res, "/post", .{ + .{ "type=\"hidden\" name=\"referer\" value=\"{s}\"", .{referer} }, + "type=\"text\" name=\"text\"", + "type=\"submit\" value=\"Post\"", }); } else { - try html_form(res, "/downvote/{}", .{@intFromEnum(post_id)}, .{ - .{ "type=\"submit\" value=\"⬇ {}\"", .{post.downvotes} }, + try self.res.write("not logged in", .{}); + } + } + pub fn @"/edit"(self: Self) !void { + if (self.logged_in) |login| { + try self.res.write("
Username: ", .{}); + try html_form(self.res, "/set_username", .{ + .{ "type=\"text\" name=\"username\" placeholder=\"{s}\"", .{login.user.name.constSlice()} }, + "type=\"submit\" value=\"Change\"", + }); + try self.res.write("
Display Name: ", .{}); + try html_form(self.res, "/set_display_name", .{ + .{ "type=\"text\" name=\"display_name\" placeholder=\"{s}\"", .{login.user.display_name.constSlice()} }, + "type=\"submit\" value=\"Change\"", + }); + try self.res.write("
Password: ", .{}); + try html_form(self.res, "/set_password", .{ + "type=\"text\" name=\"password\"", + "type=\"submit\" value=\"Change\"", }); + } else { + try self.res.write("not logged in", .{}); } - try res.write( - \\💭 {} - \\
- , .{comments_view.len()}); } -} + pub fn @"/"(self: Self) !void { + if (self.logged_in) |login| { + try write_timeline(self.res, self.txn, self.logged_in, login.user.following); + } else { + // TODO: generic home + try self.res.write("Homepage", .{}); + } + } +}; +// }}} + +// POST {{{ +const POST = struct { + const Self = @This(); + + env: lmdb.Env, + req: http.Request, + res: *http.Response, + logged_in: ?Login, + + pub fn handle(self: Self) !bool { + const ti = @typeInfo(Self); + 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) { + _ = try @call(.auto, f, .{self}); + } else { + 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| { + const str = self.req.get_value(field.name) orelse return error.ArgNotFound; + const field_ti = @typeInfo(field.type); + switch (field_ti) { + .Int => { + @field(args, field.name) = try std.fmt.parseUnsigned(field.type, str, 16); + }, + .Enum => { + @field(args, field.name) = try parse_enum(field.type, str, 16); + }, + else => { + @field(args, field.name) = str; + }, + } + } + try @call(.auto, f, .{ self, args }); + } + return true; + } + } + return false; + } + + pub fn @"/register"(self: Self, args: struct { username: []const u8, password: []const u8 }) !void { + // TODO: handle args not supplied + std.debug.print("New user: {s} {s}\n", .{ args.username, args.password }); + _ = try Chirp.register_user(self.env, args.username, args.password); + } + pub fn @"/login"(self: Self, args: struct { username: []const u8, password: []const u8 }) !void { + // TODO: handle args not supplied + std.debug.print("New login: {s} {s}\n", .{ args.username, args.password }); + if (Chirp.login_user(self.env, args.username, args.password)) |session_token| { + self.res.status = .see_other; + try self.res.add_header( + "Set-Cookie", + .{ "session_token={x}; HttpOnly", .{session_token} }, + ); + } else |err| { + std.debug.print("login_user err: {}\n", .{err}); + } + } + pub fn @"/logout"(self: Self) !void { + if (self.logged_in) |login| { + try Chirp.logout_user(self.env, login.session_token); + + try self.res.add_header( + "Set-Cookie", + .{"session_token=deleted; Expires=Thu, 01 Jan 1970 00:00:00 GMT"}, + ); + } + } + pub fn @"/set_username"(self: Self, args: struct { username: []const u8 }) !void { + const login = self.logged_in orelse return error.NotLoggedIn; + const username = try Username.fromSlice(args.username); + + const txn = try self.env.txn(); + defer txn.commit() catch {}; + + const user_ids = try Db.user_ids(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); + var user = login.user; + user.name = username; + try users.put(login.user.id, user); + } + } + pub fn @"/set_display_name"(self: Self, args: struct { display_name: []const u8 }) !void { + const login = self.logged_in orelse return error.NotLoggedIn; + const display_name = try DisplayName.fromSlice(args.display_name); + + const txn = try self.env.txn(); + defer txn.commit() catch {}; + + const users = try Db.users(txn); + var user = login.user; + user.display_name = display_name; + 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; + + const txn = try self.env.txn(); + defer txn.commit() catch {}; + + const users = try Db.users(txn); + var user = login.user; + user.password_hash = try Chirp.hash_password(args.password); + try users.put(login.user.id, user); + } + pub fn @"/post"(self: Self) !void { + if (self.logged_in) |login| { + const text = self.req.get_value("text").?; + const has_referer = self.req.get_value("referer"); + + try Chirp.post(self.env, login.user.id, text); + + if (has_referer) |r| { + const decoded = try decode(r); + try self.res.redirect(decoded.constSlice()); + } + } + } + pub fn @"/comment"(self: Self) !void { + if (self.logged_in) |login| { + const text = self.req.get_value("text") orelse return error.NoText; + const post_id_str = self.req.get_value("post_id") orelse return error.NoPostId; + const post_id = try parse_enum(PostId, post_id_str, 16); + + try Chirp.comment(self.env, login.user.id, post_id, text); + } + } + pub fn @"/quote"(self: Self) !void { + if (self.logged_in) |login| { + const text = self.req.get_value("text") orelse return error.NoText; + const post_id_str = self.req.get_value("post_id") orelse return error.NoPostId; + const has_referer = self.req.get_value("referer"); + + const post_id = try parse_enum(PostId, post_id_str, 16); + + try Chirp.quote(self.env, login.user.id, post_id, text); + + if (has_referer) |r| { + const decoded = try decode(r); + try self.res.redirect(decoded.constSlice()); + } + } + } + // TODO: add arguments instead of parsing manually + pub fn @"/create_list"(self: Self) !void { + if (self.logged_in) |login| { + const name_str = self.req.get_value("name") orelse return error.NoName; + const name = try Name.fromSlice(name_str); + // TODO: decode name + + var txn = try self.env.txn(); + const postlist = try PostList.init(txn); + try txn.commit(); + + txn = try self.env.txn(); + var post_lists_view = try login.user.post_lists.open(txn); + _ = try post_lists_view.append(.{ .name = name, .list = postlist }); + try txn.commit(); + } + } + pub fn @"/delete_list"(self: Self, args: struct { list_id: PostList.Index }) !void { + if (self.logged_in) |login| { + var post_list: ?PostList = null; + { + const txn = try self.env.txn(); + defer txn.commit() catch {}; + var post_lists_view = try login.user.post_lists.open(txn); + post_list = (try post_lists_view.get(args.list_id)).list; + try post_lists_view.del(args.list_id); + } + if (post_list != null) { + const txn = try self.env.txn(); + defer txn.commit() catch {}; + var list_view = try post_list.?.open(txn); + try list_view.clear(); + } + } + } + pub fn @"/list_add"(self: Self, args: struct { list_id: PostList.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 }; + 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); + } else { + try post_list_view.append(args.post_id); + } + } + } + pub fn @"/create_feed"(self: Self) !void { + if (self.logged_in) |login| { + const name_str = self.req.get_value("name") orelse return error.NoName; + const name = try Name.fromSlice(name_str); + + var txn = try self.env.txn(); + const userlist = try UserList.init(txn); + try txn.commit(); + + txn = try self.env.txn(); + var feeds_view = try login.user.feeds.open(txn); + _ = try feeds_view.append(.{ .name = name, .list = userlist }); + try txn.commit(); + } + } + pub fn @"/delete_feed"(self: Self, args: struct { list_id: UserList.Index }) !void { + if (self.logged_in) |login| { + var user_list: ?UserList = null; + + { + const txn = try self.env.txn(); + defer txn.commit() catch {}; + var feeds_view = try login.user.feeds.open(txn); + user_list = (try feeds_view.get(args.list_id)).list; + try feeds_view.del(args.list_id); + } + if (user_list != null) { + const txn = try self.env.txn(); + defer txn.commit() catch {}; + var list_view = try user_list.?.open(txn); + try list_view.clear(); + } + } + } + pub fn @"/feed_add"(self: Self, args: struct { feed_id: UserList.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 }; + 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); + } else { + try user_list_view.append(args.user_id); + } + } + } + pub fn @"/upvote"(self: Self) !void { + const login = self.logged_in orelse return error.NotLoggedIn; + + const post_id_str = self.req.get_value("post_id") orelse return error.NoPostId; + const post_id: PostId = @enumFromInt(try std.fmt.parseUnsigned(u64, post_id_str, 16)); + + try Chirp.vote(self.env, post_id, login.user.id, .Up); + } + pub fn @"/downvote"(self: Self) !void { + const login = self.logged_in orelse return error.NotLoggedIn; + + const post_id_str = self.req.get_value("post_id") orelse return error.NoPostId; + const post_id: PostId = @enumFromInt(try std.fmt.parseUnsigned(u64, post_id_str, 16)); + + try Chirp.vote(self.env, post_id, login.user.id, .Down); + } + pub fn @"/follow"(self: Self) !void { + const login = self.logged_in orelse return error.NotLoggedIn; + + const user_id_str = self.req.get_value("user_id") orelse return error.NoUserId; + const user_id: UserId = @enumFromInt(try std.fmt.parseUnsigned(u64, user_id_str, 16)); + + try Chirp.follow(self.env, login.user.id, user_id); + } + pub fn @"/quit"(self: Self) !void { + if (self.req.get_header("Referer")) |ref| { + try self.res.redirect(ref); + } else { + try self.res.redirect("/"); + } + try self.res.send(); + // break :accept; + } +}; // }}} fn list_users(env: lmdb.Env) !void { @@ -452,8 +1501,13 @@ fn list_posts(env: lmdb.Env) !void { } const ReqBufferSize = 4096; -const ResHeadBufferSize = 1024 * 16; -const ResBodyBufferSize = 1024 * 16; +const ResHeadBufferSize = 1024 * 64; +const ResBodyBufferSize = 1024 * 64; + +// TODO: static? +var req_buffer: [ReqBufferSize]u8 = undefined; +var res_head_buffer: [ResHeadBufferSize]u8 = undefined; +var res_body_buffer: [ResBodyBufferSize]u8 = undefined; pub fn main() !void { // server @@ -464,16 +1518,24 @@ pub fn main() !void { var env = try lmdb.Env.open("db", 1024 * 1024 * 10); defer env.close(); - // std.debug.print("Users:\n", .{}); - // try list_users(env); - // std.debug.print("User IDs:\n", .{}); - // try list_user_ids(env); - // std.debug.print("Sessions:\n", .{}); - // try list_sessions(env); - // std.debug.print("Posts:\n", .{}); - // try list_posts(env); + std.debug.print("Users:\n", .{}); + try list_users(env); + std.debug.print("User IDs:\n", .{}); + try list_user_ids(env); + std.debug.print("Sessions:\n", .{}); + try list_sessions(env); + std.debug.print("Posts:\n", .{}); + try list_posts(env); - try handle_connection(&server, &env); + while (true) { + 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); + }; + } + } // const ThreadCount = 1; // var ts: [ThreadCount]std.Thread = undefined; @@ -487,221 +1549,64 @@ pub fn main() !void { std.debug.print("done\n", .{}); } -fn handle_connection(server: *http.Server, env: *lmdb.Env) !void { - // TODO: static? - var req_buffer: [ReqBufferSize]u8 = undefined; - var res_head_buffer: [ResHeadBufferSize]u8 = undefined; - var res_body_buffer: [ResBodyBufferSize]u8 = undefined; - - accept: while (true) { - server.wait(); - - while (try server.next_request(&req_buffer)) |req| { - // std.debug.print("[{}]: {s}\n", .{ req.method, req.target }); - - // reponse - var res = http.Response.init(req.fd, &res_head_buffer, &res_body_buffer); - - // check session token - var logged_in: ?Login = null; - - if (req.get_cookie("session_token")) |session_token_str| { - const session_token = try std.fmt.parseUnsigned(SessionToken, session_token_str, 16); - // const session_token = try std.fmt.parseUnsigned(SessionToken, session_token_str, 10); - // const session_token = std.mem.bytesToValue(SessionToken, session_token_str); - if (Chirp.get_session_user_id(env, session_token)) |user_id| { - const txn = try env.txn(); - defer txn.abort(); - const users = try Db.users(txn); - - logged_in = .{ - .user = try users.get(user_id), - .user_id = user_id, - .session_token = session_token, - }; - } else |err| { - std.debug.print("get_session_user err: {}\n", .{err}); - - try res.add_header( - "Set-Cookie", - .{"session_token=deleted; Expires=Thu, 01 Jan 1970 00:00:00 GMT"}, - ); - } - } - - // html - if (req.method == .GET) { - try write_header(&res, logged_in); - - if (std.mem.eql(u8, req.target, "/register")) { - try res.write( - \\
- \\ - \\ - \\ - \\
- , .{}); - try res.send(); - } else if (std.mem.eql(u8, req.target, "/login")) { - try res.write( - \\
- \\ - \\ - \\ - \\
- , .{}); - try res.send(); - } else if (std.mem.startsWith(u8, req.target, "/user/")) { - const username = req.target[6..req.target.len]; - - const txn = try env.txn(); - defer txn.abort(); - - const user_ids = try Db.user_ids(txn); - if (user_ids.get(try Username.fromSlice(username))) |user_id| { - const users = try Db.users(txn); - const user = try users.get(user_id); - try write_posts(&res, txn, user, logged_in); - } else |err| { - try res.write( - \\

User not found [{}]

- , .{err}); - } - try res.send(); - } else { - if (logged_in) |login| { - const user = try Chirp.get_user(env, login.user_id); - - const txn = try env.txn(); - defer txn.abort(); - - try write_posts(&res, txn, user, logged_in); - - try res.send(); - } else { - try res.write("[GET] {s}", .{req.target}); - try res.send(); - } - } - } - // api - else { - if (std.mem.eql(u8, req.target, "/register")) { - // TODO: handle args not supplied - const username = req.get_value("username").?; - const password = req.get_value("password").?; - - std.debug.print("New user: {s} {s}\n", .{ username, password }); - if (try Chirp.register_user(env, username, password)) { - try res.redirect("/login"); - } else { - try res.redirect("/register"); - } - try res.send(); - } else if (std.mem.eql(u8, req.target, "/login")) { - // TODO: handle args not supplied - const username = req.get_value("username").?; - const password = req.get_value("password").?; - - std.debug.print("New login: {s} {s}\n", .{ username, password }); - if (Chirp.login_user(env, username, password)) |session_token| { - res.status = .see_other; - try res.add_header( - "Location", - .{ "/user/{s}", .{username} }, - ); - try res.add_header( - "Set-Cookie", - .{ "session_token={x}; Secure; HttpOnly", .{session_token} }, - ); - - try res.send(); - } else |err| { - std.debug.print("login_user err: {}\n", .{err}); - try res.redirect("/login"); - try res.send(); - } - } else if (std.mem.eql(u8, req.target, "/logout")) { - if (logged_in) |login| { - try Chirp.logout_user(env, login.session_token); - - try res.add_header( - "Set-Cookie", - .{"session_token=deleted; Expires=Thu, 01 Jan 1970 00:00:00 GMT"}, - ); - - try res.redirect("/"); - try res.send(); - } - } else if (std.mem.eql(u8, req.target, "/post")) { - if (logged_in) |login| { - const text = req.get_value("text").?; - try Chirp.post(env, login.user_id, text); - - try res.redirect("/"); - try res.send(); - } - } else if (std.mem.eql(u8, req.target, "/quit")) { - try res.redirect("/"); - try res.send(); - break :accept; - } else if (std.mem.startsWith(u8, req.target, "/upvote/")) { - const login = logged_in orelse return error.NotLoggedIn; - - const post_id_str = req.target[8..req.target.len]; - const post_id: PostId = @enumFromInt(try std.fmt.parseUnsigned(u64, post_id_str, 10)); - - try Chirp.vote(env, post_id, login.user_id, .Up); - - if (req.get_header("Referer")) |ref| { - try res.redirect(ref); - } - try res.send(); - } else if (std.mem.startsWith(u8, req.target, "/downvote/")) { - const login = logged_in orelse return error.NotLoggedIn; - - const post_id_str = req.target[10..req.target.len]; - const post_id: PostId = @enumFromInt(try std.fmt.parseUnsigned(u64, post_id_str, 10)); +fn handle_error(env: lmdb.Env, req: http.Request) !void { + _ = env; + var res = http.Response.init(req.fd, &res_head_buffer, &res_body_buffer); + try write_start(&res); + try res.write("Oops, something went terribly wrong there D:", .{}); + try write_end(&res); + try res.send(); +} +fn handle_request(env: lmdb.Env, req: http.Request) !void { + // std.debug.print("[{}]: {s}\n", .{ req.method, req.target }); + // std.debug.print("[{}]: {s}\n", .{ req.method, req.head.? }); - try Chirp.vote(env, post_id, login.user_id, .Down); + // reponse + var res = http.Response.init(req.fd, &res_head_buffer, &res_body_buffer); - if (req.get_header("Referer")) |ref| { - try res.redirect(ref); - } - try res.send(); - } else if (std.mem.startsWith(u8, req.target, "/unupvote/")) { - // TODO: maybe move to one /unvote? - const login = logged_in orelse return error.NotLoggedIn; + // check session token + const logged_in: ?Login = try check_login(env, req, &res); - const post_id_str = req.target[10..req.target.len]; - const post_id: PostId = @enumFromInt(try std.fmt.parseUnsigned(u64, post_id_str, 10)); + // html + if (req.method == .GET) { + try write_start(&res); + try write_header(&res, logged_in); - try Chirp.unvote(env, post_id, login.user_id); - - if (req.get_header("Referer")) |ref| { - try res.redirect(ref); - } - try res.send(); - } else if (std.mem.startsWith(u8, req.target, "/undownvote/")) { - const login = logged_in orelse return error.NotLoggedIn; + const txn = try env.txn(); + defer txn.abort(); - const post_id_str = req.target[12..req.target.len]; - const post_id: PostId = @enumFromInt(try std.fmt.parseUnsigned(u64, post_id_str, 10)); + const get = GET{ + .txn = txn, + .req = req, + .res = &res, + .logged_in = logged_in, + }; + if (try get.handle()) {} else { + try res.redirect("/"); + } - try Chirp.unvote(env, post_id, login.user_id); + try write_end(&res); + try res.send(); + } + // api + else { + const post = POST{ + .env = env, + .req = req, + .res = &res, + .logged_in = logged_in, + }; + if (try post.handle()) {} else { + try res.write("

[POST] {s}

", .{req.target}); + } - if (req.get_header("Referer")) |ref| { - try res.redirect(ref); - } - try res.send(); - } else { - // try req.respond( - // \\

POST

- // , .{}); - try res.write("

[POST] {s}

", .{req.target}); - try res.send(); - } + if (!res.has_header("Location")) { + if (req.get_header("Referer")) |ref| { + try res.redirect(ref); + } else { + try res.redirect("/"); } } + try res.send(); } }