UNPKG

wechaty-puppet

Version:

Abstract Puppet for Wechaty

90 lines 4.01 kB
import { log, } from '../config.js'; import { DirtyType } from '../schemas/dirty.js'; const friendshipMixin = (mixinBase) => { class FriendshipMixin extends mixinBase { constructor(...args) { super(...args); log.verbose('PuppetFriendshipMixin', 'constructor()'); } /** * Huan(202203): `friendshipSearchWeixin()` will be removed in Puppet v2.0 * @deprecated use `friendshipSearchHandle()` instead. */ friendshipSearchWeixin(weixin) { log.warn('Puppet', 'friendshipSearchWeixin() is deprecated. use `friendshipSearchHandle()` instead.'); console.error(new Error().stack); return this.friendshipSearchHandle(weixin); } async friendshipSearch(searchQueryFilter) { log.verbose('PuppetFriendshipMixin', 'friendshipSearch("%s")', JSON.stringify(searchQueryFilter)); if (Object.keys(searchQueryFilter).length !== 1) { throw new Error('searchQueryFilter should only include one key for query!'); } if (searchQueryFilter.phone) { return this.friendshipSearchPhone(searchQueryFilter.phone); } else if (searchQueryFilter.weixin) { /** * Huan(202203): `weixin` will be removed in Puppet v2.0 * @deprecate use `handle` instead */ log.warn('Puppet', 'friendshipSearch() `{ weixin: ... }` is deprecated. use `{ handle: ... }` instead.'); console.error(new Error().stack); return this.friendshipSearchHandle(searchQueryFilter.weixin); } else if (searchQueryFilter.handle) { return this.friendshipSearchHandle(searchQueryFilter.handle); } throw new Error(`unknown key from searchQueryFilter: ${Object.keys(searchQueryFilter).join('')}`); } /** * Issue #155 - https://github.com/wechaty/puppet/issues/155 * * @protected */ friendshipPayloadCache(friendshipId) { log.silly('PuppetFriendshipMixin', 'friendshipPayloadCache(id=%s) @ %s', friendshipId, this); if (!friendshipId) { throw new Error('no id'); } const cachedPayload = this.cache.friendship.get(friendshipId); if (cachedPayload) { // log.silly('PuppetFriendshipMixin', 'friendshipPayloadCache(%s) cache HIT', friendshipId) } else { log.silly('PuppetFriendshipMixin', 'friendshipPayloadCache(%s) cache MISS', friendshipId); } return cachedPayload; } async friendshipPayload(friendshipId, newPayload) { log.verbose('PuppetFriendshipMixin', 'friendshipPayload(%s)', friendshipId, newPayload ? ',' + JSON.stringify(newPayload) : ''); if (typeof newPayload === 'object') { await this.cache.friendship.set(friendshipId, newPayload); return; } /** * 1. Try to get from cache first */ const cachedPayload = this.friendshipPayloadCache(friendshipId); if (cachedPayload) { return cachedPayload; } /** * 2. Cache not found */ const rawPayload = await this.friendshipRawPayload(friendshipId); const payload = await this.friendshipRawPayloadParser(rawPayload); this.cache.friendship.set(friendshipId, payload); return payload; } async friendshipPayloadDirty(id) { log.verbose('PuppetFriendshipMixin', 'friendshipPayloadDirty(%s)', id); await this.__dirtyPayloadAwait(DirtyType.Friendship, id); } } return FriendshipMixin; }; export { friendshipMixin }; //# sourceMappingURL=friendship-mixin.js.map