<commit_before>package com.softwaremill.codebrag.service.user

import com.typesafe.scalalogging.slf4j.Logging
import com.softwaremill.codebrag.domain.{UserSettings, User, Authentication}
import java.util.UUID
import com.softwaremill.codebrag.service.invitations.InvitationService
import com.softwaremill.codebrag.service.notification.NotificationService
import com.softwaremill.codebrag.dao.user.UserDAO

class RegisterService(userDao: UserDAO, newUserAdder: NewUserAdder, invitationService: InvitationService, notificationService: NotificationService) extends Logging {

  def firstRegistration: Boolean = {
    userDao.findAll() match {
      case x if x.isEmpty => true
      case _ => false
    }
  }

  def register(login: String, email: String, password: String, invitationCode: String): Either[String, Unit] = {
    logger.info(s"Trying to register $login")
    val emailLowerCase = email.toLowerCase
    if (firstRegistration) {
      registerUser(login, emailLowerCase, password)
    } else {
      registerUserWithInvitation(login, emailLowerCase, password, invitationCode)
    }
  }

  private def registerUserWithInvitation(login: String, emailLowerCase: String, password: String, invitationCode: String): Either[String, Unit] = {
    for {
      _ <- leftIfTrue(invitationCode.isEmpty, "To register in Codebrag you need a registration link. Ask another Codebrag user to send you one.").right
      _ <- leftIfTrue(!invitationService.verify(invitationCode), "The registration link is not valid or was already used. Ask your friend for another one.").right
      _ <- leftIfSome(userDao.findByLowerCasedLogin(login), "User with the given login already exists").right
      _ <- leftIfSome(userDao.findByEmail(emailLowerCase), "User with the given email already exists").right
    } yield {
      registerUser(login, emailLowerCase, password)
    }
  }

  private def registerUser(login: String, emailLowerCase: String, password: String): Either[String, Unit] = {
    val user = User(Authentication.basic(login, password), login, emailLowerCase,
      UUID.randomUUID().toString, UserSettings.defaultAvatarUrl(emailLowerCase))
    val addedUser = newUserAdder.add(user)
    notificationService.sendWelcomeNotification(addedUser)
    Right()
  }

  private def leftIfTrue(cond: Boolean, message: String) = {
    if (cond) {
      Left(message)
    } else {
      Right()
    }
  }

  private def leftIfSome[A](userOpt: Option[A], msg: String) = {
    userOpt.map({
      user => Left(msg)
    }).getOrElse(Right())
  }
}
<commit_msg>Minor - checking if it is first registration to one-liner<commit_after>package com.softwaremill.codebrag.service.user

import com.typesafe.scalalogging.slf4j.Logging
import com.softwaremill.codebrag.domain.{UserSettings, User, Authentication}
import java.util.UUID
import com.softwaremill.codebrag.service.invitations.InvitationService
import com.softwaremill.codebrag.service.notification.NotificationService
import com.softwaremill.codebrag.dao.user.UserDAO

class RegisterService(userDao: UserDAO, newUserAdder: NewUserAdder, invitationService: InvitationService, notificationService: NotificationService) extends Logging {

  def firstRegistration: Boolean = userDao.findAll().isEmpty

  def register(login: String, email: String, password: String, invitationCode: String): Either[String, Unit] = {
    logger.info(s"Trying to register $login")
    val emailLowerCase = email.toLowerCase
    if (firstRegistration) {
      registerUser(login, emailLowerCase, password)
    } else {
      registerUserWithInvitation(login, emailLowerCase, password, invitationCode)
    }
  }

  private def registerUserWithInvitation(login: String, emailLowerCase: String, password: String, invitationCode: String): Either[String, Unit] = {
    for {
      _ <- leftIfTrue(invitationCode.isEmpty, "To register in Codebrag you need a registration link. Ask another Codebrag user to send you one.").right
      _ <- leftIfTrue(!invitationService.verify(invitationCode), "The registration link is not valid or was already used. Ask your friend for another one.").right
      _ <- leftIfSome(userDao.findByLowerCasedLogin(login), "User with the given login already exists").right
      _ <- leftIfSome(userDao.findByEmail(emailLowerCase), "User with the given email already exists").right
    } yield {
      registerUser(login, emailLowerCase, password)
    }
  }

  private def registerUser(login: String, emailLowerCase: String, password: String): Either[String, Unit] = {
    val user = User(Authentication.basic(login, password), login, emailLowerCase,
      UUID.randomUUID().toString, UserSettings.defaultAvatarUrl(emailLowerCase))
    val addedUser = newUserAdder.add(user)
    notificationService.sendWelcomeNotification(addedUser)
    Right()
  }

  private def leftIfTrue(cond: Boolean, message: String) = {
    if (cond) {
      Left(message)
    } else {
      Right()
    }
  }

  private def leftIfSome[A](userOpt: Option[A], msg: String) = {
    userOpt.map({
      user => Left(msg)
    }).getOrElse(Right())
  }
}

{'fraction_non_alphanumeric': 0.10007911392405064, 'fraction_numerical': 0.00039556962025316455, 'mean_word_length': 4.514721919302072, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 7, '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': '6022093', 'n_tokens_mistral': 1446, 'n_tokens_neox': 1438, 'n_words': 358}