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

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

上级 028fb3a6
...@@ -92,7 +92,6 @@ export class Contact implements Sayable { ...@@ -92,7 +92,6 @@ export class Contact implements Sayable {
public weixin() { return this.obj && this.obj.weixin || '' } public weixin() { return this.obj && this.obj.weixin || '' }
public name() { return UtilLib.plainText(this.obj && this.obj.name || '') } 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 stranger() { return this.obj && this.obj.stranger }
public star() { return this.obj && this.obj.star } public star() { return this.obj && this.obj.star }
...@@ -199,6 +198,33 @@ export class Contact implements Sayable { ...@@ -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> { public static async find(query: ContactQueryFilter): Promise<Contact> {
log.verbose('Contact', 'find(%s)', query.name) log.verbose('Contact', 'find(%s)', query.name)
......
...@@ -129,6 +129,15 @@ export class Bridge { ...@@ -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[]> { public contactFind(filterFunc: string): Promise<string[]> {
return this.proxyWechaty('contactFindAsync', filterFunc) return this.proxyWechaty('contactFindAsync', filterFunc)
.catch(e => { .catch(e => {
......
...@@ -370,6 +370,22 @@ export class PuppetWeb extends Puppet { ...@@ -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[]> { public contactFind(filterFunc: string): Promise<Contact[]> {
if (!this.bridge) { if (!this.bridge) {
return Promise.reject(new Error('contactFind fail: no bridge(yet)!')) return Promise.reject(new Error('contactFind fail: no bridge(yet)!'))
......
...@@ -196,6 +196,7 @@ ...@@ -196,6 +196,7 @@
var chatFactory = injector.get('chatFactory') var chatFactory = injector.get('chatFactory')
var contactFactory = injector.get('contactFactory') var contactFactory = injector.get('contactFactory')
var confFactory = injector.get('confFactory') var confFactory = injector.get('confFactory')
var emojiFactory = injector.get('emojiFactory')
var loginFactory = injector.get('loginFactory') var loginFactory = injector.get('loginFactory')
var http = injector.get('$http') var http = injector.get('$http')
...@@ -244,6 +245,7 @@ ...@@ -244,6 +245,7 @@
WechatyBro.glue = { WechatyBro.glue = {
injector: injector injector: injector
, http: http , http: http
, mmHttp
, state , state
, accountFactory: accountFactory , accountFactory: accountFactory
...@@ -251,6 +253,7 @@ ...@@ -251,6 +253,7 @@
, chatFactory: chatFactory , chatFactory: chatFactory
, confFactory: confFactory , confFactory: confFactory
, contactFactory: contactFactory , contactFactory: contactFactory
, emojiFactory
, loginFactory: loginFactory , loginFactory: loginFactory
, rootScope: rootScope , rootScope: rootScope
...@@ -520,6 +523,38 @@ ...@@ -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) { function roomFind(filterFunction) {
var contactFactory = WechatyBro.glue.contactFactory var contactFactory = WechatyBro.glue.contactFactory
...@@ -703,6 +738,7 @@ ...@@ -703,6 +738,7 @@
// for Wechaty Contact Class // for Wechaty Contact Class
, contactFindAsync , contactFindAsync
, contactRemarkAsync
// for Wechaty Room Class // for Wechaty Room Class
, roomCreateAsync , roomCreateAsync
......
...@@ -81,6 +81,7 @@ export abstract class Puppet extends EventEmitter implements Sayable { ...@@ -81,6 +81,7 @@ export abstract class Puppet extends EventEmitter implements Sayable {
* Contact * Contact
*/ */
public abstract contactFind(filterFunc: string): Promise<Contact[]> public abstract contactFind(filterFunc: string): Promise<Contact[]>
public abstract contactRemark(contact: Contact, remark: string): Promise<boolean>
} }
export default Puppet export default Puppet
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册