From: patrick-scho Date: Sun, 6 Apr 2025 14:22:12 +0000 (+0200) Subject: add user description X-Git-Url: https://gitweb.ps.run/chirp/commitdiff_plain/4cf4bee6f41428be1e4138b329b3f9d0a7ccf4f4?ds=inline add user description --- diff --git a/src/main.zig b/src/main.zig index 7aa34bb..cc33216 100644 --- a/src/main.zig +++ b/src/main.zig @@ -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( + \\
+ // \\« {s} » + \\{s} + \\
+ , .{user.description.constSlice()}); + } + try res.write("
", .{}); } 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("
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("
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;