Skip to content

Commit

Permalink
enh(participants): extract patchParticipants action, reuse in breakou…
Browse files Browse the repository at this point in the history
…tRooms store

Signed-off-by: Maksim Sukharev <antreesy.web@gmail.com>
  • Loading branch information
Antreesy committed Jan 17, 2024
1 parent ae3de4c commit 83b1179
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 44 deletions.
26 changes: 10 additions & 16 deletions src/store/breakoutRoomsStore.js
Original file line number Diff line number Diff line change
Expand Up @@ -200,23 +200,17 @@ const actions = {
async getBreakoutRoomsParticipantsAction(context, { token }) {
try {
const response = await getBreakoutRoomsParticipants(token)

// Purge the participants of the breakout rooms before adding the updated ones
context.state.breakoutRooms[token].forEach(breakoutRoom => {
context.commit('purgeParticipantsStore', breakoutRoom.token)
const splittedParticipants = response.data.ocs.data.reduce((acc, participant) => {
if (!acc[participant.roomToken]) {
acc[participant.roomToken] = []
}
acc[participant.roomToken].push(participant)
return acc
}, {})

Object.entries(splittedParticipants).forEach(([token, newParticipants]) => {
context.dispatch('patchParticipants', { token, newParticipants, hasUserStatuses: false })
})

// Purge the participants of the main room
context.commit('purgeParticipantsStore', token)

// Add the participants of the breakout rooms to the participants store
response.data.ocs.data.forEach(participant => {
context.dispatch('addParticipant', {
token: participant.roomToken,
participant,
})
})

} catch (error) {
console.error(error)
}
Expand Down
69 changes: 41 additions & 28 deletions src/store/participantsStore.js
Original file line number Diff line number Diff line change
Expand Up @@ -627,7 +627,6 @@ const actions = {
* @return {object|null}
*/
async fetchParticipants(context, { token }) {
const guestNameStore = useGuestNameStore()
// Cancel a previous request
context.dispatch('cancelFetchParticipants')
// Get a new cancelable request function and cancel function pair
Expand All @@ -639,33 +638,7 @@ const actions = {
const response = await request(token)
const hasUserStatuses = !!response.headers['x-nextcloud-has-user-statuses']

const currentParticipants = context.state.attendees[token]
const newParticipants = response.data.ocs.data
for (const attendeeId of Object.keys(Object(currentParticipants))) {
if (!newParticipants.some(participant => participant.attendeeId === +attendeeId)) {
context.commit('deleteParticipant', { token, attendeeId })
}
}

newParticipants.forEach(participant => {
if (context.state.attendees[token]?.[participant.attendeeId]) {
context.dispatch('updateParticipantIfHasChanged', { token, participant, hasUserStatuses })
} else {
context.dispatch('addParticipant', { token, participant })
if (hasUserStatuses) {
emitUserStatusUpdated(participant)
}
}

if (participant.participantType === PARTICIPANT.TYPE.GUEST
|| participant.participantType === PARTICIPANT.TYPE.GUEST_MODERATOR) {
guestNameStore.addGuestName({
token,
actorId: Hex.stringify(SHA1(participant.sessionIds[0])),
actorDisplayName: participant.displayName,
}, { noUpdate: false })
}
})
context.dispatch('patchParticipants', { token, newParticipants: response.data.ocs.data, hasUserStatuses })

if (context.state.initialised[token] === false) {
context.commit('setParticipantsInitialised', { token, initialised: true })
Expand All @@ -685,6 +658,46 @@ const actions = {
}
},

/**
* Update participants in the store with specified token.
*
* @param {object} context default store context;
* @param {object} data the wrapping object;
* @param {string} data.token the conversation token;
* @param {object} data.newParticipants the participant array;
* @param {boolean} data.hasUserStatuses whether participants has user statuses or not;
*/
async patchParticipants(context, { token, newParticipants, hasUserStatuses }) {
const guestNameStore = useGuestNameStore()

const currentParticipants = context.state.attendees[token]
for (const attendeeId of Object.keys(Object(currentParticipants))) {
if (!newParticipants.some(participant => participant.attendeeId === +attendeeId)) {
context.commit('deleteParticipant', { token, attendeeId })
}
}

newParticipants.forEach(participant => {
if (context.state.attendees[token]?.[participant.attendeeId]) {
context.dispatch('updateParticipantIfHasChanged', { token, participant, hasUserStatuses })
} else {
context.dispatch('addParticipant', { token, participant })
if (hasUserStatuses) {
emitUserStatusUpdated(participant)
}
}

if (participant.participantType === PARTICIPANT.TYPE.GUEST
|| participant.participantType === PARTICIPANT.TYPE.GUEST_MODERATOR) {
guestNameStore.addGuestName({
token,
actorId: Hex.stringify(SHA1(participant.sessionIds[0])),
actorDisplayName: participant.displayName,
}, { noUpdate: false })
}
})
},

/**
* Update participant in store according to a new participant object
*
Expand Down

0 comments on commit 83b1179

Please sign in to comment.