<commit_before>package com.anchortab.snippet

import scala.collection.JavaConversions._

import net.liftweb._
  import common._
  import http._
    import provider._
    import js._
      import JsCmds._
    import SHtml._
    import LiftRules._
  import util._
    import Helpers._
  import json._
    import JsonDSL._
    import Extraction._
  import mongodb.BsonDSL._

import org.joda.time._

import com.anchortab.model._
import com.anchortab.constantcontact.ConstantContact
import com.anchortab.mailchimp._
import com.anchortab.campaignmonitor._

object OAuth extends Loggable {
  implicit val formats = User.formats

  def dispatch : DispatchPF = {
    case req @ Req("oauth2" :: "constant-contact" :: Nil, _, _) =>
      () => {
        for {
          session <- userSession.is
          code <- req.param("code")
          username <- req.param("username")
          token <- ConstantContact.retrieveAccessTokenForCode(code)
        } yield {
          val serviceCredential = UserServiceCredentials("Constant Contact", username, Map("token" -> token))
          User.update("_id" -> session.userId,
            ("$addToSet" -> (("serviceCredentials" -> decompose(serviceCredential)))) ~
            ("$unset" -> (
              ("firstSteps." + UserFirstStep.Keys.ConnectAnExternalService) -> true
            ))
          )

          Notices.notice("Your Constant Contact account has been successfully connected.")
          RedirectResponse("/manager/services")
        }
      }

    case req @ Req("oauth2" :: "mailchimp" :: Nil, _, _) =>
      () => {
        import com.ecwid.mailchimp._
          import method.helper._

        val mcClient = new MailChimpClient
        val getAccountInfoMethod = new GetAccountDetailsMethod

        // Ain't nobody got time for that
        getAccountInfoMethod.exclude = "modules" :: "orders" :: "rewards-credits" ::
            "rewards-inspections" :: "rewards-referrals" :: "rewards-applied" :: Nil

        for {
          session <- userSession.is
          code <- req.param("code")
          token <- MailchimpOAuth.retrieveAccessTokenAndDcForCode(code)
        } yield {
          getAccountInfoMethod.apikey = token
          val accountInformation = mcClient.execute(getAccountInfoMethod)
          val serviceCredential = UserServiceCredentials("Mailchimp", accountInformation.username, Map("token" -> token))
          User.update("_id" -> session.userId,
            ("$addToSet" -> (("serviceCredentials" -> decompose(serviceCredential)))) ~
            ("$unset" -> (
              ("firstSteps." + UserFirstStep.Keys.ConnectAnExternalService) -> true
            ))
          )

          Notices.notice("Your MailChimp account has been successfully connected.")
          RedirectResponse("/manager/services")
        }
      }

    case req @ Req("oauth2" :: "campaign-monitor" :: Nil, _, _) =>
      () => {
        for {
          session <- userSession.is
          code <- req.param("code")
          tokenDetails <- CampaignMonitor.exchangeToken(code)
        } yield {
          val expiresAt = (new DateTime()).plusSeconds(tokenDetails.expires_in)

          val serviceCredential = UserServiceCredentials(
            CampaignMonitor.serviceIdentifier,
            tokenDetails.refresh_token,
            Map(
              "accessToken" -> tokenDetails.access_token,
              "refreshToken" -> tokenDetails.refresh_token,
              "expiresAt" -> decompose(expiresAt).extract[String]
            )
          )

          User.update("_id" -> session.userId,
            ("$addToSet" -> (("serviceCredentials" -> decompose(serviceCredential)))) ~
            ("$unset" -> (
              ("firstSteps." + UserFirstStep.Keys.ConnectAnExternalService) -> true
            ))
          )

          Notices.notice("Your Campaign Monitor account has been successfully connected.")
          RedirectResponse("/manager/services")
        }
      }
  }
}<commit_msg>Add error handling for Campaign Monitor OAuth.<commit_after>package com.anchortab.snippet

import scala.collection.JavaConversions._

import net.liftweb._
  import common._
  import http._
    import provider._
    import js._
      import JsCmds._
    import SHtml._
    import LiftRules._
  import util._
    import Helpers._
  import json._
    import JsonDSL._
    import Extraction._
  import mongodb.BsonDSL._

import org.joda.time._

import com.anchortab.model._
import com.anchortab.constantcontact.ConstantContact
import com.anchortab.mailchimp._
import com.anchortab.campaignmonitor._

object OAuth extends Loggable {
  implicit val formats = User.formats

  def dispatch : DispatchPF = {
    case req @ Req("oauth2" :: "constant-contact" :: Nil, _, _) =>
      () => {
        for {
          session <- userSession.is
          code <- req.param("code")
          username <- req.param("username")
          token <- ConstantContact.retrieveAccessTokenForCode(code)
        } yield {
          val serviceCredential = UserServiceCredentials("Constant Contact", username, Map("token" -> token))
          User.update("_id" -> session.userId,
            ("$addToSet" -> (("serviceCredentials" -> decompose(serviceCredential)))) ~
            ("$unset" -> (
              ("firstSteps." + UserFirstStep.Keys.ConnectAnExternalService) -> true
            ))
          )

          Notices.notice("Your Constant Contact account has been successfully connected.")
          RedirectResponse("/manager/services")
        }
      }

    case req @ Req("oauth2" :: "mailchimp" :: Nil, _, _) =>
      () => {
        import com.ecwid.mailchimp._
          import method.helper._

        val mcClient = new MailChimpClient
        val getAccountInfoMethod = new GetAccountDetailsMethod

        // Ain't nobody got time for that
        getAccountInfoMethod.exclude = "modules" :: "orders" :: "rewards-credits" ::
            "rewards-inspections" :: "rewards-referrals" :: "rewards-applied" :: Nil

        for {
          session <- userSession.is
          code <- req.param("code")
          token <- MailchimpOAuth.retrieveAccessTokenAndDcForCode(code)
        } yield {
          getAccountInfoMethod.apikey = token
          val accountInformation = mcClient.execute(getAccountInfoMethod)
          val serviceCredential = UserServiceCredentials("Mailchimp", accountInformation.username, Map("token" -> token))
          User.update("_id" -> session.userId,
            ("$addToSet" -> (("serviceCredentials" -> decompose(serviceCredential)))) ~
            ("$unset" -> (
              ("firstSteps." + UserFirstStep.Keys.ConnectAnExternalService) -> true
            ))
          )

          Notices.notice("Your MailChimp account has been successfully connected.")
          RedirectResponse("/manager/services")
        }
      }

    case req @ Req("oauth2" :: "campaign-monitor" :: Nil, _, _) if req.param("code").isDefined =>
      () => {
        for {
          session <- userSession.is
          code <- req.param("code")
          tokenDetails <- CampaignMonitor.exchangeToken(code)
        } yield {
          val expiresAt = (new DateTime()).plusSeconds(tokenDetails.expires_in)

          val serviceCredential = UserServiceCredentials(
            CampaignMonitor.serviceIdentifier,
            tokenDetails.refresh_token,
            Map(
              "accessToken" -> tokenDetails.access_token,
              "refreshToken" -> tokenDetails.refresh_token,
              "expiresAt" -> decompose(expiresAt).extract[String]
            )
          )

          User.update("_id" -> session.userId,
            ("$addToSet" -> (("serviceCredentials" -> decompose(serviceCredential)))) ~
            ("$unset" -> (
              ("firstSteps." + UserFirstStep.Keys.ConnectAnExternalService) -> true
            ))
          )

          Notices.notice("Your Campaign Monitor account has been successfully connected.")
          RedirectResponse("/manager/services")
        }
      }

    case req @ Req("oauth2" :: "campaign-monitor" :: Nil, _, _) if req.param("error").isDefined =>
      () => {
        for {
          error <- req.param("error")
          errorDescription <- req.param("error_description")
        } yield {
          Notices.error("Campaign Monitor Error: " + errorDescription)
          RedirectResponse("/manager/services")
        }
      }
  }
}
{'fraction_non_alphanumeric': 0.1186196980589504, 'fraction_numerical': 0.0008387251377905584, 'mean_word_length': 2.226517201391573, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 57, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2644405', 'n_tokens_mistral': 2306, 'n_tokens_neox': 2234, 'n_words': 440}