Skip to content

Commit

Permalink
Merge 1b88912 into 1af8b9b
Browse files Browse the repository at this point in the history
  • Loading branch information
lijiarui committed Feb 6, 2017
2 parents 1af8b9b + 1b88912 commit 8d466c4
Show file tree
Hide file tree
Showing 6 changed files with 93 additions and 59 deletions.
69 changes: 43 additions & 26 deletions src/contact.ts
Expand Up @@ -22,7 +22,7 @@ type ContactObj = {
id: string
name: string
province: string
remark: string|null
alias: string|null
sex: Gender
signature: string
star: boolean
Expand Down Expand Up @@ -55,8 +55,8 @@ export enum Gender {
}

export type ContactQueryFilter = {
name?: string | RegExp
remark?: string | RegExp
name?: string | RegExp
alias?: string | RegExp
}

export class Contact implements Sayable {
Expand All @@ -78,7 +78,7 @@ export class Contact implements Sayable {
if (!this.obj) {
return this.id
}
return this.obj.remark || this.obj.name || this.id
return this.obj.alias || this.obj.name || this.id
}

public toStringEx() { return `Contact(${this.obj && this.obj.name}[${this.id}])` }
Expand All @@ -93,7 +93,7 @@ export class Contact implements Sayable {
, uin: rawObj.Uin // stable id: 4763975 || getCookie("wxuin")
, weixin: rawObj.Alias // Wechat ID
, name: rawObj.NickName
, remark: rawObj.RemarkName
, alias: rawObj.RemarkName
, sex: rawObj.Sex
, province: rawObj.Province
, city: rawObj.City
Expand Down Expand Up @@ -222,12 +222,17 @@ export class Contact implements Sayable {
}

/**
* find contact by `name`(NickName) or `remark`(RemarkName)
* find contact by `name` or `alias`
*/
public static async findAll(queryArg?: ContactQueryFilter): Promise<Contact[]> {
public static async findAll(queryArg?: ContactQueryFilter | {remark: string | RegExp}): Promise<Contact[]> {
let query: ContactQueryFilter
if (queryArg) {
query = queryArg
if (queryArg[0] === 'remark') {
log.warn('Contact', 'Contact.findAll(remark:%s) DEPRECATED, use Contact.findAll(alias:%s) instead.')
query = { alias: queryArg[1]}
} else {
query = queryArg
}
} else {
query = { name: /.*/ }
}
Expand All @@ -248,7 +253,7 @@ export class Contact implements Sayable {

const keyMap = {
name: 'NickName',
remark: 'RemarkName',
alias: 'RemarkName',
}

filterKey = keyMap[filterKey]
Expand Down Expand Up @@ -287,51 +292,63 @@ export class Contact implements Sayable {
}

/**
* get the remark for contact
* get the alias for contact
*/
public remark(): string | null
public alias(): string | null
/**
* set the remark for contact
* set the alias for contact
* @return {Promise<boolean>} A promise to the result. true for success, false for failure
*/
public remark(newRemark: string): Promise<boolean>
public alias(newAlias: string): Promise<boolean>
/**
* delete the remark for a contact
* delete the alias for a contact
*/
public remark(empty: null): Promise<boolean>
public alias(empty: null): Promise<boolean>

public remark(newRemark?: string|null): Promise<boolean> | string | null {
log.silly('Contact', 'remark(%s)', newRemark || '')
public alias(newAlias?: string|null): Promise<boolean> | string | null {
log.silly('Contact', 'alias(%s)', newAlias || '')

if (newRemark === undefined) {
return this.obj && this.obj.remark || null
if (newAlias === undefined) {
return this.obj && this.obj.alias || null
}

return Config.puppetInstance()
.contactRemark(this, newRemark)
.contactAlias(this, newAlias)
.then(ret => {
if (ret) {
if (this.obj) {
this.obj.remark = newRemark
this.obj.alias = newAlias
} else {
log.error('Contact', 'remark() without this.obj?')
log.error('Contact', 'alias() without this.obj?')
}
} else {
log.warn('Contact', 'remark(%s) fail', newRemark)
log.warn('Contact', 'alias(%s) fail', newAlias)
}
return ret
})
.catch(e => {
log.error('Contact', 'remark(%s) rejected: %s', newRemark, e.message)
log.error('Contact', 'alias(%s) rejected: %s', newAlias, e.message)
return false // fail safe
})
}

// function should be deprecated
public remark(newRemark?: string|null): Promise<boolean> | string | null {
log.warn('Contact', 'remark(%s) DEPRECATED, use alias(%s) instead.')
log.silly('Contact', 'remark(%s)', newRemark || '')

if (newRemark) {
return this.alias(newRemark)
} else {
return this.alias()
}
}

/**
* 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)
public static async find(query: ContactQueryFilter | {remark: string | RegExp}): Promise<Contact> {
log.verbose('Contact', 'find(%s)', query)

const contactList = await Contact.findAll(query)
if (!contactList || !contactList.length) {
Expand Down
2 changes: 1 addition & 1 deletion src/puppet-web/puppet-web.ts
Expand Up @@ -374,7 +374,7 @@ export class PuppetWeb extends Puppet {
}
}

public async contactRemark(contact: Contact, remark: string|null): Promise<boolean> {
public async contactAlias(contact: Contact, remark: string|null): Promise<boolean> {
try {
const ret = await this.bridge.contactRemark(contact.id, remark)
if (!ret) {
Expand Down
2 changes: 1 addition & 1 deletion src/puppet.ts
Expand Up @@ -67,5 +67,5 @@ export abstract class Puppet extends EventEmitter implements Sayable {
* Contact
*/
public abstract contactFind(filterFunc: string): Promise<Contact[]>
public abstract contactRemark(contact: Contact, remark: string|null): Promise<boolean>
public abstract contactAlias(contact: Contact, alias: string|null): Promise<boolean>
}
59 changes: 33 additions & 26 deletions src/room.ts
Expand Up @@ -25,12 +25,12 @@ type RoomObj = {
topic: string
ownerUin: number
memberList: Contact[]
nickMap: Map<string, string>
remarkMap: Map<string, string>
displayMap: Map<string, string>
nameMap: Map<string, string>
aliasMap: Map<string, string>
roomAliasMap: Map<string, string>
}

type NameType = 'nick' | 'display' | 'remark'
type NameType = 'name' | 'alias' | 'roomAlias'

export type RoomRawMember = {
UserName: string
Expand All @@ -57,9 +57,9 @@ export type RoomQueryFilter = {
}

export type MemberQueryFilter = {
nick?: string
remark?: string
display?: string
name?: string
alias?: string
roomAlias?: string
}

export class Room extends EventEmitter implements Sayable {
Expand Down Expand Up @@ -206,10 +206,10 @@ export class Room extends EventEmitter implements Sayable {
return null
}

const memberList = this.parseMemberList(rawObj.MemberList)
const nickMap = this.parseMap(rawObj.MemberList, 'nick')
const remarkMap = this.parseMap(rawObj.MemberList, 'remark')
const displayMap = this.parseMap(rawObj.MemberList, 'display')
const memberList = this.parseMemberList(rawObj.MemberList)
const nameMap = this.parseMap(rawObj.MemberList, 'name')
const aliasMap = this.parseMap(rawObj.MemberList, 'alias')
const roomAliasMap = this.parseMap(rawObj.MemberList, 'roomAlias')

return {
id: rawObj.UserName,
Expand All @@ -218,9 +218,9 @@ export class Room extends EventEmitter implements Sayable {
ownerUin: rawObj.OwnerUin,

memberList,
nickMap,
remarkMap,
displayMap,
nameMap,
aliasMap,
roomAliasMap,
}
}

Expand All @@ -237,13 +237,13 @@ export class Room extends EventEmitter implements Sayable {
let tmpName: string
let contact = Contact.load(member.UserName)
switch (parseContent) {
case 'nick':
case 'name':
tmpName = contact.name()
break
case 'remark':
tmpName = contact.remark() || ''
case 'alias':
tmpName = contact.alias() || ''
break
case 'display':
case 'roomAlias':
tmpName = member.DisplayName
break
default:
Expand All @@ -252,7 +252,8 @@ export class Room extends EventEmitter implements Sayable {
/**
* ISSUE #64 emoji need to be striped
* ISSUE #104 never use remark name because sys group message will never use that
* @rui: cannot use argument NickName because it mix real nick and remark
* @rui: Wrong for 'never use remark name because sys group message will never use that', see more in the latest comment in #104
* @rui: cannot use argument NickName because it mix real name and alias
*/
mapList[member.UserName] = UtilLib.stripEmoji(tmpName)
})
Expand Down Expand Up @@ -350,11 +351,17 @@ export class Room extends EventEmitter implements Sayable {
return UtilLib.plainText(this.obj ? this.obj.topic : '')
}

// should be deprecated
public nick(contact: Contact): string {
if (!this.obj || !this.obj.nickMap) {
log.warn('Room', 'nick(Contact) DEPRECATED, use name(Contact) instead.')
return this.alias(contact)
}

public alias(contact: Contact): string {
if (!this.obj || !this.obj.nameMap) {
return ''
}
return this.obj.displayMap[contact.id] || this.obj.nickMap[contact.id]
return this.obj.roomAliasMap[contact.id] || this.obj.nameMap[contact.id]
}

public has(contact: Contact): boolean {
Expand Down Expand Up @@ -390,7 +397,7 @@ export class Room extends EventEmitter implements Sayable {
}

/**
* find member by `nick`(NickName) / `display`(DisplayName) / `remark`(RemarkName)
* find member by `name` / `roomAlias` / `alias`
* when use member(name:string), find all name by default
*/

Expand All @@ -399,7 +406,7 @@ export class Room extends EventEmitter implements Sayable {

public member(queryArg: MemberQueryFilter | string): Contact | null {
if (typeof queryArg === 'string') {
return this.member({remark: queryArg}) || this.member({display: queryArg}) || this.member({nick: queryArg})
return this.member({alias: queryArg}) || this.member({roomAlias: queryArg}) || this.member({name: queryArg})
}

log.silly('Room', 'member({ %s })'
Expand All @@ -423,9 +430,9 @@ export class Room extends EventEmitter implements Sayable {
let filterValue: string = UtilLib.stripEmoji(queryArg[filterKey])

const keyMap = {
nick: 'nickMap',
remark: 'remarkMap',
display: 'displayMap'
name: 'nameMap',
alias: 'aliasMap',
roomAlias: 'roomAliasMap'
}

filterKey = keyMap[filterKey]
Expand Down
4 changes: 4 additions & 0 deletions test/contact.spec.ts
Expand Up @@ -18,6 +18,7 @@ test('Contact smoke testing', async t => {
/* tslint:disable:variable-name */
const UserName = '@0bb3e4dd746fdbd4a80546aef66f4085'
const NickName = 'Nick Name Test'
const RemarkName = 'Alias Test'

// Mock
const mockContactGetter = function (id) {
Expand All @@ -27,6 +28,7 @@ test('Contact smoke testing', async t => {
return resolve({
UserName: UserName
, NickName: NickName
, RemarkName: RemarkName
})
}, 200)
})
Expand All @@ -38,6 +40,8 @@ test('Contact smoke testing', async t => {
const r = await c.ready(mockContactGetter)
t.is(r.get('id') , UserName, 'UserName set')
t.is(r.get('name') , NickName, 'NickName set')
t.is(r.name(), NickName, 'should get the right name from Contact')
t.is(r.alias(), RemarkName, 'should get the right alias from Contact')

const s = r.toString()
t.is(typeof s, 'string', 'toString()')
Expand Down
16 changes: 11 additions & 5 deletions test/room.spec.ts
Expand Up @@ -109,11 +109,17 @@ test('Room smoking test', async t => {

const contact1 = new Contact(EXPECTED.memberId1)
const nick1 = r.nick(contact1)
t.is(nick1, EXPECTED.memberNick1, 'should get nick1 from DisplayName')
t.is(nick1, EXPECTED.memberNick1, 'should get nick1 from roomAlias')

const name1 = r.alias(contact1)
t.is(name1, EXPECTED.memberNick1, 'should get name1 from roomAlias')

const contact2 = new Contact(EXPECTED.memberId2)
const nick2 = r.nick(contact2)
t.is(nick2, EXPECTED.memberNick2, 'should get nick2 from NickName because there is no DisplayName, ')
t.is(nick2, EXPECTED.memberNick2, 'should get nick2 from name because there is no roomAlias, ')

const name2 = r.alias(contact2)
t.is(name2, EXPECTED.memberNick2, 'should get nick2 from name because there is no roomAlias, ')

t.truthy(r.has(contact1), 'should has contact1')
const noSuchContact = new Contact('not exist id')
Expand All @@ -131,9 +137,9 @@ test('Room smoking test', async t => {
if (!contactA || !contactB || !contactC) {
throw new Error('no a or b')
}
t.is(contactA.id, EXPECTED.memberId1, 'should get the right id from nick 1, find member by nickName')
t.is(contactB.id, EXPECTED.memberId2, 'should get the right id from nick 2, find member by displayName')
t.is(contactC.id, EXPECTED.memberId3, 'should get the right id from nick 3, find member by remark')
t.is(contactA.id, EXPECTED.memberId1, 'should get the right id from nick 1, find member by name')
t.is(contactB.id, EXPECTED.memberId2, 'should get the right id from nick 2, find member by roomAlias')
t.is(contactC.id, EXPECTED.memberId3, 'should get the right id from nick 3, find member by alias')

const s = r.toString()
t.is(typeof s, 'string', 'toString()')
Expand Down

0 comments on commit 8d466c4

Please sign in to comment.