UNPKG

@juzi/wechaty

Version:

Wechaty is a RPA SDK for Chatbot Makers.

341 lines 12.7 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.TagImpl = void 0; /** * Wechaty Chatbot SDK - https://github.com/wechaty/wechaty * * @copyright 2016 Huan LI (李卓桓) <https://github.com/huan>, and * Wechaty Contributors <https://github.com/wechaty>. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. * */ const PUPPET = __importStar(require("@juzi/wechaty-puppet")); const rx_queue_1 = require("rx-queue"); const config_js_1 = require("../config.js"); const poolify_js_1 = require("../user-mixins/poolify.js"); const assert_1 = __importDefault(require("assert")); 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 TagMixin extends MixinBase { id; /** * * Instance properties * @ignore * */ payload; /** * @hideconstructor */ constructor(id) { super(); this.id = id; config_js_1.log.silly('Tag', 'constructor()'); } type() { return (this.payload && this.payload.type) || PUPPET.types.Tag.Unspecific; } name() { return (this.payload && this.payload.name) || ''; } groupId() { return (this.payload && this.payload.groupId) || ''; } async group() { return this.payload?.groupId ? this.wechaty.TagGroup.find({ id: this.payload.groupId }) : undefined; } static async list() { config_js_1.log.verbose('Tag', 'list()'); const tagIdList = await this.wechaty.puppet.tagTagList(); let continuousErrorCount = 0; let totalErrorCount = 0; const totalErrorThreshold = Math.round(tagIdList.length / 5); const idToTag = 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 tagIterator = (0, rx_queue_1.concurrencyExecuter)(CONCURRENCY)(idToTag)(tagIdList); const tagList = []; for await (const tag of tagIterator) { if (tag) { tagList.push(tag); } } return tagList; } static async find(filter) { config_js_1.log.silly('Tag', 'find(%s)', JSON.stringify(filter)); if (filter.id) { const tag = this.wechaty.Tag.load(filter.id); try { await tag.ready(); } catch (e) { this.wechaty.emitError(e); return undefined; } return tag; } if (filter.name) { const tags = (await this.wechaty.Tag.list()).filter(t => t.name() === filter.name); if (tags.length > 0) { return tags[0]; } } return undefined; // TODO: use a puppet method to find tag, like how contact and room do it } static async findMulti(filters) { config_js_1.log.silly('Tag', 'find(%s)', JSON.stringify(filters)); const filterIdList = filters.filter(i => !!i.id).map(i => i.id); const filterNameList = filters.filter(i => !!i.name).map(i => i.name); const resultSet = new Set(); if (filterIdList.length) { const tags = filterIdList.map(i => { (0, assert_1.default)(i, 'Should not be undefined'); return this.wechaty.Tag.load(i); }); try { await Promise.all(tags.map(i => i.ready())); } catch (e) { this.wechaty.emitError(e); return undefined; } for (const tag of tags) { resultSet.add(tag); } } if (filterNameList.length) { const list = await this.wechaty.Tag.list(); const tagMap = new Map(); // FIXME: There are two tag types in wework, and the names can be repeated list.forEach(i => tagMap.set(i.name(), i)); for (const name of filterNameList) { (0, assert_1.default)(typeof name === 'string', 'Only supported string'); const tag = tagMap.get(name); if (tag) { resultSet.add(tag); } } } return resultSet.size ? Array.from(resultSet) : undefined; // TODO: use a puppet method to find tag, like how contact and room do it } /** * Force reload data for Tag, Sync data from low-level API again. * * @returns {Promise<this>} * @example * await tag.sync() */ async sync() { await this.wechaty.puppet.tagPayloadDirty(this.id); await this.ready(true); } /** * @ignore */ isReady() { return !!(this.payload && this.payload.name); } /** * `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('Tag', 'ready() @ %s with Tag key="%s"', this.wechaty.puppet, this.id); if (!forceSync && this.isReady()) { // already ready config_js_1.log.silly('Tag', 'ready() isReady() true'); return; } try { this.payload = await this.wechaty.puppet.tagPayload(this.id); } catch (e) { this.wechaty.emitError(e); config_js_1.log.verbose('Tag', 'ready() this.wechaty.puppet.tagPayload(%s) exception: %s', this.id, e.message); throw e; } } async contactList() { config_js_1.log.verbose('Tag', 'contactList() for tag : %s', this); const contactIds = await this.wechaty.puppet.tagTagContactList(this.id); const contactPromises = contactIds.map(id => this.wechaty.Contact.find({ id })); return (await Promise.all(contactPromises)).filter(contact => !!contact); } async tag(contacts) { config_js_1.log.verbose('Tag', 'tag(%s) for tag : %s', contacts, this); let contactIds; if (Array.isArray(contacts)) { contactIds = contacts.map(c => c.id); } else { contactIds = [contacts.id]; } await this.wechaty.puppet.tagContactTagAdd([this.id], contactIds); } static async createTag(name, tagGroup) { config_js_1.log.verbose('Tag', 'createTag(%s, %s)', tagGroup, name); try { const tagInfoList = await this.wechaty.puppet.tagTagAdd([name], tagGroup?.name()); if (tagInfoList?.length) { const filter = { id: tagInfoList[0]?.id, }; const newTag = await this.find(filter); return newTag; } } catch (e) { this.wechaty.emitError(e); config_js_1.log.error('Tag', 'createTag() exception: %s', e.message); } } static async createMultiTag(nameList, tagGroup) { config_js_1.log.verbose('Tag', 'createMultiTag(%s, %s)', tagGroup, nameList); try { const tagInfoList = await this.wechaty.puppet.tagTagAdd(nameList, tagGroup?.name()); if (tagInfoList?.length) { const filterList = tagInfoList.map(i => ({ id: i.id, })); const newTagList = await this.findMulti(filterList); return newTagList; } } catch (e) { this.wechaty.emitError(e); config_js_1.log.error('Tag', 'createMultiTag() exception: %s', e.message); } } static async deleteTag(tagInstance) { config_js_1.log.verbose('Tag', 'deleteTag(%s, %s)', tagInstance); try { await this.wechaty.puppet.tagTagDelete([tagInstance.id]); } catch (e) { this.wechaty.emitError(e); config_js_1.log.error('Tag', 'deleteTag() exception: %s', e.message); } } static async deleteMultiTag(tagInstances) { config_js_1.log.verbose('Tag', 'deleteMultiTag(%s, %s)', tagInstances); try { await this.wechaty.puppet.tagTagDelete(tagInstances.map(i => i.id)); } catch (e) { this.wechaty.emitError(e); config_js_1.log.error('Tag', 'deleteMultiTag() exception: %s', e.message); } } static async modifyTag(tagInstance, tagNewName) { config_js_1.log.verbose('Tag', 'modifyTag(%s, %s)', tagInstance); try { const tagNewInfo = { id: tagInstance.id, name: tagNewName, }; const tagInfoList = await this.wechaty.puppet.tagTagModify([tagNewInfo]); if (tagInfoList?.length) { const filter = { id: tagInfoList[0]?.id, }; const newTag = await this.find(filter); return newTag; } } catch (e) { this.wechaty.emitError(e); config_js_1.log.error('Tag', 'modifyTag() exception: %s', e.message); } } static async modifyMultiTag(tagInfos) { config_js_1.log.verbose('Tag', 'modifyMultiTag(%o)', tagInfos); try { const tagNewInfoList = tagInfos.map(i => ({ id: i.tag.id, name: i.newName, })); const tagInfoList = await this.wechaty.puppet.tagTagModify(tagNewInfoList); if (tagInfoList?.length) { const filterList = tagInfoList.map(i => ({ id: i.id, })); const newTagList = await this.findMulti(filterList); return newTagList; } } catch (e) { this.wechaty.emitError(e); config_js_1.log.error('Tag', 'modifyMultiTag() exception: %s', e.message); } } toString() { return `<Tag#${this.name() || this.id}>`; } } class TagImplBase extends (0, validation_js_1.validationMixin)(TagMixin)() { } class TagImpl extends (0, validation_js_1.validationMixin)(TagImplBase)() { } exports.TagImpl = TagImpl; //# sourceMappingURL=tag.js.map