import config from 'config';
import Promise from 'bluebird';
import Twitter from 'twitter';
import activityType from '../constants/activities';

export function tweetActivity(Sequelize, activity) {
  if (activity.type === activityType.GROUP_TRANSACTION_CREATED
    && activity.data.transaction.amount > 0
    // users without twitterHandle are ignored
    && activity.data.user.twitterHandle) {
      return Sequelize.models.Group.findById(activity.GroupId)
        .then(group => group.getTwitterSettings().thankDonation)
        .then(template => template.replace('$backer', activity.data.user.twitterHandle))
        .then(status => tweetStatus(Sequelize, activity.GroupId, status));
  } else {
    return Promise.resolve();
  }
}

export function tweetStatus(Sequelize, GroupId, status) {
  return Sequelize.models.ConnectedAccount.findOne({
    where: {
      GroupId,
      provider: 'twitter'
    }
  })
  .tap(connectedAccount => {
    // groups without twitter credentials are ignored
    if (connectedAccount) {
      const client = new Twitter({
        consumer_key: config.twitter.consumerKey,
        consumer_secret: config.twitter.consumerSecret,
        access_token_key: connectedAccount.clientId,
        access_token_secret: connectedAccount.secret
      });

      const tweet = Promise.promisify(client.post, { context: client });
      console.log(`Tweeting for group ID ${GroupId}: ${status}`);
      return tweet("statuses/update", { status });
    }
  });
}

@@ -570,16 +570,20 @@
 acker', 
+%60@$%7B
 activity
@@ -606,16 +606,18 @@
 erHandle
+%7D%60
 ))%0A     

Add missing at symbol in thank you tweet (#572)