<commit_before>package com.anchortab.actor

import scala.xml.NodeSeq

import net.liftweb._
  import common._
  import actor._
  import mongodb._
    import BsonDSL._
  import util.Helpers._
  import json._
    import ext._
    import Extraction._
  import util._
  import http._

import com.anchortab.model._

import org.joda.time._

import java.util.Date

import org.bson.types.ObjectId

case object ScheduleQuotaReset
case object QuotaReset
case class CheckQuotaCounts(userId: ObjectId)

object QuotasActor extends LiftActor with Loggable {
  private def timeSpanUntilNextQuotaReset = {
    val beginningOfNextMonth = Props.mode match {
      case Props.RunModes.Development => (new DateTime()).plusMinutes(5).getMillis
      case _ => (new DateTime()).withTimeAtStartOfDay().withDayOfMonth(1).plusMonths(1).getMillis
    }
    val now = new DateTime().getMillis

    new TimeSpan(beginningOfNextMonth - now)
  }

  def messageHandler = {
    case ScheduleQuotaReset =>
      // Schedule next months quota reset.
      val delay = timeSpanUntilNextQuotaReset

      logger.info("Scheduling next quota reset for " + delay)

      Schedule(() => this ! QuotaReset, delay)

    case QuotaReset =>
      logger.info("Runing quota reset.")

      // Todo: Fix me to filter by those not updated since the last month.
      User.update(
        JObject(Nil),
        (
          "$unset" -> (
            (("quotaCounts." + Plan.Quotas.Views) -> true) ~
            (("quotaCounts." + Plan.Quotas.EmailSubscriptions) -> true)
          )
        ),
        Multi
      )

      // Schedule next month's reset
      this ! ScheduleQuotaReset

    case CheckQuotaCounts(userId) =>
      for {
        user <- User.find(userId)
          if ! user.admin_?
      } {
        logger.info("Checking quota for " + user.email)
        if (! user.withinQuotaFor_?(Plan.Quotas.Views) ||
            ! user.withinQuotaFor_?(Plan.Quotas.EmailSubscriptions)) {
          EmailActor ! SendQuotaErrorEmail(user.email)
        } else if(user.nearQuotaFor_?(Plan.Quotas.Views) ||
                  user.nearQuotaFor_?(Plan.Quotas.EmailSubscriptions)) {
          EmailActor ! SendQuotaWarningEmail(user.email)
        }
      }
  }
}<commit_msg>Don't send quota alert emails if alert emails are disabled.<commit_after>package com.anchortab.actor

import scala.xml.NodeSeq

import net.liftweb._
  import common._
  import actor._
  import mongodb._
    import BsonDSL._
  import util.Helpers._
  import json._
    import ext._
    import Extraction._
  import util._
  import http._

import com.anchortab.model._

import org.joda.time._

import java.util.Date

import org.bson.types.ObjectId

case object ScheduleQuotaReset
case object QuotaReset
case class CheckQuotaCounts(userId: ObjectId)

object QuotasActor extends LiftActor with Loggable {
  private def timeSpanUntilNextQuotaReset = {
    val beginningOfNextMonth = Props.mode match {
      case Props.RunModes.Development => (new DateTime()).plusMinutes(5).getMillis
      case _ => (new DateTime()).withTimeAtStartOfDay().withDayOfMonth(1).plusMonths(1).getMillis
    }
    val now = new DateTime().getMillis

    new TimeSpan(beginningOfNextMonth - now)
  }

  def messageHandler = {
    case ScheduleQuotaReset =>
      // Schedule next months quota reset.
      val delay = timeSpanUntilNextQuotaReset

      logger.info("Scheduling next quota reset for " + delay)

      Schedule(() => this ! QuotaReset, delay)

    case QuotaReset =>
      logger.info("Runing quota reset.")

      // Todo: Fix me to filter by those not updated since the last month.
      User.update(
        JObject(Nil),
        (
          "$unset" -> (
            (("quotaCounts." + Plan.Quotas.Views) -> true) ~
            (("quotaCounts." + Plan.Quotas.EmailSubscriptions) -> true)
          )
        ),
        Multi
      )

      // Schedule next month's reset
      this ! ScheduleQuotaReset

    case CheckQuotaCounts(userId) =>
      for {
        user <- User.find(userId)
          if ! user.admin_? && user.notificationSettings.alertEmails
      } {
        logger.info("Checking quota for " + user.email)
        if (! user.withinQuotaFor_?(Plan.Quotas.Views) ||
            ! user.withinQuotaFor_?(Plan.Quotas.EmailSubscriptions)) {
          EmailActor ! SendQuotaErrorEmail(user.email)
        } else if(user.nearQuotaFor_?(Plan.Quotas.Views) ||
                  user.nearQuotaFor_?(Plan.Quotas.EmailSubscriptions)) {
          EmailActor ! SendQuotaWarningEmail(user.email)
        }
      }
  }
}
{'fraction_non_alphanumeric': 0.1014780498566071, 'fraction_numerical': 0.0013236267372600927, 'mean_word_length': 2.5728920409771474, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 21, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 2}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6883982', 'n_tokens_mistral': 1414, 'n_tokens_neox': 1382, 'n_words': 306}