|
|
@@ -873,6 +873,91 @@ export class ChatRecordsService {
|
|
|
|
|
|
return processedMessages;
|
|
|
}
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取简化格式的聊天记录
|
|
|
+ * @param peerId 对话ID
|
|
|
+ * @param limit 获取消息数量,默认20条
|
|
|
+ * @returns 简化格式的聊天记录JSON字符串
|
|
|
+ */
|
|
|
+ public async getSimplifiedChatRecords(peerId: string | number, limit: number = 20): 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 currentUserId = rootScope.myId;
|
|
|
+ const simplifiedMessages: {text: string, isFromMe: boolean, senderId: string}[] = [];
|
|
|
+
|
|
|
+ for(const message of historyResult.messages) {
|
|
|
+ try {
|
|
|
+ let textContent = '';
|
|
|
+ let isFromMe = false;
|
|
|
+ let senderId = '';
|
|
|
+
|
|
|
+ // 提取文本内容
|
|
|
+ if('message' in message && message.message) {
|
|
|
+ textContent = message.message;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 跳过空消息
|
|
|
+ if(!textContent) {
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 判断是否是自己发送的消息
|
|
|
+ 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 {
|
|
|
+ fromPeerId = message.from_id;
|
|
|
+ }
|
|
|
+ } else if('fromId' in message && message.fromId) {
|
|
|
+ fromPeerId = message.fromId;
|
|
|
+ }
|
|
|
+
|
|
|
+ if(fromPeerId) {
|
|
|
+ const fromPeerIdStr = fromPeerId.toString();
|
|
|
+ const currentUserIdStr = currentUserId.toString();
|
|
|
+
|
|
|
+ // 判断是否是自己发送的消息
|
|
|
+ isFromMe = fromPeerId === currentUserId || fromPeerIdStr === currentUserIdStr;
|
|
|
+ senderId = fromPeerIdStr;
|
|
|
+ }
|
|
|
+
|
|
|
+ simplifiedMessages.push({
|
|
|
+ text: textContent,
|
|
|
+ isFromMe: isFromMe,
|
|
|
+ senderId: senderId
|
|
|
+ });
|
|
|
+ } catch(error) {
|
|
|
+ // 处理错误,跳过此消息
|
|
|
+ }
|
|
|
+ }
|
|
|
+ // 返回JSON字符串
|
|
|
+ return JSON.stringify(simplifiedMessages);
|
|
|
+ } catch(error) {
|
|
|
+ this.log('获取简化聊天记录失败:', error);
|
|
|
+ return '[]';
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
export const chatRecordsService = new ChatRecordsService();
|