@juzi/wechaty
Version:
Wechaty is a RPA SDK for Chatbot Makers.
187 lines • 6.62 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.TagGroupImpl = void 0;
const rx_queue_1 = require("rx-queue");
const config_js_1 = require("../config.js");
const poolify_js_1 = require("../user-mixins/poolify.js");
const validation_js_1 = require("../user-mixins/validation.js");
const wechatify_js_1 = require("../user-mixins/wechatify.js");
const MixinBase = (0, wechatify_js_1.wechatifyMixin)((0, poolify_js_1.poolifyMixin)(Object)());
class TagGroupMixin extends MixinBase {
id;
/**
*
* Instance properties
* @ignore
*
*/
payload;
/**
* @hideconstructor
*/
constructor(id) {
super();
this.id = id;
config_js_1.log.silly('TagGroup', 'constructor()');
}
name() {
return (this.payload && this.payload.name) || '';
}
static async list() {
config_js_1.log.verbose('TagGroup', 'list()');
try {
const tagGroupIds = await this.wechaty.puppet.tagGroupList();
let continuousErrorCount = 0;
let totalErrorCount = 0;
const totalErrorThreshold = Math.round(tagGroupIds.length / 5);
const idToTagGroup = async (id) => {
if (!this.wechaty.isLoggedIn) {
throw new Error('wechaty not logged in');
}
const result = this.find({ id }).catch(e => {
this.wechaty.emitError(e);
continuousErrorCount++;
totalErrorCount++;
if (continuousErrorCount > 5) {
throw new Error('5 continuous errors!');
}
if (totalErrorCount > totalErrorThreshold) {
throw new Error(`${totalErrorThreshold} total errors!`);
}
});
continuousErrorCount = 0;
return result;
};
const CONCURRENCY = 17;
const tagGroupIterator = (0, rx_queue_1.concurrencyExecuter)(CONCURRENCY)(idToTagGroup)(tagGroupIds);
const tagGroupList = [];
for await (const tagGroup of tagGroupIterator) {
if (tagGroup) {
tagGroupList.push(tagGroup);
}
}
return tagGroupList;
}
catch (e) {
this.wechaty.emitError(e);
config_js_1.log.error('TagGroup', 'list() exception: %s', e.message);
return [];
}
}
static async createTagGroup(name) {
config_js_1.log.verbose('TagGroup', 'createTagGroup(%s, %s)', name);
try {
const groupId = await this.wechaty.puppet.tagGroupAdd(name);
if (groupId) {
const newTagGroup = await this.find({ id: groupId });
return newTagGroup;
}
}
catch (e) {
this.wechaty.emitError(e);
config_js_1.log.error('Contact', 'createTag() exception: %s', e.message);
}
}
static async deleteTagGroup(tagGroup) {
config_js_1.log.verbose('TagGroup', 'deleteTagGroup(%s)', tagGroup);
try {
await this.wechaty.puppet.tagGroupDelete(tagGroup.id);
}
catch (e) {
this.wechaty.emitError(e);
config_js_1.log.error('TagGroup', 'deleteTagGroup() exception: %s', e.message);
}
}
async tags() {
config_js_1.log.verbose('TagGroup', 'tags(%s)', this);
try {
const tagIdList = await this.wechaty.puppet.tagGroupTagList(this.id);
const idToTag = async (tagId) => this.wechaty.Tag.find({ id: tagId }).catch(e => this.wechaty.emitError(e));
const CONCURRENCY = 17;
const tagIterator = (0, rx_queue_1.concurrencyExecuter)(CONCURRENCY)(idToTag)(tagIdList);
const tagList = [];
for await (const tag of tagIterator) {
if (tag) {
tagList.push(tag);
}
}
return tagList;
}
catch (e) {
this.wechaty.emitError(e);
config_js_1.log.error('TagGroup', 'list() exception: %s', e.message);
return [];
}
}
static async find(filter) {
config_js_1.log.silly('TagGroup', 'find(%s)', JSON.stringify(filter));
if (filter.id) {
const tagGroup = this.wechaty.TagGroup.load(filter.id);
try {
await tagGroup.ready();
}
catch (e) {
this.wechaty.emitError(e);
return undefined;
}
return tagGroup;
}
if (filter.name) {
const tagGroups = (await this.wechaty.TagGroup.list()).filter(tagGroup => tagGroup.name() === filter.name);
if (tagGroups.length > 0) {
return tagGroups[0];
}
}
return undefined;
// TODO: use a puppet method to find tag, like how contact and room do it
}
/**
* Force reload data for TagGroup, Sync data from low-level API again.
*
* @returns {Promise<this>}
* @example
* await tagGroup.sync()
*/
async sync() {
await this.wechaty.puppet.tagGroupPayloadDirty(this.id);
await this.ready(true);
}
/**
* @ignore
*/
isReady() {
return !!(this.payload);
}
/**
* `ready()` is For FrameWork ONLY!
*
* Please not to use `ready()` at the user land.
* If you want to sync data, use `sync()` instead.
*
* @ignore
*/
async ready(forceSync = false) {
config_js_1.log.silly('TagGroup', 'ready() @ %s with TagGroup="%s"', this.wechaty.puppet, this.id);
if (!forceSync && this.isReady()) { // already ready
config_js_1.log.silly('TagGroup', 'ready() isReady() true');
return;
}
try {
this.payload = await this.wechaty.puppet.tagGroupPayload(this.id);
}
catch (e) {
this.wechaty.emitError(e);
config_js_1.log.verbose('TagGroup', 'ready() this.wechaty.puppet.tagGroupPayload(%s) exception: %s', this.id, e.message);
throw e;
}
}
toString() {
return `<TagGroup#${this.name() || this.id}>`;
}
}
class TagGroupImplBase extends (0, validation_js_1.validationMixin)(TagGroupMixin)() {
}
class TagGroupImpl extends (0, validation_js_1.validationMixin)(TagGroupImplBase)() {
}
exports.TagGroupImpl = TagGroupImpl;
//# sourceMappingURL=tag-group.js.map