2016-08-27 03:12:23 +00:00
/* eslint-disable camelcase */
import { IncomingWebhook } from '@slack/client' ;
2016-08-27 04:09:54 +00:00
import logger from './logger' ;
2016-08-27 03:12:23 +00:00
import { TAVERN _ID } from '../models/group' ;
import nconf from 'nconf' ;
const SLACK _FLAGGING _URL = nconf . get ( 'SLACK:FLAGGING_URL' ) ;
const SLACK _FLAGGING _FOOTER _LINK = nconf . get ( 'SLACK:FLAGGING_FOOTER_LINK' ) ;
2016-12-09 02:08:56 +00:00
const SLACK _SUBSCRIPTIONS _URL = nconf . get ( 'SLACK:SUBSCRIPTIONS_URL' ) ;
2016-08-27 03:12:23 +00:00
const BASE _URL = nconf . get ( 'BASE_URL' ) ;
2016-08-27 04:09:54 +00:00
let flagSlack ;
2016-12-09 02:08:56 +00:00
let subscriptionSlack ;
2016-08-27 04:09:54 +00:00
try {
flagSlack = new IncomingWebhook ( SLACK _FLAGGING _URL ) ;
2016-12-09 02:08:56 +00:00
subscriptionSlack = new IncomingWebhook ( SLACK _SUBSCRIPTIONS _URL ) ;
2016-08-27 04:09:54 +00:00
} catch ( err ) {
logger . error ( err ) ;
}
2016-08-27 03:12:23 +00:00
function sendFlagNotification ( {
2016-09-30 12:42:51 +00:00
authorEmail ,
2016-08-27 03:12:23 +00:00
flagger ,
group ,
message ,
} ) {
2016-08-27 04:09:54 +00:00
if ( ! SLACK _FLAGGING _URL ) {
return ;
}
2016-08-27 03:12:23 +00:00
let titleLink ;
2016-09-02 20:23:56 +00:00
let authorName ;
2016-08-27 03:12:23 +00:00
let title = ` Flag in ${ group . name } ` ;
2016-11-02 20:22:09 +00:00
let text = ` ${ flagger . profile . name } ( ${ flagger . id } ) flagged a message (language: ${ flagger . preferences . language } ) ` ;
2016-08-27 03:12:23 +00:00
if ( group . id === TAVERN _ID ) {
titleLink = ` ${ BASE _URL } /#/options/groups/tavern ` ;
} else if ( group . privacy === 'public' ) {
titleLink = ` ${ BASE _URL } /#/options/groups/guilds/ ${ group . id } ` ;
} else {
title += ` - ( ${ group . privacy } ${ group . type } ) ` ;
}
2016-09-02 20:23:56 +00:00
if ( ! message . user && message . uuid === 'system' ) {
authorName = 'System Message' ;
} else {
2016-09-30 12:42:51 +00:00
authorName = ` ${ message . user } - ${ authorEmail } - ${ message . uuid } ` ;
2016-09-02 20:23:56 +00:00
}
2016-08-27 03:12:23 +00:00
flagSlack . send ( {
text ,
attachments : [ {
fallback : 'Flag Message' ,
color : 'danger' ,
2016-09-02 20:23:56 +00:00
author _name : authorName ,
2016-08-27 03:12:23 +00:00
title ,
title _link : titleLink ,
text : message . text ,
footer : ` < ${ SLACK _FLAGGING _FOOTER _LINK } ?groupId= ${ group . id } &chatId= ${ message . id } |Flag this message> ` ,
mrkdwn _in : [
'text' ,
] ,
} ] ,
} ) ;
}
2016-12-09 02:08:56 +00:00
function sendSubscriptionNotification ( {
buyer ,
recipient ,
paymentMethod ,
months ,
2017-03-10 01:11:21 +00:00
groupId ,
2016-12-09 02:08:56 +00:00
} ) {
if ( ! SLACK _SUBSCRIPTIONS _URL ) {
return ;
}
let text ;
let timestamp = new Date ( ) ;
2017-01-07 15:42:03 +00:00
if ( recipient . id ) {
2016-12-09 02:08:56 +00:00
text = ` ${ buyer . name } ${ buyer . id } ${ buyer . email } bought a ${ months } -month gift subscription for ${ recipient . name } ${ recipient . id } ${ recipient . email } using ${ paymentMethod } on ${ timestamp } ` ;
2017-03-10 01:11:21 +00:00
} else if ( groupId ) {
text = ` ${ buyer . name } ${ buyer . id } ${ buyer . email } bought a 1-month recurring group-plan for ${ groupId } using ${ paymentMethod } on ${ timestamp } ` ;
2016-12-09 02:08:56 +00:00
} else {
2017-01-07 15:42:03 +00:00
text = ` ${ buyer . name } ${ buyer . id } ${ buyer . email } bought a ${ months } -month recurring subscription using ${ paymentMethod } on ${ timestamp } ` ;
2016-12-09 02:08:56 +00:00
}
subscriptionSlack . send ( {
text ,
} ) ;
}
2016-08-27 03:12:23 +00:00
module . exports = {
sendFlagNotification ,
2016-12-09 02:08:56 +00:00
sendSubscriptionNotification ,
2016-08-27 03:12:23 +00:00
} ;