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

#32 add Room.nick()

上级 edce2e10
......@@ -525,6 +525,8 @@ wechaty.on('friend', (contact: Contact, request: FriendRequest) => {
})
```
Talk is cheap, show me the code: [Example/Friend-Bot](https://github.com/wechaty/wechaty/blob/master/example/friend-bot.js)
### FriendRequest.hello: string
verify message
......
......@@ -42,11 +42,12 @@ class Contact {
}
}
name() { return UtilLib.plainText(this.obj.name) }
remark() { return this.obj.remark }
stranger() { return this.obj.stranger }
star() { return this.obj.star }
get(prop) { return this.obj[prop] }
name() { return UtilLib.plainText(this.obj.name) }
remark() { return this.obj.remark }
stranger() { return this.obj.stranger }
star() { return this.obj.star }
get(prop) { return this.obj[prop] }
ready(contactGetter) {
log.silly('Contact', 'ready(' + (contactGetter ? typeof contactGetter : '') + ')')
......@@ -103,7 +104,9 @@ Contact.init = function() { Contact.pool = {} }
Contact.init()
Contact.load = function(id) {
if (!id) { return null }
if (!id || typeof id !== 'string') {
throw new Error('id must be string')
}
if (!(id in Contact.pool)) {
Contact.pool[id] = new Contact(id)
......
......@@ -29,7 +29,7 @@ class Room {
// @private
isReady() {
return this.obj.memberList && this.obj.memberList.length
return this.obj.contactList && this.obj.contactList.length
}
refresh() {
......@@ -47,7 +47,7 @@ class Room {
} else if (this.isReady()) {
return Promise.resolve(this)
} else if (this.obj.id) {
log.warn('Room', 'ready() has obj.id but memberList empty in room %s. reloading', this.obj.topic)
log.warn('Room', 'ready() has obj.id but contactList empty in room %s. reloading', this.obj.topic)
}
contactGetter = contactGetter || Config.puppetInstance()
......@@ -75,20 +75,24 @@ class Room {
id: rawObj.UserName
, encryId: rawObj.EncryChatRoomId // ???
, topic: rawObj.NickName
, memberList: this.parseMemberList(rawObj.MemberList)
, contactList: this.parseContactList(rawObj.MemberList)
, nickMap: this.parseNickMap(rawObj.MemberList)
}
}
parseMemberList(memberList) {
parseContactList(memberList) {
if (!memberList || !memberList.map) {
return []
}
return memberList.map(m => {
return {
id: m.UserName
, name: m.DisplayName // nick name for this room?
}
})
return memberList.map(m => m.UserName)
}
parseNickMap(memberList) {
const nickMap = {}
if (memberList && memberList.map) {
memberList.forEach(m => nickMap[m.UserName] = m.DisplayName)
}
return nickMap
}
dumpRaw() {
......@@ -114,28 +118,28 @@ class Room {
delLocal(contact) {
log.verbose('Room', 'delLocal(%s)', contact)
const memberList = this.obj.memberList
if (!memberList || memberList.length === 0) {
const contactList = this.obj.contactList
if (!contactList || contactList.length === 0) {
return true // already in refreshing
}
let i
for (i=0; i<memberList.length; i++) {
for (i=0; i<contactList.length; i++) {
// XXX
// console.log('########################')
// console.log(i)
// console.log(memberList[i].id)
// console.log(contactList[i].id)
// console.log(contact.get('id'))
// console.log('!!!!!!!!!!!!!!!!!!!!!!!!!!')
if (memberList[i].id === contact.get('id')) {
if (contactList[i].id === contact.get('id')) {
break
}
}
// console.log('found i=' + i)
if (i < memberList.length) {
// console.log('splicing before: ' + memberList.length)
memberList.splice(i, 1)
// console.log('splicing after: ' + memberList.length)
if (i < contactList.length) {
// console.log('splicing before: ' + contactList.length)
contactList.splice(i, 1)
// console.log('splicing after: ' + contactList.length)
return true
}
return false
......@@ -166,6 +170,13 @@ class Room {
return this.get('topic')
}
nick(contactId) {
if (!this.obj.nickMap) {
return ''
}
return this.obj.nickMap[contactId]
}
static create(contactList) {
log.verbose('Room', 'create(%s)', contactList.join(','))
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册