X-Git-Url: https://gitweb.ps.run/chirp/blobdiff_plain/e8c4ebe56c1382517e02f9a770397799b0e0b066..48e80ae12c563913af56f867268c02650292f7ee:/src/main.zig
diff --git a/src/main.zig b/src/main.zig
index 2fa757c..9fb872f 100644
--- a/src/main.zig
+++ b/src/main.zig
@@ -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");
}
@@ -29,11 +41,13 @@ const User = struct {
id: UserId,
name: Username,
display_name: DisplayName,
+ description: UserDescription,
password_hash: PasswordHash,
- posts: PostList,
- replies: PostList,
- following: UserList,
- followers: UserList,
+
+ posts: PostSet,
+
+ following: UserSet,
+ followers: UserSet,
post_lists: PostListList,
feeds: UserListList,
@@ -50,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 {
@@ -83,23 +97,27 @@ const PostId = enum(u64) { _ };
const Timestamp = i64;
const Username = std.BoundedArray(u8, 32);
const DisplayName = std.BoundedArray(u8, 64);
+const UserDescription = std.BoundedArray(u8, 1024);
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);
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));
+ 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);
+fn reencode(comptime T: type, text: []const u8) !T {
+ var result = try T.init(0);
const len = @min(text.len, 1024); // TODO: PostText length
@@ -108,28 +126,19 @@ fn reencode(text: []const u8) !PostText {
const c = text[idx];
if (c == '+') {
try result.append(' ');
- } else if (c == '%') {
- // special case of ...
- // assume only , no
- 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;
- }
+ } else if (c == '%' and idx + 2 < text.len) {
+ const allow = &[_]u8{ 0x26, 0x23, 0x3b, 0x0a };
- 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;
- }
+ const escaped_value = std.fmt.parseUnsigned(u8, text[idx + 1 .. idx + 3], 16) catch continue;
+
+ if (escaped_value == 0x0d) {
+ try std.fmt.format(result.writer(), "
", .{});
+ } else if (std.mem.indexOfScalar(u8, allow, escaped_value) != null) {
+ try std.fmt.format(result.writer(), "{c}", .{escaped_value});
+ } else {
+ try std.fmt.format(result.writer(), "{x};", .{escaped_value});
}
- try std.fmt.format(result.writer(), "{s};", .{text[idx + 1 .. idx + 3]});
idx += 2;
} else {
try result.append(c);
@@ -167,18 +176,20 @@ 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);
@@ -187,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(),
@@ -221,11 +231,11 @@ const Chirp = struct {
.id = user_id,
.name = username_array,
.display_name = display_name,
+ .description = try UserDescription.init(0),
.password_hash = try hash_password(password),
- .posts = try PostList.init(txn),
- .replies = 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),
});
@@ -271,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
@@ -285,7 +295,7 @@ const Chirp = struct {
const posts = try Db.posts(txn);
post_id = try db.Prng.gen(posts.dbi, PostId);
- const decoded_text = try reencode(text);
+ const decoded_text = try reencode(PostText, text);
try posts.put(post_id, Post{
.id = post_id,
.parent_id = parent_id,
@@ -293,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,
});
}
@@ -328,11 +338,7 @@ const Chirp = struct {
txn.abort();
const post_id = try append_post(env, user_id, user.posts, null, null, text);
-
- txn = try env.txn();
- var replies_view = try user.replies.open(txn);
- try replies_view.append(post_id);
- try txn.commit();
+ _ = post_id;
}
fn comment(env: lmdb.Env, user_id: UserId, parent_post_id: PostId, text: []const u8) !void {
@@ -347,7 +353,7 @@ const Chirp = struct {
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.replies.open(txn);
+ var replies_view = try user.posts.open(txn);
try replies_view.append(post_id);
try txn.commit();
}
@@ -359,11 +365,7 @@ const Chirp = struct {
txn.abort();
const post_id = try append_post(env, user_id, user.posts, null, quote_post_id, text);
-
- txn = try env.txn();
- var replies_view = try user.replies.open(txn);
- try replies_view.append(post_id);
- try txn.commit();
+ _ = post_id;
}
fn vote(env: lmdb.Env, post_id: PostId, user_id: UserId, kind: Vote.Kind) !void {
@@ -450,15 +452,86 @@ const Chirp = struct {
// }}}
// html {{{
+pub fn Paginate(comptime T: type) type {
+ return struct {
+ const Self = @This();
+
+ const IterateResult = T.Base.View.Iterator.Result;
+
+ res: *http.Response,
+ view: T.View,
+ per_page: u64,
+
+ it: T.Base.View.Iterator,
+ starting_idx: ?T.Base.Key,
+ count: u64 = 0,
+
+ pub fn init(res: *http.Response, view: T.View, per_page: u64) !Self {
+ var it = view.reverse_iterator();
+ if (res.req.get_param("starting_at")) |starting_at_str| {
+ it.idx = try parse_enum(T.Base.Key, starting_at_str, 16);
+ }
+
+ return .{
+ .res = res,
+ .view = view,
+ .per_page = per_page,
+ .it = it,
+ .starting_idx = it.idx,
+ };
+ }
+ pub fn next(self: *Self) ?IterateResult {
+ if (self.it.next()) |kv| {
+ if (self.count < self.per_page) {
+ self.count += 1;
+ return kv;
+ }
+ }
+ return null;
+ }
+ pub fn write_navigation(self: *Self) !void {
+ const next_idx = self.it.next();
+
+ if (self.view.base.head.last != self.starting_idx) {
+ var prev_it = self.view.iterator();
+ prev_it.idx = self.starting_idx.?;
+ var oldest_idx = self.starting_idx.?;
+
+ var count: u64 = 0;
+ while (prev_it.next()) |kv| {
+ oldest_idx = kv.key;
+
+ if (count > self.per_page) {
+ break;
+ } else {
+ count += 1;
+ }
+ }
+
+ try self.res.write("Prev ", .{ self.res.req.target, @intFromEnum(oldest_idx) });
+ }
+
+ if (next_idx) |kv| {
+ try self.res.write("Next", .{ self.res.req.target, @intFromEnum(kv.key) });
+ }
+ }
+ };
+}
fn html_form(res: *http.Response, action: []const u8, inputs: anytype) !void {
try res.write("