UNPKG

wechaty-puppet

Version:

Abstract Puppet for Wechaty

132 lines 5.04 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.loginMixin = void 0; const config_js_1 = require("../config.js"); const event_js_1 = require("../schemas/event.js"); const loginMixin = (mixinBase) => { class LoginMixin extends mixinBase { /** * @internal used by public API `currentUserId` */ __currentUserId; /** * The current logged in user id. */ get currentUserId() { config_js_1.log.silly('PuppetLoginMixin', 'get currentUserId()'); if (!this.__currentUserId) { throw new Error('not logged in, no this.__currentUserId yet.'); } return this.__currentUserId; } /** * Boolean value indicates whether the user is logged in or not. */ get isLoggedIn() { return !!this.__currentUserId; } __authQrCode; get authQrCode() { return this.__authQrCode; } constructor(...args) { super(...args); config_js_1.log.verbose('PuppetLoginMixin', 'constructor()'); } async start() { config_js_1.log.verbose('PuppetLoginMixin', 'start()'); const cleanAuthQrCode = () => { this.__authQrCode = undefined; }; const onScan = ({ qrcode, status }) => { switch (status) { case event_js_1.ScanStatus.Cancel: case event_js_1.ScanStatus.Confirmed: case event_js_1.ScanStatus.Scanned: cleanAuthQrCode(); break; case event_js_1.ScanStatus.Timeout: // TODO: confirm the `Timeout` spec (define it if it is not defined) case event_js_1.ScanStatus.Waiting: this.__authQrCode = qrcode; break; case event_js_1.ScanStatus.Unknown: default: break; } }; this.addListener('scan', onScan); this.addListener('login', cleanAuthQrCode); this.addListener('stop', cleanAuthQrCode); await super.start(); } /** * ref: https://github.com/wechaty/puppet/issues/184 */ async stop() { config_js_1.log.verbose('PuppetLoginMixin', 'stop()'); // await this.logout() if (this.isLoggedIn) { this.emit('logout', { contactId: this.currentUserId, data: 'puppet stop()', }); } await super.stop(); } /** * Need to be called internally when the puppet is logined. * this method will emit a `login` event * @internal for puppet internal usage */ login(userId) { config_js_1.log.verbose('PuppetLoginMixin', 'login(%s)', userId); if (this.__currentUserId) { throw new Error('must logout first before login again!'); } this.__currentUserId = userId; this.emit('login', { contactId: userId }); } /** * Need to be called internally/externally when the puppet need to be logouted * this method will emit a `logout` event, * * Note: must set `this.currentUserId = undefined` in this function. */ async logout(reason = 'logout()') { config_js_1.log.verbose('PuppetLoginMixin', 'logout(%s)', reason); if (!this.isLoggedIn) { config_js_1.log.verbose('PuppetLoginMixin', 'logout() isLoggedIn === false, do nothing'); return; } this.emit('logout', { contactId: this.currentUserId, data: reason, }); /** * Huan(202111): We postpone the `this._currentUserId = undefined` to here, * in case of the `logout` event listener need to check the `this.currentUserId` */ await new Promise(resolve => setImmediate(() => { this.__currentUserId = undefined; resolve(); })); } /** * @deprecated use `currentUserId` instead. (will be removed in v2.0) */ selfId() { config_js_1.log.warn('PuppetLoginMixin', 'selfId() is deprecated, use `currentUserId` instead:\n%s', new Error().stack); return this.currentUserId; } /** * @deprecated use isLoggedIn instead. will be removed in v2.0 */ logonoff() { config_js_1.log.warn('PuppetLoginMixin', 'logonoff() is deprecated, use `isLoggedIn` instead:\n%s', new Error().stack); return this.isLoggedIn; } } return LoginMixin; }; exports.loginMixin = loginMixin; //# sourceMappingURL=login-mixin.js.map