]> gitweb.ps.run Git - chirp/blobdiff - src/main.zig
add user description
[chirp] / src / main.zig
index 0800e2b41439a301da86e345d4b30388577195fd..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);
@@ -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),
@@ -814,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 {
@@ -1258,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\"",
@@ -1381,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;
 
@@ -1653,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;