<commit_before>package im.actor.server.api.rpc.service.messaging

import akka.actor._
import akka.contrib.pattern.DistributedPubSubMediator
import slick.driver.PostgresDriver.api._

import im.actor.api.rpc.Implicits._
import im.actor.api.rpc.messaging._
import im.actor.api.rpc.peers.{ PeerType, Peer }
import im.actor.server.models
import im.actor.server.push.SeqUpdatesManagerRegion
import im.actor.server.social.SocialManagerRegion

sealed trait Event

object Events {
  final case class PeerMessage(fromPeer: Peer, toPeer: Peer, randomId: Long, message: Message) extends Event
}

object MessagingService {
  def messagesTopic(peer: models.Peer) = {
    val strType = peer.typ match {
      case models.PeerType.Private ⇒ "private"
      case models.PeerType.Group   ⇒ "group"
    }

    s"messaging.messages.${strType}.${peer.id}"
  }

  def messagesTopic(peer: Peer): String =
    messagesTopic(peer.asModel)

  def publish(mediator: ActorRef, message: Events.PeerMessage): Unit = {
    message.toPeer.`type` match {
      case PeerType.Private ⇒
        val senderTopic = MessagingService.messagesTopic(Peer(PeerType.Private, message.fromPeer.id))
        val receiverTopic = messagesTopic(message.toPeer)

        mediator ! DistributedPubSubMediator.Publish(senderTopic, message)
        mediator ! DistributedPubSubMediator.Publish(receiverTopic, message)
      case PeerType.Group ⇒
        val topic = messagesTopic(message.toPeer)
        mediator ! DistributedPubSubMediator.Publish(topic, message)
    }
  }
}

object MessagingServiceImpl {
  def apply(mediator: ActorRef)(
    implicit
    seqUpdManagerRegion: SeqUpdatesManagerRegion,
    socialManagerRegion: SocialManagerRegion,
    db:                  Database,
    actorSystem:         ActorSystem
  ): MessagingServiceImpl = {
    val onMessage = (MessagingService.publish _).curried(mediator)

    new MessagingServiceImpl(onMessage)
  }<commit_msg>feat(messaging): publish all messages to messaging.messages.group and messages.messaging.private topics<commit_after>package im.actor.server.api.rpc.service.messaging

import akka.actor._
import akka.contrib.pattern.DistributedPubSubMediator
import slick.driver.PostgresDriver.api._

import im.actor.api.rpc.Implicits._
import im.actor.api.rpc.messaging._
import im.actor.api.rpc.peers.{ PeerType, Peer }
import im.actor.server.models
import im.actor.server.push.SeqUpdatesManagerRegion
import im.actor.server.social.SocialManagerRegion

sealed trait Event

object Events {
  final case class PeerMessage(fromPeer: Peer, toPeer: Peer, randomId: Long, message: Message) extends Event
}

object MessagingService {
  val privateMessagesTopic: String = "messaging.messages.private"
  val groupMessagesTopic: String = "messaging.messages.group"

  def messagesTopic(peer: models.Peer): String = {
    val strType = peer.typ match {
      case models.PeerType.Private ⇒ "private"
      case models.PeerType.Group   ⇒ "group"
    }

    s"messaging.messages.${strType}.${peer.id}"
  }

  def messagesTopic(peer: Peer): String =
    messagesTopic(peer.asModel)

  def publish(mediator: ActorRef, message: Events.PeerMessage): Unit = {
    message.toPeer.`type` match {
      case PeerType.Private ⇒
        val senderTopic = MessagingService.messagesTopic(Peer(PeerType.Private, message.fromPeer.id))
        val receiverTopic = messagesTopic(message.toPeer)

        mediator ! DistributedPubSubMediator.Publish(privateMessagesTopic, message)
        mediator ! DistributedPubSubMediator.Publish(senderTopic, message)
        mediator ! DistributedPubSubMediator.Publish(receiverTopic, message)
      case PeerType.Group ⇒
        val topic = messagesTopic(message.toPeer)

        mediator ! DistributedPubSubMediator.Publish(groupMessagesTopic, message)
        mediator ! DistributedPubSubMediator.Publish(topic, message)
    }
  }
}

object MessagingServiceImpl {
  def apply(mediator: ActorRef)(
    implicit
    seqUpdManagerRegion: SeqUpdatesManagerRegion,
    socialManagerRegion: SocialManagerRegion,
    db:                  Database,
    actorSystem:         ActorSystem
  ): MessagingServiceImpl = {
    val onMessage = (MessagingService.publish _).curried(mediator)

    new MessagingServiceImpl(onMessage)
  }
{'fraction_non_alphanumeric': 0.09202453987730061, 'fraction_numerical': 0.0, 'mean_word_length': 4.01063829787234, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '22866', 'n_tokens_mistral': 1298, 'n_tokens_neox': 1237, 'n_words': 257}