UNPKG

@juzi/wechaty

Version:

Wechaty is a RPA SDK for Chatbot Makers.

184 lines 6.23 kB
import { concurrencyExecuter } from 'rx-queue'; import { log } from '../config.js'; import { poolifyMixin } from '../user-mixins/poolify.js'; import { validationMixin } from '../user-mixins/validation.js'; import { wechatifyMixin, } from '../user-mixins/wechatify.js'; const MixinBase = wechatifyMixin(poolifyMixin(Object)()); class TagGroupMixin extends MixinBase { id; /** * * Instance properties * @ignore * */ payload; /** * @hideconstructor */ constructor(id) { super(); this.id = id; log.silly('TagGroup', 'constructor()'); } name() { return (this.payload && this.payload.name) || ''; } static async list() { 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 = 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); log.error('TagGroup', 'list() exception: %s', e.message); return []; } } static async createTagGroup(name) { 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); log.error('Contact', 'createTag() exception: %s', e.message); } } static async deleteTagGroup(tagGroup) { log.verbose('TagGroup', 'deleteTagGroup(%s)', tagGroup); try { await this.wechaty.puppet.tagGroupDelete(tagGroup.id); } catch (e) { this.wechaty.emitError(e); log.error('TagGroup', 'deleteTagGroup() exception: %s', e.message); } } async tags() { 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 = 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); log.error('TagGroup', 'list() exception: %s', e.message); return []; } } static async find(filter) { 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) { log.silly('TagGroup', 'ready() @ %s with TagGroup="%s"', this.wechaty.puppet, this.id); if (!forceSync && this.isReady()) { // already ready log.silly('TagGroup', 'ready() isReady() true'); return; } try { this.payload = await this.wechaty.puppet.tagGroupPayload(this.id); } catch (e) { this.wechaty.emitError(e); 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 validationMixin(TagGroupMixin)() { } class TagGroupImpl extends validationMixin(TagGroupImplBase)() { } export { TagGroupImpl, }; //# sourceMappingURL=tag-group.js.map