@@ -208,12 +208,11 @@ const subscribeToAccount = async (
208
208
} ;
209
209
210
210
const unsubscribeFromDatasetOrProject = async (
211
- userId ,
212
211
channelid ,
213
212
command ,
214
- responseUrl ,
215
- token
213
+ responseUrl
216
214
) => {
215
+ let token ;
217
216
try {
218
217
const commandText = process . env . SLASH_COMMAND ;
219
218
@@ -223,10 +222,23 @@ const unsubscribeFromDatasetOrProject = async (
223
222
const [
224
223
hasSubscriptionInChannel ,
225
224
removeDWSubscription
226
- ] = await helper . getSubscriptionStatus ( resourceId , channelid , userId ) ;
225
+ ] = await helper . getSubscriptionStatus ( resourceId , channelid ) ;
227
226
228
227
// If subscription belongs to channel and the actor(user), then go ahead and unsubscribe
229
228
if ( hasSubscriptionInChannel ) {
229
+ const channelSubscription = await Subscription . findOne ( {
230
+ where : {
231
+ resourceId : `${ commandParams . owner } /${ commandParams . id } ` ,
232
+ channelId : channelid
233
+ }
234
+ } ) ;
235
+
236
+ const user = await User . findOne ( {
237
+ where : { slackId : channelSubscription . slackUserId }
238
+ } ) ;
239
+
240
+ token = user . dwAccessToken ;
241
+
230
242
// will be true if user subscribed to this resource in one channel
231
243
if ( removeDWSubscription ) {
232
244
// use dataworld wrapper to unsubscribe from dataset
@@ -241,7 +253,6 @@ const unsubscribeFromDatasetOrProject = async (
241
253
await removeSubscriptionRecord (
242
254
commandParams . owner ,
243
255
commandParams . id ,
244
- userId ,
245
256
channelid
246
257
) ;
247
258
// send successful unsubscription message to Slack
@@ -259,7 +270,6 @@ const unsubscribeFromDatasetOrProject = async (
259
270
console . warn ( "Failed to unsubscribe from dataset : " , error . message ) ;
260
271
// Handle as project
261
272
await unsubscribeFromProject (
262
- userId ,
263
273
channelid ,
264
274
command ,
265
275
responseUrl ,
@@ -269,7 +279,6 @@ const unsubscribeFromDatasetOrProject = async (
269
279
} ;
270
280
271
281
const unsubscribeFromProject = async (
272
- userId ,
273
282
channelId ,
274
283
command ,
275
284
responseUrl ,
@@ -282,7 +291,7 @@ const unsubscribeFromProject = async (
282
291
const [
283
292
hasSubscriptionInChannel ,
284
293
removeDWSubscription
285
- ] = await helper . getSubscriptionStatus ( resourceId , channelId , userId ) ;
294
+ ] = await helper . getSubscriptionStatus ( resourceId , channelId ) ;
286
295
287
296
if ( removeDWSubscription ) {
288
297
await dataworld . unsubscribeFromProject (
@@ -295,7 +304,6 @@ const unsubscribeFromProject = async (
295
304
await removeSubscriptionRecord (
296
305
commandParams . owner ,
297
306
commandParams . id ,
298
- userId ,
299
307
channelId
300
308
) ;
301
309
// send successful unsubscription message to Slack
@@ -314,11 +322,9 @@ const unsubscribeFromProject = async (
314
322
} ;
315
323
316
324
const unsubscribeFromAccount = async (
317
- userId ,
318
325
channelid ,
319
326
command ,
320
- responseUrl ,
321
- token
327
+ responseUrl
322
328
) => {
323
329
const commandText = process . env . SLASH_COMMAND ;
324
330
@@ -328,16 +334,26 @@ const unsubscribeFromAccount = async (
328
334
const [
329
335
hasSubscriptionInChannel ,
330
336
removeDWSubscription
331
- ] = await helper . getSubscriptionStatus ( resourceId , channelid , userId ) ;
337
+ ] = await helper . getSubscriptionStatus ( resourceId , channelid ) ;
332
338
if ( hasSubscriptionInChannel ) {
333
339
try {
340
+ const channelSubscription = await Subscription . findOne ( {
341
+ where : {
342
+ resourceId : `${ commandParams . owner } /${ commandParams . id } ` ,
343
+ channelId : channelid
344
+ }
345
+ } ) ;
346
+
347
+ const user = await User . findOne ( {
348
+ where : { slackId : channelSubscription . slackUserId }
349
+ } ) ;
350
+
334
351
if ( removeDWSubscription ) {
335
- await dataworld . unsubscribeFromAccount ( commandParams . id , token ) ;
352
+ await dataworld . unsubscribeFromAccount ( commandParams . id , user . dwAccessToken ) ;
336
353
}
337
354
await removeSubscriptionRecord (
338
355
commandParams . owner ,
339
356
commandParams . id ,
340
- userId ,
341
357
channelid
342
358
) ;
343
359
// send successful unsubscription message to Slack
@@ -373,10 +389,6 @@ const listSubscription = async (
373
389
where : { channelId : channelid }
374
390
} ) ;
375
391
376
- const user = await User . findOne ( {
377
- where : { slackId : userId }
378
- } ) ;
379
-
380
392
let message ;
381
393
let blocks ;
382
394
let options = [ ] ;
@@ -388,6 +400,10 @@ const listSubscription = async (
388
400
await Promise . all (
389
401
subscriptions . map ( async subscription => {
390
402
try {
403
+ const user = await User . findOne ( {
404
+ where : { slackId : subscription . slackUserId }
405
+ } ) ;
406
+
391
407
let isProject = false ;
392
408
if ( subscription . resourceId . includes ( "/" ) ) {
393
409
const data = subscription . resourceId . split ( "/" ) ;
@@ -410,15 +426,13 @@ const listSubscription = async (
410
426
isProject
411
427
) ;
412
428
if ( existsInDW ) {
413
- if ( subscription . slackUserId === userId ) {
414
- options . push ( {
415
- "text" : {
416
- "type" : "plain_text" ,
417
- "text" : subscription . resourceId
418
- } ,
419
- "value" : subscription . resourceId
420
- } ) ;
421
- }
429
+ options . push ( {
430
+ "text" : {
431
+ "type" : "plain_text" ,
432
+ "text" : subscription . resourceId
433
+ } ,
434
+ "value" : subscription . resourceId
435
+ } ) ;
422
436
blockText += `• ${ baseUrl } /${ subscription . resourceId
423
437
} \n *created by :* <@${ subscription . slackUserId } > \n`;
424
438
}
@@ -520,11 +534,11 @@ const addSubscriptionRecord = async (owner, id, userId, channelId) => {
520
534
}
521
535
} ;
522
536
523
- const removeSubscriptionRecord = async ( owner , id , userId , channelId ) => {
537
+ const removeSubscriptionRecord = async ( owner , id , channelId ) => {
524
538
// delete subscription
525
539
const resourceId = owner ? `${ owner } /${ id } ` : `${ id } ` ;
526
540
await Subscription . destroy ( {
527
- where : { resourceId : resourceId , slackUserId : userId , channelId : channelId }
541
+ where : { resourceId : resourceId , channelId : channelId }
528
542
} ) ;
529
543
} ;
530
544
@@ -622,20 +636,16 @@ const subscribeOrUnsubscribe = (req, token) => {
622
636
break ;
623
637
case UNSUBSCRIBE_DATASET_OR_PROJECT :
624
638
unsubscribeFromDatasetOrProject (
625
- req . body . user_id ,
626
639
req . body . channel_id ,
627
640
command ,
628
- responseUrl ,
629
- token
641
+ responseUrl
630
642
) ;
631
643
break ;
632
644
case UNSUBSCRIBE_ACCOUNT :
633
645
unsubscribeFromAccount (
634
- req . body . user_id ,
635
646
req . body . channel_id ,
636
647
command ,
637
- responseUrl ,
638
- token
648
+ responseUrl
639
649
) ;
640
650
break ;
641
651
default :
@@ -721,29 +731,25 @@ const handleButtonAction = async (payload, action, user) => {
721
731
}
722
732
} ;
723
733
724
- const handleMenuAction = async ( payload , action , user ) => {
734
+ const handleMenuAction = async ( payload , action ) => {
725
735
if ( action . action_id === "unsubscribe_menu" ) {
726
736
const value = action . selected_option . value ;
727
737
if ( value . includes ( "/" ) ) {
728
738
//unsubscribe from project of dataset
729
739
await unsubscribeFromDatasetOrProject (
730
- payload . user . id ,
731
740
payload . channel . id ,
732
741
`unsubscribe ${ value } ` ,
733
- payload . response_url ,
734
- user . dwAccessToken
742
+ payload . response_url
735
743
) ;
736
744
} else {
737
745
// unsubscribe from account
738
746
await unsubscribeFromAccount (
739
- payload . user . id ,
740
747
payload . channel . id ,
741
748
`unsubscribe ${ value } ` ,
742
- payload . response_url ,
743
- user . dwAccessToken
749
+ payload . response_url
744
750
) ;
745
751
}
746
- // Update list of subscriptions
752
+ // Updated list of subscriptions
747
753
await listSubscription (
748
754
payload . response_url ,
749
755
payload . channel . id ,
@@ -801,7 +807,7 @@ const performAction = async (req, res) => {
801
807
if ( action . type === "button" ) {
802
808
await handleButtonAction ( payload , action , user ) ;
803
809
} else if ( action . type === "static_select" ) {
804
- await handleMenuAction ( payload , action , user ) ;
810
+ await handleMenuAction ( payload , action ) ;
805
811
} else {
806
812
console . warn ( "Unknown action type : " , action . action_id )
807
813
}
0 commit comments