You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

middleware.ts 3.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. import { JitsiConferenceEvents } from '../base/lib-jitsi-meet';
  2. import { getParticipantById } from '../base/participants/functions';
  3. import MiddlewareRegistry from '../base/redux/MiddlewareRegistry';
  4. import StateListenerRegistry from '../base/redux/StateListenerRegistry';
  5. import { editMessage } from '../chat/actions.any';
  6. import { MESSAGE_TYPE_REMOTE } from '../chat/constants';
  7. import { UPDATE_BREAKOUT_ROOMS } from './actionTypes';
  8. import { moveToRoom } from './actions';
  9. import logger from './logger';
  10. import { IRooms } from './types';
  11. /**
  12. * Registers a change handler for state['features/base/conference'].conference to
  13. * set the event listeners needed for the breakout rooms feature to operate.
  14. */
  15. StateListenerRegistry.register(
  16. state => state['features/base/conference'].conference,
  17. (conference, { dispatch }, previousConference) => {
  18. if (conference && !previousConference) {
  19. conference.on(JitsiConferenceEvents.BREAKOUT_ROOMS_MOVE_TO_ROOM, (roomId: string) => {
  20. logger.debug(`Moving to room: ${roomId}`);
  21. dispatch(moveToRoom(roomId));
  22. });
  23. conference.on(JitsiConferenceEvents.BREAKOUT_ROOMS_UPDATED, ({ rooms, roomCounter }: {
  24. roomCounter: number; rooms: IRooms;
  25. }) => {
  26. logger.debug('Room list updated');
  27. if (typeof APP !== 'undefined') {
  28. APP.API.notifyBreakoutRoomsUpdated(rooms);
  29. }
  30. dispatch({
  31. type: UPDATE_BREAKOUT_ROOMS,
  32. rooms,
  33. roomCounter
  34. });
  35. });
  36. }
  37. });
  38. MiddlewareRegistry.register(({ dispatch, getState }) => next => action => {
  39. const { type } = action;
  40. switch (type) {
  41. case UPDATE_BREAKOUT_ROOMS: {
  42. // edit name if it was overwritten
  43. if (!action.updatedNames) {
  44. const { overwrittenNameList } = getState()['features/base/participants'];
  45. if (Object.keys(overwrittenNameList).length > 0) {
  46. const newRooms: IRooms = {};
  47. Object.entries(action.rooms as IRooms).forEach(([ key, r ]) => {
  48. let participants = r?.participants || {};
  49. let jid;
  50. for (const id of Object.keys(overwrittenNameList)) {
  51. jid = Object.keys(participants).find(p => p.slice(p.indexOf('/') + 1) === id);
  52. if (jid) {
  53. participants = {
  54. ...participants,
  55. [jid]: {
  56. ...participants[jid],
  57. displayName: overwrittenNameList[id as keyof typeof overwrittenNameList]
  58. }
  59. };
  60. }
  61. }
  62. newRooms[key] = {
  63. ...r,
  64. participants
  65. };
  66. });
  67. action.rooms = newRooms;
  68. }
  69. }
  70. // edit the chat history to match names for participants in breakout rooms
  71. const { messages } = getState()['features/chat'];
  72. messages?.forEach(m => {
  73. if (m.messageType === MESSAGE_TYPE_REMOTE && !getParticipantById(getState(), m.participantId)) {
  74. const rooms: IRooms = action.rooms;
  75. for (const room of Object.values(rooms)) {
  76. const participants = room.participants || {};
  77. const matchedJid = Object.keys(participants).find(jid => jid.endsWith(m.participantId));
  78. if (matchedJid) {
  79. m.displayName = participants[matchedJid].displayName;
  80. dispatch(editMessage(m));
  81. }
  82. }
  83. }
  84. });
  85. break;
  86. }
  87. }
  88. return next(action);
  89. });