msg.py 3.2 KB

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