|
| 1 | +import { XMPPEvents } from '../../service/xmpp/XMPPEvents'; |
| 2 | + |
| 3 | +import ChatRoom from './ChatRoom'; |
| 4 | +import XMPP from './xmpp'; |
| 5 | + |
| 6 | +export const COMMAND_ANSWER_POLL = 'answer-poll'; |
| 7 | +export const COMMAND_NEW_POLL = 'new-poll'; |
| 8 | +export const COMMAND_OLD_POLLS = 'old-polls'; |
| 9 | + |
| 10 | +export default class Polls { |
| 11 | + private _mainRoom: ChatRoom; |
| 12 | + private _xmpp: XMPP; |
| 13 | + |
| 14 | + /** |
| 15 | + * Constructs polls for a room. |
| 16 | + * |
| 17 | + * @param {ChatRoom} room the main room. |
| 18 | + */ |
| 19 | + constructor(room: ChatRoom) { |
| 20 | + this._mainRoom = room; |
| 21 | + this._xmpp = room.xmpp; |
| 22 | + |
| 23 | + this._handleMessages = this._handleMessages.bind(this); |
| 24 | + this._mainRoom.xmpp.addListener(XMPPEvents.POLLS_EVENT, this._handleMessages); |
| 25 | + } |
| 26 | + |
| 27 | + /** |
| 28 | + * Whether polls is supported on backend. |
| 29 | + * |
| 30 | + * @returns {boolean} whether polls is supported on backend. |
| 31 | + */ |
| 32 | + isSupported() { |
| 33 | + return Boolean(this._xmpp.pollsComponentAddress); |
| 34 | + } |
| 35 | + |
| 36 | + /** |
| 37 | + * Stops listening for events. |
| 38 | + */ |
| 39 | + dispose() { |
| 40 | + this._mainRoom.xmpp.removeListener(XMPPEvents.POLLS_EVENT, this._handleMessages); |
| 41 | + } |
| 42 | + |
| 43 | + /** |
| 44 | + * Creates and sends a new poll. |
| 45 | + * |
| 46 | + * @param pollId |
| 47 | + * @param question |
| 48 | + * @param answers |
| 49 | + */ |
| 50 | + createPoll(pollId: string, question: string, answers: Array<{ name: string; }>) { |
| 51 | + this._mainRoom.sendPrivateMessage( |
| 52 | + this._xmpp.pollsComponentAddress, |
| 53 | + JSON.stringify({ |
| 54 | + answers: answers, |
| 55 | + command: COMMAND_NEW_POLL, |
| 56 | + pollId, |
| 57 | + question, |
| 58 | + type: 'polls' |
| 59 | + }), |
| 60 | + 'json-message', |
| 61 | + true); |
| 62 | + } |
| 63 | + |
| 64 | + /** |
| 65 | + * Sends answers for a poll. |
| 66 | + * |
| 67 | + * @param pollId |
| 68 | + * @param answers |
| 69 | + */ |
| 70 | + answerPoll(pollId: string, answers: Array<boolean>) { |
| 71 | + this._mainRoom.sendPrivateMessage( |
| 72 | + this._xmpp.pollsComponentAddress, |
| 73 | + JSON.stringify({ |
| 74 | + answers, |
| 75 | + command: COMMAND_ANSWER_POLL, |
| 76 | + pollId, |
| 77 | + type: 'polls' |
| 78 | + }), |
| 79 | + 'json-message', |
| 80 | + true); |
| 81 | + } |
| 82 | + |
| 83 | + /** |
| 84 | + * Handles a message for polls. |
| 85 | + * |
| 86 | + * @param {object} payload - Arbitrary data. |
| 87 | + */ |
| 88 | + _handleMessages(payload) { |
| 89 | + switch (payload.command) { |
| 90 | + case COMMAND_NEW_POLL: |
| 91 | + this._mainRoom.eventEmitter.emit(XMPPEvents.POLLS_RECEIVE_EVENT, payload); |
| 92 | + |
| 93 | + break; |
| 94 | + case COMMAND_OLD_POLLS: { |
| 95 | + payload?.polls?.forEach((poll: any) => { |
| 96 | + this._mainRoom.eventEmitter.emit(XMPPEvents.POLLS_RECEIVE_EVENT, { |
| 97 | + history: true, |
| 98 | + ...poll |
| 99 | + }); |
| 100 | + }); |
| 101 | + break; |
| 102 | + } |
| 103 | + case COMMAND_ANSWER_POLL: { |
| 104 | + this._mainRoom.eventEmitter.emit(XMPPEvents.POLLS_ANSWER_EVENT, payload); |
| 105 | + break; |
| 106 | + } |
| 107 | + } |
| 108 | + } |
| 109 | +} |
0 commit comments