From 596943f0378aad9c23dd802ef1afcf9961a5e12a Mon Sep 17 00:00:00 2001 From: "Zhuohuan LI (CARPE DIEM)" Date: Mon, 31 Oct 2016 21:27:39 +0800 Subject: [PATCH] #34 add `Contact.remark(newRemark: string)` support --- src/contact.ts | 28 ++++++++++++++++++++++++++- src/puppet-web/bridge.ts | 9 +++++++++ src/puppet-web/puppet-web.ts | 16 ++++++++++++++++ src/puppet-web/wechaty-bro.js | 36 +++++++++++++++++++++++++++++++++++ src/puppet.ts | 1 + 5 files changed, 89 insertions(+), 1 deletion(-) diff --git a/src/contact.ts b/src/contact.ts index 64aa7a85..3ef4fa23 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 5766c727..2ba5048a 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 cd4e6f16..4834d016 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 dc6a2c71..881ada14 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 914f3d41..2c0829d5 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 -- GitLab