Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix_function_room.member_#173 #211

Merged
merged 29 commits into from Jan 24, 2017
Merged
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
119 changes: 92 additions & 27 deletions src/room.ts
Expand Up @@ -26,8 +26,12 @@ type RoomObj = {
ownerUin: number
memberList: Contact[]
nickMap: Map<string, string>
remarkMap: Map<string, string>
displayMap: Map<string, string>
}

type NameType = 'nick' | 'display' | 'remark'

export type RoomRawMember = {
UserName: string
NickName: string
Expand All @@ -52,6 +56,12 @@ export type RoomQueryFilter = {
topic: string | RegExp
}

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

export class Room extends EventEmitter implements Sayable {
private static pool = new Map<string, Room>()

Expand Down Expand Up @@ -90,6 +100,14 @@ export class Room extends EventEmitter implements Sayable {
// return this.load(contactGetter)
// }

private async readyAllMembers(memberList: RoomRawMember[]): Promise<void> {
for (let member of memberList) {
let contact = Contact.load(member.UserName)
await contact.ready()
}
return
}

public async ready(contactGetter?: (id: string) => Promise<any>): Promise<void> {
log.silly('Room', 'ready(%s)', contactGetter ? contactGetter.constructor.name : '')
if (!this.id) {
Expand All @@ -114,6 +132,7 @@ export class Room extends EventEmitter implements Sayable {
const data = await contactGetter(this.id)
log.silly('Room', `contactGetter(${this.id}) resolved`)
this.rawObj = data
await this.readyAllMembers(this.rawObj.MemberList)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good

this.obj = this.parse(data)

if (!this.obj) {
Expand Down Expand Up @@ -189,7 +208,9 @@ export class Room extends EventEmitter implements Sayable {
}

const memberList = this.parseMemberList(rawObj.MemberList)
const nickMap = this.parseNickMap(rawObj.MemberList)
const nickMap = this.parseMap(rawObj.MemberList, 'nick')
const remarkMap = this.parseMap(rawObj.MemberList, 'remark')
const displayMap = this.parseMap(rawObj.MemberList, 'display')

return {
id: rawObj.UserName,
Expand All @@ -199,6 +220,8 @@ export class Room extends EventEmitter implements Sayable {

memberList,
nickMap,
remarkMap,
displayMap,
}
}

Expand All @@ -209,21 +232,34 @@ export class Room extends EventEmitter implements Sayable {
return rawMemberList.map(m => Contact.load(m.UserName))
}

private parseNickMap(memberList: RoomRawMember[]): Map<string, string> {
const nickMap: Map<string, string> = new Map<string, string>()

private parseMap(memberList: RoomRawMember[], parseContent: NameType): Map<string, string> {
const mapList: Map<string, string> = new Map<string, string>()
if (memberList && memberList.map) {
memberList.forEach(member => {
let tmpName: string
let contact = Contact.load(member.UserName)
switch (parseContent) {
case 'nick':
tmpName = contact.name()
break
case 'remark':
tmpName = contact.remark() || ''
break
case 'display':
tmpName = member.DisplayName
break
default:
throw new Error('parseMap failed, member not found')
}
/**
* 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
*/
nickMap[member.UserName] = UtilLib.stripEmoji(
member.DisplayName || member.NickName
)
mapList[member.UserName] = UtilLib.stripEmoji(tmpName)
})
}
return nickMap
return mapList
}

public dumpRaw() {
Expand Down Expand Up @@ -356,31 +392,60 @@ export class Room extends EventEmitter implements Sayable {
}

/**
* NickName / DisplayName / RemarkName of member
* find member by `nick`(NickName) / `display`(DisplayName) / `remark`(RemarkName)
* when use member(name:string), find member by nickName by default
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should by all name by default.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you missed this review.

*/
public member(name: string): Contact | null {
log.verbose('Room', 'member(%s)', name)
public member(queryArg: MemberQueryFilter | string): Contact | null {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add function declarations before function implement like this:

+ public member(filter: MemberQueryFilter): Contact | null
+ public member(name: string): Contact | null
+ 
public member(queryArg: MemberQueryFilter | string): Contact | null {

if (typeof queryArg === 'string') {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Need to support member(name: string) here, which name could be any of remark/display/nick.

log.verbose('Room', 'function member should use member(queryArg: MemberQueryFilter)')
return this.member({remark: queryArg}) || this.member({display: queryArg}) || this.member({nick: queryArg})
} else {
Copy link
Member

@huan huan Jan 19, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove else here, and get all the code in this block out.

log.verbose('Room', 'member({ %s })'
, Object.keys(queryArg)
.map(k => `${k}: ${queryArg[k]}`)
.join(', ')
)

if (Object.keys(queryArg).length !== 1) {
throw new Error('Room member find quaryArg only support one key. multi key support is not availble now.')
}

if (!this.obj || !this.obj.memberList) {
log.warn('Room', 'member() not ready')
return null
}
if (!this.obj || !this.obj.memberList) {
log.warn('Room', 'member() not ready')
return null
}
let filterKey = Object.keys(queryArg)[0]
/**
* ISSUE #64 emoji need to be striped
*/
let filterValue: string = UtilLib.stripEmoji(queryArg[filterKey])

const keyMap = {
nick: 'nickMap',
remark: 'remarkMap',
display: 'displayMap'
}

filterKey = keyMap[filterKey]
if (!filterKey) {
throw new Error('unsupport filter key')
}

/**
* ISSUE #64 emoji need to be striped
*/
name = UtilLib.stripEmoji(name)
if (!filterValue) {
throw new Error('filterValue not found')
}

const nickMap = this.obj.nickMap
const idList = Object.keys(nickMap)
.filter(k => nickMap[k] === name)
const filterMap = this.obj[filterKey]
const idList = Object.keys(filterMap)
.filter(k => filterMap[k] === filterValue)

log.silly('Room', 'member() check nickMap: %s', JSON.stringify(nickMap))
log.silly('Room', 'member() check %s: %s', filterKey, JSON.stringify(filterKey))

if (idList.length) {
return Contact.load(idList[0])
} else {
return null
if (idList.length) {
return Contact.load(idList[0])
} else {
return null
}
}
}

Expand Down