您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

subscriber.js 4.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. // @flow
  2. import { getCurrentConference } from '../base/conference';
  3. import { JitsiConferenceEvents } from '../base/lib-jitsi-meet';
  4. import { StateListenerRegistry } from '../base/redux';
  5. import {
  6. NOTIFICATION_TIMEOUT,
  7. NOTIFICATION_TYPE,
  8. showNotification
  9. } from '../notifications';
  10. import { receiveAnswer, receivePoll } from './actions';
  11. import { COMMAND_NEW_POLL, COMMAND_ANSWER_POLL, COMMAND_OLD_POLLS } from './constants';
  12. import type { Answer, Poll } from './types';
  13. const parsePollData = (pollData): Poll | null => {
  14. if (typeof pollData !== 'object' || pollData === null) {
  15. return null;
  16. }
  17. const { id, senderId, senderName, question, answers } = pollData;
  18. if (typeof id !== 'string' || typeof senderId !== 'string' || typeof senderName !== 'string'
  19. || typeof question !== 'string' || !(answers instanceof Array)) {
  20. return null;
  21. }
  22. const answersParsed = [];
  23. for (const answer of answers) {
  24. const voters = new Map();
  25. for (const [ voterId, voter ] of Object.entries(answer.voters)) {
  26. if (typeof voter !== 'string') {
  27. return null;
  28. }
  29. voters.set(voterId, voter);
  30. }
  31. answersParsed.push({
  32. name: answer.name,
  33. voters
  34. });
  35. }
  36. return {
  37. changingVote: false,
  38. senderId,
  39. senderName,
  40. question,
  41. showResults: true,
  42. lastVote: null,
  43. answers: answersParsed
  44. };
  45. };
  46. StateListenerRegistry.register(
  47. state => getCurrentConference(state),
  48. (conference, store, previousConference) => {
  49. if (conference && conference !== previousConference) {
  50. const receiveMessage = (_, data) => {
  51. switch (data.type) {
  52. case COMMAND_NEW_POLL: {
  53. const { question, answers, pollId, senderId, senderName } = data;
  54. const poll = {
  55. changingVote: false,
  56. senderId,
  57. senderName,
  58. showResults: false,
  59. lastVote: null,
  60. question,
  61. answers: answers.map(answer => {
  62. return {
  63. name: answer,
  64. voters: new Map()
  65. };
  66. })
  67. };
  68. store.dispatch(receivePoll(pollId, poll, true));
  69. store.dispatch(showNotification({
  70. appearance: NOTIFICATION_TYPE.NORMAL,
  71. titleKey: 'polls.notification.title',
  72. descriptionKey: 'polls.notification.description'
  73. }, NOTIFICATION_TIMEOUT));
  74. break;
  75. }
  76. case COMMAND_ANSWER_POLL: {
  77. const { pollId, answers, voterId, voterName } = data;
  78. const receivedAnswer: Answer = {
  79. voterId,
  80. voterName,
  81. pollId,
  82. answers
  83. };
  84. store.dispatch(receiveAnswer(pollId, receivedAnswer));
  85. break;
  86. }
  87. case COMMAND_OLD_POLLS: {
  88. const { polls } = data;
  89. for (const pollData of polls) {
  90. const poll = parsePollData(pollData);
  91. if (poll === null) {
  92. console.warn('[features/polls] Invalid old poll data');
  93. } else {
  94. store.dispatch(receivePoll(pollData.id, poll, false));
  95. }
  96. }
  97. break;
  98. }
  99. }
  100. };
  101. conference.on(JitsiConferenceEvents.ENDPOINT_MESSAGE_RECEIVED, receiveMessage);
  102. conference.on(JitsiConferenceEvents.NON_PARTICIPANT_MESSAGE_RECEIVED, receiveMessage);
  103. }
  104. }
  105. );