room.ts 14.8 KB
Newer Older
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
1 2 3 4 5 6 7
/**
 *
 * wechaty: Wechat for Bot. and for human who talk to bot/robot
 *
 * Licenst: ISC
 * https://github.com/zixia/wechaty
 *
8 9
 * Add/Del/Topic: https://github.com/wechaty/wechaty/issues/32
 *
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
10
 */
11
import { EventEmitter } from 'events'
12

13 14 15
import {
    Config
  , Sayable
16
  , log
17
}                 from './config'
18 19 20
import { Contact }    from './contact'
import { Message }    from './message'
import { UtilLib }    from './util-lib'
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
21

22 23 24 25 26 27 28
type RoomObj = {
  id:         string
  encryId:    string
  topic:      string
  ownerUin:   number
  memberList: Contact[]
  nickMap:    Map<string, string>
29 30
  remarkMap:  Map<string, string>
  displayMap: Map<string, string>
31 32
}

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

35
export type RoomRawMember = {
36
  UserName:     string
37
  NickName:     string
38 39 40
  DisplayName:  string
}

41
export type RoomRawObj = {
42 43 44 45
  UserName:         string
  EncryChatRoomId:  string
  NickName:         string
  OwnerUin:         number
46
  ChatRoomOwner:    string
47
  MemberList:       RoomRawMember[]
48 49
}

50 51 52
export type RoomEventName = 'join'
                          | 'leave'
                          | 'topic'
53 54
                          | 'EVENT_PARAM_ERROR'

55
export type RoomQueryFilter = {
56 57 58
  topic: string | RegExp
}

59 60 61 62 63 64
export type MemberQueryFilter = {
  nick?:    string
  remark?:  string
  display?: string
}

Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
65
export class Room extends EventEmitter implements Sayable {
66 67
  private static pool = new Map<string, Room>()

68 69
  private dirtyObj: RoomObj | null // when refresh, use this to save dirty data for query
  private obj:      RoomObj | null
70 71
  private rawObj:   RoomRawObj

72
  constructor(public id: string) {
73
    super()
74
    log.silly('Room', `constructor(${id})`)
75
  }
76

77
  public toString()    { return this.id }
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
78
  public toStringEx()  { return `Room(${this.obj && this.obj.topic}[${this.id}])` }
79

80
  public isReady(): boolean {
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
81
    return !!(this.obj && this.obj.memberList && this.obj.memberList.length)
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
82 83
  }

Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
84 85 86 87 88
  // public refresh() {
  //   log.warn('Room', 'refresh() DEPRECATED. use reload() instead.')
  //   return this.reload()
  // }

Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
89
  public async refresh(): Promise<void> {
90 91 92
    if (this.isReady()) {
      this.dirtyObj = this.obj
    }
93
    this.obj = null
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
94 95
    await this.ready()
    return
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
96 97
  }

98 99 100 101 102 103 104 105
  private async readyAllMembers(memberList: RoomRawMember[]): Promise<void> {
    for (let member of memberList) {
      let contact = Contact.load(member.UserName)
      await contact.ready()
    }
    return
  }

Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
106 107 108 109 110
  // public ready(contactGetter?: (id: string) => Promise<any>) {
  //   log.warn('Room', 'ready() DEPRECATED. use load() instad.')
  //   return this.load(contactGetter)
  // }

Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
111
  public async ready(contactGetter?: (id: string) => Promise<any>): Promise<void> {
112
    log.silly('Room', 'ready(%s)', contactGetter ? contactGetter.constructor.name : '')
113
    if (!this.id) {
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
114 115
      const e = new Error('ready() on a un-inited Room')
      log.warn('Room', e.message)
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
116
      throw e
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
117
    } else if (this.isReady()) {
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
118
      return
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
119
    } else if (this.obj && this.obj.id) {
120
      log.warn('Room', 'ready() has obj.id but memberList empty in room %s. reloading', this.obj.topic)
121
    }
122

123 124 125 126 127 128 129 130
    if (!contactGetter) {
      contactGetter = Config.puppetInstance()
                            .getContact.bind(Config.puppetInstance())
    }
    if (!contactGetter) {
      throw new Error('no contactGetter')
    }

131 132
    try {
      const data = await contactGetter(this.id)
133
      log.silly('Room', `contactGetter(${this.id}) resolved`)
134
      this.rawObj = data
135 136
      await this.readyAllMembers(this.rawObj.MemberList)
      this.obj    = this.parse(this.rawObj)
137 138 139
      if (!this.obj) {
        throw new Error('no this.obj set after contactGetter')
      }
140
      await Promise.all(this.obj.memberList.map(c => c.ready(contactGetter)))
141

Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
142
      return
143

144
    } catch (e) {
145 146
      log.error('Room', 'contactGetter(%s) exception: %s', this.id, e.message)
      throw e
147
    }
148 149
  }

150 151 152
  public on(event: 'leave', listener: (this: Room, leaver: Contact) => void): this
  public on(event: 'join' , listener: (this: Room, inviteeList: Contact[] , inviter: Contact)  => void): this
  public on(event: 'topic', listener: (this: Room, topic: string, oldTopic: string, changer: Contact) => void): this
153
  public on(event: 'EVENT_PARAM_ERROR', listener: () => void): this
154

155
  public on(event: RoomEventName, listener: Function): this {
156
    log.verbose('Room', 'on(%s, %s)', event, typeof listener)
157

Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
158 159 160 161 162 163 164 165 166 167 168
    // const thisWithSay = {
    //   say: (content: string) => {
    //     return Config.puppetInstance()
    //                   .say(content)
    //   }
    // }
    // super.on(event, function() {
    //   return listener.apply(thisWithSay, arguments)
    // })

    super.on(event, listener) // Room is `Sayable`
169
    return this
170 171
  }

Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
172 173 174
  public say(content: string): Promise<any>
  public say(content: string, replyTo: Contact): Promise<void>
  public say(content: string, replyTo: Contact[]): Promise<void>
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
175

Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
176
  public say(content: string, replyTo?: Contact|Contact[]): Promise<void> {
177 178 179 180
    log.verbose('Room', 'say(%s, %s)'
                      , content
                      , Array.isArray(replyTo)
                        ? replyTo.map(c => c.name()).join(', ')
181
                        : replyTo ? replyTo.name() : ''
182
    )
183 184 185 186

    const m = new Message()
    m.room(this)

Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
187
    const replyToList: Contact[] = [].concat(replyTo as any || [])
188

Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
189 190 191 192 193 194 195
    if (replyToList.length > 0) {
      const mentionList = replyToList.map(c => '@' + c.name()).join(' ')
      m.content(mentionList + ' ' + content)
    } else {
      m.content(content)
    }
    // m.to(replyToList[0])
196 197 198 199 200

    return Config.puppetInstance()
                  .send(m)
  }

Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
201
  public get(prop): string { return (this.obj && this.obj[prop]) || (this.dirtyObj && this.dirtyObj[prop]) }
202

203
  private parse(rawObj: RoomRawObj): RoomObj | null {
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
204
    if (!rawObj) {
205
      log.warn('Room', 'parse() on a empty rawObj?')
206
      return null
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
207
    }
Huan (李卓桓)'s avatar
#104  
Huan (李卓桓) 已提交
208 209

    const memberList  = this.parseMemberList(rawObj.MemberList)
210 211 212
    const nickMap     = this.parseMap(rawObj.MemberList, 'nick')
    const remarkMap   = this.parseMap(rawObj.MemberList, 'remark')
    const displayMap  = this.parseMap(rawObj.MemberList, 'display')
Huan (李卓桓)'s avatar
#104  
Huan (李卓桓) 已提交
213

Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
214
    return {
Huan (李卓桓)'s avatar
#104  
Huan (李卓桓) 已提交
215 216 217 218
      id:         rawObj.UserName,
      encryId:    rawObj.EncryChatRoomId, // ???
      topic:      rawObj.NickName,
      ownerUin:   rawObj.OwnerUin,
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
219

Huan (李卓桓)'s avatar
#104  
Huan (李卓桓) 已提交
220 221
      memberList,
      nickMap,
222 223
      remarkMap,
      displayMap,
224 225 226
    }
  }

227 228
  private parseMemberList(rawMemberList: RoomRawMember[]): Contact[] {
    if (!rawMemberList || !rawMemberList.map) {
229 230
      return []
    }
231
    return rawMemberList.map(m => Contact.load(m.UserName))
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
232
  }
233 234
  private parseMap(memberList: RoomRawMember[], parseContent: NameType): Map<string, string> {
    const mapList: Map<string, string> = new Map<string, string>()
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
235
    if (memberList && memberList.map) {
Huan (李卓桓)'s avatar
#104  
Huan (李卓桓) 已提交
236
      memberList.forEach(member => {
237 238 239 240 241 242 243 244 245 246 247 248 249 250 251
        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')
        }
252 253
        /**
         * ISSUE #64 emoji need to be striped
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
254
         * ISSUE #104 never use remark name because sys group message will never use that
255
         * @rui: cannot use argument NickName because it mix real nick and remark
256
         */
257
        mapList[member.UserName] = UtilLib.stripEmoji(tmpName)
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
258
      })
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
259
    }
260
    return mapList
261 262
  }

263
  public dumpRaw() {
264
    console.error('======= dump raw Room =======')
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
265
    Object.keys(this.rawObj).forEach(k => console.error(`${k}: ${this.rawObj[k]}`))
266
  }
267
  public dump() {
268
    console.error('======= dump Room =======')
269
    Object.keys(this.obj).forEach(k => console.error(`${k}: ${this.obj && this.obj[k]}`))
270 271
  }

Huan (李卓桓)'s avatar
#119  
Huan (李卓桓) 已提交
272
  public async add(contact: Contact): Promise<number> {
273
    log.verbose('Room', 'add(%s)', contact)
274 275 276 277 278

    if (!contact) {
      throw new Error('contact not found')
    }

Huan (李卓桓)'s avatar
#119  
Huan (李卓桓) 已提交
279 280 281
    const n = Config.puppetInstance()
                      .roomAdd(this, contact)
    return n
282 283
  }

Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
284
  public async del(contact: Contact): Promise<number> {
285
    log.verbose('Room', 'del(%s)', contact.name())
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
286 287 288 289

    if (!contact) {
      throw new Error('contact not found')
    }
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
290
    const n = await Config.puppetInstance()
Huan (李卓桓)'s avatar
#119  
Huan (李卓桓) 已提交
291 292
                            .roomDel(this, contact)
                            .then(_ => this.delLocal(contact))
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
293
    return n
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
294 295
  }

296
  private delLocal(contact: Contact): number {
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
297 298
    log.verbose('Room', 'delLocal(%s)', contact)

Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
299
    const memberList = this.obj && this.obj.memberList
300
    if (!memberList || memberList.length === 0) {
301
      return 0 // already in refreshing
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
302 303 304
    }

    let i
305 306
    for (i = 0; i < memberList.length; i++) {
      if (memberList[i].id === contact.id) {
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
307 308 309
        break
      }
    }
310 311
    if (i < memberList.length) {
      memberList.splice(i, 1)
312
      return 1
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
313
    }
314
    return 0
315
  }
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
316

317
  public quit() {
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
318 319
    throw new Error('wx web not implement yet')
    // WechatyBro.glue.chatroomFactory.quit("@@1c066dfcab4ef467cd0a8da8bec90880035aa46526c44f504a83172a9086a5f7"
320
  }
321

322 323 324 325 326 327 328 329 330 331
  /**
   * get topic
   */
  public topic(): string
  /**
   * set topic
   */
  public topic(newTopic: string): void

  public topic(newTopic?: string): string | void {
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
332
    if (!this.isReady()) {
333
      log.warn('Room', 'topic() room not ready')
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
334 335
    }

Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
336 337
    if (newTopic) {
      log.verbose('Room', 'topic(%s)', newTopic)
338
      Config.puppetInstance().roomTopic(this, newTopic)
339 340 341 342 343 344 345 346 347
                              .catch(e => {
                                log.warn('Room', 'topic(newTopic=%s) exception: %s',
                                                  newTopic, e && e.message || e
                                )
                              })
      if (!this.obj) {
        this.obj = <RoomObj>{}
      }
      Object.assign(this.obj, { topic: newTopic })
348
      return
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
349
    }
350
    return UtilLib.plainText(this.obj ? this.obj.topic : '')
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
351 352
  }

353
  public nick(contact: Contact): string {
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
354
    if (!this.obj || !this.obj.nickMap) {
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
355 356
      return ''
    }
357
    return this.obj.displayMap[contact.id] || this.obj.nickMap[contact.id]
358 359
  }

360
  public has(contact: Contact): boolean {
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
361
    if (!this.obj || !this.obj.memberList) {
362 363 364 365 366 367 368
      return false
    }
    return this.obj.memberList
                    .filter(c => c.id === contact.id)
                    .length > 0
  }

369
  public owner(): Contact | null {
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
370 371
    const ownerUin = this.obj && this.obj.ownerUin
    let memberList = (this.obj && this.obj.memberList) || []
372 373 374 375 376 377 378 379

    let user = Config.puppetInstance()
                      .user

    if (user && user.get('uin') === ownerUin) {
      return user
    }

380
    memberList = memberList.filter(m => m.get('uin') === ownerUin)
381 382 383
    if (memberList.length > 0) {
      return memberList[0]
    }
J
jaslin 已提交
384 385

    if (this.rawObj.ChatRoomOwner) {
386 387 388 389
      return Contact.load(this.rawObj.ChatRoomOwner)
    }

    return null
390 391
  }

Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
392
  /**
393 394
   * find member by `nick`(NickName) / `display`(DisplayName) / `remark`(RemarkName)
   * when use member(name:string), find all name by default
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
395
   */
396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413

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

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

    log.silly('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.')
    }
414

Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
415
    if (!this.obj || !this.obj.memberList) {
416
      log.warn('Room', 'member() not ready')
417 418
      return null
    }
419
    let filterKey            = Object.keys(queryArg)[0]
420 421 422
    /**
     * ISSUE #64 emoji need to be striped
     */
423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438
    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')
    }

    if (!filterValue) {
      throw new Error('filterValue not found')
    }
439

440 441 442
    const filterMap = this.obj[filterKey]
    const idList = Object.keys(filterMap)
                          .filter(k => filterMap[k] === filterValue)
443

444
    log.silly('Room', 'member() check %s: %s', filterKey, filterValue)
445

446 447 448 449 450
    if (idList.length) {
      return Contact.load(idList[0])
    } else {
      return null
    }
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
451 452
  }

453
  public memberList(): Contact[] {
454
    log.verbose('Room', 'memberList')
455 456 457

    if (!this.obj || !this.obj.memberList || this.obj.memberList.length < 1) {
      log.warn('Room', 'memberList() not ready')
458
      return []
459 460 461 462
    }
    return this.obj.memberList
  }

Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
463
  public static create(contactList: Contact[], topic?: string): Promise<Room> {
464
    log.verbose('Room', 'create(%s, %s)', contactList.join(','), topic)
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
465

Huan (李卓桓)'s avatar
bug fix  
Huan (李卓桓) 已提交
466
    if (!contactList || !Array.isArray(contactList)) {
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
467 468
      throw new Error('contactList not found')
    }
469

470
    return Config.puppetInstance()
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
471
                  .roomCreate(contactList, topic)
472 473 474
                  .catch(e => {
                    log.error('Room', 'create() exception: %s', e && e.stack || e.message || e)
                    throw e
475
                  })
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
476 477
  }

478 479 480 481
  public static async findAll(query?: RoomQueryFilter): Promise<Room[]> {
    if (!query) {
      query = { topic: /.*/ }
    }
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
482
    log.verbose('Room', 'findAll({ topic: %s })', query.topic)
483

Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
484
    let topicFilter = query.topic
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
485

486 487
    if (!topicFilter) {
      throw new Error('topicFilter not found')
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
488 489
    }

Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
490 491
    let filterFunction: string

492
    if (topicFilter instanceof RegExp) {
493
      filterFunction = `(function (c) { return ${topicFilter.toString()}.test(c) })`
494
    } else if (typeof topicFilter === 'string') {
495
      topicFilter = topicFilter.replace(/'/g, '\\\'')
496
      filterFunction = `(function (c) { return c === '${topicFilter}' })`
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
497
    } else {
498
      throw new Error('unsupport topic type')
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
499 500
    }

501 502 503
    return Config.puppetInstance()
                  .roomFind(filterFunction)
                  .catch(e => {
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
504
                    log.verbose('Room', 'findAll() rejected: %s', e.message)
505 506
                    return [] // fail safe
                  })
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
507 508
  }

Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
509
  public static async find(query: RoomQueryFilter): Promise<Room> {
510
    log.verbose('Room', 'find({ topic: %s })', query.topic)
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
511

Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
512 513 514 515
    const roomList = await Room.findAll(query)
    if (!roomList || roomList.length < 1) {
      throw new Error('no room found')
    }
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
516 517 518
    const room = roomList[0]
    await room.ready()
    return room
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
519 520
  }

521 522 523 524
  public static load(id: string): Room {
    if (!id) {
      throw new Error('Room.load() no id')
    }
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
525 526 527 528 529 530 531

    if (id in Room.pool) {
      return Room.pool[id]
    }
    return Room.pool[id] = new Room(id)
  }

Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
532
}