]> gitweb.ps.run Git - chirp/blobdiff - src/main.zig
add user description
[chirp] / src / main.zig
index 2ccb16b4da4f35d5469110979b4e486f5b49a1b6..cc3321686b188a3498ffdf7bc9f639e357753f5a 100644 (file)
@@ -29,8 +29,11 @@ const User = struct {
     id: UserId,
     name: Username,
     display_name: DisplayName,
+    description: UserDescription,
     password_hash: PasswordHash,
+
     posts: PostList,
+
     following: UserList,
     followers: UserList,
 
@@ -82,6 +85,7 @@ 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);
@@ -97,8 +101,8 @@ fn parse_enum(comptime E: type, buf: []const u8, base: u8) !E {
 }
 
 // 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
 
@@ -223,6 +227,7 @@ 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),
                 .following = try UserList.init(txn),
@@ -286,7 +291,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,
@@ -447,24 +452,20 @@ pub fn Paginate(comptime T: type) type {
     return struct {
         const Self = @This();
 
-        const IterateResult = @typeInfo(@TypeOf(T.View.Iterator.next)).Fn.return_type.?;
+        const IterateResult = T.Base.View.Iterator.Result;
 
         res: *http.Response,
         view: T.View,
         per_page: u64,
 
-        it: T.View.Iterator,
-        starting_idx: ?T.Key,
+        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.Key, starting_at_str, 16);
-            }
-
-            if (it.idx == null) {
-                return error.InvalidIterator;
+                it.idx = try parse_enum(T.Base.Key, starting_at_str, 16);
             }
 
             return .{
@@ -472,7 +473,7 @@ pub fn Paginate(comptime T: type) type {
                 .view = view,
                 .per_page = per_page,
                 .it = it,
-                .starting_idx = it.idx.?,
+                .starting_idx = it.idx,
             };
         }
         pub fn next(self: *Self) IterateResult {
@@ -487,7 +488,7 @@ pub fn Paginate(comptime T: type) type {
         pub fn write_navigation(self: *Self) !void {
             const next_idx = self.it.next();
 
-            if (self.view.head.last != self.starting_idx) {
+            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.?;
@@ -702,7 +703,7 @@ fn write_post(res: *http.Response, txn: lmdb.Txn, logged_in: ?Login, post_id: Po
         // TODO: mark lists that already contain post
         while (it.next()) |kv| {
             const name = kv.val.name;
-            const id = kv.val.list.idx.?;
+            const id = kv.val.list.base.idx.?;
             const list_view = try kv.val.list.open(txn);
             try res.write("<option value=\"{x}\">{s}{s}</option>", .{ id, name.constSlice(), if (list_view.has(post_id) catch false) " *" else "" });
         }
@@ -783,7 +784,7 @@ fn write_profile(res: *http.Response, txn: lmdb.Txn, logged_in: ?Login, user: Us
         var it = feeds_view.iterator();
         while (it.next()) |kv| {
             const name = kv.val.name;
-            const id = kv.val.list.idx.?;
+            const id = kv.val.list.base.idx.?;
             const list_view = try kv.val.list.open(txn);
             try res.write("<option value=\"{x}\">{s}{s}</option>", .{ id, name.constSlice(), if (list_view.has(user.id) catch false) " *" else "" });
         }
@@ -818,6 +819,15 @@ fn write_profile(res: *http.Response, txn: lmdb.Txn, logged_in: ?Login, user: Us
         , .{});
     }
 
+    if (user.description.len > 0) {
+        try res.write(
+            \\<div style="padding-left: 5px; border-left: 1px solid grey;">
+            // \\&#x00AB; {s} &#x00BB;
+            \\<i>{s}</i>
+            \\</div>
+        , .{user.description.constSlice()});
+    }
+
     try res.write("<br />", .{});
 }
 fn write_posts(res: *http.Response, txn: lmdb.Txn, logged_in: ?Login, post_list: PostList, options: struct {
@@ -1170,8 +1180,8 @@ const GET = struct {
             .show_comments = false,
         });
     }
-    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 }, .{
+    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 } }, .{
             .show_posts = true,
             .show_quotes = true,
             .show_comments = true,
@@ -1194,7 +1204,7 @@ const GET = struct {
                 const post_list = kv.val.list;
                 try self.res.write(
                     \\<a href="/list/{x}">{s}</a> 
-                , .{ post_list.idx.?, name.constSlice() });
+                , .{ post_list.base.idx.?, name.constSlice() });
                 try html_form(self.res, "/delete_list", .{
                     .{ "type=\"hidden\" name=\"list_id\" value=\"{x}\"", .{kv.key} },
                     "type=\"submit\" value=\"Delete\"",
@@ -1205,8 +1215,8 @@ const GET = struct {
             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 @"/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 @"/feeds"(self: Self) !void {
         if (self.logged_in) |login| {
@@ -1225,7 +1235,7 @@ const GET = struct {
                 const user_list = kv.val.list;
                 try self.res.write(
                     \\<a href="/feed/{x}">{s}</a> 
-                , .{ user_list.idx.?, name.constSlice() });
+                , .{ user_list.base.idx.?, name.constSlice() });
                 try html_form(self.res, "/delete_feed", .{
                     .{ "type=\"hidden\" name=\"list_id\" value=\"{x}\"", .{kv.key} },
                     "type=\"submit\" value=\"Delete\"",
@@ -1262,6 +1272,11 @@ const GET = struct {
                 .{ "type=\"text\" name=\"display_name\" placeholder=\"{s}\"", .{login.user.display_name.constSlice()} },
                 "type=\"submit\" value=\"Change\"",
             });
+            try self.res.write("<br />Description: ", .{});
+            try html_form(self.res, "/set_description", .{
+                .{ "type=\"text\" name=\"description\" placeholder=\"{s}\"", .{login.user.description.constSlice()} },
+                "type=\"submit\" value=\"Change\"",
+            });
             try self.res.write("<br />Password: ", .{});
             try html_form(self.res, "/set_password", .{
                 "type=\"text\" name=\"password\"",
@@ -1385,6 +1400,18 @@ const POST = struct {
         user.display_name = display_name;
         try users.put(login.user.id, user);
     }
+    pub fn @"/set_description"(self: Self, args: struct { description: []const u8 }) !void {
+        const login = self.logged_in orelse return error.NotLoggedIn;
+        const description = try reencode(UserDescription, args.description);
+
+        const txn = try self.env.txn();
+        defer txn.commit() catch {};
+
+        const users = try Db.users(txn);
+        var user = login.user;
+        user.description = description;
+        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;
 
@@ -1451,7 +1478,7 @@ const POST = struct {
             try txn.commit();
         }
     }
-    pub fn @"/delete_list"(self: Self, args: struct { list_id: PostList.Index }) !void {
+    pub fn @"/delete_list"(self: Self, args: struct { list_id: PostList.Base.Index }) !void {
         if (self.logged_in) |login| {
             var post_list: ?PostList = null;
             {
@@ -1469,14 +1496,14 @@ const POST = struct {
             }
         }
     }
-    pub fn @"/list_add"(self: Self, args: struct { list_id: PostList.Index, post_id: PostId }) !void {
+    pub fn @"/list_add"(self: Self, args: struct { list_id: PostList.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{ .idx = args.list_id };
+            const post_list = PostList{ .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);
@@ -1500,7 +1527,7 @@ const POST = struct {
             try txn.commit();
         }
     }
-    pub fn @"/delete_feed"(self: Self, args: struct { list_id: UserList.Index }) !void {
+    pub fn @"/delete_feed"(self: Self, args: struct { list_id: UserList.Base.Index }) !void {
         if (self.logged_in) |login| {
             var user_list: ?UserList = null;
 
@@ -1519,14 +1546,14 @@ const POST = struct {
             }
         }
     }
-    pub fn @"/feed_add"(self: Self, args: struct { feed_id: UserList.Index, user_id: UserId }) !void {
+    pub fn @"/feed_add"(self: Self, args: struct { feed_id: UserList.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{ .idx = args.feed_id };
+            const user_list = UserList{ .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);
@@ -1657,9 +1684,10 @@ pub fn main() !void {
         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);
-            };
+            // handle_request(env, req) catch {
+            //     try handle_error(env, req);
+            // };
+            try handle_request(env, req);
         }
     }
     // const ThreadCount = 1;