create isInviteValid helper
This commit is contained in:
parent
eb001b39a4
commit
ee7ff9e69a
1 changed files with 7 additions and 2 deletions
|
@ -396,6 +396,12 @@ fn ApiConn(comptime DbConn: type) type {
|
||||||
return try services.invites.get(self.db, invite_id, self.allocator);
|
return try services.invites.get(self.db, invite_id, self.allocator);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn isInviteValid(invite: services.invites.Invite) bool {
|
||||||
|
if (invite.max_uses != null and invite.times_used >= invite.max_uses.?) return false;
|
||||||
|
if (invite.expires_at != null and DateTime.now().isAfter(invite.expires_at.?)) return false;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
pub fn register(self: *Self, username: []const u8, password: []const u8, opt: RegistrationOptions) !UserResponse {
|
pub fn register(self: *Self, username: []const u8, password: []const u8, opt: RegistrationOptions) !UserResponse {
|
||||||
const tx = try self.db.beginOrSavepoint();
|
const tx = try self.db.beginOrSavepoint();
|
||||||
const maybe_invite = if (opt.invite_code) |code|
|
const maybe_invite = if (opt.invite_code) |code|
|
||||||
|
@ -406,8 +412,7 @@ fn ApiConn(comptime DbConn: type) type {
|
||||||
|
|
||||||
if (maybe_invite) |invite| {
|
if (maybe_invite) |invite| {
|
||||||
if (!Uuid.eql(invite.community_id, self.community.id)) return error.WrongCommunity;
|
if (!Uuid.eql(invite.community_id, self.community.id)) return error.WrongCommunity;
|
||||||
if (invite.max_uses != null and invite.times_used >= invite.max_uses.?) return error.InviteExpired;
|
if (!isInviteValid(invite)) return error.InvalidInvite;
|
||||||
if (invite.expires_at != null and DateTime.now().isAfter(invite.expires_at.?)) return error.InviteExpired;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const invite_kind = if (maybe_invite) |inv| inv.kind else .user;
|
const invite_kind = if (maybe_invite) |inv| inv.kind else .user;
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue