Skip to content

Commit

Permalink
feat: Add groups to recipients
Browse files Browse the repository at this point in the history
  • Loading branch information
cballevre committed Mar 21, 2024
1 parent 2e09263 commit 4be590d
Show file tree
Hide file tree
Showing 4 changed files with 287 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ const MemberRecipientPermissions = ({
onRevoke,
onRevokeSelf,
sharingId,
index
memberIndex
}) => {
const { t } = useI18n()
const buttonRef = useRef()
Expand All @@ -47,12 +47,12 @@ const MemberRecipientPermissions = ({
const handleRevocation = useCallback(async () => {
setRevoking(true)
if (isOwner) {
await onRevoke(document, sharingId, index)
await onRevoke(document, sharingId, memberIndex)
} else {
await onRevokeSelf(document)
}
setRevoking(false)
}, [isOwner, onRevoke, onRevokeSelf, document, sharingId, index])
}, [isOwner, onRevoke, onRevokeSelf, document, sharingId, memberIndex])

const actions = makeActions([permission, divider, revokeMember], {
t,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ const RecipientList = ({
return (
<MemberRecipient
{...recipient}
key={`key_r_${recipient.index}`}
key={recipient.index}
isOwner={isOwner}
document={document}
documentType={documentType}
Expand Down
55 changes: 53 additions & 2 deletions packages/cozy-sharing/src/state.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import get from 'lodash/get'

import flag from 'cozy-flags'

const RECEIVE_SHARINGS = 'RECEIVE_SHARINGS'
const ADD_SHARING = 'ADD_SHARING'
const UPDATE_SHARING = 'UPDATE_SHARING'
Expand Down Expand Up @@ -280,14 +282,24 @@ export const getOwner = (state, docId) =>
getRecipients(state, docId).find(r => r.status === 'owner')

export const getRecipients = (state, docId) => {
const recipients = getDocumentSharings(state, docId)
const sharings = getDocumentSharings(state, docId)
if (flag('sharing.show-recipient-groups')) {
return getRecipientsWithGroups(sharings, docId)
}

return getRecipientsWithoutGroups(sharings, docId)
}

const getRecipientsWithoutGroups = (sharings, docId) => {
const recipients = sharings
.map(sharing => {
const type = getDocumentSharingType(sharing, docId)
return sharing.attributes.members.map((m, idx) => ({
...m,
index: `sharing-${sharing.id}-member-${idx}`,
type: m.read_only ? 'one-way' : type,
sharingId: sharing.id,
index: idx,
memberIndex: idx,
avatarPath: `/sharings/${sharing.id}/recipients/${idx}/avatar`
}))
})
Expand All @@ -299,6 +311,45 @@ export const getRecipients = (state, docId) => {
return recipients
}

export const getRecipientsWithGroups = (sharings, docId) => {
const recipients = sharings.flatMap(sharing => {
const type = getDocumentSharingType(sharing, docId)
const sharingId = sharing.id

const members = sharing.attributes.members.map((m, idx) => ({
...m,
type: m.read_only ? 'one-way' : type,
sharingId,
index: `sharing-${sharing.id}-member-${idx}`,
memberIndex: idx,
avatarPath: `/sharings/${sharingId}/recipients/${idx}/avatar`
}))

const groups =
sharing.attributes.groups
?.map((g, idx) => ({
...g,
sharingId,
index: `sharing-${sharing.id}-group-${idx}`,
groupIndex: idx,
owner: members[g.addedBy],
members: members.filter(member => member.groups?.includes(idx))
}))
.filter(g => !g.revoked) || []

return [
...members.filter(m => !m.only_in_groups && m.status !== 'revoked'),
...groups
]
})

if (recipients[0] && recipients[0].status === 'owner') {
return [recipients[0], ...recipients.filter(r => r.status !== 'owner')]
}

return recipients
}

export const getSharingLink = (state, docId, documentType) => {
// This shouldn't have happened, but unfortunately some duplicate sharing links have been created in the past
const perms = getDocumentPermissions(state, docId)
Expand Down

0 comments on commit 4be590d

Please sign in to comment.