// db {{{
const Db = struct {
- 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;
};
// }}}
description: UserDescription,
password_hash: PasswordHash,
- posts: PostList,
+ posts: PostSet,
- following: UserList,
- followers: UserList,
+ following: UserSet,
+ followers: UserSet,
post_lists: PostListList,
feeds: UserListList,
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 {
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 SessionList = db.SetList(SessionToken, 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
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;
- }
+ } 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(), "<br />", .{});
+ } 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{x};", .{escaped_value});
}
- try std.fmt.format(result.writer(), "&#x{s};", .{text[idx + 1 .. idx + 3]});
idx += 2;
} else {
try result.append(c);
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);
}
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(),
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,
.description = try UserDescription.init(0),
.password_hash = try hash_password(password),
- .posts = 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),
});
- try user_ids.put(username_array, user_id);
+ try user_ids.append(username_array, user_id);
return true;
}
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;
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);
}
- 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
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);
.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,
});
}
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);
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();
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();
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();
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);
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);
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);
}
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);
}
};
.starting_idx = it.idx,
};
}
- pub fn next(self: *Self) IterateResult {
+ pub fn next(self: *Self) ?IterateResult {
if (self.it.next()) |kv| {
if (self.count < self.per_page) {
self.count += 1;
inline for (inputs) |input| {
switch (@typeInfo(@TypeOf(input))) {
- .Struct => {
- try res.write("<input ", .{});
- try res.write(input[0], input[1]);
- try res.write(" />", .{});
+ .@"struct" => |s| {
+ if (s.fields.len == 3) {
+ try res.write("<{s} ", .{input[0]});
+ try res.write(input[1], input[2]);
+ try res.write("></{s}>", .{input[0]});
+ } else {
+ try res.write("<input ", .{});
+ try res.write(input[0], input[1]);
+ try res.write(" />", .{});
+ }
},
else => {
try res.write("<input ", .{});
\\<link rel="icon" href="data:image/svg+xml,<svg xmlns=%22http://www.w3.org/2000/svg%22 viewBox=%220 0 100 100%22><text y=%22.9em%22 font-size=%2290%22>🐣</text></svg>">
\\<meta name="viewport" content="width=device-width, initial-scale=1.0" />
\\<style>
+ \\ :root {
+ \\ color-scheme: light dark;
+ \\ }
\\ form {
\\ display: inline-block;
\\ }
+ \\ body {
+ \\ margin:40px auto;max-width:650px;line-height:1.6;font-size:18px;padding:0 10px;
+ \\ }
+ \\ h1,h2,h3{line-height:1.2}
\\</style>
\\</head>
\\<body>
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(
try res.write("<option value=\"{x}\">{s}{s}</option>", .{ id, name.constSlice(), if (list_view.has(post_id) catch false) " *" else "" });
}
try res.write("</select>", .{});
- try res.write("<input type=\"hidden\" name=\"post_id\" value=\"{x}\"></input>", .{@intFromEnum(post_id)});
- try res.write("<input type=\"submit\" value=\"Save\"></input>", .{});
+ try res.write("<input type=\"hidden\" name=\"post_id\" value=\"{x}\" />", .{@intFromEnum(post_id)});
+ try res.write("<input type=\"submit\" value=\"Save\" />", .{});
try res.write("</form>", .{});
}
try res.write("<br /><br />", .{});
try html_form(res, "/comment", .{
.{ "type=\"hidden\" value=\"{x}\" name=\"post_id\"", .{@intFromEnum(post.id)} },
- "type=\"text\" name=\"text\" placeholder=\"Text\"",
+ .{ "textarea", "type=\"text\" name=\"text\" placeholder=\"Text\"", .{} },
"type=\"submit\" value=\"Comment\"",
});
try res.write("<br />", .{});
try res.write("<option value=\"{x}\">{s}{s}</option>", .{ id, name.constSlice(), if (list_view.has(user.id) catch false) " *" else "" });
}
try res.write("</select>", .{});
- try res.write("<input type=\"hidden\" name=\"user_id\" value=\"{x}\"></input>", .{@intFromEnum(user.id)});
- try res.write("<input type=\"submit\" value=\"Add to feed\"></input>", .{});
+ try res.write("<input type=\"hidden\" name=\"user_id\" value=\"{x}\" />", .{@intFromEnum(user.id)});
+ try res.write("<input type=\"submit\" value=\"Add to feed\" />", .{});
try res.write("</form>", .{});
}
try res.write(
try res.write("<br />", .{});
}
-fn write_posts(res: *http.Response, txn: lmdb.Txn, logged_in: ?Login, post_list: PostList, options: struct {
+fn write_posts(res: *http.Response, txn: lmdb.Txn, logged_in: ?Login, post_list: PostSet, options: struct {
show_posts: bool,
show_quotes: bool,
show_comments: bool,
}) !void {
const posts_view = try post_list.open(txn);
- var paginate = try Paginate(PostList).init(res, posts_view, Chirp.PostsPerPage);
+ 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
try paginate.write_navigation();
}
-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);
+fn write_timeline(res: *http.Response, txn: lmdb.Txn, logged_in: ?Login, user_list: UserSet) !void {
+ // TODO: paginate
+ 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;
const following = try user_list.open(txn);
+ if (following.len() == 0) {
+ try res.write("Empty timeline (no users)", .{});
+ return;
+ }
while (true) {
var newest_post: ?Post = null;
try res.write("<br />", .{});
}
}
+fn write_frontpage(res: *http.Response, txn: lmdb.Txn) !void {
+ const posts = try Db.posts.open(txn);
+ var counter: u64 = 0;
+ var it = try posts.reverse_iterator();
+ while (it.next()) |p| {
+ if (p.val.parent_id == null and p.val.quote_id == null) {
+ try write_post(res, txn, null, p.key, .{ .recurse = 1 });
+ counter += 1;
+ }
+
+ if (counter >= 10) break;
+ }
+}
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>
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),
fn handle(self: Self) !bool {
const ti = @typeInfo(Self);
- inline for (ti.Struct.decls) |f_decl| {
+ 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) {
+ if (fi.@"fn".params.len == 1) {
try @call(.auto, f, .{self});
} else {
- const arg_type = fi.Fn.params[1].type.?;
+ 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];
+ const field = arg_info.@"struct".fields[0];
if (self.req.target.len <= f_decl.name.len) {
return error.NoArgProvided;
}
const field_ti = @typeInfo(field.type);
switch (field_ti) {
// TODO: maybe handle BoundedArray?
- .Int => {
+ .int => {
@field(arg, field.name) = try std.fmt.parseUnsigned(field.type, str, 16);
},
- .Enum => {
+ .@"enum" => {
@field(arg, field.name) = try parse_enum(field.type, str, 16);
},
else => {
}
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);
}
}
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);
}
}
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);
}
}
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);
}
}
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);
- var paginate = try Paginate(UserList).init(self.res, following_view, Chirp.UsersPerPage);
+ var paginate = try Paginate(UserSet).init(self.res, following_view, Chirp.UsersPerPage);
try self.res.write(
\\<h2><a href="/user/{s}">{s}</a> follows:</h2>
}
}
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);
- var paginate = try Paginate(UserList).init(self.res, followers_view, Chirp.UsersPerPage);
+ var paginate = try Paginate(UserSet).init(self.res, followers_view, Chirp.UsersPerPage);
try self.res.write(
\\<h2><a href="/user/{s}">{s}</a> followers:</h2>
});
}
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;
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\" autofocus",
+ .{ "textarea", "type=\"text\" name=\"text\" placeholder=\"Text\" autofocus", .{} },
"type=\"submit\" value=\"Quote\"",
});
try self.res.write("<br />", .{});
.show_comments = false,
});
}
- pub fn @"/list/"(self: Self, args: struct { list_id: PostList.Base.Index }) !void {
- try write_posts(self.res, self.txn, self.logged_in, PostList{ .base = .{ .idx = args.list_id } }, .{
+ pub fn @"/list/"(self: Self, args: struct { list_id: PostSet.Base.Index }) !void {
+ try write_posts(self.res, self.txn, self.logged_in, PostSet{ .base = .{ .idx = args.list_id } }, .{
.show_posts = true,
.show_quotes = true,
.show_comments = true,
try self.res.write("not logged in", .{});
}
}
- pub fn @"/feed/"(self: Self, args: struct { feed_id: UserList.Base.Index }) !void {
- try write_timeline(self.res, self.txn, self.logged_in, UserList{ .base = .{ .idx = args.feed_id } });
+ pub fn @"/feed/"(self: Self, args: struct { feed_id: UserSet.Base.Index }) !void {
+ try write_timeline(self.res, self.txn, self.logged_in, UserSet{ .base = .{ .idx = args.feed_id } });
}
pub fn @"/feeds"(self: Self) !void {
if (self.logged_in) |login| {
try html_form(self.res, "/post", .{
.{ "type=\"hidden\" name=\"referer\" value=\"{s}\"", .{referer} },
- "type=\"text\" name=\"text\" placeholder=\"Text\" autofocus",
+ .{ "textarea", "type=\"text\" name=\"text\" placeholder=\"Text\" autofocus", .{} },
"type=\"submit\" value=\"Post\"",
});
} else {
});
try self.res.write("<br />Description: ", .{});
try html_form(self.res, "/set_description", .{
- .{ "type=\"text\" name=\"description\" placeholder=\"{s}\"", .{login.user.description.constSlice()} },
+ .{ "textarea", "type=\"text\" name=\"description\" placeholder=\"{s}\"", .{login.user.description.constSlice()} },
"type=\"submit\" value=\"Change\"",
});
try self.res.write("<br />Password: ", .{});
} else {
// TODO: generic home
try self.res.write("Homepage", .{});
+ // try write_frontpage(self.res, self.txn);
}
}
};
pub fn handle(self: Self) !bool {
const ti = @typeInfo(Self);
- inline for (ti.Struct.decls) |f_decl| {
+ 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) {
+ 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 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| {
+ 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 => {
+ .int => {
@field(args, field.name) = try std.fmt.parseUnsigned(field.type, str, 16);
},
- .Enum => {
+ .@"enum" => {
@field(args, field.name) = try parse_enum(field.type, str, 16);
},
else => {
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);
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);
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);
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);
// TODO: decode name
var txn = try self.env.txn();
- const postlist = try PostList.init(txn);
+ const postlist = try PostSet.init(txn);
try txn.commit();
txn = try self.env.txn();
try txn.commit();
}
}
- pub fn @"/delete_list"(self: Self, args: struct { list_id: PostList.Base.Index }) !void {
+ pub fn @"/delete_list"(self: Self, args: struct { list_id: PostSet.Base.Index }) !void {
if (self.logged_in) |login| {
- var post_list: ?PostList = null;
+ var post_list: ?PostSet = null;
{
const txn = try self.env.txn();
defer txn.commit() catch {};
}
}
}
- pub fn @"/list_add"(self: Self, args: struct { list_id: PostList.Base.Index, post_id: PostId }) !void {
+ pub fn @"/list_add"(self: Self, args: struct { list_id: PostSet.Base.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{ .base = .{ .idx = args.list_id } };
+ const post_list = PostSet{ .base = .{ .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);
const name = try Name.fromSlice(name_str);
var txn = try self.env.txn();
- const userlist = try UserList.init(txn);
+ const userlist = try UserSet.init(txn);
try txn.commit();
txn = try self.env.txn();
try txn.commit();
}
}
- pub fn @"/delete_feed"(self: Self, args: struct { list_id: UserList.Base.Index }) !void {
+ pub fn @"/delete_feed"(self: Self, args: struct { list_id: UserSet.Base.Index }) !void {
if (self.logged_in) |login| {
- var user_list: ?UserList = null;
+ var user_list: ?UserSet = null;
{
const txn = try self.env.txn();
}
}
}
- pub fn @"/feed_add"(self: Self, args: struct { feed_id: UserList.Base.Index, user_id: UserId }) !void {
+ pub fn @"/feed_add"(self: Self, args: struct { feed_id: UserSet.Base.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{ .base = .{ .idx = args.feed_id } };
+ const user_list = UserSet{ .base = .{ .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);
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;
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;
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;
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;
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", .{});