|
@@ -692,6 +692,187 @@ export class ChatRecordsService {
|
|
|
private delay(ms: number): Promise<void> {
|
|
private delay(ms: number): Promise<void> {
|
|
|
return new Promise(resolve => setTimeout(resolve, ms));
|
|
return new Promise(resolve => setTimeout(resolve, ms));
|
|
|
}
|
|
}
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 根据peerId获取最近的聊天记录(仅文本)
|
|
|
|
|
+ * @param peerId 对话ID
|
|
|
|
|
+ * @param limit 获取消息数量,默认30条
|
|
|
|
|
+ * @returns 处理后的聊天记录数据的JSON字符串
|
|
|
|
|
+ */
|
|
|
|
|
+ public async getRecentChatRecords(peerId: string | number, limit: number = 30): Promise<string> {
|
|
|
|
|
+ try {
|
|
|
|
|
+ // 获取历史消息
|
|
|
|
|
+ const historyResult = await rootScope.managers.appMessagesManager.getHistory({
|
|
|
|
|
+ peerId: typeof peerId === 'string' ? parseInt(peerId) : peerId,
|
|
|
|
|
+ limit: limit,
|
|
|
|
|
+ offsetId: 0,
|
|
|
|
|
+ addOffset: 0,
|
|
|
|
|
+ searchType: 'uncached'
|
|
|
|
|
+ });
|
|
|
|
|
+
|
|
|
|
|
+ if(!historyResult || !historyResult.messages || historyResult.messages.length === 0) {
|
|
|
|
|
+ return '[]';
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ // 处理消息
|
|
|
|
|
+ const processedMessages = await this.processTextOnlyMessages(historyResult.messages, peerId.toString());
|
|
|
|
|
+
|
|
|
|
|
+ // 返回JSON字符串
|
|
|
|
|
+ return JSON.stringify(processedMessages, null, 2);
|
|
|
|
|
+ } catch(error) {
|
|
|
|
|
+ this.log('获取聊天记录失败:', error);
|
|
|
|
|
+ throw error;
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 处理消息(仅文本,跳过图片处理)
|
|
|
|
|
+ */
|
|
|
|
|
+ private async processTextOnlyMessages(messages: any[], peerId: string): Promise<ChatRecordData[]> {
|
|
|
|
|
+ const currentUserId = rootScope.myId;
|
|
|
|
|
+ const processedMessages: ChatRecordData[] = [];
|
|
|
|
|
+
|
|
|
|
|
+ for(const message of messages) {
|
|
|
|
|
+ try {
|
|
|
|
|
+ let textContent = '';
|
|
|
|
|
+ let mediaType = 'None';
|
|
|
|
|
+ let date = '';
|
|
|
|
|
+ let isFromMe = false;
|
|
|
|
|
+ let senderId = '';
|
|
|
|
|
+ let senderName = '';
|
|
|
|
|
+
|
|
|
|
|
+ if(message) {
|
|
|
|
|
+ // 判断是否是自己发送的消息
|
|
|
|
|
+ let fromPeerId = null;
|
|
|
|
|
+
|
|
|
|
|
+ // 检查各种可能的发送者字段
|
|
|
|
|
+ if('from_id' in message && message.from_id) {
|
|
|
|
|
+ // 处理from_id可能是对象的情况
|
|
|
|
|
+ if(typeof message.from_id === 'object' && message.from_id) {
|
|
|
|
|
+ if('user_id' in message.from_id) {
|
|
|
|
|
+ fromPeerId = message.from_id.user_id;
|
|
|
|
|
+ } else if('id' in message.from_id) {
|
|
|
|
|
+ fromPeerId = message.from_id.id;
|
|
|
|
|
+ } else if('channel_id' in message.from_id) {
|
|
|
|
|
+ fromPeerId = message.from_id.channel_id;
|
|
|
|
|
+ } else if('chat_id' in message.from_id) {
|
|
|
|
|
+ fromPeerId = message.from_id.chat_id;
|
|
|
|
|
+ }
|
|
|
|
|
+ } else {
|
|
|
|
|
+ fromPeerId = message.from_id;
|
|
|
|
|
+ }
|
|
|
|
|
+ } else if('fromId' in message && message.fromId) {
|
|
|
|
|
+ fromPeerId = message.fromId;
|
|
|
|
|
+ } else if('from' in message && message.from) {
|
|
|
|
|
+ if(typeof message.from === 'object' && message.from.id) {
|
|
|
|
|
+ fromPeerId = message.from.id;
|
|
|
|
|
+ }
|
|
|
|
|
+ } else if('peer_id' in message && message.peer_id) {
|
|
|
|
|
+ fromPeerId = message.peer_id;
|
|
|
|
|
+ } else if('sender_id' in message && message.sender_id) {
|
|
|
|
|
+ fromPeerId = message.sender_id;
|
|
|
|
|
+ } else if('senderId' in message && message.senderId) {
|
|
|
|
|
+ fromPeerId = message.senderId;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ if(fromPeerId) {
|
|
|
|
|
+ // 尝试不同的ID比较方法
|
|
|
|
|
+ const fromPeerIdStr = fromPeerId.toString();
|
|
|
|
|
+ const currentUserIdStr = currentUserId.toString();
|
|
|
|
|
+
|
|
|
|
|
+ // 直接比较
|
|
|
|
|
+ isFromMe = fromPeerId === currentUserId;
|
|
|
|
|
+
|
|
|
|
|
+ // 如果直接比较失败,尝试字符串比较
|
|
|
|
|
+ if(!isFromMe) {
|
|
|
|
|
+ isFromMe = fromPeerIdStr === currentUserIdStr;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ // 如果还是失败,尝试转换为数字比较
|
|
|
|
|
+ if(!isFromMe) {
|
|
|
|
|
+ const fromPeerIdNum = parseInt(fromPeerIdStr);
|
|
|
|
|
+ const currentUserIdNum = parseInt(currentUserIdStr);
|
|
|
|
|
+ if(!isNaN(fromPeerIdNum) && !isNaN(currentUserIdNum)) {
|
|
|
|
|
+ isFromMe = fromPeerIdNum === currentUserIdNum;
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ senderId = fromPeerIdStr;
|
|
|
|
|
+
|
|
|
|
|
+ // 获取发送者名称
|
|
|
|
|
+ if(isFromMe) {
|
|
|
|
|
+ senderName = '我';
|
|
|
|
|
+ } else {
|
|
|
|
|
+ try {
|
|
|
|
|
+ if(await rootScope.managers.appPeersManager.isUser(fromPeerId)) {
|
|
|
|
|
+ const user = await rootScope.managers.appUsersManager.getUser(fromPeerId.toUserId());
|
|
|
|
|
+ if(user) {
|
|
|
|
|
+ const firstName = user.first_name || '';
|
|
|
|
|
+ const lastName = user.last_name || '';
|
|
|
|
|
+ const tgName = `${firstName} ${lastName}`.trim();
|
|
|
|
|
+
|
|
|
|
|
+ if(user.username) {
|
|
|
|
|
+ senderName = `@${user.username}`;
|
|
|
|
|
+ } else if(tgName) {
|
|
|
|
|
+ senderName = tgName;
|
|
|
|
|
+ } else {
|
|
|
|
|
+ senderName = '未知用户';
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ } else if(await rootScope.managers.appPeersManager.isAnyChat(fromPeerId)) {
|
|
|
|
|
+ const chat = await rootScope.managers.appChatsManager.getChat(fromPeerId.toChatId());
|
|
|
|
|
+ senderName = chat?.title || '未知群组';
|
|
|
|
|
+ }
|
|
|
|
|
+ } catch(error) {
|
|
|
|
|
+ senderName = '未知用户';
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ } else {
|
|
|
|
|
+ senderName = '系统';
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ // 提取文本内容
|
|
|
|
|
+ if('message' in message && message.message) {
|
|
|
|
|
+ textContent = message.message;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ // 提取媒体类型
|
|
|
|
|
+ if('media' in message && message.media) {
|
|
|
|
|
+ mediaType = message.media._.replace('messageMedia', '');
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ // 提取日期
|
|
|
|
|
+ if(message.date) {
|
|
|
|
|
+ date = new Date(message.date * 1000).toLocaleString();
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ processedMessages.push({
|
|
|
|
|
+ id: (message.mid || message.id).toString(),
|
|
|
|
|
+ text: textContent,
|
|
|
|
|
+ mediaType: mediaType,
|
|
|
|
|
+ date: date,
|
|
|
|
|
+ imagePath: null, // 始终设置为null,跳过图片
|
|
|
|
|
+ isFromMe: isFromMe,
|
|
|
|
|
+ senderId: senderId,
|
|
|
|
|
+ senderName: senderName
|
|
|
|
|
+ });
|
|
|
|
|
+ } catch(error) {
|
|
|
|
|
+ processedMessages.push({
|
|
|
|
|
+ id: (message.mid || message.id || 'unknown').toString(),
|
|
|
|
|
+ text: '[消息处理失败]',
|
|
|
|
|
+ mediaType: 'Error',
|
|
|
|
|
+ date: new Date().toLocaleString(),
|
|
|
|
|
+ imagePath: null,
|
|
|
|
|
+ isFromMe: false,
|
|
|
|
|
+ senderId: '',
|
|
|
|
|
+ senderName: '系统'
|
|
|
|
|
+ });
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ return processedMessages;
|
|
|
|
|
+ }
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
export const chatRecordsService = new ChatRecordsService();
|
|
export const chatRecordsService = new ChatRecordsService();
|