diff --git a/src/contact.ts b/src/contact.ts index 64aa7a85d2ce41d28804358bc1c9646e329bd823..3ef4fa23b69c6cb6689a849543e13e4807aa1952 100644 --- a/src/contact.ts +++ b/src/contact.ts @@ -92,7 +92,6 @@ export class Contact implements Sayable { public weixin() { return this.obj && this.obj.weixin || '' } public name() { return UtilLib.plainText(this.obj && this.obj.name || '') } - public remark() { return this.obj && this.obj.remark } public stranger() { return this.obj && this.obj.stranger } public star() { return this.obj && this.obj.star } @@ -199,6 +198,33 @@ export class Contact implements Sayable { }) } + /** + * get the remark for contact + */ + public remark(): string + /** + * set the remark for contact + */ + public remark(newRemark: string): Promise + + public remark(newRemark?: string): Promise | string { + log.verbose('Contact', 'remark(%s)', newRemark || '') + + if (newRemark === undefined) { + return this.obj && this.obj.remark || '' + } + + return Config.puppetInstance() + .contactRemark(this, newRemark) + .catch(e => { + log.error('Contact', 'remark(%s) rejected: %s', newRemark, e.message) + return false // fail safe + }) + } + + /** + * try to find a contact by filter: {name: string | RegExp} + */ public static async find(query: ContactQueryFilter): Promise { log.verbose('Contact', 'find(%s)', query.name) diff --git a/src/puppet-web/bridge.ts b/src/puppet-web/bridge.ts index 5766c7273972eb50a0f696adf617e3ea35b3b455..2ba5048ae99dea3a76696dac923ad306290e057c 100644 --- a/src/puppet-web/bridge.ts +++ b/src/puppet-web/bridge.ts @@ -129,6 +129,15 @@ export class Bridge { }) } + public async contactRemark(contactId: string, remark: string): Promise { + try { + return await this.proxyWechaty('contactRemarkAsync', contactId, remark) + } catch (e) { + log.error('PuppetWebBridge', 'contactRemarkAsync() exception: %s', e.message) + throw e + } + } + public contactFind(filterFunc: string): Promise { return this.proxyWechaty('contactFindAsync', filterFunc) .catch(e => { diff --git a/src/puppet-web/puppet-web.ts b/src/puppet-web/puppet-web.ts index cd4e6f16f1e294d0e255fed7ea7e9bb12c3ebccf..4834d01660d225307258407ae2bc495ba080a46e 100644 --- a/src/puppet-web/puppet-web.ts +++ b/src/puppet-web/puppet-web.ts @@ -370,6 +370,22 @@ export class PuppetWeb extends Puppet { }) } + public async contactRemark(contact: Contact, remark: string): Promise { + try { + const ret = await this.bridge.contactRemark(contact.id, remark) + if (!ret) { + log.warn('PuppetWeb', 'contactRemark(%s, %s) bridge.contactRemark() return false' + , contact.id, remark + ) + } + return ret + + } catch (e) { + log.warn('PuppetWeb', 'contactFind(%s) rejected: %s', filterFunc, e.message) + throw e + } + } + public contactFind(filterFunc: string): Promise { if (!this.bridge) { return Promise.reject(new Error('contactFind fail: no bridge(yet)!')) diff --git a/src/puppet-web/wechaty-bro.js b/src/puppet-web/wechaty-bro.js index dc6a2c710f440ab804243e34f21ac863c6e9e876..881ada14010b26251610e7b1f92e0b27a77b82f3 100644 --- a/src/puppet-web/wechaty-bro.js +++ b/src/puppet-web/wechaty-bro.js @@ -196,6 +196,7 @@ var chatFactory = injector.get('chatFactory') var contactFactory = injector.get('contactFactory') var confFactory = injector.get('confFactory') + var emojiFactory = injector.get('emojiFactory') var loginFactory = injector.get('loginFactory') var http = injector.get('$http') @@ -244,6 +245,7 @@ WechatyBro.glue = { injector: injector , http: http + , mmHttp , state , accountFactory: accountFactory @@ -251,6 +253,7 @@ , chatFactory: chatFactory , confFactory: confFactory , contactFactory: contactFactory + , emojiFactory , loginFactory: loginFactory , rootScope: rootScope @@ -520,6 +523,38 @@ } } + function contactRemarkAsync(UserName, remark) { + const callback = arguments[arguments.length - 1] + if (typeof callback !== 'function') { + // here we should in sync mode, because there's no callback + throw new Error('async method need to be called via webdriver.executeAsyncScript') + } + + var accountFactory = WechatyBro.glue.accountFactory + var confFactory = WechatyBro.glue.confFactory + var emojiFactory = WechatyBro.glue.emojiFactory + var mmHttp = WechatyBro.glue.mmHttp + + mmHttp({ + method: "POST", + url: confFactory.API_webwxoplog, + data: angular.extend({ + UserName: UserName, + CmdId: confFactory.oplogCmdId.MODREMARKNAME, + RemarkName: emojiFactory.formatHTMLToSend(remark) + }, accountFactory.getBaseRequest()), + MMRetry: { + count: 3, + timeout: 1e4, + serial: !0 + } + }).success(function() { + callback(true) + }).error(function() { + callback(false) + }) + } + function roomFind(filterFunction) { var contactFactory = WechatyBro.glue.contactFactory @@ -703,6 +738,7 @@ // for Wechaty Contact Class , contactFindAsync + , contactRemarkAsync // for Wechaty Room Class , roomCreateAsync diff --git a/src/puppet.ts b/src/puppet.ts index 914f3d414c56366ecc121c8565893facfa954d41..2c0829d59f6faf9790ad91ccd7e2f7ad3e34c005 100644 --- a/src/puppet.ts +++ b/src/puppet.ts @@ -81,6 +81,7 @@ export abstract class Puppet extends EventEmitter implements Sayable { * Contact */ public abstract contactFind(filterFunc: string): Promise + public abstract contactRemark(contact: Contact, remark: string): Promise } export default Puppet