wechaty-puppet-padplus
Version:
Puppet Padplus for Wechaty
368 lines • 15.1 kB
JavaScript
"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __asyncValues = (this && this.__asyncValues) || function (o) {
if (!Symbol.asyncIterator) throw new TypeError("Symbol.asyncIterator is not defined.");
var m = o[Symbol.asyncIterator], i;
return m ? m.call(o) : (o = typeof __values === "function" ? __values(o) : o[Symbol.iterator](), i = {}, verb("next"), verb("throw"), verb("return"), i[Symbol.asyncIterator] = function () { return this; }, i);
function verb(n) { i[n] = o[n] && function (v) { return new Promise(function (resolve, reject) { v = o[n](v), settle(resolve, reject, v.done, v.value); }); }; }
function settle(resolve, reject, d, v) { Promise.resolve(v).then(function(v) { resolve({ value: v, done: d }); }, reject); }
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const fs_extra_1 = __importDefault(require("fs-extra"));
const os_1 = __importDefault(require("os"));
const path_1 = __importDefault(require("path"));
const flash_store_1 = require("flash-store");
const config_1 = require("../config");
const PRE = 'CacheManager';
class CacheManager {
static get Instance() {
if (!this._instance) {
throw new Error(`${PRE} cache manager instance not initialized.`);
}
return this._instance;
}
static init(userId) {
return __awaiter(this, void 0, void 0, function* () {
config_1.log.verbose(PRE, `init()`);
if (this._instance) {
config_1.log.verbose(PRE, `init() CacheManager has been initialized, no need to initialize again.`);
return;
}
this._instance = new CacheManager();
yield this._instance.initCache(userId);
});
}
static release() {
return __awaiter(this, void 0, void 0, function* () {
config_1.log.verbose(PRE, `release()`);
if (!this._instance) {
config_1.log.verbose(PRE, `release() CacheManager not exist, no need to release it.`);
return;
}
yield this._instance.releaseCache();
this._instance = undefined;
});
}
/**
* -------------------------------
* Account-WXID Section
* --------------------------------
*/
getAccountWXID(account) {
return __awaiter(this, void 0, void 0, function* () {
if (!this.cacheWXID) {
throw new Error(`${PRE} getWXID() has no cache.`);
}
return this.cacheWXID.get(account);
});
}
setAccountWXID(account, wxid) {
return __awaiter(this, void 0, void 0, function* () {
if (!this.cacheWXID) {
throw new Error(`${PRE} setWXID() has no cache.`);
}
yield this.cacheWXID.set(account, wxid);
});
}
/**
* -------------------------------
* Contact Section
* --------------------------------
*/
getContact(contactId) {
return __awaiter(this, void 0, void 0, function* () {
if (!this.cacheContactRawPayload) {
throw new Error(`${PRE} getContact() has no cache.`);
}
return this.cacheContactRawPayload.get(contactId);
});
}
setContact(contactId, payload) {
return __awaiter(this, void 0, void 0, function* () {
if (!this.cacheContactRawPayload || !contactId) {
throw new Error(`${PRE} setContact() has no cache.`);
}
yield this.cacheContactRawPayload.set(contactId, payload);
});
}
deleteContact(contactId) {
return __awaiter(this, void 0, void 0, function* () {
if (!this.cacheContactRawPayload) {
throw new Error(`${PRE} deleteContact() has no cache.`);
}
yield this.cacheContactRawPayload.delete(contactId);
});
}
getContactIds() {
var e_1, _a;
return __awaiter(this, void 0, void 0, function* () {
if (!this.cacheContactRawPayload) {
throw new Error(`${PRE} getContactIds() has no cache.`);
}
const result = [];
try {
for (var _b = __asyncValues(this.cacheContactRawPayload.keys()), _c; _c = yield _b.next(), !_c.done;) {
const key = _c.value;
result.push(key);
}
}
catch (e_1_1) { e_1 = { error: e_1_1 }; }
finally {
try {
if (_c && !_c.done && (_a = _b.return)) yield _a.call(_b);
}
finally { if (e_1) throw e_1.error; }
}
return result;
});
}
getAllContacts() {
var e_2, _a;
return __awaiter(this, void 0, void 0, function* () {
if (!this.cacheContactRawPayload) {
throw new Error(`${PRE} getAllContacts() has no cache.`);
}
const result = [];
try {
for (var _b = __asyncValues(this.cacheContactRawPayload.values()), _c; _c = yield _b.next(), !_c.done;) {
const value = _c.value;
result.push(value);
}
}
catch (e_2_1) { e_2 = { error: e_2_1 }; }
finally {
try {
if (_c && !_c.done && (_a = _b.return)) yield _a.call(_b);
}
finally { if (e_2) throw e_2.error; }
}
return result;
});
}
hasContact(contactId) {
return __awaiter(this, void 0, void 0, function* () {
if (!this.cacheContactRawPayload) {
throw new Error(`${PRE} hasContact() has no cache.`);
}
return this.cacheContactRawPayload.has(contactId);
});
}
getContactCount() {
return __awaiter(this, void 0, void 0, function* () {
if (!this.cacheContactRawPayload) {
throw new Error(`${PRE} getContactCount() has no cache.`);
}
return this.cacheContactRawPayload.size;
});
}
/**
* -------------------------------
* Room Section
* --------------------------------
*/
getRoom(roomId) {
return __awaiter(this, void 0, void 0, function* () {
if (!this.cacheRoomRawPayload) {
throw new Error(`${PRE} getRoom() has no cache.`);
}
return this.cacheRoomRawPayload.get(roomId);
});
}
setRoom(roomId, payload) {
return __awaiter(this, void 0, void 0, function* () {
if (!this.cacheRoomRawPayload) {
throw new Error(`${PRE} setRoom() has no cache.`);
}
yield this.cacheRoomRawPayload.set(roomId, payload);
});
}
deleteRoom(roomId) {
return __awaiter(this, void 0, void 0, function* () {
if (!this.cacheRoomRawPayload) {
throw new Error(`${PRE} setRoom() has no cache.`);
}
yield this.cacheRoomRawPayload.delete(roomId);
});
}
getRoomIds() {
var e_3, _a;
return __awaiter(this, void 0, void 0, function* () {
if (!this.cacheRoomRawPayload) {
throw new Error(`${PRE} getRoomIds() has no cache.`);
}
const result = [];
try {
for (var _b = __asyncValues(this.cacheRoomRawPayload.keys()), _c; _c = yield _b.next(), !_c.done;) {
const key = _c.value;
result.push(key);
}
}
catch (e_3_1) { e_3 = { error: e_3_1 }; }
finally {
try {
if (_c && !_c.done && (_a = _b.return)) yield _a.call(_b);
}
finally { if (e_3) throw e_3.error; }
}
return result;
});
}
getRoomCount() {
return __awaiter(this, void 0, void 0, function* () {
if (!this.cacheRoomRawPayload) {
throw new Error(`${PRE} getRoomCount() has no cache.`);
}
return this.cacheRoomRawPayload.size;
});
}
hasRoom(roomId) {
return __awaiter(this, void 0, void 0, function* () {
if (!this.cacheRoomRawPayload) {
throw new Error(`${PRE} hasRoom() has no cache.`);
}
return this.cacheRoomRawPayload.has(roomId);
});
}
/**
* -------------------------------
* Room Member Section
* --------------------------------
*/
getRoomMember(roomId) {
return __awaiter(this, void 0, void 0, function* () {
if (!this.cacheRoomMemberRawPayload) {
throw new Error(`${PRE} getRoomMember() has no cache.`);
}
return this.cacheRoomMemberRawPayload.get(roomId);
});
}
setRoomMember(roomId, payload) {
return __awaiter(this, void 0, void 0, function* () {
if (!this.cacheRoomMemberRawPayload) {
throw new Error(`${PRE} setRoomMember() has no cache.`);
}
yield this.cacheRoomMemberRawPayload.set(roomId, payload);
});
}
deleteRoomMember(roomId) {
return __awaiter(this, void 0, void 0, function* () {
if (!this.cacheRoomMemberRawPayload) {
throw new Error(`${PRE} deleteRoomMember() has no cache.`);
}
yield this.cacheRoomMemberRawPayload.delete(roomId);
});
}
/**
* -------------------------------
* Room Invitation Section
* -------------------------------
*/
getRoomInvitation(messageId) {
return __awaiter(this, void 0, void 0, function* () {
if (!this.cacheRoomInvitationRawPayload) {
throw new Error(`${PRE} getRoomInvitationRawPayload() has no cache.`);
}
return this.cacheRoomInvitationRawPayload.get(messageId);
});
}
setRoomInvitation(messageId, payload) {
return __awaiter(this, void 0, void 0, function* () {
if (!this.cacheRoomInvitationRawPayload) {
throw new Error(`${PRE} setRoomInvitationRawPayload() has no cache.`);
}
yield this.cacheRoomInvitationRawPayload.set(messageId, payload);
});
}
deleteRoomInvitation(messageId) {
return __awaiter(this, void 0, void 0, function* () {
if (!this.cacheRoomInvitationRawPayload) {
throw new Error(`${PRE} deleteRoomInvitation() has no cache.`);
}
yield this.cacheRoomInvitationRawPayload.delete(messageId);
});
}
/**
* -------------------------------
* Friendship Cache Section
* --------------------------------
*/
getFriendshipRawPayload(id) {
return __awaiter(this, void 0, void 0, function* () {
if (!this.cacheFriendshipRawPayload) {
throw new Error(`${PRE} getFriendshipRawPayload() has no cache.`);
}
return this.cacheFriendshipRawPayload.get(id);
});
}
setFriendshipRawPayload(id, payload) {
return __awaiter(this, void 0, void 0, function* () {
if (!this.cacheFriendshipRawPayload) {
throw new Error(`${PRE} setFriendshipRawPayload() has no cache.`);
}
yield this.cacheFriendshipRawPayload.set(id, payload);
});
}
/**
* -------------------------------
* Private Method Section
* --------------------------------
*/
initCache(userId) {
return __awaiter(this, void 0, void 0, function* () {
config_1.log.verbose(PRE, 'initCache(%s)', userId);
if (this.cacheContactRawPayload) {
throw new Error('cache exists');
}
const baseDir = path_1.default.join(os_1.default.homedir(), path_1.default.sep, '.wechaty', 'puppet-padplus-cache', path_1.default.sep, 'flash-store-v0.14', path_1.default.sep, userId);
const baseDirExist = yield fs_extra_1.default.pathExists(baseDir);
if (!baseDirExist) {
yield fs_extra_1.default.mkdirp(baseDir);
}
this.cacheWXID = new flash_store_1.FlashStore(path_1.default.join(baseDir, 'accound-wxid'));
this.cacheContactRawPayload = new flash_store_1.FlashStore(path_1.default.join(baseDir, 'contact-raw-payload'));
this.cacheRoomMemberRawPayload = new flash_store_1.FlashStore(path_1.default.join(baseDir, 'room-member-raw-payload'));
this.cacheRoomRawPayload = new flash_store_1.FlashStore(path_1.default.join(baseDir, 'room-raw-payload'));
this.cacheFriendshipRawPayload = new flash_store_1.FlashStore(path_1.default.join(baseDir, 'friendship'));
const contactTotal = this.cacheContactRawPayload.size;
config_1.log.verbose(PRE, `initCache() inited ${contactTotal} Contacts, cachedir="${baseDir}"`);
});
}
releaseCache() {
return __awaiter(this, void 0, void 0, function* () {
config_1.log.verbose(PRE, 'releaseCache()');
if (this.cacheContactRawPayload
&& this.cacheRoomMemberRawPayload
&& this.cacheRoomRawPayload
&& this.cacheFriendshipRawPayload) {
config_1.log.silly(PRE, 'releaseCache() closing caches ...');
yield Promise.all([
this.cacheContactRawPayload.close(),
this.cacheRoomMemberRawPayload.close(),
this.cacheRoomRawPayload.close(),
this.cacheFriendshipRawPayload.close(),
]);
this.cacheContactRawPayload = undefined;
this.cacheRoomMemberRawPayload = undefined;
this.cacheRoomRawPayload = undefined;
this.cacheFriendshipRawPayload = undefined;
config_1.log.silly(PRE, 'releaseCache() cache closed.');
}
else {
config_1.log.verbose(PRE, 'releaseCache() cache not exist.');
}
});
}
}
exports.CacheManager = CacheManager;
//# sourceMappingURL=cache-manager.js.map