From f49e59bb47ab9603ab399162fd67413430c08d34 Mon Sep 17 00:00:00 2001 From: jaina heartles Date: Fri, 9 Dec 2022 22:33:39 -0800 Subject: [PATCH] Fix community creation --- src/api/services/actors.zig | 7 ++++--- src/api/services/auth.zig | 4 ++-- src/api/services/communities.zig | 3 +-- 3 files changed, 7 insertions(+), 7 deletions(-) diff --git a/src/api/services/actors.zig b/src/api/services/actors.zig index c51c23e..f35e9e0 100644 --- a/src/api/services/actors.zig +++ b/src/api/services/actors.zig @@ -67,12 +67,12 @@ pub const UsernameValidationError = error{ /// - Be at least 1 character /// - Be no more than 32 characters /// - All characters are in [A-Za-z0-9_] -pub fn validateUsername(username: []const u8) UsernameValidationError!void { +pub fn validateUsername(username: []const u8, lax: bool) UsernameValidationError!void { if (username.len == 0) return error.UsernameEmpty; if (username.len > max_username_chars) return error.UsernameTooLong; for (username) |ch| { - const valid = std.ascii.isAlNum(ch) or ch == '_'; + const valid = std.ascii.isAlNum(ch) or ch == '_' or (lax and ch == '.'); if (!valid) return error.UsernameContainsInvalidChar; } } @@ -81,11 +81,12 @@ pub fn create( db: anytype, username: []const u8, community_id: Uuid, + lax_username: bool, alloc: std.mem.Allocator, ) CreateError!Uuid { const id = Uuid.randV4(util.getThreadPrng()); - try validateUsername(username); + try validateUsername(username, lax_username); db.insert("actor", .{ .id = id, diff --git a/src/api/services/auth.zig b/src/api/services/auth.zig index 426c734..88e0f20 100644 --- a/src/api/services/auth.zig +++ b/src/api/services/auth.zig @@ -36,14 +36,14 @@ pub fn register( if (password.len < min_password_chars) return error.PasswordTooShort; // perform pre-validation to avoid having to hash the password if it fails - try actors.validateUsername(username); + try actors.validateUsername(username, false); const hash = try hashPassword(password, alloc); defer alloc.free(hash); const tx = db.beginOrSavepoint() catch return error.DatabaseFailure; errdefer tx.rollback(); - const id = try actors.create(tx, username, community_id, alloc); + const id = try actors.create(tx, username, community_id, false, alloc); tx.insert("account", .{ .id = id, .invite_id = options.invite_id, diff --git a/src/api/services/communities.zig b/src/api/services/communities.zig index 824957e..81ae8b4 100644 --- a/src/api/services/communities.zig +++ b/src/api/services/communities.zig @@ -97,7 +97,7 @@ pub fn create(db: anytype, origin: []const u8, options: CreateOptions, alloc: st }, alloc); if (options.kind == .local) { - const actor_id = actors.create(tx, "community.actor", id, alloc) catch |err| switch (err) { + const actor_id = actors.create(tx, "community.actor", id, true, alloc) catch |err| switch (err) { error.UsernameContainsInvalidChar, error.UsernameTooLong, error.UsernameEmpty, @@ -109,7 +109,6 @@ pub fn create(db: anytype, origin: []const u8, options: CreateOptions, alloc: st \\UPDATE community \\SET community_actor_id = $1 \\WHERE id = $2 - \\LIMIT 1 , .{ actor_id, id }, alloc); }