Fix community creation

This commit is contained in:
jaina heartles 2022-12-09 22:33:39 -08:00
parent 1c6b3aceee
commit f49e59bb47
3 changed files with 7 additions and 7 deletions

View file

@ -67,12 +67,12 @@ pub const UsernameValidationError = error{
/// - Be at least 1 character /// - Be at least 1 character
/// - Be no more than 32 characters /// - Be no more than 32 characters
/// - All characters are in [A-Za-z0-9_] /// - 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 == 0) return error.UsernameEmpty;
if (username.len > max_username_chars) return error.UsernameTooLong; if (username.len > max_username_chars) return error.UsernameTooLong;
for (username) |ch| { 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; if (!valid) return error.UsernameContainsInvalidChar;
} }
} }
@ -81,11 +81,12 @@ pub fn create(
db: anytype, db: anytype,
username: []const u8, username: []const u8,
community_id: Uuid, community_id: Uuid,
lax_username: bool,
alloc: std.mem.Allocator, alloc: std.mem.Allocator,
) CreateError!Uuid { ) CreateError!Uuid {
const id = Uuid.randV4(util.getThreadPrng()); const id = Uuid.randV4(util.getThreadPrng());
try validateUsername(username); try validateUsername(username, lax_username);
db.insert("actor", .{ db.insert("actor", .{
.id = id, .id = id,

View file

@ -36,14 +36,14 @@ pub fn register(
if (password.len < min_password_chars) return error.PasswordTooShort; if (password.len < min_password_chars) return error.PasswordTooShort;
// perform pre-validation to avoid having to hash the password if it fails // 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); const hash = try hashPassword(password, alloc);
defer alloc.free(hash); defer alloc.free(hash);
const tx = db.beginOrSavepoint() catch return error.DatabaseFailure; const tx = db.beginOrSavepoint() catch return error.DatabaseFailure;
errdefer tx.rollback(); 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", .{ tx.insert("account", .{
.id = id, .id = id,
.invite_id = options.invite_id, .invite_id = options.invite_id,

View file

@ -97,7 +97,7 @@ pub fn create(db: anytype, origin: []const u8, options: CreateOptions, alloc: st
}, alloc); }, alloc);
if (options.kind == .local) { 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.UsernameContainsInvalidChar,
error.UsernameTooLong, error.UsernameTooLong,
error.UsernameEmpty, error.UsernameEmpty,
@ -109,7 +109,6 @@ pub fn create(db: anytype, origin: []const u8, options: CreateOptions, alloc: st
\\UPDATE community \\UPDATE community
\\SET community_actor_id = $1 \\SET community_actor_id = $1
\\WHERE id = $2 \\WHERE id = $2
\\LIMIT 1
, .{ actor_id, id }, alloc); , .{ actor_id, id }, alloc);
} }