@@ -56,6 +56,10 @@ app.use(express.urlencoded({
5656app . use ( cors ( ) ) ;
5757
5858const userState = { } ;
59+
60+ // store subscription requests
61+ let subscriptionRequests = [ ] ;
62+
5963/**
6064 * sleep function
6165 */
@@ -178,6 +182,82 @@ app.post('/login', (req, res) => {
178182 } ) ;
179183} ) ;
180184
185+ /**
186+ * List of subscription requests
187+ */
188+ app . get ( '/subscriptionRequests' , ( req , res ) => {
189+ res . send ( { "subscriptionRequests" : subscriptionRequests } )
190+ } )
191+
192+ /**
193+ * Get suscription request by Id
194+ */
195+ app . get ( '/subscriptionRequests/:id' , ( req , res ) => {
196+ const reqId = req . params . id ;
197+ const requestObj = subscriptionRequests . find ( ele => ele . joinId === reqId )
198+ if ( requestObj ) {
199+ res . json ( { requestObject : requestObj } )
200+ }
201+ else {
202+ res . send ( "Id not found" )
203+ }
204+
205+ } )
206+
207+ /**
208+ * @param id Id of the subscription request to be approved
209+ * Approve the subscription request of the given id
210+ */
211+
212+ app . post ( '/subscriptionRequests/:id/accept' , ( req , res ) => {
213+
214+ let id = req . params . id ;
215+ let subscriptionObjIndex = subscriptionRequests . findIndex ( request => request . joinId === id )
216+ subscriptionRequests [ subscriptionObjIndex ] . status = 'approved'
217+ const { joinId , userName, userFQDN, role, channelId, repoID} = subscriptionRequests [ subscriptionObjIndex ]
218+ axios . post ( `${ databaseAccessControlUrl } /addUser` , { userName, role, channelId } ) . then ( ( data ) => {
219+ const credentials = data . data ;
220+ credentials . joinId = joinId ;
221+ credentials . repoID = repoID ;
222+ res . json ( { Message : `Subscription with id ${ id } has been approved` } )
223+ // Create a private direct chat to send the database credentials
224+ client . createRoom ( {
225+ preset : 'trusted_private_chat' ,
226+ invite : [ userFQDN ] ,
227+ is_direct : true ,
228+ name : `${ userName } -${ channelId } -${ new Date ( ) . getTime ( ) } ` ,
229+ room_alias_name : `${ userName } -${ channelId } -${ new Date ( ) . getTime ( ) } ` ,
230+ } ) . then ( async ( roomId ) => {
231+ const content = {
232+ body : JSON . stringify ( credentials ) ,
233+ msgtype : 'm.text' ,
234+ } ;
235+ // Send the credentials to the user
236+ client . sendMessage ( roomId . room_id , content )
237+ . then ( ( ) => { console . log ( 'message sent' ) ; } )
238+ . catch ( ( err ) => {
239+ console . log ( err ) ;
240+ } ) ;
241+ } ) . catch ( ( err ) => {
242+ console . log ( err ) ;
243+ } ) ;
244+ } ) . catch ( ( err ) => {
245+ console . log ( err ) ;
246+ } ) ;
247+
248+ } )
249+
250+ /**
251+ * @param id Id of the subscription request to be rejected
252+ * Reject the subscription request of the given id
253+ */
254+ app . post ( '/subscriptionRequest/:id/reject' , ( req , res ) => {
255+ let id = req . params . id
256+ let subscriptionObjIndex = subscriptionRequests . findIndex ( request => request . joinId === id )
257+ subscriptionRequests [ subscriptionObjIndex ] . status = 'rejected'
258+ res . json ( { Message : `Subscription with id ${ id } has been rejected` } )
259+ } )
260+
181261/**
182262 * Send a request to subscribe a channel
183263 * @param {string } roomId Id of the remote public room
@@ -254,36 +334,17 @@ client.on('Room.timeline', async (event, room) => {
254334 if ( foundRepo . length > 0 ) {
255335 const userFQDN = event . getSender ( ) ;
256336 const userName = event . getSender ( ) . split ( ':' ) [ 0 ] . slice ( 1 ) ;
257- // call database access control agent to get the credentials
258- axios . post ( `${ databaseAccessControlUrl } /addUser` , { userName, role, channelId } ) . then ( ( data ) => {
259- const credentials = data . data ;
260- credentials . joinId = joinId ;
261- credentials . repoID = repoID ;
262-
263- // Create a private direct chat to send the database credentials
264- client . createRoom ( {
265- preset : 'trusted_private_chat' ,
266- invite : [ userFQDN ] ,
267- is_direct : true ,
268- name : `${ userName } -${ channelId } -${ new Date ( ) . getTime ( ) } ` ,
269- room_alias_name : `${ userName } -${ channelId } -${ new Date ( ) . getTime ( ) } ` ,
270- } ) . then ( async ( roomId ) => {
271- const content = {
272- body : JSON . stringify ( credentials ) ,
273- msgtype : 'm.text' ,
274- } ;
275- // Send the credentials to the user
276- client . sendMessage ( roomId . room_id , content )
277- . then ( ( ) => { console . log ( 'message sent' ) ; } )
278- . catch ( ( err ) => {
279- console . log ( err ) ;
280- } ) ;
281- } ) . catch ( ( err ) => {
282- console . log ( err ) ;
283- } ) ;
284- } ) . catch ( ( err ) => {
285- console . log ( err ) ;
286- } ) ;
337+ // push subscription request to the queue
338+ const subscriptionDetails = {
339+ joinId : `${ joinId } ` ,
340+ userName : `${ userName } ` ,
341+ userFQDN : `${ userFQDN } ` ,
342+ channelId : `${ channelId } ` ,
343+ repoID : `${ repoID } ` ,
344+ role : `${ role } ` ,
345+ status : 'pending'
346+ } ;
347+ subscriptionRequests . push ( subscriptionDetails )
287348 }
288349 } else {
289350 console . log ( `${ repoID } not found` ) ;
0 commit comments