aijinkela-wechaty
Version:
Wechaty is a RPA SDK for Chatbot Makers.
163 lines • 6.39 kB
JavaScript
;
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 wechaty_puppet_1 = require("wechaty-puppet");
const contact_js_1 = require("./contact.js");
const favorite_js_1 = require("./favorite.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, poolify_js_1.poolifyMixin)((0, wechatify_js_1.wechatifyMixinBase)())();
class TagMixin extends MixinBase {
id;
/**
* @hideconstructor
*/
constructor(id) {
super();
this.id = id;
wechaty_puppet_1.log.silly('Tag', `constructor(${id})`);
}
/**
* Get a Tag instance for "tag"
*
* > Tips:
* This function is depending on the Puppet Implementation, see [puppet-compatible-table](https://github.com/Chatie/wechaty/wiki/Puppet#3-puppet-compatible-table)
*
* @static
* @param {string} [tag] the tag name which want to create
* @returns {Promise<TagImpl>}
* @example
* const bot = new Wechaty()
* await bot.Tag.get('TagName')
*/
static async get(tag) {
wechaty_puppet_1.log.verbose('Tag', 'get(%s)', tag);
return this.load(tag);
}
/**
* Delete a tag from Wechat
*
* If you want to delete a tag, please make sure there's no more Contact/Favorite(s) are using this tag.
* If this tag is be used by any Contact/Favorite, then it can not be deleted.
* (This is for protecting the tag being deleted by mistake)
*
* @static
* @returns {Promise<TagInterface[]>}
* @example
* const tag = wechaty.Tag.get('tag')
* await wechaty.Tag.delete(tag)
*/
/**
* TODO: refactoring the target: do not use ContactIml or FavoriteImpl
*/
static async delete(tag, target) {
wechaty_puppet_1.log.verbose('Tag', 'static delete(%s)', tag);
/**
* Huan(202110) TODO: refactory this design:
* 1. we should not pass `typeof ContactImpl` as argument
* 2. find a better way to manage tag.
*/
try {
/**
* TODO(huan): add tag check code here for checking if this tag is still being used.
*/
if (!target || target === contact_js_1.ContactImpl || target === this.wechaty.Contact) {
await this.wechaty.puppet.tagContactDelete(tag.id);
// TODO:
// } else if (!target || target === Favorite || target === this.wechaty.Favorite) {
// await this.wechaty.puppet.tagFavoriteDelete(tag.id)
}
}
catch (e) {
this.wechaty.emitError(e);
wechaty_puppet_1.log.error('Tag', 'static delete() exception: %s', e.message);
}
}
/**
* Add tag for contact
*
* > Tips:
* This function is depending on the Puppet Implementation, see [puppet-compatible-table](https://github.com/Chatie/wechaty/wiki/Puppet#3-puppet-compatible-table)
*
* @param {ContactInterface} [to] the contact which need to add tag
* @returns {Promise<void>}
* @example
* await tag.add(contact)
*/
async add(to) {
wechaty_puppet_1.log.verbose('Tag', 'add(%s) for %s', to, this.id);
/**
* Huan(202110): TODO: refactory this design:
* 1. we should not pass `typeof ContactImpl` as argument
* 2. use instanceof to check the type of `to`
*/
try {
if (contact_js_1.ContactImpl.valid(to)) {
await this.wechaty.puppet.tagContactAdd(this.id, to.id);
}
else if (favorite_js_1.FavoriteImpl.valid(to)) {
// TODO: await this.wechaty.puppet.tagAddFavorite(this.tag, to.id)
}
}
catch (e) {
this.wechaty.emitError(e);
wechaty_puppet_1.log.error('Tag', 'add() exception: %s', e.message);
throw new Error(`add error : ${e}`);
}
}
/**
* Remove this tag from Contact/Favorite
*
* > Tips:
* This function is depending on the Puppet Implementation, see [puppet-compatible-table](https://github.com/Chatie/wechaty/wiki/Puppet#3-puppet-compatible-table)
*
* @returns {Promise<void>}
* @example
* await tag.remove(contact)
*/
async remove(from) {
wechaty_puppet_1.log.verbose('Tag', 'remove(%s) for %s', from, this.id);
/**
* Huan(202110): TODO: refactory this design:
* 1. we should not pass `typeof ContactImpl` as argument
* 2. use instanceof to check the type of `to`
*/
try {
if (from instanceof contact_js_1.ContactImpl) {
await this.wechaty.puppet.tagContactRemove(this.id, from.id);
}
else if (from instanceof favorite_js_1.FavoriteImpl) {
// TODO await this.wechaty.puppet.tagRemoveFavorite(this.tag, from.id)
}
}
catch (e) {
this.wechaty.emitError(e);
wechaty_puppet_1.log.error('Tag', 'remove() exception: %s', e.message);
throw new Error(`remove error : ${e}`);
}
}
}
class TagImpl extends (0, validation_js_1.validationMixin)(TagMixin)() {
}
exports.TagImpl = TagImpl;
//# sourceMappingURL=tag.js.map