-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMessageManager.py
More file actions
369 lines (321 loc) · 14.1 KB
/
MessageManager.py
File metadata and controls
369 lines (321 loc) · 14.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
# MessageManager.py
import uuid
import datetime
import asyncio
from typing import Dict, List, Optional, Any
from pymongo import AsyncMongoClient
from enums import MessageType
import logging
import config
uri = config.Config.mongo_uri
class MessageManager:
"""消息管理器"""
def __init__(self):
self.logger = logging.getLogger("MessageManager")
self.dbclient = AsyncMongoClient(uri)
self.db_messages = self.dbclient["IM"]["messages"]
# 创建索引
# asyncio.run(self._create_indexes())
async def initialize(self):
await self._create_indexes()
async def _create_indexes(self):
"""创建数据库索引"""
try:
# 复合索引,支持快速查询
await self.db_messages.create_index([("sender_id", 1), ("receiver_id", 1), ("timestamp", -1)])
await self.db_messages.create_index([("group_id", 1), ("timestamp", -1)])
await self.db_messages.create_index([("sender_id", 1), ("timestamp", -1)])
await self.db_messages.create_index([("receiver_id", 1), ("timestamp", -1)])
await self.db_messages.create_index("message_id", unique=True)
await self.db_messages.create_index("timestamp")
self.logger.debug("消息管理器索引创建完成")
except Exception as e:
self.logger.error(f"创建索引失败: {e}")
async def save_private_message(self, message_data: Dict[str, Any]) -> str:
"""保存私聊消息"""
try:
message_id = message_data.get("message_id", str(uuid.uuid4()))
timestamp = message_data.get("timestamp", int(datetime.datetime.now().timestamp()))
message_record = {
"message_id": message_id,
"sender_id": message_data["sender_id"],
"receiver_id": message_data["receiver_id"],
"type": message_data.get("type", MessageType.TEXT.value),
"content": message_data["content"],
"timestamp": timestamp,
"client_msg_id": message_data.get("client_msg_id"),
"delivered": message_data.get("delivered", False),
"read": message_data.get("read", False),
"created_at": datetime.datetime.now(),
"is_group": False
}
await self.db_messages.insert_one(message_record)
self.logger.debug(f"保存私聊消息: {message_id}")
return message_id
except DeprecationWarning as e:
self.logger.error(f"保存私聊消息失败: {e}")
return ""
async def save_group_message(self, message_data: Dict[str, Any]) -> str:
"""保存群聊消息"""
try:
message_id = message_data.get("message_id", str(uuid.uuid4()))
timestamp = message_data.get("timestamp", int(datetime.datetime.now().timestamp()))
message_record = {
"message_id": message_id,
"sender_id": message_data.get("sender_id", message_data.get("data").get("sender_info").get("user_id")),
"group_id": message_data["data"]["group_id"],
"type": message_data.get("type", MessageType.TEXT.value),
"content": message_data["data"]["content"],
"timestamp": timestamp,
"client_msg_id": message_data.get("client_msg_id"),
"at_users": message_data.get("at_users", []),
"at_all": message_data.get("at_all", False),
"created_at": datetime.datetime.now(),
"is_group": True
}
result = await self.db_messages.insert_one(message_record)
self.logger.debug(f"保存群聊消息: {message_id} 群组: {message_data['data']['group_id']}")
return message_id
except DeprecationWarning as e:
self.logger.error(f"保存群聊消息失败: {e}")
return ""
async def get_private_messages(self, user1_id: int, user2_id: int,
limit: int = 50, last_msg_id: Optional[str] = None,
start_time: Optional[int] = None,
end_time: Optional[int] = None) -> List[Dict[str, Any]]:
"""获取私聊历史消息"""
try:
# 构建查询条件
query = {
"is_group": False,
"$or": [
{"sender_id": user1_id, "receiver_id": user2_id},
{"sender_id": user2_id, "receiver_id": user1_id}
]
}
# 添加时间范围条件
if start_time or end_time:
time_query = {}
if start_time:
time_query["$gte"] = start_time
if end_time:
time_query["$lte"] = end_time
if time_query:
query["timestamp"] = time_query
# 如果指定了最后一条消息ID,获取该消息的时间戳
if last_msg_id:
last_msg = await self.db_messages.find_one({"message_id": last_msg_id})
if last_msg:
query["timestamp"] = {"$lt": last_msg["timestamp"]}
# 查询消息
cursor = self.db_messages.find(query).sort("timestamp", -1).limit(limit)
messages = await cursor.to_list(length=limit)
# 转换为标准格式
result = []
for msg in reversed(messages): # 反转以获取正序时间
result.append({
"message_id": msg["message_id"],
"sender_id": msg["sender_id"],
"receiver_id": msg["receiver_id"],
"type": msg["type"],
"content": msg["content"],
"timestamp": msg["timestamp"],
"client_msg_id": msg.get("client_msg_id"),
"delivered": msg.get("delivered", False),
"read": msg.get("read", False)
})
return result
except Exception as e:
self.logger.error(f"获取私聊历史消息失败: {e}")
return []
async def get_group_messages(self, group_id: str, limit: int = 50,
last_msg_id: Optional[str] = None,
start_time: Optional[int] = None,
end_time: Optional[int] = None) -> List[Dict[str, Any]]:
"""获取群聊历史消息"""
try:
# 构建查询条件
query = {
"is_group": True,
"group_id": group_id
}
# 添加时间范围条件
if start_time or end_time:
time_query = {}
if start_time:
time_query["$gte"] = start_time
if end_time:
time_query["$lte"] = end_time
if time_query:
query["timestamp"] = time_query
# 如果指定了最后一条消息ID,获取该消息的时间戳
if last_msg_id:
last_msg = await self.db_messages.find_one({"message_id": last_msg_id})
if last_msg:
query["timestamp"] = {"$lt": last_msg["timestamp"]}
# 查询消息
cursor = self.db_messages.find(query).sort("timestamp", -1).limit(limit)
messages = await cursor.to_list(length=limit)
# 转换为标准格式
result = []
for msg in reversed(messages): # 反转以获取正序时间
result.append({
"message_id": msg["message_id"],
"group_id": msg["group_id"],
"sender_id": msg["sender_id"],
"type": msg["type"],
"content": msg["content"],
"timestamp": msg["timestamp"],
"client_msg_id": msg.get("client_msg_id"),
"at_users": msg.get("at_users", []),
"at_all": msg.get("at_all", False)
})
return result
except Exception as e:
self.logger.error(f"获取群聊历史消息失败: {e}")
return []
async def get_user_messages_by_time(self, user_id: int,
start_time: int,
end_time: int,
message_type: Optional[str] = None) -> List[Dict[str, Any]]:
"""按时间段获取用户的所有消息(包括发送和接收)"""
try:
query = {
"timestamp": {"$gte": start_time, "$lte": end_time},
"$or": [
{"sender_id": user_id},
{"receiver_id": user_id}
]
}
# 添加消息类型筛选
if message_type:
query["type"] = message_type
cursor = self.db_messages.find(query).sort("timestamp", 1)
messages = await cursor.to_list(length=None)
result = []
for msg in messages:
message_info = {
"message_id": msg["message_id"],
"sender_id": msg["sender_id"],
"type": msg["type"],
"content": msg["content"],
"timestamp": msg["timestamp"],
"client_msg_id": msg.get("client_msg_id"),
"is_group": msg["is_group"]
}
if msg["is_group"]:
message_info["group_id"] = msg["group_id"]
else:
message_info["receiver_id"] = msg["receiver_id"]
result.append(message_info)
return result
except Exception as e:
self.logger.error(f"按时间段获取消息失败: {e}")
return []
async def mark_message_delivered(self, message_id: str) -> bool:
"""标记消息为已送达"""
try:
result = await self.db_messages.update_one(
{"message_id": message_id},
{"$set": {"delivered": True}}
)
return result.modified_count > 0
except Exception as e:
self.logger.error(f"标记消息为已送达失败: {e}")
return False
async def mark_message_read(self, message_id: str) -> bool:
"""标记消息为已读"""
try:
result = await self.db_messages.update_one(
{"message_id": message_id},
{"$set": {"read": True}}
)
return result.modified_count > 0
except Exception as e:
self.logger.error(f"标记消息为已读失败: {e}")
return False
async def get_unread_count(self, user_id: int) -> int:
"""获取用户未读消息数量"""
try:
count = await self.db_messages.count_documents({
"receiver_id": user_id,
"read": False,
"is_group": False
})
return count
except Exception as e:
self.logger.error(f"获取未读消息数量失败: {e}")
return 0
async def delete_message(self, message_id: str, user_id: int) -> bool:
"""删除消息(软删除)"""
try:
result = await self.db_messages.update_one(
{
"message_id": message_id,
"$or": [
{"sender_id": user_id},
{"receiver_id": user_id}
]
},
{"$set": {"deleted": True, "deleted_at": datetime.datetime.now()}}
)
return result.modified_count > 0
except Exception as e:
self.logger.error(f"删除消息失败: {e}")
return False
async def get_message_statistics(self, user_id: Optional[int] = None,
group_id: Optional[str] = None) -> Dict[str, Any]:
"""获取消息统计信息"""
try:
pipeline = []
# 构建查询条件
match_condition = {}
if user_id:
match_condition["$or"] = [
{"sender_id": user_id},
{"receiver_id": user_id}
]
if group_id:
match_condition["group_id"] = group_id
if match_condition:
pipeline.append({"$match": match_condition})
# 聚合统计
pipeline.extend([
{
"$group": {
"_id": {
"year": {"$year": "$created_at"},
"month": {"$month": "$created_at"},
"day": {"$dayOfMonth": "$created_at"}
},
"count": {"$sum": 1},
"private_count": {
"$sum": {"$cond": [{"$eq": ["$is_group", False]}, 1, 0]}
},
"group_count": {
"$sum": {"$cond": [{"$eq": ["$is_group", True]}, 1, 0]}
}
}
},
{"$sort": {"_id.year": 1, "_id.month": 1, "_id.day": 1}}
])
cursor = await self.db_messages.aggregate(pipeline)
stats = await cursor.to_list(length=None)
# 计算总数
total = 0
for stat in stats:
total += stat["count"]
return {
"total_messages": total,
"daily_stats": stats,
"private_total": sum(stat["private_count"] for stat in stats),
"group_total": sum(stat["group_count"] for stat in stats)
}
except Exception as e:
self.logger.error(f"获取消息统计失败: {e}")
return {
"total_messages": 0,
"daily_stats": [],
"private_total": 0,
"group_total": 0
}