type signatures in owncast_helpers

+ typo fix
This commit is contained in:
Lili (Tlapka) 2024-10-05 22:08:19 +02:00
parent afd0a0d0a3
commit c22af69b2a

View File

@ -1,10 +1,11 @@
from flask import current_app
from sqlite3 import Error
from sqlite3 import Error, Connection
from re import sub
from typing import Tuple
# # # db stuff # # #
def read_users_points(db, user_id):
def read_users_points(db: Connection, user_id: str) -> int:
"""Errors out if user doesn't exist."""
try:
cursor = db.execute(
@ -13,11 +14,11 @@ def read_users_points(db, user_id):
)
return cursor.fetchone()[0]
except Error as e:
current_app.logger.error(f"Error occured reading points: {e.args[0]}")
current_app.logger.error(f"To user: {user_id}")
current_app.logger.error(f"Error occurred reading points: {e.args[0]}")
current_app.logger.error(f"Of user: {user_id}")
def read_all_users_with_username(db, username):
def read_all_users_with_username(db: Connection, username: str) -> list[Tuple[str, int]]:
try:
cursor = db.execute(
"SELECT name, points FROM points WHERE name = ?",
@ -26,11 +27,11 @@ def read_all_users_with_username(db, username):
users = cursor.fetchall()
return users
except Error as e:
current_app.logger.error(f"Error occured reading points by username: {e.args[0]}")
current_app.logger.error(f"To everyone with username: {username}")
current_app.logger.error(f"Error occurred reading points by username: {e.args[0]}")
current_app.logger.error(f"Of everyone with username: {username}")
def give_points_to_user(db, user_id, points):
def give_points_to_user(db: Connection, user_id: str, points: int) -> None:
try:
db.execute(
"UPDATE points SET points = points + ? WHERE id = ?",
@ -38,11 +39,11 @@ def give_points_to_user(db, user_id, points):
)
db.commit()
except Error as e:
current_app.logger.error(f"Error occured giving points: {e.args[0]}")
current_app.logger.error(f"Error occurred giving points: {e.args[0]}")
current_app.logger.error(f"To user: {user_id} amount of points: {points}")
def use_points(db, user_id, points):
def use_points(db: Connection, user_id: str, points: int) -> bool:
try:
db.execute(
"UPDATE points SET points = points - ? WHERE id = ?",
@ -51,12 +52,12 @@ def use_points(db, user_id, points):
db.commit()
return True
except Error as e:
current_app.logger.error(f"Error occured using points: {e.args[0]}")
current_app.logger.error(f"Error occurred using points: {e.args[0]}")
current_app.logger.error(f"From user: {user_id} amount of points: {points}")
return False
def user_exists(db, user_id):
def user_exists(db: Connection, user_id: str) -> bool:
try:
cursor = db.execute(
"SELECT points FROM points WHERE id = ?",
@ -66,11 +67,11 @@ def user_exists(db, user_id):
return False
return True
except Error as e:
current_app.logger.error(f"Error occured checking if user exists: {e.args[0]}")
current_app.logger.error(f"Error occurred checking if user exists: {e.args[0]}")
current_app.logger.error(f"To user: {user_id}")
def add_user_to_database(db, user_id, display_name):
def add_user_to_database(db: Connection, user_id: str, display_name: str) -> None:
""" Adds a new user to the database. Does nothing if user is already in."""
try:
cursor = db.execute(
@ -92,11 +93,11 @@ def add_user_to_database(db, user_id, display_name):
)
db.commit()
except Error as e:
current_app.logger.error(f"Error occured adding user to db: {e.args[0]}")
current_app.logger.error(f"Error occurred adding user to db: {e.args[0]}")
current_app.logger.error(f"To user id: {user_id}, with display name: {display_name}")
def change_display_name(db, user_id, new_name):
def change_display_name(db: Connection, user_id: str, new_name: str) -> None:
try:
db.execute(
"UPDATE points SET name = ? WHERE id = ?",
@ -104,11 +105,11 @@ def change_display_name(db, user_id, new_name):
)
db.commit()
except Error as e:
current_app.logger.error(f"Error occured changing display name: {e.args[0]}")
current_app.logger.error(f"Error occurred changing display name: {e.args[0]}")
current_app.logger.error(f"To user id: {user_id}, with display name: {new_name}")
def remove_duplicate_usernames(db, user_id, username):
def remove_duplicate_usernames(db: Connection, user_id: str, username: str) -> None:
try:
db.execute(
"""UPDATE points
@ -118,12 +119,12 @@ def remove_duplicate_usernames(db, user_id, username):
)
db.commit()
except Error as e:
current_app.logger.error(f"Error occured removing duplicate usernames: {e.args[0]}")
current_app.logger.error(f"Error occurred removing duplicate usernames: {e.args[0]}")
# # # misc. stuff # # #
# This is now unused since rawBody attribute of the webhook now returns cleaned-up emotes.
def remove_emoji(message):
def remove_emoji(message: str) -> str:
return sub(
r'<img class="emoji" alt="(:.*?:)" title=":.*?:" src="/img/emoji/.*?">',
r'\1',