Skip to content

Commit

Permalink
Merge pull request #653 from enguerran/feat.share
Browse files Browse the repository at this point in the history
fix: adapt share process with new stack
  • Loading branch information
enguerran authored Jan 9, 2018
2 parents 7c60fc9 + 5163ba3 commit f2a77e5
Show file tree
Hide file tree
Showing 15 changed files with 420 additions and 305 deletions.
17 changes: 10 additions & 7 deletions .babelrc
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
{
"presets": [
["env", {
"targets": {
"chrome": 42,
"browsers": ["last 2 versions"]
},
"useBuiltIns": true
}],
[
"env",
{
"targets": {
"chrome": 42,
"browsers": ["last 2 versions"]
},
"useBuiltIns": true
}
],
"react"
],
"plugins": ["transform-object-rest-spread", "transform-class-properties"]
Expand Down
15 changes: 9 additions & 6 deletions src/drive/locales/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -55,14 +55,14 @@
"revoked": "Error"
},
"type": {
"master-slave": "Can View",
"master-master": "Can Change"
"one-way": "Can View (coming soon)",
"two-way": "Can Change"
}
},
"Files": {
"share": {
"cta": "Share",
"title": "Share with others",
"title": "Share",
"details": {
"title": "Sharing details",
"createdAt": "On %{date}",
Expand All @@ -77,9 +77,7 @@
"desc": "Anyone with the provided link can see and download your files."
},
"shareByEmail": {
"title": "By email",
"subtitle": "Share by email",
"email": "email",
"email": "To:",
"emailPlaceholder": "Enter email address or name of your recipient",
"send": "Send",
"genericSuccess": "You sent an invite to %{count} contacts.",
Expand Down Expand Up @@ -108,6 +106,11 @@
"error": {
"generic":
"An error occurred when creating the file share link, please try again."
},
"specialCase": {
"base": "This %{type} cannot be shared but with a link as it",
"isInSharedFolder": "is in a shared folder",
"hasSharedFolder": "contains a shared folder"
}
},
"viewer-fallback":
Expand Down
4 changes: 2 additions & 2 deletions src/lib/cozy-client/CozyClient.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import SharingsCollection, {
} from './collections/SharingsCollection'
import AppsCollection, { APPS_DOCTYPE } from './collections/AppsCollection'
import { authenticateWithCordova } from './authentication/mobile'
import { getIndexFields, isV2 } from './helpers'
import { getIndexFields, isV2 as isV2Helper } from './helpers'

const FILES_DOCTYPE = 'io.cozy.files'

Expand Down Expand Up @@ -49,7 +49,7 @@ export default class CozyClient {
}

isV2(cozyURL) {
return isV2(cozyURL)
return isV2Helper(cozyURL)
}

async isRegistered(clientInfos) {
Expand Down
2 changes: 1 addition & 1 deletion src/lib/cozy-client/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ Depending on whether this action creator is called with or without a document ID
withMe: <boolean>,
byLink: <boolean>,
readOnly: <boolean>,
sharingType: 'master-slave' | 'master-master',
sharingType: 'one-way' | 'two-way',
owner
}
```
Expand Down
53 changes: 24 additions & 29 deletions src/lib/cozy-client/collections/SharingsCollection.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,13 @@ export const SHARED_WITH_OTHERS = 'sharedWithOthers'
export default class SharingsCollection {
// TODO: find a more standard arg list
create(permissions, contactIds, sharingType, description) {
return cozy.client.fetchJSON('POST', '/sharings/', {
desc: description,
const payload = {
description,
permissions,
recipients: contactIds.map(contactId => ({
recipient: {
id: contactId,
type: 'io.cozy.contacts'
}
})),
recipients: contactIds,
sharing_type: sharingType
})
}
return cozy.client.fetchJSON('POST', '/sharings/', payload)
}

destroy(id) {
Expand All @@ -27,38 +23,37 @@ export default class SharingsCollection {

async findByDoctype(doctype) {
const permissions = await this.fetchSharingPermissions(doctype)
const sharingIds = [
...permissions.byMe.map(p => p.attributes.source_id),
...permissions.withMe.map(p => p.attributes.source_id)
const getSharingsURL = permission => permission.links.related
const sharingURLs = [
...permissions.byMe.map(getSharingsURL),
...permissions.withMe.map(getSharingsURL)
]
const sharings = await Promise.all(
sharingIds.map(id => this.fetchSharing(id))
sharingURLs.map(url => this.fetchSharing(url))
)
return { permissions, sharings }
}

async fetchSharingPermissions(doctype) {
const fetchPermissions = (doctype, sharingType) =>
cozy.client.fetchJSON(
'GET',
`/permissions/doctype/${doctype}/${sharingType}`
)
cozy.client
.fetchJSON('GET', `/permissions/doctype/${doctype}/${sharingType}`)
.catch(e => {
console.error(e)
return []
})

// if we catch an exception (server's error), we init values with empty array
const byMe = await fetchPermissions(doctype, SHARED_WITH_OTHERS).catch(
() => []
)
const byLink = await fetchPermissions(doctype, SHARED_BY_LINK).catch(
() => []
)
const withMe = await fetchPermissions(doctype, SHARED_WITH_ME).catch(
() => []
)
const byMe = await fetchPermissions(doctype, SHARED_WITH_OTHERS)
const byLink = await fetchPermissions(doctype, SHARED_BY_LINK)
const withMe = await fetchPermissions(doctype, SHARED_WITH_ME)
return { byMe, byLink, withMe }
}

fetchSharing(id) {
return cozy.client.fetchJSON('GET', `/sharings/${id}`).catch(() => ({}))
fetchSharing(url) {
return cozy.client.fetchJSON('GET', url).catch(e => {
console.error(e)
return {}
})
}

revoke(sharingId) {
Expand Down
37 changes: 18 additions & 19 deletions src/lib/cozy-client/slices/sharings.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ const permissions = (state = {}, action) => {
{
attributes: {
permissions: {
rule0: { type: action.doctype, values: [action.id] }
files: { type: action.doctype, values: [action.id] }
},
source_id: action.response.attributes.sharing_id,
type: 'io.cozy.sharings'
Expand Down Expand Up @@ -157,7 +157,9 @@ export const share = (document, recipients, sharingType, sharingDesc) => async (
recipients.map(
recipient =>
recipient.id ||
dispatch(createContact(recipient)).then(c => c.data[0].id)
dispatch(createContact(recipient))
.then(response => response.data[0])
.then(contact => contact.id)
)
)
trackSharingByEmail(document)
Expand Down Expand Up @@ -233,7 +235,7 @@ export const revokeLink = document => async (dispatch, getState) => {
const createSharing = (
document,
contactIds,
sharingType = 'master-slave',
sharingType = 'two-way',
description = ''
) => ({
types: [CREATE_SHARING, RECEIVE_NEW_SHARING, RECEIVE_ERROR],
Expand Down Expand Up @@ -344,12 +346,11 @@ const getSharing = (state, id) =>
s => s.attributes && s.attributes.sharing_id === id
)
const getContact = (state, id) => getDocument(state, 'io.cozy.contacts', id)
const getDoctypePermissions = (state, doctype) => {
if (state.cozy.sharings.permissions[doctype]) {
return state.cozy.sharings.permissions[doctype]
}
return doctypePermsetInitialState
}

const getDoctypePermissions = (state, doctype) =>
state.cozy.sharings.permissions[doctype]
? state.cozy.sharings.permissions[doctype]
: doctypePermsetInitialState

const getSharingLink = (state, doctype, id) => {
const perm = getSharingLinkPermission(state, doctype, id)
Expand Down Expand Up @@ -393,10 +394,10 @@ const getDocumentActiveSharings = (state, doctype, id) => {
const perms = getDoctypePermissions(state, doctype)
return [
...perms.byMe.filter(
perm => perm.attributes.permissions['rule0'].values.indexOf(id) !== -1
perm => perm.attributes.permissions.files.values.indexOf(id) !== -1
),
...perms.withMe.filter(
perm => perm.attributes.permissions['rule0'].values.indexOf(id) !== -1
perm => perm.attributes.permissions.rule0.values.indexOf(id) !== -1
)
]
.map(p => getSharing(state, p.attributes.source_id))
Expand All @@ -407,8 +408,8 @@ export const getSharings = (state, doctype, options = {}) => {
const perms = getDoctypePermissions(state, doctype)
const type = doctype === 'io.cozy.files' ? 'files' : 'collection'
return {
byMe: perms.byMe.map(p => p.attributes.permissions['rule0'].values[0]),
withMe: perms.withMe.map(p => p.attributes.permissions['rule0'].values[0]),
byMe: perms.byMe.map(p => p.attributes.permissions.files.values[0]),
withMe: perms.withMe.map(p => p.attributes.permissions.rule0.values[0]),
byLink: perms.byLink.map(p => p.attributes.permissions[type].values[0])
}
}
Expand All @@ -419,11 +420,9 @@ export const getSharingStatus = (state, doctype, id) => {
shared: sharings.length !== 0,
owner:
sharings.length === 0 || sharings.some(s => s.attributes.owner === true),
sharingType: sharings.some(
s => s.attributes.sharing_type === 'master-master'
)
? 'master-master'
: 'master-slave',
sharingType: sharings.some(s => s.attributes.sharing_type === 'two-way')
? 'two-way'
: 'one-way',
sharings
}
}
Expand All @@ -444,7 +443,7 @@ export const getSharingDetails = (state, doctype, id, options = {}) => {
shared && !owner
? { name: 'John Doe', url: sharings[0].attributes.sharer.url }
: null,
readOnly: !owner && sharingType === 'master-slave',
readOnly: !owner && sharingType === 'one-way',
recipients: shared && owner ? getSharingRecipients(state, sharings) : [],
byMe: shared && owner === true,
withMe: shared && !owner,
Expand Down
2 changes: 1 addition & 1 deletion src/photos/containers/AlbumItem.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ const AlbumItemDesc = ({ t, photoCount, shared, thumbnail }) => (
{(shared.byMe || shared.withMe) &&
` - ${t(
`Albums.album_item_shared_${
shared.sharingType === 'master-slave' ? 'ro' : 'rw'
shared.sharingType === 'one-way' ? 'ro' : 'rw'
}`
)}`}
</h4>
Expand Down
10 changes: 6 additions & 4 deletions src/photos/locales/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,8 @@
"revoked": "Error"
},
"type": {
"master-slave": "Can View",
"master-master": "Can Change"
"one-way": "Can View (coming soon)",
"two-way": "Can Change"
}
},
"Albums": {
Expand Down Expand Up @@ -147,7 +147,8 @@
"send": "Send",
"genericSuccess": "You sent an invite to %{count} contacts.",
"success": "You sent an invite to %{email}.",
"comingsoon": "Coming soon! You will be able to share documents and photos in a single click with your family, your friends, and even your coworkers. Don't worry, we'll let you know when it's ready!"
"comingsoon":
"Coming soon! You will be able to share documents and photos in a single click with your family, your friends, and even your coworkers. Don't worry, we'll let you know when it's ready!"
},
"unshare": {
"title": "Remove from album",
Expand Down Expand Up @@ -232,7 +233,8 @@
"download": "Download"
},
"loading": {
"error": "This file could not be loaded. Do you have a working internet connection right now?",
"error":
"This file could not be loaded. Do you have a working internet connection right now?",
"retry": "Retry"
}
},
Expand Down
Loading

0 comments on commit f2a77e5

Please sign in to comment.