11const fetch = require ( 'node-fetch' ) ;
22const { Errors : E } = require ( 'moleculer-web' ) ;
33const { MIME_TYPES } = require ( '@semapps/mime-types' ) ;
4+ const { getType, arrayOf } = require ( '@semapps/ldp' ) ;
45const { collectionPermissionsWithAnonRead, getSlugFromUri, objectIdToCurrent } = require ( '../../../utils' ) ;
56const { ACTOR_TYPES } = require ( '../../../constants' ) ;
67const AwaitActivityMixin = require ( '../../../mixins/await-activity' ) ;
@@ -41,7 +42,8 @@ const OutboxService = {
4142 } ,
4243 actions : {
4344 async post ( ctx ) {
44- let { collectionUri, username, ...activity } = ctx . params ;
45+ let { collectionUri, username, transient, ...activity } = ctx . params ;
46+ let activityUri ;
4547
4648 const collectionExists = await ctx . call ( 'activitypub.collection.exist' , { resourceUri : collectionUri } ) ;
4749 if ( ! collectionExists ) {
@@ -69,7 +71,10 @@ const OutboxService = {
6971 activity [ '@context' ] = await ctx . call ( 'jsonld.context.get' ) ;
7072 }
7173
72- if ( ! ctx . meta . doNotProcessObject ) {
74+ // Wrap object in Create activity, if necessary
75+ activity = await ctx . call ( 'activitypub.object.wrap' , { activity } ) ;
76+
77+ if ( ! ctx . meta . doNotProcessObject && transient !== true ) {
7378 // Process object create, update or delete
7479 // and return an activity with the object ID
7580 activity = await ctx . call ( 'activitypub.object.process' , { activity, actorUri } ) ;
@@ -80,42 +85,44 @@ const OutboxService = {
8085 }
8186
8287 // Use the current time for the activity's publish date
83- // TODO use it to order the ordered collections
8488 activity . published = new Date ( ) . toISOString ( ) ;
8589
86- const activitiesContainerUri = await ctx . call ( 'activitypub.activity.getContainerUri' , {
87- webId : actorUri
88- } ) ;
89-
90- // There might be a capability attached which should not be persisted
91- // because other's could otherwise read it from the (public) outbox.
92- let { capability, ...activityToPersist } = activity ;
90+ if ( transient === true ) {
91+ // Object or actor URI + hash with lower case activity
92+ activityUri = `${ activity . object || activity . actor } #${ arrayOf ( getType ( activity ) ) [ 0 ] . toLowerCase ( ) } ` ;
93+ activity = { id : activityUri , ...activity } ;
94+ } else {
95+ const activitiesContainerUri = await ctx . call ( 'activitypub.activity.getContainerUri' , {
96+ webId : actorUri
97+ } ) ;
9398
94- const activityUri = await ctx . call ( 'activitypub.activity.post' , {
95- containerUri : activitiesContainerUri ,
96- resource : activityToPersist ,
97- contentType : MIME_TYPES . JSON ,
98- webId : 'system' // Post as system since there is no write permission to the activities container
99- } ) ;
99+ activityUri = await ctx . call ( 'activitypub.activity.post' , {
100+ containerUri : activitiesContainerUri ,
101+ resource : activity ,
102+ contentType : MIME_TYPES . JSON ,
103+ webId : 'system' // Post as system since there is no write permission to the activities container
104+ } ) ;
100105
101- activityToPersist = await ctx . call ( 'activitypub.activity.get' , { resourceUri : activityUri , webId : 'system' } ) ;
102- // Reattach capability for further processing (if available).
103- activity = { ...activityToPersist , capability } ;
106+ activity = await ctx . call ( 'activitypub.activity.get' , { resourceUri : activityUri , webId : 'system' } ) ;
107+ }
104108
105109 try {
106- // Notify listeners of activities.
107110 await ctx . call ( 'activitypub.side-effects.processOutbox' , { activity } ) ;
108111 } catch ( e ) {
109- await ctx . call ( 'activitypub.activity.delete' , { resourceUri : activityUri , webId : 'system' } ) ;
112+ if ( transient !== true ) {
113+ await ctx . call ( 'activitypub.activity.delete' , { resourceUri : activityUri , webId : 'system' } ) ;
114+ }
110115 // TODO unprocess objects
111116 throw e ;
112117 }
113118
114- // Attach the newly-created activity to the outbox
115- await ctx . call ( 'activitypub.collection.add' , {
116- collectionUri,
117- item : activityToPersist
118- } ) ;
119+ if ( transient !== true ) {
120+ // Attach the newly-created activity to the outbox
121+ await ctx . call ( 'activitypub.collection.add' , {
122+ collectionUri,
123+ item : activity
124+ } ) ;
125+ }
119126
120127 const localRecipients = [ ] ;
121128 const remoteRecipients = [ ] ;
0 commit comments