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
Changes from 7 commits
2f977b9
4272819
6184454
bf8c62c
72d094b
f5f0f09
0104f28
f329526
5978d74
ed409a3
6d7d290
d2ab92b
8e50b40
2bedd3b
7f373b3
16006d4
581c095
ae65b2a
6223ef4
fb7b7b2
bd14978
05a16e5
1471d4e
6d2a5bc
b46b23d
31ec862
d9ef2f2
0151712
bb2cf5d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
|
@@ -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>() | ||
|
||
|
@@ -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) { | ||
|
@@ -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) | ||
this.obj = this.parse(data) | ||
|
||
if (!this.obj) { | ||
|
@@ -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, | ||
|
@@ -199,6 +220,8 @@ export class Room extends EventEmitter implements Sayable { | |
|
||
memberList, | ||
nickMap, | ||
remarkMap, | ||
displayMap, | ||
} | ||
} | ||
|
||
|
@@ -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() { | ||
|
@@ -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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should by There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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') { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Need to support |
||
log.verbose('Room', 'function member should use member(queryArg: MemberQueryFilter)') | ||
return this.member({remark: queryArg}) || this.member({display: queryArg}) || this.member({nick: queryArg}) | ||
} else { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Remove |
||
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 | ||
} | ||
} | ||
} | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
good