2017-07-09 15:02:17 +09:00
const postCatchError = Meteor . wrapAsync ( ( url , options , resolve ) => {
HTTP . post ( url , options , ( err , res ) => {
if ( err ) {
resolve ( null , err . response ) ;
} else {
resolve ( null , res ) ;
}
} ) ;
} ) ;
2019-08-29 22:07:40 -04:00
const Lock = {
_lock : { } ,
has ( id ) {
return ! ! this . _lock [ id ] ;
} ,
set ( id ) {
this . _lock [ id ] = 1 ;
} ,
unset ( id ) {
delete this . _lock [ id ] ;
} ,
} ;
2019-06-28 12:52:09 -05:00
const webhooksAtbts = ( process . env . WEBHOOKS _ATTRIBUTES &&
process . env . WEBHOOKS _ATTRIBUTES . split ( ',' ) ) || [
'cardId' ,
'listId' ,
'oldListId' ,
'boardId' ,
'comment' ,
'user' ,
'card' ,
'commentId' ,
'swimlaneId' ,
] ;
2019-08-29 22:07:40 -04:00
const responseFunc = 'reactOnHookResponse' ;
2017-07-09 15:02:17 +09:00
Meteor . methods ( {
2019-08-29 22:07:40 -04:00
[ responseFunc ] ( data ) {
check ( data , Object ) ;
const paramCommentId = data . commentId ;
const paramCardId = data . cardId ;
const paramBoardId = data . boardId ;
const newComment = data . comment ;
if ( paramCardId && paramBoardId && newComment ) { // only process data with the cardid, boardid and comment text, TODO can expand other functions here to react on returned data
const comment = CardComments . findOne ( {
_id : paramCommentId ,
cardId : paramCardId ,
boardId : paramBoardId ,
} ) ;
if ( comment ) {
CardComments . update ( comment . _id , {
$set : {
text : newComment ,
} ,
} ) ;
} else {
CardComments . insert ( {
text : newComment ,
cardId ,
boardId ,
} ) ;
}
}
} ,
outgoingWebhooks ( integration , description , params ) {
check ( integration , Object ) ;
2017-07-09 15:02:17 +09:00
check ( description , String ) ;
check ( params , Object ) ;
2019-08-29 22:07:40 -04:00
this . unblock ( ) ;
2017-07-09 15:02:17 +09:00
2019-03-04 12:04:12 +02:00
// label activity did not work yet, see wekan/models/activities.js
2017-07-09 15:02:17 +09:00
const quoteParams = _ . clone ( params ) ;
2019-08-29 22:07:40 -04:00
const clonedParams = _ . clone ( params ) ;
2019-06-28 12:52:09 -05:00
[
'card' ,
'list' ,
'oldList' ,
'board' ,
'oldBoard' ,
'comment' ,
'checklist' ,
'swimlane' ,
'oldSwimlane' ,
'label' ,
'attachment' ,
] . forEach ( key => {
2017-07-09 15:02:17 +09:00
if ( quoteParams [ key ] ) quoteParams [ key ] = ` " ${ params [ key ] } " ` ;
} ) ;
2019-06-28 12:52:09 -05:00
const userId = params . userId ? params . userId : integrations [ 0 ] . userId ;
2017-08-31 21:20:46 -03:00
const user = Users . findOne ( userId ) ;
2019-06-28 12:52:09 -05:00
const text = ` ${ params . user } ${ TAPi18n . _ _ (
description ,
quoteParams ,
user . getLanguage ( ) ,
) } \ n$ { params . url } ` ;
2017-07-09 15:02:17 +09:00
if ( text . length === 0 ) return ;
const value = {
text : ` ${ text } ` ,
} ;
2019-06-28 12:52:09 -05:00
webhooksAtbts . forEach ( key => {
2017-08-15 11:08:16 -03:00
if ( params [ key ] ) value [ key ] = params [ key ] ;
} ) ;
2017-08-31 21:20:46 -03:00
value . description = description ;
2019-08-29 22:07:40 -04:00
//integrations.forEach(integration => {
const is2way = integration . type === Integrations . Const . TWOWAY ;
const token = integration . token || '' ;
const headers = {
'Content-Type' : 'application/json' ,
} ;
if ( token ) headers [ 'X-Wekan-Token' ] = token ;
2017-07-09 15:02:17 +09:00
const options = {
2019-08-29 22:07:40 -04:00
headers ,
data : is2way ? clonedParams : value ,
2017-07-09 15:02:17 +09:00
} ;
2019-08-29 22:07:40 -04:00
const url = integration . url ;
const response = postCatchError ( url , options ) ;
2017-07-09 15:02:17 +09:00
2019-08-29 22:07:40 -04:00
if ( response && response . statusCode && response . statusCode === 200 ) {
if ( is2way ) {
const cid = params . commentId ;
const tooSoon = Lock . has ( cid ) ; // if an activity happens to fast, notification shouldn't fire with the same id
if ( ! tooSoon ) {
let clearNotification = ( ) => { } ;
if ( cid ) {
Lock . set ( cid ) ;
const clearNotificationFlagTimeout = 1000 ;
clearNotification = ( ) => Lock . unset ( cid ) ;
Meteor . setTimeout ( clearNotification , clearNotificationFlagTimeout ) ;
}
const data = response . data ; // only an JSON encoded response will be actioned
if ( data ) {
Meteor . call ( responseFunc , data , ( ) => {
clearNotification ( ) ;
} ) ;
}
}
2017-08-29 14:41:11 -03:00
}
2019-08-29 22:07:40 -04:00
return response ; // eslint-disable-line consistent-return
} else {
throw new Meteor . Error ( 'error-invalid-webhook-response' ) ;
}
//});
2017-07-09 15:02:17 +09:00
} ,
} ) ;