UNPKG

@juzi/wechaty-puppet-whatsapp

Version:
334 lines 13.4 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); __setModuleDefault(result, mod); return result; }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.CacheManager = void 0; const path = __importStar(require("path")); const fs = __importStar(require("fs-extra")); const os = __importStar(require("os")); const flash_store_1 = require("flash-store"); const config_js_1 = require("../config.js"); const error_type_js_1 = require("../exception/error-type.js"); const whatsapp_error_js_1 = __importDefault(require("../exception/whatsapp-error.js")); require("@juzi/wechaty-puppet/payloads"); const PRE = 'CacheManager'; class CacheManager { /** * ************************************************************************ * Static Methods * ************************************************************************ */ static _instance; static get Instance() { if (!this._instance) { throw (0, whatsapp_error_js_1.default)(error_type_js_1.WA_ERROR_TYPE.ERR_NO_CACHE, 'no instance'); } return this._instance; } static async init(userId) { if (this._instance) { return; } this._instance = new CacheManager(); await this._instance.initCache(userId); } static async release() { if (!this._instance) { return; } await this._instance.releaseCache(); this._instance = undefined; } /** * ************************************************************************ * Instance Methods * ************************************************************************ */ // Static cache, won't change over time cacheMessageRawPayload; cacheContactOrRoomRawPayload; cacheRoomMemberIdList; cacheRoomInvitationRawPayload; cacheLatestMessageTimestampForChat; cacheFriendshipRawPayload; /** * ------------------------------- * Message Cache Section * -------------------------------- */ async getMessageRawPayload(id) { const cache = this.getMessageCache(); return cache.get(id); } async setMessageRawPayload(id, payload) { const cache = this.getMessageCache(); // @ts-ignore client is in implementation but not in interface const { client, ...rest } = payload; await cache.set(id, rest); } deleteMessage(id) { const cache = this.getMessageCache(); return cache.delete(id); } getMessageCache() { if (!this.cacheMessageRawPayload) { throw (0, whatsapp_error_js_1.default)(error_type_js_1.WA_ERROR_TYPE.ERR_NO_CACHE, 'getMessageCache() has no cache'); } return this.cacheMessageRawPayload; } /** * ------------------------------- * Contact And Room Cache Section * -------------------------------- */ async getContactOrRoomRawPayload(id) { const cache = this.getContactOrRoomCache(); return cache.get(id); } async setContactOrRoomRawPayload(id, payload) { const cache = this.getContactOrRoomCache(); // @ts-ignore client is in implementation but not in interface const { client, ...rest } = payload; await cache.set(id, rest); } deleteContactOrRoom(id) { const cache = this.getContactOrRoomCache(); return cache.delete(id); } getContactOrRoomCache() { if (!this.cacheContactOrRoomRawPayload) { throw (0, whatsapp_error_js_1.default)(error_type_js_1.WA_ERROR_TYPE.ERR_NO_CACHE, 'getContactOrRoomCache() has no cache'); } return this.cacheContactOrRoomRawPayload; } async getContactIdList() { const cache = this.getContactOrRoomCache(); const list = []; for await (const key of cache.keys()) { const value = await cache.get(key); if (!value) { continue; } if (!value.isGroup && value.id._serialized) { list.push(value.id._serialized); } } return list; } async getRoomIdList() { const cache = this.getContactOrRoomCache(); const list = []; for await (const key of cache.keys()) { const value = await cache.get(key); if (!value) { continue; } if (value.isGroup && value.id._serialized) { list.push(value.id._serialized); } } return list; } /** * ------------------------------- * Room Member Cache Section * -------------------------------- */ async getRoomMemberIdList(roomId) { const cache = this.getRoomMemberCache(); const memberIdList = await cache.get(roomId); return memberIdList || []; } async setRoomMemberIdList(roomId, list) { const cache = this.getRoomMemberCache(); await cache.set(roomId, list); } async addRoomMemberToList(roomId, memberIds) { const memberIdListInCache = await this.getRoomMemberIdList(roomId); if (Array.isArray(memberIds)) { memberIds.forEach(memberId => !memberIdListInCache.includes(memberId) && memberIdListInCache.push(memberId)); await this.setRoomMemberIdList(roomId, memberIdListInCache); } else { !memberIdListInCache.includes(memberIds) && memberIdListInCache.push(memberIds); await this.setRoomMemberIdList(roomId, memberIdListInCache); } } async removeRoomMemberFromList(roomId, memberIds) { const memberIdListInCache = await this.getRoomMemberIdList(roomId); if (Array.isArray(memberIds)) { const memberIdList = memberIdListInCache.filter(id => !memberIds.includes(id)); await this.setRoomMemberIdList(roomId, memberIdList); } else { if (memberIdListInCache.includes(memberIds)) { const memberIdList = memberIdListInCache.filter(id => id !== memberIds); await this.setRoomMemberIdList(roomId, memberIdList); } } } async deleteRoomMemberIdList(roomId) { const cache = this.getRoomMemberCache(); await cache.delete(roomId); } getRoomMemberCache() { if (!this.cacheRoomMemberIdList) { throw (0, whatsapp_error_js_1.default)(error_type_js_1.WA_ERROR_TYPE.ERR_NO_CACHE, 'getRoomMemberCache() has no cache'); } return this.cacheRoomMemberIdList; } /** * ------------------------------- * Room Invitation Cache Section * -------------------------------- */ async getRoomInvitationRawPayload(id) { const cache = this.getRoomInvitationCache(); return cache.get(id); } async setRoomInvitationRawPayload(id, payload) { const cache = this.getRoomInvitationCache(); await cache.set(id, payload); } deleteRoomInvitation(id) { const cache = this.getRoomInvitationCache(); return cache.delete(id); } getRoomInvitationCache() { if (!this.cacheRoomInvitationRawPayload) { throw (0, whatsapp_error_js_1.default)(error_type_js_1.WA_ERROR_TYPE.ERR_NO_CACHE, 'getRoomInvitationCache() has no cache'); } return this.cacheRoomInvitationRawPayload; } /** * ------------------------------- * Message Cache Section * -------------------------------- */ /** * get timestamp of the latest message for contact or room, if timestamp is not in cache, return Number.MAX_SAFE_INTEGER * @param {string} id message id * @returns {number} timestamp or Number.MAX_SAFE_INTEGER */ async getLatestMessageTimestampForChat(id) { const cache = this.getLatestMessageTimestampForChatCache(); const timestamp = await cache.get(id); if (!timestamp) { return Number.MAX_SAFE_INTEGER; } return timestamp; } async setLatestMessageTimestampForChat(id, num) { const cache = this.getLatestMessageTimestampForChatCache(); await cache.set(id, num); } getLatestMessageTimestampForChatCache() { if (!this.cacheLatestMessageTimestampForChat) { throw (0, whatsapp_error_js_1.default)(error_type_js_1.WA_ERROR_TYPE.ERR_NO_CACHE, 'getLatestMessageTimestampForChatCache() has no cache'); } return this.cacheLatestMessageTimestampForChat; } /** * ------------------------------- * Friendship Cache Section * -------------------------------- */ async getFriendshipRawPayload(id) { const cache = this.getFriendshipCache(); return cache.get(id); } async setFriendshipRawPayload(id, payload) { const cache = this.getFriendshipCache(); // @ts-ignore client is in implementation but not in interface const { client, ...rest } = payload; await cache.set(id, rest); } deleteFriendship(id) { const cache = this.getFriendshipCache(); return cache.delete(id); } getFriendshipCache() { if (!this.cacheFriendshipRawPayload) { throw (0, whatsapp_error_js_1.default)(error_type_js_1.WA_ERROR_TYPE.ERR_NO_CACHE, 'getFriendshipCache() has no cache'); } return this.cacheFriendshipRawPayload; } /** * ------------------------------- * Private Method Section * -------------------------------- */ async initCache(userId) { if (this.cacheMessageRawPayload) { throw (0, whatsapp_error_js_1.default)(error_type_js_1.WA_ERROR_TYPE.ERR_INIT, 'cacheMessageRawPayload does not exist.'); } const baseDir = path.join(os.homedir(), '.wechaty', 'puppet-whatsapp', 'flash-store-v0.12', userId); const baseDirExist = await fs.pathExists(baseDir); if (!baseDirExist) { await fs.mkdirp(baseDir); } this.cacheMessageRawPayload = new flash_store_1.FlashStore(path.join(baseDir, 'message')); this.cacheContactOrRoomRawPayload = new flash_store_1.FlashStore(path.join(baseDir, 'contact-or-room')); this.cacheRoomInvitationRawPayload = new flash_store_1.FlashStore(path.join(baseDir, 'room-invitation')); this.cacheRoomMemberIdList = new flash_store_1.FlashStore(path.join(baseDir, 'room-member')); this.cacheLatestMessageTimestampForChat = new flash_store_1.FlashStore(path.join(baseDir, 'latest-message-timestamp-for-chat')); this.cacheFriendshipRawPayload = new flash_store_1.FlashStore(path.join(baseDir, 'friendship')); const messageTotal = await this.cacheMessageRawPayload.size; config_js_1.log.verbose(PRE, `initCache() inited Messages: ${messageTotal} cacheDir="${baseDir}"`); } async releaseCache() { config_js_1.log.verbose(PRE, 'releaseCache()'); if (this.cacheMessageRawPayload && this.cacheContactOrRoomRawPayload && this.cacheRoomInvitationRawPayload && this.cacheRoomMemberIdList && this.cacheLatestMessageTimestampForChat && this.cacheFriendshipRawPayload) { config_js_1.log.silly(PRE, 'releaseCache() closing caches ...'); await Promise.all([ this.cacheMessageRawPayload.close(), this.cacheContactOrRoomRawPayload.close(), this.cacheRoomInvitationRawPayload.close(), this.cacheRoomMemberIdList.close(), this.cacheLatestMessageTimestampForChat.close(), this.cacheFriendshipRawPayload.close(), ]); this.cacheMessageRawPayload = undefined; this.cacheContactOrRoomRawPayload = undefined; this.cacheRoomInvitationRawPayload = undefined; this.cacheRoomMemberIdList = undefined; this.cacheLatestMessageTimestampForChat = undefined; this.cacheFriendshipRawPayload = undefined; config_js_1.log.silly(PRE, 'releaseCache() cache closed.'); } else { config_js_1.log.verbose(PRE, 'releaseCache() cache not exist.'); } } } exports.CacheManager = CacheManager; //# sourceMappingURL=cache-manager.js.map