user_handlers.py 9.7 KB

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