@@ -12,7 +12,7 @@ const {
1212 getSupportedContainers
1313} = require ( '../../../config/shared/activities' ) ;
1414const { containerRegistry } = require ( '../content-plugins' ) ;
15- const difference = require ( 'lodash/difference ' ) ;
15+ const differenceWith = require ( 'lodash/differenceWith ' ) ;
1616const filter = require ( 'lodash/filter' ) ;
1717const find = require ( 'lodash/find' ) ;
1818const findIndex = require ( 'lodash/findIndex' ) ;
@@ -35,6 +35,8 @@ const CC_ATTRS = ['id', 'uid', 'type', 'position', 'createdAt', 'updatedAt'];
3535function publishActivity ( activity ) {
3636 return getStructureData ( activity ) . then ( data => {
3737 const { repository, predecessors, spine } = data ;
38+ const prevPublishedContainers = spine . structure
39+ . reduce ( ( acc , it ) => [ ...acc , ...( it . contentContainers || [ ] ) ] , [ ] ) ;
3840
3941 predecessors . forEach ( it => {
4042 const exists = find ( spine . structure , { id : it . id } ) ;
@@ -45,6 +47,11 @@ function publishActivity(activity) {
4547 addToSpine ( spine , activity ) ;
4648
4749 return publishContent ( activity )
50+ . then ( async content => {
51+ const { containers } = content ;
52+ await unpublishDeletedContainers ( activity , prevPublishedContainers , containers ) ;
53+ return content ;
54+ } )
4855 . then ( content => {
4956 const publishedData = find ( spine . structure , { id : activity . id } ) ;
5057 return attachContentSummary ( publishedData , content ) ;
@@ -135,10 +142,7 @@ async function fetchActivityContent(activity, signed = false) {
135142}
136143
137144function publishContent ( activity ) {
138- return publishContainers ( activity ) . then ( async containers => {
139- await unpublishDeletedContainers ( activity , containers ) ;
140- return { containers } ;
141- } ) ;
145+ return publishContainers ( activity ) . then ( containers => ( { containers } ) ) ;
142146}
143147
144148function publishContainers ( parent ) {
@@ -195,21 +199,12 @@ async function fetchCustomContainers(parent, config) {
195199 } , [ ] ) ;
196200}
197201
198- function unpublishDeletedContainers ( parent , containers ) {
202+ function unpublishDeletedContainers ( parent , prevContainers , containers ) {
199203 const baseUrl = getBaseUrl ( parent . repositoryId , parent . id ) ;
204+ const prevFilePaths = getContainersFilePaths ( baseUrl , prevContainers ) ;
200205 const filePaths = getContainersFilePaths ( baseUrl , containers ) ;
201- const assetsPath = storage . getPath ( parent . repositoryId ) ;
202- return storage
203- . listFiles ( baseUrl )
204- . then ( publishedFilePaths => {
205- publishedFilePaths = removeAssetsFilePath ( publishedFilePaths , assetsPath ) ;
206- const redundantFilePaths = difference ( publishedFilePaths , filePaths ) ;
207- if ( redundantFilePaths . length ) return storage . deleteFiles ( redundantFilePaths ) ;
208- } ) ;
209- }
210-
211- function removeAssetsFilePath ( publishedFilePaths , assetsPath ) {
212- return publishedFilePaths . filter ( it => ! it . startsWith ( assetsPath ) ) ;
206+ const redundantFilePaths = differenceWith ( prevFilePaths , filePaths ) ;
207+ if ( redundantFilePaths . length ) return storage . deleteFiles ( redundantFilePaths ) ;
213208}
214209
215210function resolveContainer ( container ) {
0 commit comments