Skip to content

Commit

Permalink
add more contact methods: gender(), city(), provionce(), avatar() #121
Browse files Browse the repository at this point in the history
  • Loading branch information
huan committed Dec 7, 2016
1 parent e08411e commit 467b53e
Showing 1 changed file with 46 additions and 12 deletions.
58 changes: 46 additions & 12 deletions src/contact.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,12 @@
import {
Config
, Sayable
} from './config'
import { Message } from './message'
import { UtilLib } from './util-lib'
import { Wechaty } from './wechaty'
import { log } from './brolog-env'
} from './config'
import { Message } from './message'
import { PuppetWeb } from './puppet-web'
import { UtilLib } from './util-lib'
import { Wechaty } from './wechaty'
import { log } from './brolog-env'

type ContactObj = {
address: string
Expand All @@ -22,12 +23,13 @@ type ContactObj = {
name: string
province: string
remark: string|null
sex: string
sex: Gender
signature: string
star: boolean
stranger: boolean
uin: string
weixin: string
avatar: string // XXX URL of HeadImgUrl
}

export type ContactRawObj = {
Expand All @@ -36,15 +38,22 @@ export type ContactRawObj = {
NickName: string
Province: string
RemarkName: string
Sex: string
Sex: Gender
Signature: string
StarFriend: string
Uin: string
UserName: string
HeadImgUrl: string

stranger: string // assign by injectio.js
}

export enum Gender {
Unknown = 0,
Male = 1,
Female = 2,
}

export type ContactQueryFilter = {
name: string | RegExp
}
Expand Down Expand Up @@ -93,15 +102,40 @@ export class Contact implements Sayable {

, star: !!rawObj.StarFriend
, stranger: !!rawObj.stranger // assign by injectio.js
, avatar: rawObj.HeadImgUrl
}
}

public weixin() { return this.obj && this.obj.weixin || '' }
public name() { return UtilLib.plainText(this.obj && this.obj.name || '') }
public stranger() { return this.obj && this.obj.stranger }
public star() { return this.obj && this.obj.star }
public weixin() { return this.obj && this.obj.weixin || '' }
public name() { return UtilLib.plainText(this.obj && this.obj.name || '') }
public stranger() { return this.obj && this.obj.stranger }
public star() { return this.obj && this.obj.star }
/**
* Contact gender
* @return Gender.Male(2) | Gender.Femail(1) | Gender.Unknown(0)
*/
public gender() { return this.obj ? this.obj.sex : Gender.Unknown }
public province() { return this.obj && this.obj.province }
public city() { return this.obj && this.obj.city }

/**
* Get avatar picture file stream
*/
public async avatar(): Promise<NodeJS.ReadableStream> {
if (!this.obj || !this.obj.avatar) {
throw new Error('Can not get avatar: not ready')
}

try {
const cookies = await (Config.puppetInstance() as PuppetWeb).browser.readCookie()
return UtilLib.urlStream(this.obj.avatar, cookies)
} catch (e) {
log.warn('Contact', 'avatar() exception: %s', e.stack)
throw e
}
}

public get(prop) { return this.obj && this.obj[prop] }
public get(prop) { return this.obj && this.obj[prop] }

public isReady(): boolean {
return !!(this.obj && this.obj.id && this.obj.name !== undefined)
Expand Down

0 comments on commit 467b53e

Please sign in to comment.