Skip to content
Branch: master
Find file Copy path
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
76 lines (63 sloc) 2.89 KB
package io.pager.subscription
import io.pager.client.telegram.ChatId
import io.pager.logging.Logger
import io.pager.subscription.Repository.{ Name, Version }
import zio.macros.annotation.mockable
import zio.{ RIO, Task, ZIO }
trait SubscriptionLogic {
val subscriptionLogic: SubscriptionLogic.Service[Any]
object SubscriptionLogic {
trait Service[R] {
def subscribe(chatId: ChatId, name: Name): RIO[R, Unit]
def unsubscribe(chatId: ChatId, name: Name): RIO[R, Unit]
def listSubscriptions(chatId: ChatId): RIO[R, Set[Name]]
def listRepositories: RIO[R, Map[Name, Option[Version]]]
def listSubscribers(name: Name): RIO[R, Set[ChatId]]
def updateVersions(updatedVersions: Map[Name, Version]): RIO[R, Unit]
trait Live extends SubscriptionLogic {
def logger: Logger.Service
def chatStorage: ChatStorage.Service[Any]
def repositoryVersionStorage: RepositoryVersionStorage.Service[Any]
override val subscriptionLogic: Service[Any] = new Service[Any] {
override def subscribe(chatId: ChatId, name: Name): Task[Unit] ="$chatId subscribed to $name") *>
chatStorage.subscribe(chatId, name) *>
override def unsubscribe(chatId: ChatId, name: Name): Task[Unit] ="Chat $chatId unsubscribed from $name") *>
chatStorage.unsubscribe(chatId, name)
override def listSubscriptions(chatId: ChatId): Task[Set[Name]] ="Chat $chatId requested subscriptions") *>
override def listRepositories: Task[Map[Name, Option[Version]]] ="Listing repositories") *>
override def listSubscribers(name: Name): Task[Set[ChatId]] ="Listing repository ${name.value} subscribers") *>
override def updateVersions(updatedVersions: Map[Name, Version]): Task[Unit] =
.foreach(updatedVersions) {
case (name, version) =>"Updating repository ${name.value} version to $version") *>
repositoryVersionStorage.updateVersion(name, version)
object Live {
def make(
logger: Logger.Service,
chatStorageService: ChatStorage.Service[Any],
repositoryVersionStorageService: RepositoryVersionStorage.Service[Any]
): SubscriptionLogic.Service[Any] =
new SubscriptionLogic.Live {
override def logger: Logger.Service = Logger.Test
override def chatStorage: ChatStorage.Service[Any] = chatStorageService
override def repositoryVersionStorage: RepositoryVersionStorage.Service[Any] = repositoryVersionStorageService
You can’t perform that action at this time.