user_handlers.py 10 KB

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