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 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,

View File

@ -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,

View File

@ -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);
}