msg.py 4.0 KB

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