room-events.ts 4.3 KB
Newer Older
1
import { EventEmitter }   from 'events'
2
import type TypedEventEmitter  from 'typed-emitter'
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116

import {
  Contact,
  Message,
  Room,
  RoomInvitation,
}                   from '../user/mod'

export const ROOM_EVENT_DICT = {
  invite  : 'tbw',
  join    : 'tbw',
  leave   : 'tbw',
  message : 'message that received in this room',
  topic   : 'tbw',
}
export type RoomEventName = keyof typeof ROOM_EVENT_DICT

/**
 * @desc       Room Class Event Type
 * @typedef    RoomEventName
 * @property   {string}  join  - Emit when anyone join any room.
 * @property   {string}  topic - Get topic event, emitted when someone change room topic.
 * @property   {string}  leave - Emit when anyone leave the room.<br>
 *                               If someone leaves the room by themselves, WeChat will not notice other people in the room, so the bot will never get the "leave" event.
 */

/**
 * @desc       Room Class Event Function
 * @typedef    RoomEventFunction
 * @property   {Function} room-join       - (this: Room, inviteeList: Contact[] , inviter: Contact)  => void
 * @property   {Function} room-topic      - (this: Room, topic: string, oldTopic: string, changer: Contact) => void
 * @property   {Function} room-leave      - (this: Room, leaver: Contact) => void
 */

/**
 * @listens Room
 * @param   {RoomEventName}      event      - Emit WechatyEvent
 * @param   {RoomEventFunction}  listener   - Depends on the WechatyEvent
 * @return  {this}                          - this for chain
 *
 * @example <caption>Event:join </caption>
 * const bot = new Wechaty()
 * await bot.start()
 * // after logged in...
 * const room = await bot.Room.find({topic: 'topic of your room'}) // change `event-room` to any room topic in your WeChat
 * if (room) {
 *   room.on('join', (room, inviteeList, inviter) => {
 *     const nameList = inviteeList.map(c => c.name()).join(',')
 *     console.log(`Room got new member ${nameList}, invited by ${inviter}`)
 *   })
 * }
 *
 * @example <caption>Event:leave </caption>
 * const bot = new Wechaty()
 * await bot.start()
 * // after logged in...
 * const room = await bot.Room.find({topic: 'topic of your room'}) // change `event-room` to any room topic in your WeChat
 * if (room) {
 *   room.on('leave', (room, leaverList) => {
 *     const nameList = leaverList.map(c => c.name()).join(',')
 *     console.log(`Room lost member ${nameList}`)
 *   })
 * }
 *
 * @example <caption>Event:message </caption>
 * const bot = new Wechaty()
 * await bot.start()
 * // after logged in...
 * const room = await bot.Room.find({topic: 'topic of your room'}) // change `event-room` to any room topic in your WeChat
 * if (room) {
 *   room.on('message', (message) => {
 *     console.log(`Room received new message: ${message}`)
 *   })
 * }
 *
 * @example <caption>Event:topic </caption>
 * const bot = new Wechaty()
 * await bot.start()
 * // after logged in...
 * const room = await bot.Room.find({topic: 'topic of your room'}) // change `event-room` to any room topic in your WeChat
 * if (room) {
 *   room.on('topic', (room, topic, oldTopic, changer) => {
 *     console.log(`Room topic changed from ${oldTopic} to ${topic} by ${changer.name()}`)
 *   })
 * }
 *
 * @example <caption>Event:invite </caption>
 * const bot = new Wechaty()
 * await bot.start()
 * // after logged in...
 * const room = await bot.Room.find({topic: 'topic of your room'}) // change `event-room` to any room topic in your WeChat
 * if (room) {
 *   room.on('invite', roomInvitation => roomInvitation.accept())
 * }
 *
 */

export type RoomInviteEventListener  = (this: Room, inviter: Contact, invitation: RoomInvitation)                   => void
export type RoomJoinEventListener    = (this: Room, inviteeList: Contact[], inviter: Contact,  date?: Date)         => void
export type RoomLeaveEventListener   = (this: Room, leaverList: Contact[], remover?: Contact, date?: Date)          => void
export type RoomMessageEventListener = (this: Room, message: Message, date?: Date)                                  => void
export type RoomTopicEventListener   = (this: Room, topic: string, oldTopic: string, changer: Contact, date?: Date) => void

interface RoomEvents {
  invite  : RoomInviteEventListener
  join    : RoomJoinEventListener,
  leave   : RoomLeaveEventListener,
  message : RoomMessageEventListener,
  topic   : RoomTopicEventListener,
}

export const RoomEventEmitter = EventEmitter as new () => TypedEventEmitter<
  RoomEvents
>