Skip to content

Commit

Permalink
fix: Delete File
Browse files Browse the repository at this point in the history
- We should not remove the reference before
deleting the file. cozy-stack needs to know
if the document was shared before moving it
to the Trash in order to revoke the sharing

- Use the route to remove all the referenced_by
by doing only one request instead of one by
reference.
- Since there is a bug ATM in cozy-stack we check
at both place for the data
  • Loading branch information
Crash-- committed Nov 15, 2022
1 parent 931587e commit 47e4e07
Showing 1 changed file with 12 additions and 11 deletions.
23 changes: 12 additions & 11 deletions packages/cozy-stack-client/src/FileCollection.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ import logger from './logger'
* @property {FileAttributes} attributes - Attributes of the file
* @property {object} meta - Meta
* @property {object} relationships - Relationships
* @property {object} referenced_by - Referenced by
*/

/**
Expand Down Expand Up @@ -372,17 +373,7 @@ class FileCollection extends DocumentCollection {
*/
async destroy(file, { ifMatch = '' } = {}) {
const { _id, relationships } = file
if (
relationships &&
relationships.referenced_by &&
Array.isArray(relationships.referenced_by.data)
) {
for (const ref of relationships.referenced_by.data) {
await this.removeReferencesTo({ _id: ref.id, _type: ref.type }, [
{ _id }
])
}
}

const resp = await this.stackClient.fetchJSON(
'DELETE',
uri`/files/${_id}`,
Expand All @@ -393,6 +384,16 @@ class FileCollection extends DocumentCollection {
}
}
)
// needed because we had a bug in cozy-stack https://github.com/cozy/cozy-stack/pull/3566
// to remove once the code is deployed everywhere
const references = get(
relationships,
'referenced_by.data',
file.referenced_by
)
if (Array.isArray(references)) {
await this.removeReferencedBy(file, references)
}
return {
data: normalizeFile(resp.data)
}
Expand Down

0 comments on commit 47e4e07

Please sign in to comment.