msg.py 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. from sql import db
  2. from sql.cache import (read_msg_from_cache, write_msg_to_cache, delete_msg_from_cache,
  3. get_msg_cout_from_cache, write_msg_count_to_cache, delete_msg_count_from_cache,
  4. get_user_msg_count_from_cache, write_user_msg_count_to_cache,
  5. delete_all_user_msg_count_from_cache, delete_user_msg_count_from_cache)
  6. from typing import Optional
  7. def read_msg_list(limit: Optional[int] = None, offset: Optional[int] = None, show_secret: bool = False):
  8. if show_secret:
  9. if limit is not None and offset is not None:
  10. cur = db.search("SELECT MsgID "
  11. "FROM message_user "
  12. "ORDER BY UpdateTime DESC "
  13. "LIMIT %s "
  14. "OFFSET %s", limit, offset)
  15. else:
  16. cur = db.search("SELECT MsgID "
  17. "FROM message_user "
  18. "ORDER BY UpdateTime DESC")
  19. else:
  20. if limit is not None and offset is not None:
  21. cur = db.search("SELECT MsgID "
  22. "FROM message_user "
  23. "WHERE Secret=0 "
  24. "ORDER BY UpdateTime DESC "
  25. "LIMIT %s "
  26. "OFFSET %s", limit, offset)
  27. else:
  28. cur = db.search("SELECT MsgID "
  29. "FROM message_user "
  30. "WHERE Secret=0 "
  31. "ORDER BY UpdateTime DESC")
  32. if cur is None or cur.rowcount == 0:
  33. return []
  34. return [i[0] for i in cur.fetchall()]
  35. def create_msg(auth: int, content: str, secret: bool = False):
  36. delete_msg_count_from_cache()
  37. delete_user_msg_count_from_cache(auth)
  38. content = content.replace("'", "''")
  39. cur = db.insert("INSERT INTO message(Auth, Content, Secret) "
  40. "VALUES (%s, %s, %s)", auth, content, 1 if secret else 0)
  41. if cur is None or cur.rowcount != 1:
  42. return None
  43. return cur.lastrowid
  44. def read_msg(msg_id: int):
  45. res = read_msg_from_cache(msg_id)
  46. if res is not None:
  47. return res
  48. cur = db.search("SELECT Email, Content, UpdateTime, Secret "
  49. "FROM message_user "
  50. "WHERE MsgID=%s", msg_id)
  51. if cur is None or cur.rowcount == 0:
  52. return ["", "", "0", False]
  53. res = cur.fetchone()
  54. write_msg_to_cache(msg_id, *res)
  55. return res
  56. def delete_msg(msg_id: int):
  57. delete_msg_from_cache(msg_id)
  58. delete_msg_count_from_cache()
  59. delete_all_user_msg_count_from_cache()
  60. cur = db.delete("DELETE FROM message WHERE ID=%s", msg_id)
  61. if cur is None or cur.rowcount == 0:
  62. return False
  63. return True
  64. def get_msg_count():
  65. res = get_msg_cout_from_cache()
  66. if res is not None:
  67. return res
  68. cur = db.search("SELECT COUNT(*) FROM message")
  69. if cur is None or cur.rowcount == 0:
  70. return 0
  71. res = cur.fetchone()[0]
  72. write_msg_count_to_cache(res)
  73. return res
  74. def get_user_msg_count(user_id: int):
  75. res = get_user_msg_count_from_cache(user_id)
  76. if res is not None:
  77. return res
  78. cur = db.search("SELECT COUNT(*) FROM message WHERE Auth=%s", user_id)
  79. if cur is None or cur.rowcount == 0:
  80. return 0
  81. res = cur.fetchone()[0]
  82. write_user_msg_count_to_cache(user_id, res)
  83. return res