user_handlers.py 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  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.args[0]}')
  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 Error as guperror:
  28. current_app.logger.error(f'Couldn\'t look up points for {user_id}: {guperror.args[0]}')
  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 Error as gecerror:
  37. current_app.logger.error(f'Couldn\'t look up points for {user_id}: {gecerror.args[0]}')
  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 Error as gaubeerror:
  47. current_app.logger.error(f'Couldn\'t look up user id for {email} by email: {gaubeerror.args[0]}')
  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 Error as gaubnerror:
  57. current_app.logger.error(f'Couldn\'t look up points for {username} by username: {gaubnerror.args[0]}')
  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 Error as gauwuierror:
  67. current_app.logger.error(f'Couldn\'t look up points for {user_id} by user_id: {gauwuierror.args[0]}')
  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 Error as gauerror:
  76. current_app.logger.error(f'Couldn\'t get all users\' points: {gauerror.args[0]}')
  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 Error as acperror:
  86. current_app.logger.error(f'Couldn\'t give {points} points to {user_id}: {acperror.args[0]}')
  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 Error as secerror:
  97. current_app.logger.error(f'Couldn\'t set reg code \"{reg_code}\" for {user_id}: {secerror.args[0]}')
  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 Error as decerror:
  108. current_app.logger.error(f'Couldn\'t remove reg code for {user_id}: {decerror.args[0]}')
  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. return True
  118. except Error as aperror:
  119. current_app.logger.error(f'Couldn\'t adjust points for {user_id}: {aperror.args[0]}')
  120. return False
  121. def delete_user(db, user_id):
  122. try:
  123. db.execute(
  124. "DELETE FROM points WHERE id = ?",
  125. (user_id,)
  126. )
  127. db.commit()
  128. return True
  129. except Error as du_error:
  130. current_app.logger.error(f'Couldn\'t change delete {user_id} from the database: {du_error.args[0]}')
  131. return False
  132. def change_email(db, user_id, new_email): # For streamer to manually adjust a user's points
  133. try:
  134. ids = get_id_by_email(db, new_email)
  135. for user in ids:
  136. if user[0] != user_id: # If the found email belongs to a different user
  137. if 'temp' in user[0]: # If the email address belongs to a temp user
  138. existing_points = get_users_points(db, user_id)
  139. new_points = existing_points + user[1]
  140. if adjust_points(db, user_id, new_points): # Add points to correct user
  141. current_app.logger.info(f'Email was already in the database as a {user[0]}. Added points to {user_id}')
  142. if delete_user(db, user[0]): # Delete temp user
  143. current_app.logger.info(f'Removed temp user {user_id} from the database.')
  144. elif new_email is not None:
  145. current_app.logger.error(f'Couldn\'t change email address for {user_id}. {new_email} already belongs to {user[0]}')
  146. db.execute(
  147. "UPDATE points SET email = ? WHERE id = ?",
  148. (new_email, user_id,)
  149. )
  150. db.commit()
  151. return True
  152. except Error as ce_error:
  153. current_app.logger.error(f'Couldn\'t change email address for {user_id}: {ce_error.args[0]}')
  154. return False
  155. def spend_points(db, user_id, points): # A user spends points on a redeem
  156. try:
  157. db.execute(
  158. "UPDATE points SET points = points - ? WHERE id = ?",
  159. (points, user_id,)
  160. )
  161. db.commit()
  162. return True
  163. except Error as sperror:
  164. current_app.logger.error(f'Couldn\'t spend {user_id}\'s {points} points: {sperror.args[0]}')
  165. return False
  166. def refund_points(db, user_id, points): # Streamer refunds points for a redeem
  167. try:
  168. db.execute(
  169. "UPDATE points SET points = points + ? WHERE id = ?",
  170. (points, user_id,)
  171. )
  172. db.commit()
  173. return True
  174. except Error as rerror:
  175. current_app.logger.error(f'Couldn\'t refund {points} points for {user_id}: {rerror.args[0]}')
  176. return False
  177. def user_in_points(db, user_id): # Check if a user is in the points database
  178. try:
  179. cursor = db.execute(
  180. "SELECT points FROM points WHERE id = ?",
  181. (user_id,)
  182. )
  183. if cursor.fetchone() is None:
  184. return False
  185. return True
  186. except Error as uiperror:
  187. current_app.logger.error(f'Couldn\'t for {user_id} in points database: {uiperror.args[0]}')
  188. def add_email_to_points(db, email, points): # Add an anonymous user and points to the database
  189. try:
  190. id = f'temp{random.randint(10000, 99999)}' # Create random id with "temp" prepended
  191. db.execute(
  192. "INSERT INTO points(id, points, user_authed, email) VALUES(?, ?, 0, ?)",
  193. (id, points, email)
  194. )
  195. db.commit()
  196. return True
  197. except Error as aetperror:
  198. current_app.logger.error(f'Couldn\'t add {email} to points database: {aetperror.args[0]}')
  199. return False
  200. def add_user_to_points(db, user_id, display_name, authed): # Add a user to the points database
  201. try:
  202. cursor = db.execute(
  203. "SELECT points, name, user_authed FROM points WHERE id = ?",
  204. (user_id,)
  205. )
  206. user = cursor.fetchone()
  207. if user is None: # Add the user if they're not in the database
  208. cursor.execute(
  209. "INSERT INTO points(id, name, points, user_authed) VALUES(?, ?, 10, ?)",
  210. (user_id, display_name, authed)
  211. )
  212. if user is not None and user[1] is None: # If their name has changed, change name in the database
  213. cursor.execute(
  214. "UPDATE points SET name = ?, user_authed = ? WHERE id = ?",
  215. (display_name, authed, user_id)
  216. )
  217. if user is not None and user[2] != authed: # If they've authenticated, update the database
  218. cursor.execute(
  219. "UPDATE points SET user_authed = ? WHERE id = ?",
  220. (authed, user_id)
  221. )
  222. db.commit()
  223. return True
  224. except Error as autperror:
  225. current_app.logger.error(f'Couldn\'t add {user_id}/{display_name}/{authed} to database: {autperror.args[0]}')
  226. return False
  227. def change_name(db, user_id, new_name): # Change a user name in the points database
  228. try:
  229. db.execute(
  230. "UPDATE points SET name = ? WHERE id = ?",
  231. (new_name, user_id)
  232. )
  233. db.commit()
  234. except Error as cnerror:
  235. current_app.logger.error(f'Couldn\'t change name to {new_name} for {user_id}: {cnerror.args[0]}')
  236. def remove_duplicates(db, user_id, username): # Remove duplicate usernames
  237. try:
  238. db.execute(
  239. "UPDATE points SET name = NULL WHERE name = ? AND NOT id = ?",
  240. (username, user_id)
  241. )
  242. db.commit()
  243. except Error as rderror:
  244. current_app.logger.error(f'Couldn\'t remove duplicate username {username} for {user_id}: {rderror.args[0]}')