msg.py 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. from sql import db
  2. from sql.cache import (get_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. cur = db.insert("INSERT INTO message(Auth, Content, Secret) "
  39. "VALUES (%s, %s, %s)", auth, content, 1 if secret else 0)
  40. if cur is None or cur.rowcount != 1:
  41. return None
  42. return cur.lastrowid
  43. def read_msg(msg_id: int):
  44. res = get_msg_from_cache(msg_id)
  45. if res is not None:
  46. return res
  47. cur = db.search("SELECT Email, Content, UpdateTime, Secret "
  48. "FROM message_user "
  49. "WHERE MsgID=%s", msg_id)
  50. if cur is None or cur.rowcount == 0:
  51. return ["", "", "0", False]
  52. res = cur.fetchone()
  53. write_msg_to_cache(msg_id, *res)
  54. return res
  55. def delete_msg(msg_id: int):
  56. delete_msg_from_cache(msg_id)
  57. delete_msg_count_from_cache()
  58. delete_all_user_msg_count_from_cache()
  59. cur = db.delete("DELETE FROM message WHERE ID=%s", msg_id)
  60. if cur is None or cur.rowcount == 0:
  61. return False
  62. return True
  63. def get_msg_count():
  64. res = get_msg_cout_from_cache()
  65. if res is not None:
  66. return res
  67. cur = db.search("SELECT COUNT(*) FROM message")
  68. if cur is None or cur.rowcount == 0:
  69. return 0
  70. res = cur.fetchone()[0]
  71. write_msg_count_to_cache(res)
  72. return res
  73. def get_user_msg_count(user_id: int):
  74. res = get_user_msg_count_from_cache(user_id)
  75. if res is not None:
  76. return res
  77. cur = db.search("SELECT COUNT(*) FROM message WHERE Auth=%s", user_id)
  78. if cur is None or cur.rowcount == 0:
  79. return 0
  80. res = cur.fetchone()[0]
  81. write_user_msg_count_to_cache(user_id, res)
  82. return res