3 var Promise = require('bluebird');
4 var env = require('gitter-web-env');
5 var config = env.config;
6 var TwitterService = require('./twitter-service');
7 var badgerMessageComposer = require('./badger-message-composer');
8 var debug = require('debug')('gitter:app:twitter:badger');
10 var CONSUMER_KEY = config.get('twitterbadger:consumer_key');
11 var CONSUMER_SECRET = config.get('twitterbadger:consumer_secret');
12 var BADGER_ACCESS_TOKEN = config.get('twitterbadger:access_token');
13 var BADGER_ACCESS_TOKEN_SECRET = config.get('twitterbadger:access_token_secret');
15 var twitterService = new TwitterService(
19 BADGER_ACCESS_TOKEN_SECRET
22 var DEVELOPMENT_TWITTER_ALLOWED = [
36 ].reduce(function(memo, twitterUsername) {
37 memo[twitterUsername.toLowerCase()] = true;
42 if (process.env.NODE_ENV === 'prod') {
43 userFilter = function() {
47 userFilter = function(user) {
48 if (!user) return false;
49 if (!user.twitterUsername) return false;
52 if (/GitterTestUser/.test(user.twitterUsername)) {
56 return DEVELOPMENT_TWITTER_ALLOWED[user.twitterUsername.toLowerCase()];
60 function sendUserInviteTweets(invitingUser, users, name, url) {
62 throw new Error('No user provided to show as the person inviting other users');
66 throw new Error('No url provided to point users to');
69 var mentionList = users
71 .filter(function(user) {
72 return user && user.twitterUsername;
75 return '@' + user.twitterUsername;
78 if (!mentionList) return [];
80 var invitingUserName = invitingUser.twitterUsername
81 ? '@' + invitingUser.twitterUsername
82 : invitingUser.username;
83 var tweets = badgerMessageComposer(invitingUserName, mentionList, name, url);
85 debug('Sending tweets: %j', tweets);
89 return twitterService.sendTweet(tweet).return(tweet);
96 sendUserInviteTweets: Promise.method(sendUserInviteTweets)