UNPKG

imessage-ts

Version:

TypeScript library for interacting with iMessage on macOS - send messages, monitor chats, and automate responses

131 lines 5.22 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.ConversationRepository = void 0; /** * Repository for accessing conversations from the iMessage database */ class ConversationRepository { constructor(database) { this.database = database; } /** * Get all conversations */ async getConversations(options = {}) { const { limit = 50, offset = 0, searchTerm, onlyGroups, onlyWithUnread, } = options; let query = ` SELECT c.ROWID as id, c.guid, c.chat_identifier as chatIdentifier, c.display_name as displayName, (SELECT count(*) FROM chat_message_join cmj INNER JOIN message m ON cmj.message_id = m.ROWID WHERE cmj.chat_id = c.ROWID AND m.is_read = 0 AND m.is_from_me = 0) as unreadCount, (SELECT datetime(m.date / 1000000000 + 978307200, 'unixepoch', 'localtime') FROM message m INNER JOIN chat_message_join cmj ON m.ROWID = cmj.message_id WHERE cmj.chat_id = c.ROWID ORDER BY m.date DESC LIMIT 1) as lastActivity, (SELECT count(*) > 1 FROM chat_handle_join chj WHERE chj.chat_id = c.ROWID) as isGroup FROM chat c WHERE 1=1 `; const params = {}; if (searchTerm) { query += ` AND (c.chat_identifier LIKE $searchTerm OR c.display_name LIKE $searchTerm)`; params.searchTerm = `%${searchTerm}%`; } if (onlyGroups) { query += ` AND (SELECT count(*) FROM chat_handle_join chj WHERE chj.chat_id = c.ROWID) > 1`; } if (onlyWithUnread) { query += ` AND (SELECT count(*) FROM chat_message_join cmj INNER JOIN message m ON cmj.message_id = m.ROWID WHERE cmj.chat_id = c.ROWID AND m.is_read = 0 AND m.is_from_me = 0) > 0`; } query += ` ORDER BY lastActivity DESC LIMIT $limit OFFSET $offset`; params.limit = limit; params.offset = offset; const conversations = await this.database.query(query, params); // Fetch participants for each conversation for (const conversation of conversations) { conversation.participants = await this.getConversationParticipants(conversation.id); } return conversations; } /** * Get a conversation by ID */ async getConversationById(id) { const query = ` SELECT c.ROWID as id, c.guid, c.chat_identifier as chatIdentifier, c.display_name as displayName, (SELECT count(*) FROM chat_message_join cmj INNER JOIN message m ON cmj.message_id = m.ROWID WHERE cmj.chat_id = c.ROWID AND m.is_read = 0 AND m.is_from_me = 0) as unreadCount, (SELECT datetime(m.date / 1000000000 + 978307200, 'unixepoch', 'localtime') FROM message m INNER JOIN chat_message_join cmj ON m.ROWID = cmj.message_id WHERE cmj.chat_id = c.ROWID ORDER BY m.date DESC LIMIT 1) as lastActivity, (SELECT count(*) > 1 FROM chat_handle_join chj WHERE chj.chat_id = c.ROWID) as isGroup FROM chat c WHERE c.ROWID = $id `; const conversation = await this.database.queryOne(query, { id }); if (conversation) { conversation.participants = await this.getConversationParticipants(conversation.id); } return conversation; } /** * Get a conversation by chat identifier (phone number, email, or group ID) */ async getConversationByChatIdentifier(chatIdentifier) { const query = ` SELECT c.ROWID as id, c.guid, c.chat_identifier as chatIdentifier, c.display_name as displayName, (SELECT count(*) FROM chat_message_join cmj INNER JOIN message m ON cmj.message_id = m.ROWID WHERE cmj.chat_id = c.ROWID AND m.is_read = 0 AND m.is_from_me = 0) as unreadCount, (SELECT datetime(m.date / 1000000000 + 978307200, 'unixepoch', 'localtime') FROM message m INNER JOIN chat_message_join cmj ON m.ROWID = cmj.message_id WHERE cmj.chat_id = c.ROWID ORDER BY m.date DESC LIMIT 1) as lastActivity, (SELECT count(*) > 1 FROM chat_handle_join chj WHERE chj.chat_id = c.ROWID) as isGroup FROM chat c WHERE c.chat_identifier = $chatIdentifier `; const conversation = await this.database.queryOne(query, { chatIdentifier }); if (conversation) { conversation.participants = await this.getConversationParticipants(conversation.id); } return conversation; } /** * Get participants in a conversation */ async getConversationParticipants(conversationId) { const query = ` SELECT h.ROWID as id, h.id as handle, h.service, h.country FROM handle h INNER JOIN chat_handle_join chj ON h.ROWID = chj.handle_id WHERE chj.chat_id = $conversationId `; return this.database.query(query, { conversationId }); } } exports.ConversationRepository = ConversationRepository; //# sourceMappingURL=conversation-repository.js.map