X-Git-Url: https://gitweb.ps.run/chirp/blobdiff_plain/e8c4ebe56c1382517e02f9a770397799b0e0b066..d4496703a509fb5ad5352787ae277ebe204195c8:/src/main.zig
diff --git a/src/main.zig b/src/main.zig
index 2fa757c..0b22dbb 100644
--- a/src/main.zig
+++ b/src/main.zig
@@ -31,7 +31,6 @@ const User = struct {
display_name: DisplayName,
password_hash: PasswordHash,
posts: PostList,
- replies: PostList,
following: UserList,
followers: UserList,
@@ -223,7 +222,6 @@ const Chirp = struct {
.display_name = display_name,
.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),
.post_lists = try PostListList.init(txn),
@@ -328,11 +326,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 +341,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 +353,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 {
@@ -616,7 +606,7 @@ fn write_post(res: *http.Response, txn: lmdb.Txn, logged_in: ?Login, post_id: Po
// Quote
try res.write(
- \\🔁 {}
+ \\🔁 {}
, .{ @intFromEnum(post.id), quotes_view.len() });
// Save to List
@@ -725,7 +715,11 @@ fn write_profile(res: *http.Response, txn: lmdb.Txn, logged_in: ?Login, user: Us
user.name.constSlice(), followers.len(),
});
- try res.write("Replies
", .{
+ try res.write(
+ \\All Posts
+ \\ Comments
+ \\ Quotes
+ , .{
user.name.constSlice(),
});
@@ -740,13 +734,24 @@ 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) !void {
+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| {
- try write_post(res, txn, logged_in, post_id, .{ .recurse = 1 });
- try res.write("
", .{});
+ 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 {
@@ -887,14 +892,18 @@ const GET = struct {
try write_profile(self.res, self.txn, self.logged_in, user);
- try write_posts(self.res, self.txn, self.logged_in, user.posts);
+ 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 @"/replies/"(self: Self, args: struct { username: []const u8 }) !void { + 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); @@ -902,7 +911,49 @@ const GET = struct { try write_profile(self.res, self.txn, self.logged_in, user); - try write_posts(self.res, self.txn, self.logged_in, user.replies); + 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 [{}]
@@ -967,7 +1018,7 @@ const GET = struct { .show_comment_field = true, }); } - pub fn @"/quotes/"(self: Self, args: struct { post_id: PostId }) !void { + 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); @@ -991,7 +1042,11 @@ const GET = struct { } } 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 }); + 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| {