X-Git-Url: https://gitweb.ps.run/chirp/blobdiff_plain/975845bfb7532dd679a4668e1ad1fda9424cd30d..48e80ae12c563913af56f867268c02650292f7ee:/src/main.zig
diff --git a/src/main.zig b/src/main.zig
index 259f3da..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");
}
@@ -32,10 +44,10 @@ const User = struct {
description: UserDescription,
password_hash: PasswordHash,
- posts: PostList,
+ posts: PostSet,
- following: UserList,
- followers: UserList,
+ following: UserSet,
+ followers: UserSet,
post_lists: PostListList,
feeds: UserListList,
@@ -52,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 {
@@ -90,8 +102,11 @@ 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);
@@ -218,9 +233,9 @@ const Chirp = struct {
.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),
});
@@ -266,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
@@ -288,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,
});
}
@@ -829,14 +844,14 @@ fn write_profile(res: *http.Response, txn: lmdb.Txn, logged_in: ?Login, user: Us
try res.write("
", .{});
}
-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);
@@ -852,7 +867,8 @@ fn write_posts(res: *http.Response, txn: lmdb.Txn, logged_in: ?Login, post_list:
try paginate.write_navigation();
}
-fn write_timeline(res: *http.Response, txn: lmdb.Txn, logged_in: ?Login, user_list: UserList) !void {
+fn write_timeline(res: *http.Response, txn: lmdb.Txn, logged_in: ?Login, user_list: UserSet) !void {
+ // TODO: paginate
const users = try Db.users(txn);
const posts = try Db.posts(txn);
@@ -900,6 +916,19 @@ fn write_timeline(res: *http.Response, txn: lmdb.Txn, logged_in: ?Login, user_li
try res.write("
", .{});
}
}
+fn write_frontpage(res: *http.Response, txn: lmdb.Txn) !void {
+ const posts = try Db.posts(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 user = try users.get(user_id);
@@ -1099,7 +1128,7 @@ const GET = struct {
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(
\\