user_handlers.py 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. from flask import current_app
  2. from sqlite3 import Error
  3. from re import sub
  4. import random
  5. def get_users_points(db, user_id): # Look up one user's points by user id
  6. try:
  7. cursor = db.execute(
  8. "SELECT points FROM points WHERE id = ?",
  9. (user_id,)
  10. )
  11. return cursor.fetchone()[0]
  12. except Error as guperror:
  13. current_app.logger.error(f'Couldn\'t look up points for {user_id}: {guperror.args[0]}')
  14. def get_id_by_email(db, email): # Look up all users' points by username
  15. try:
  16. cursor = db.execute(
  17. "SELECT id, points FROM points WHERE email = ?",
  18. (email,)
  19. )
  20. users = cursor.fetchall()
  21. return users
  22. except Error as gaubeerror:
  23. current_app.logger.error(f'Couldn\'t look up user id for {email} by email: {gaubeerror.args[0]}')
  24. def get_all_users_by_name(db, username): # Look up all users' points by username
  25. try:
  26. cursor = db.execute(
  27. "SELECT * FROM points WHERE name = ?",
  28. (username,)
  29. )
  30. users = cursor.fetchall()
  31. return users
  32. except Error as gaubnerror:
  33. current_app.logger.error(f'Couldn\'t look up points for {username} by username: {gaubnerror.args[0]}')
  34. def get_all_users_with_user_id(db, user_id): # Look up all users' points by user id
  35. try:
  36. cursor = db.execute(
  37. "SELECT * FROM points WHERE id = ?",
  38. (user_id,)
  39. )
  40. users = cursor.fetchall()
  41. return users
  42. except Error as gauwuierror:
  43. current_app.logger.error(f'Couldn\'t look up points for {user_id} by user_id: {gauwuierror.args[0]}')
  44. def get_all_users(db): # Get all users' details from points database
  45. try:
  46. cursor = db.execute(
  47. "SELECT * FROM points"
  48. )
  49. users = cursor.fetchall()
  50. return users
  51. except Error as gauerror:
  52. current_app.logger.error(f'Couldn\'t get all users\' points: {gauerror.args[0]}')
  53. def award_chat_points(db, user_id, points): # Award points to user by user id
  54. try:
  55. db.execute(
  56. "UPDATE points SET points = points + ? WHERE id = ?",
  57. (points, user_id,)
  58. )
  59. db.commit()
  60. return True
  61. except Error as acperror:
  62. current_app.logger.error(f'Couldn\'t give {points} points to {user_id}: {acperror.args[0]}')
  63. return False
  64. def adjust_points(db, user_id, points): # For streamer to manually adjust a user's points
  65. try:
  66. db.execute(
  67. "UPDATE points SET points = ? WHERE id = ?",
  68. (points, user_id,)
  69. )
  70. db.commit()
  71. return True
  72. except Error as aperror:
  73. current_app.logger.error(f'Couldn\'t adjust points for {user_id}: {aperror.args[0]}')
  74. return False
  75. def delete_user(db, user_id):
  76. try:
  77. db.execute(
  78. "DELETE FROM points WHERE id = ?",
  79. (user_id,)
  80. )
  81. db.commit()
  82. return True
  83. except Error as du_error:
  84. current_app.logger.error(f'Couldn\'t change delete {user_id} from the database: {du_error.args[0]}')
  85. return False
  86. def change_email(db, user_id, new_email): # For streamer to manually adjust a user's points
  87. try:
  88. ids = get_id_by_email(db, new_email)
  89. for user in ids:
  90. if user[0] != user_id: # If the found email belongs to a different user
  91. if 'temp' in user[0]: # If the email address belongs to a temp user
  92. existing_points = get_users_points(db, user_id)
  93. new_points = existing_points + user[1]
  94. if adjust_points(db, user_id, new_points): # Add points to correct user
  95. current_app.logger.info(f'Email was already in the database as a {user[0]}. Added points to {user_id}')
  96. if delete_user(db, user[0]): # Delete temp user
  97. current_app.logger.info(f'Removed temp user {user_id} from the database.')
  98. elif new_email is not None:
  99. current_app.logger.error(f'Couldn\'t change email address for {user_id}. {new_email} already belongs to {user[0]}')
  100. db.execute(
  101. "UPDATE points SET email = ? WHERE id = ?",
  102. (new_email, user_id,)
  103. )
  104. db.commit()
  105. return True
  106. except Error as ce_error:
  107. current_app.logger.error(f'Couldn\'t change email address for {user_id}: {ce_error.args[0]}')
  108. return False
  109. def spend_points(db, user_id, points): # A user spends points on a redeem
  110. try:
  111. db.execute(
  112. "UPDATE points SET points = points - ? WHERE id = ?",
  113. (points, user_id,)
  114. )
  115. db.commit()
  116. return True
  117. except Error as sperror:
  118. current_app.logger.error(f'Couldn\'t spend {user_id}\'s {points} points: {sperror.args[0]}')
  119. return False
  120. def refund_points(db, user_id, points): # Streamer refunds points for a redeem
  121. try:
  122. db.execute(
  123. "UPDATE points SET points = points + ? WHERE id = ?",
  124. (points, user_id,)
  125. )
  126. db.commit()
  127. return True
  128. except Error as rerror:
  129. current_app.logger.error(f'Couldn\'t refund {points} points for {user_id}: {rerror.args[0]}')
  130. return False
  131. def user_in_points(db, user_id): # Check if a user is in the points database
  132. try:
  133. cursor = db.execute(
  134. "SELECT points FROM points WHERE id = ?",
  135. (user_id,)
  136. )
  137. if cursor.fetchone() is None:
  138. return False
  139. return True
  140. except Error as uiperror:
  141. current_app.logger.error(f'Couldn\'t for {user_id} in points database: {uiperror.args[0]}')
  142. def add_email_to_points(db, email, points): # Add an anonymous user and points to the database
  143. try:
  144. id = f'temp{random.randint(10000, 99999)}' # Create random id with "temp" prepended
  145. db.execute(
  146. "INSERT INTO points(id, points, user_authed, email) VALUES(?, ?, 0, ?)",
  147. (id, points, email)
  148. )
  149. db.commit()
  150. return True
  151. except Error as aetperror:
  152. current_app.logger.error(f'Couldn\'t add {email} to points database: {aetperror.args[0]}')
  153. return False
  154. def add_user_to_points(db, user_id, display_name, authed): # Add a user to the points database
  155. try:
  156. cursor = db.execute(
  157. "SELECT points, name, user_authed FROM points WHERE id = ?",
  158. (user_id,)
  159. )
  160. user = cursor.fetchone()
  161. if user is None: # Add the user if they're not in the database
  162. cursor.execute(
  163. "INSERT INTO points(id, name, points, user_authed) VALUES(?, ?, 10, ?)",
  164. (user_id, display_name, authed)
  165. )
  166. if user is not None and user[1] is None: # If their name has changed, change name in the database
  167. cursor.execute(
  168. "UPDATE points SET name = ?, user_authed = ? WHERE id = ?",
  169. (display_name, authed, user_id)
  170. )
  171. if user is not None and user[2] != authed: # If they've authenticated, update the database
  172. cursor.execute(
  173. "UPDATE points SET user_authed = ? WHERE id = ?",
  174. (authed, user_id)
  175. )
  176. db.commit()
  177. return True
  178. except Error as autperror:
  179. current_app.logger.error(f'Couldn\'t add {user_id}/{display_name}/{authed} to database: {autperror.args[0]}')
  180. return False
  181. def change_name(db, user_id, new_name): # Change a user name in the points database
  182. try:
  183. db.execute(
  184. "UPDATE points SET name = ? WHERE id = ?",
  185. (new_name, user_id)
  186. )
  187. db.commit()
  188. except Error as cnerror:
  189. current_app.logger.error(f'Couldn\'t change name to {new_name} for {user_id}: {cnerror.args[0]}')
  190. def remove_duplicates(db, user_id, username): # Remove duplicate usernames
  191. try:
  192. db.execute(
  193. "UPDATE points SET name = NULL WHERE name = ? AND NOT id = ?",
  194. (username, user_id)
  195. )
  196. db.commit()
  197. except Error as rderror:
  198. current_app.logger.error(f'Couldn\'t remove duplicate username {username} for {user_id}: {rderror.args[0]}')