提交 596943f0 编写于 作者: Huan (李卓桓)'s avatar Huan (李卓桓)

#34 add `Contact.remark(newRemark: string)` support

上级 028fb3a6
......@@ -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<boolean>
public remark(newRemark?: string): Promise<boolean> | 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<Contact> {
log.verbose('Contact', 'find(%s)', query.name)
......
......@@ -129,6 +129,15 @@ export class Bridge {
})
}
public async contactRemark(contactId: string, remark: string): Promise<boolean> {
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<string[]> {
return this.proxyWechaty('contactFindAsync', filterFunc)
.catch(e => {
......
......@@ -370,6 +370,22 @@ export class PuppetWeb extends Puppet {
})
}
public async contactRemark(contact: Contact, remark: string): Promise<boolean> {
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<Contact[]> {
if (!this.bridge) {
return Promise.reject(new Error('contactFind fail: no bridge(yet)!'))
......
......@@ -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
......
......@@ -81,6 +81,7 @@ export abstract class Puppet extends EventEmitter implements Sayable {
* Contact
*/
public abstract contactFind(filterFunc: string): Promise<Contact[]>
public abstract contactRemark(contact: Contact, remark: string): Promise<boolean>
}
export default Puppet
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册