user_handlers.py 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. from flask import current_app
  2. from sqlite3 import Error
  3. from re import sub
  4. import random
  5. import time
  6. import json
  7. import os
  8. def save_todolist(list_items): # Save todo list items
  9. new_list = json.dumps(list_items, indent=4)
  10. list_file = os.path.join(current_app.instance_path, 'todo.py')
  11. try:
  12. with open(list_file, 'w') as f:
  13. f.write(f'LIST = {new_list}')
  14. f.close
  15. current_app.config.from_pyfile('todo.py', silent=True) # Reread the list into the app
  16. return True
  17. except Exception as stdlerror:
  18. current_app.logger.error(f'Couldn\'t save todo.py: {stdlerror}')
  19. return False
  20. def get_users_points(db, user_id): # Look up one user's points by user id
  21. try:
  22. cursor = db.execute(
  23. "SELECT points FROM points WHERE id = ?",
  24. (user_id,)
  25. )
  26. return cursor.fetchone()[0]
  27. except Exception as guperror:
  28. current_app.logger.error(f'Couldn\'t look up points for {user_id}: {guperror}')
  29. def get_email_code(db, user_id): # Get user's verification code
  30. try:
  31. cursor = db.execute(
  32. "SELECT code FROM points WHERE id = ?",
  33. (user_id,)
  34. )
  35. return cursor.fetchone()[0]
  36. except Exception as gecerror:
  37. current_app.logger.error(f'Couldn\'t look up points for {user_id}: {gecerror}')
  38. def get_id_by_email(db, email): # Look up all users' points by username
  39. try:
  40. cursor = db.execute(
  41. "SELECT id, points FROM points WHERE email = ?",
  42. (email,)
  43. )
  44. users = cursor.fetchall()
  45. return users
  46. except Exception as gaubeerror:
  47. current_app.logger.error(f'Couldn\'t look up user id for {email} by email: {gaubeerror}')
  48. def get_all_users_by_name(db, username): # Look up all users' points by username
  49. try:
  50. cursor = db.execute(
  51. "SELECT * FROM points WHERE name = ?",
  52. (username,)
  53. )
  54. users = cursor.fetchall()
  55. return users
  56. except Exception as gaubnerror:
  57. current_app.logger.error(f'Couldn\'t look up points for {username} by username: {gaubnerror}')
  58. def get_all_users_with_user_id(db, user_id): # Look up all users' points by user id
  59. try:
  60. cursor = db.execute(
  61. "SELECT * FROM points WHERE id = ?",
  62. (user_id,)
  63. )
  64. users = cursor.fetchall()
  65. return users
  66. except Exception as gauwuierror:
  67. current_app.logger.error(f'Couldn\'t look up points for {user_id} by user_id: {gauwuierror}')
  68. def get_all_users(db): # Get all users' details from points database
  69. try:
  70. cursor = db.execute(
  71. "SELECT * FROM points"
  72. )
  73. users = cursor.fetchall()
  74. return users
  75. except Exception as gauerror:
  76. current_app.logger.error(f'Couldn\'t get all users\' points: {gauerror}')
  77. def award_chat_points(db, user_id, points): # Award points to user by user id
  78. try:
  79. db.execute(
  80. "UPDATE points SET points = points + ? WHERE id = ?",
  81. (points, user_id,)
  82. )
  83. db.commit()
  84. return True
  85. except Exception as acperror:
  86. current_app.logger.error(f'Couldn\'t give {points} points to {user_id}: {acperror}')
  87. return False
  88. def set_email_code(db, user_id, reg_code): # Set verification code
  89. try:
  90. db.execute(
  91. "UPDATE points SET code = ? WHERE id = ?",
  92. (reg_code, user_id,)
  93. )
  94. db.commit()
  95. return True
  96. except Exception as secerror:
  97. current_app.logger.error(f'Couldn\'t set reg code \"{reg_code}\" for {user_id}: {secerror}')
  98. return False
  99. def del_email_code(db, user_id): # Delete verification code
  100. try:
  101. db.execute(
  102. "UPDATE points SET code = NULL WHERE id = ?",
  103. (user_id,)
  104. )
  105. db.commit()
  106. return True
  107. except Exception as decerror:
  108. current_app.logger.error(f'Couldn\'t remove reg code for {user_id}: {decerror}')
  109. return False
  110. def adjust_points(db, user_id, points): # For streamer to manually adjust a user's points
  111. try:
  112. db.execute(
  113. "UPDATE points SET points = ? WHERE id = ?",
  114. (points, user_id,)
  115. )
  116. db.commit()
  117. current_app.logger.info(f'Adjusted points for {user_id}')
  118. return True
  119. except Exception as aperror:
  120. current_app.logger.error(f'Couldn\'t adjust points for {user_id}: {aperror}')
  121. return False
  122. def delete_user(db, user_id):
  123. try:
  124. db.execute(
  125. "DELETE FROM points WHERE id = ?",
  126. (user_id,)
  127. )
  128. db.commit()
  129. return True
  130. except Exception as du_error:
  131. current_app.logger.error(f'Couldn\'t change delete {user_id} from the database: {du_error}')
  132. return False
  133. def change_email(db, user_id, new_email): # For streamer to manually adjust a user's points
  134. try:
  135. ids = get_id_by_email(db, new_email)
  136. for user in ids:
  137. if user[0] != user_id: # If the found email belongs to a different user
  138. if 'temp' in user[0]: # If the email address belongs to a temp user
  139. existing_points = get_users_points(db, user_id)
  140. new_points = existing_points + user[1]
  141. if adjust_points(db, user_id, new_points): # Add points to correct user
  142. current_app.logger.info(f'Email was already in the database as a {user[0]}. Added points to {user_id}')
  143. if delete_user(db, user[0]): # Delete temp user
  144. current_app.logger.info(f'Removed temp user {user_id} from the database.')
  145. elif new_email is not None:
  146. current_app.logger.error(f'Couldn\'t change email address for {user_id}. {new_email} already belongs to {user[0]}')
  147. db.execute(
  148. "UPDATE points SET email = ? WHERE id = ?",
  149. (new_email, user_id,)
  150. )
  151. db.commit()
  152. return True
  153. except Exception as ce_error:
  154. current_app.logger.error(f'Couldn\'t change email address for {user_id}: {ce_error}')
  155. return False
  156. def spend_points(db, user_id, points): # A user spends points on a redeem
  157. try:
  158. db.execute(
  159. "UPDATE points SET points = points - ? WHERE id = ?",
  160. (points, user_id,)
  161. )
  162. db.commit()
  163. return True
  164. except Exception as sperror:
  165. current_app.logger.error(f'Couldn\'t spend {user_id}\'s {points} points: {sperror}')
  166. return False
  167. def refund_points(db, user_id, points): # Streamer refunds points for a redeem
  168. try:
  169. db.execute(
  170. "UPDATE points SET points = points + ? WHERE id = ?",
  171. (points, user_id,)
  172. )
  173. db.commit()
  174. current_app.logger.info(f'Refunded {points} points to {user_id}.')
  175. return True
  176. except Exception as rerror:
  177. current_app.logger.error(f'Couldn\'t refund {points} points for {user_id}: {rerror}')
  178. return False
  179. def user_in_points(db, user_id): # Check if a user is in the points database
  180. try:
  181. cursor = db.execute(
  182. "SELECT points FROM points WHERE id = ?",
  183. (user_id,)
  184. )
  185. if cursor.fetchone() is None:
  186. return False
  187. return True
  188. except Exception as uiperror:
  189. current_app.logger.error(f'Couldn\'t for {user_id} in points database: {uiperror}')
  190. def add_email_to_points(db, email, points): # Add an anonymous user and points to the database
  191. try:
  192. id = f'temp{random.randint(10000, 99999)}' # Create random id with "temp" prepended
  193. db.execute(
  194. "INSERT INTO points(id, points, user_authed, email) VALUES(?, ?, 0, ?)",
  195. (id, points, email)
  196. )
  197. db.commit()
  198. return True
  199. except Exception as aetperror:
  200. current_app.logger.error(f'Couldn\'t add {email} to points database: {aetperror}')
  201. return False
  202. def add_user_to_points(db, user_id, display_name, authed): # Add a user to the points database
  203. try:
  204. cursor = db.execute(
  205. "SELECT points, name, user_authed FROM points WHERE id = ?",
  206. (user_id,)
  207. )
  208. user = cursor.fetchone()
  209. if user is None: # Add the user if they're not in the database
  210. cursor.execute(
  211. "INSERT INTO points(id, name, points, user_authed) VALUES(?, ?, 10, ?)",
  212. (user_id, display_name, authed)
  213. )
  214. if user is not None and user[1] is None: # If their name has changed, change name in the database
  215. cursor.execute(
  216. "UPDATE points SET name = ?, user_authed = ? WHERE id = ?",
  217. (display_name, authed, user_id)
  218. )
  219. if user is not None and user[2] != authed: # If they've authenticated, update the database
  220. cursor.execute(
  221. "UPDATE points SET user_authed = ? WHERE id = ?",
  222. (authed, user_id)
  223. )
  224. db.commit()
  225. return True
  226. except Exception as autperror:
  227. current_app.logger.error(f'Couldn\'t add {user_id}/{display_name}/{authed} to database: {autperror}')
  228. return False
  229. def change_name(db, user_id, new_name): # Change a user name in the points database
  230. try:
  231. db.execute(
  232. "UPDATE points SET name = ? WHERE id = ?",
  233. (new_name, user_id)
  234. )
  235. db.commit()
  236. except Exception as cnerror:
  237. current_app.logger.error(f'Couldn\'t change name to {new_name} for {user_id}: {cnerror}')
  238. def remove_duplicates(db, user_id, username): # Remove duplicate usernames
  239. try:
  240. db.execute(
  241. "UPDATE points SET name = NULL WHERE name = ? AND NOT id = ?",
  242. (username, user_id)
  243. )
  244. db.commit()
  245. except Exception as rderror:
  246. current_app.logger.error(f'Couldn\'t remove duplicate username {username} for {user_id}: {rderror}')