Skip to content

Commit

Permalink
feat: Can not transfer kudos directly to sharedkey
Browse files Browse the repository at this point in the history
Closes #474
  • Loading branch information
db0 committed Dec 7, 2024
1 parent 41b3400 commit adf2d13
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 4 deletions.
2 changes: 1 addition & 1 deletion horde/apis/v2/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -648,7 +648,7 @@ class TransferKudos(Resource):
"username",
type=str,
required=True,
help="The user ID which will receive the kudos.",
help="The user or shared key ID which will receive the kudos.",
location="json",
)
parser.add_argument(
Expand Down
8 changes: 7 additions & 1 deletion horde/classes/base/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -176,11 +176,17 @@ def consume_kudos(self, kudos):
def is_valid(self):
if self.kudos == 0:
return False, "This shared key has run out of kudos.", "SharedKeyEmpty"
if self.expiry is not None and self.expiry < datetime.utcnow():
if self.is_expired():
return False, "This shared key has expired", "SharedKeyExpired"
else:
return True, None, None

def is_expired(self) -> bool:
"""Returns true if the key has expired"""
if self.expiry is not None and self.expiry < datetime.utcnow():
return True
return False

def is_job_within_limits(
self,
*,
Expand Down
11 changes: 10 additions & 1 deletion horde/database/functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -473,13 +473,22 @@ def transfer_kudos(source_user, dest_user, amount):

def transfer_kudos_to_username(source_user, dest_username, amount):
dest_user = find_user_by_username(dest_username)
shared_key = None
if not dest_user:
return [0, "Invalid target username.", "InvalidTargetUsername"]
shared_key = find_sharedkey(dest_username)
if not shared_key:
return [0, "Invalid target username.", "InvalidTargetUsername"]
if shared_key.is_expired():
return [0, "This shared key has expired", "SharedKeyExpired"]
dest_user = shared_key.user
if dest_user == get_anon():
return [0, "Tried to burn kudos via sending to Anonymous. Assuming PEBKAC and aborting.", "KudosTransferToAnon"]
if dest_user == source_user:
return [0, "Cannot send kudos to yourself, ya monkey!", "KudosTransferToSelf"]
kudos = transfer_kudos(source_user, dest_user, amount)
if kudos[0] > 0 and shared_key is not None:
shared_key.kudos += kudos[0]
db.session.commit()
return kudos


Expand Down
2 changes: 1 addition & 1 deletion horde/templates/transfer_kudos.html
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ <h2>Welcome</h2>
</div>
{% endif %}
<div class="form-group">
<label for="user id">Unique User ID *</label>
<label for="user id">Unique User or Shared Key ID *</label>
<input type="text" class="form-control" id="username" name="username" placeholder="Example: db0#1" required>
</div>
<div class="form-group">
Expand Down

0 comments on commit adf2d13

Please sign in to comment.