diff options
author | Colin Okay <colin@cicadas.surf> | 2022-10-27 10:40:58 -0500 |
---|---|---|
committer | Colin Okay <colin@cicadas.surf> | 2022-10-27 10:40:58 -0500 |
commit | fda05f8ce638987d332c8aff4043afa74e52523f (patch) | |
tree | 716cb835372c7f35b8239e57273e73bdfb4a53ea /model.lisp | |
parent | 6f729e2d0e44252460e5b287776f7ad82ed8da06 (diff) |
Add: invite code redeeming logic
Diffstat (limited to 'model.lisp')
-rw-r--r-- | model.lisp | 36 |
1 files changed, 30 insertions, 6 deletions
@@ -3,17 +3,21 @@ (in-package :vampire) (defclass/bknr keyed () - ((key - :r :std (nuid) - :index-type string-unique-index - :index-reader object-with-key))) + ((key :r :std (nuid) + :index-type string-unique-index + :index-reader object-with-key))) (defclass/bknr user (keyed) ((name :with :index-type string-unique-index :index-reader user-with-name) (playlists :with :std (list)) - (pwsalt pwhash :with))) + (pwsalt :with :std (nuid)) + (pwhash :with))) + +(defclass/bknr invite (keyed) + ((maker :r :std nil) + (uses-remaining :std nil))) (defclass/bknr playlist (keyed) ((title :with :std (default-name "playlist")) @@ -25,7 +29,6 @@ (defmethod initialize-instance :after ((pl playlist) &key) (pushnew pl (user-playlists (playlist-user pl)) :test #'eq)) - (defclass/bknr track (keyed) ((source file title artist album thumb-url duration codec :with) (playlists :with :std (list) :doc "A list of playlists in which this track appears"))) @@ -33,6 +36,15 @@ ;;; MODEL OPERATIONS +(defun invite-by-code (code) + "Returns NIL if CODE is an invalid invite code. Returns the INVITE + instance otherwise" + (when-let (obj (object-with-key code)) + (and (typep obj 'invite) + (or (null (uses-remaining obj)) + (plusp (uses-remaining obj))) + obj))) + (defun user-with-key (key) (when-let (obj (object-with-key key)) (when (typep obj 'user) @@ -70,6 +82,18 @@ (with-transaction () (make-instance 'user :name name))) +(defun use-invite-with-code (code username pw) + (with-transaction () + (when-let (invite (invite-by-code code)) + (when (uses-remaining invite) + (decf (uses-remaining invite)) + (unless (plusp (uses-remaining invite)) + (bknr.datastore:delete-object invite)) + (let ((user (make-instance 'user :name username))) + (setf (user-pwhash user) + (hash-string pw (user-pwsalt user))) + user))))) + (defun append-track (pl tr) (with-transaction () (add-track tr pl))) |