<commit_before>package lila.study

import akka.actor._
import com.github.blemale.scaffeine.LoadingCache
import play.api.libs.json._
import scala.concurrent.duration._

import chess.format.pgn.Glyphs
import lila.hub.TimeBomb
import lila.socket.actorApi.{ Connected => _, _ }
import lila.socket.Socket.Uid
import lila.socket.{ SocketActor, History, Historical, AnaDests }
import lila.tree.Node.{ Shapes, Comment }
import lila.user.User

private final class Socket(
    studyId: Study.Id,
    jsonView: JsonView,
    studyRepo: StudyRepo,
    lightUser: lila.common.LightUser.Getter,
    val history: History[Socket.Messadata],
    uidTimeout: Duration,
    socketTimeout: Duration,
    lightStudyCache: LightStudyCache) extends SocketActor[Socket.Member](uidTimeout) with Historical[Socket.Member, Socket.Messadata] {

  import Socket._
  import JsonView._
  import jsonView.membersWrites
  import lila.tree.Node.{ openingWriter, commentWriter, glyphsWriter, shapesWrites }

  private val timeBomb = new TimeBomb(socketTimeout)

  private var delayedCrowdNotification = false

  override def preStart() {
    super.preStart()
    lilaBus.subscribe(self, Symbol(s"chat-$studyId"))
  }

  override def postStop() {
    super.postStop()
    lilaBus.unsubscribe(self)
  }

  def sendStudyJoin(userId: User.ID) {
    lightStudyCache.get(studyId) foreach { studyOption =>
      studyOption foreach { study =>
        val contributor = study.contributors.contains(userId)
        context.system.lilaBus.publish(lila.hub.actorApi.study.StudyJoin(userId, studyId.value, contributor, study.isPublic), 'study)
      }
    }
  }

  def sendStudyQuit(userId: User.ID) {
    lightStudyCache.get(studyId) foreach { studyOption =>
      studyOption foreach { study =>
        val contributor = study.contributors.contains(userId)
        context.system.lilaBus.publish(lila.hub.actorApi.study.StudyQuit(userId, studyId.value, contributor, study.isPublic), 'study)
      }
    }
  }

  def receiveSpecific = ({

    case SetPath(pos, uid) =>
      uidToUserId(uid) match {
        case Some(userId) => sendStudyJoin(userId)
        case None => {}
      }
      notifyVersion("path", Json.obj(
        "p" -> pos,
        "w" -> who(uid).map(whoWriter.writes)
      ), noMessadata)

    case AddNode(pos, node, uid) =>
      val dests = AnaDests.Ref(chess.variant.Standard, node.fen.value, pos.path.toString).compute
      notifyVersion("addNode", Json.obj(
        "n" -> TreeBuilder.toBranch(node),
        "p" -> pos,
        "w" -> who(uid),
        "d" -> dests.dests,
        "o" -> dests.opening
      ), noMessadata)

    case DeleteNode(pos, uid) => notifyVersion("deleteNode", Json.obj(
      "p" -> pos,
      "w" -> who(uid)
    ), noMessadata)

    case Promote(pos, toMainline, uid) => notifyVersion("promote", Json.obj(
      "p" -> pos,
      "toMainline" -> toMainline,
      "w" -> who(uid)
    ), noMessadata)<commit_msg>SetPath: replace match by foreach<commit_after>package lila.study

import akka.actor._
import com.github.blemale.scaffeine.LoadingCache
import play.api.libs.json._
import scala.concurrent.duration._

import chess.format.pgn.Glyphs
import lila.hub.TimeBomb
import lila.socket.actorApi.{ Connected => _, _ }
import lila.socket.Socket.Uid
import lila.socket.{ SocketActor, History, Historical, AnaDests }
import lila.tree.Node.{ Shapes, Comment }
import lila.user.User

private final class Socket(
    studyId: Study.Id,
    jsonView: JsonView,
    studyRepo: StudyRepo,
    lightUser: lila.common.LightUser.Getter,
    val history: History[Socket.Messadata],
    uidTimeout: Duration,
    socketTimeout: Duration,
    lightStudyCache: LightStudyCache) extends SocketActor[Socket.Member](uidTimeout) with Historical[Socket.Member, Socket.Messadata] {

  import Socket._
  import JsonView._
  import jsonView.membersWrites
  import lila.tree.Node.{ openingWriter, commentWriter, glyphsWriter, shapesWrites }

  private val timeBomb = new TimeBomb(socketTimeout)

  private var delayedCrowdNotification = false

  override def preStart() {
    super.preStart()
    lilaBus.subscribe(self, Symbol(s"chat-$studyId"))
  }

  override def postStop() {
    super.postStop()
    lilaBus.unsubscribe(self)
  }

  def sendStudyJoin(userId: User.ID) {
    lightStudyCache.get(studyId) foreach { studyOption =>
      studyOption foreach { study =>
        val contributor = study.contributors.contains(userId)
        context.system.lilaBus.publish(lila.hub.actorApi.study.StudyJoin(userId, studyId.value, contributor, study.isPublic), 'study)
      }
    }
  }

  def sendStudyQuit(userId: User.ID) {
    lightStudyCache.get(studyId) foreach { studyOption =>
      studyOption foreach { study =>
        val contributor = study.contributors.contains(userId)
        context.system.lilaBus.publish(lila.hub.actorApi.study.StudyQuit(userId, studyId.value, contributor, study.isPublic), 'study)
      }
    }
  }

  def receiveSpecific = ({

    case SetPath(pos, uid) =>
      uidToUserId(uid) foreach sendStudyJoin
      notifyVersion("path", Json.obj(
        "p" -> pos,
        "w" -> who(uid).map(whoWriter.writes)
      ), noMessadata)

    case AddNode(pos, node, uid) =>
      val dests = AnaDests.Ref(chess.variant.Standard, node.fen.value, pos.path.toString).compute
      notifyVersion("addNode", Json.obj(
        "n" -> TreeBuilder.toBranch(node),
        "p" -> pos,
        "w" -> who(uid),
        "d" -> dests.dests,
        "o" -> dests.opening
      ), noMessadata)

    case DeleteNode(pos, uid) => notifyVersion("deleteNode", Json.obj(
      "p" -> pos,
      "w" -> who(uid)
    ), noMessadata)

    case Promote(pos, toMainline, uid) => notifyVersion("promote", Json.obj(
      "p" -> pos,
      "toMainline" -> toMainline,
      "w" -> who(uid)
    ), noMessadata)
{'fraction_non_alphanumeric': 0.1244605558432591, 'fraction_numerical': 0.0, 'mean_word_length': 3.3110119047619047, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 47, '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': '1062875', 'n_tokens_mistral': 1928, 'n_tokens_neox': 1862, 'n_words': 373}