temporal push for keeping history
This commit is contained in:
@@ -0,0 +1,52 @@
|
||||
package com.insanusmokrassar.postssystem.core.publishing
|
||||
|
||||
import com.insanusmokrassar.postssystem.core.post.PostId
|
||||
import com.insanusmokrassar.postssystem.core.publishing.repos.PublishingKeysRepo
|
||||
import kotlinx.coroutines.channels.BroadcastChannel
|
||||
import kotlinx.coroutines.channels.Channel
|
||||
import kotlinx.coroutines.flow.Flow
|
||||
import kotlinx.coroutines.flow.asFlow
|
||||
|
||||
typealias TriggerControlKey = String
|
||||
|
||||
interface ReadPublishingRegistrar {
|
||||
suspend fun getPostIdByTriggerControlKey(
|
||||
key: TriggerControlKey
|
||||
): PostId?
|
||||
}
|
||||
|
||||
interface WritePublishingRegistrar {
|
||||
val unregisteredKeysFlow: Flow<TriggerControlKey>
|
||||
|
||||
suspend fun registerTriggerForPost(
|
||||
key: TriggerControlKey,
|
||||
postId: PostId
|
||||
): Boolean
|
||||
}
|
||||
|
||||
interface PublishingRegistrar : ReadPublishingRegistrar, WritePublishingRegistrar
|
||||
|
||||
class BusinessPublishingRegistrar(
|
||||
private val repo: PublishingKeysRepo
|
||||
) : PublishingRegistrar {
|
||||
private val unregisteredKeysChannel: BroadcastChannel<TriggerControlKey> = BroadcastChannel(Channel.BUFFERED)
|
||||
override val unregisteredKeysFlow: Flow<TriggerControlKey> = unregisteredKeysChannel.asFlow()
|
||||
|
||||
override suspend fun getPostIdByTriggerControlKey(
|
||||
key: TriggerControlKey
|
||||
): PostId? = repo.getPostIdByTriggerControlKey(key)
|
||||
|
||||
override suspend fun registerTriggerForPost(
|
||||
key: TriggerControlKey,
|
||||
postId: PostId
|
||||
): Boolean = repo.getTriggerControlKeyByPostId(
|
||||
postId
|
||||
).let { previousKey ->
|
||||
repo.setPostTriggerControlKey(postId, key).also { inserted ->
|
||||
if (inserted && previousKey != null) {
|
||||
unregisteredKeysChannel.send(previousKey)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@@ -0,0 +1,40 @@
|
||||
package com.insanusmokrassar.postssystem.core.publishing
|
||||
|
||||
import com.insanusmokrassar.postssystem.core.post.PostId
|
||||
import com.insanusmokrassar.postssystem.core.post.RegisteredPost
|
||||
import com.insanusmokrassar.postssystem.core.post.repo.PostsRepo
|
||||
import com.insanusmokrassar.postssystem.core.publishing.repos.PublishingKeysRepo
|
||||
import kotlinx.coroutines.channels.BroadcastChannel
|
||||
import kotlinx.coroutines.channels.Channel
|
||||
import kotlinx.coroutines.flow.Flow
|
||||
import kotlinx.coroutines.flow.asFlow
|
||||
|
||||
interface PublishingTrigger {
|
||||
val postingTriggeredFlow: Flow<RegisteredPost>
|
||||
|
||||
suspend fun triggerPosting(
|
||||
triggerControlKey: TriggerControlKey
|
||||
): PostId?
|
||||
}
|
||||
|
||||
class BusinessPublishingTrigger(
|
||||
private val postsRepo: PostsRepo,
|
||||
private val publishingKeysRepo: PublishingKeysRepo
|
||||
) : PublishingTrigger {
|
||||
private val postingTriggeredChannel: BroadcastChannel<RegisteredPost> = BroadcastChannel(Channel.BUFFERED)
|
||||
override val postingTriggeredFlow: Flow<RegisteredPost> = postingTriggeredChannel.asFlow()
|
||||
|
||||
override suspend fun triggerPosting(triggerControlKey: TriggerControlKey): PostId? {
|
||||
val postId = publishingKeysRepo.getPostIdByTriggerControlKey(triggerControlKey) ?: return null
|
||||
publishingKeysRepo.unsetPostTriggerControlKey(postId)
|
||||
|
||||
return postsRepo.getPostById(postId) ?.let { post ->
|
||||
if (postsRepo.deletePost(postId)) {
|
||||
postingTriggeredChannel.send(post)
|
||||
postId
|
||||
} else {
|
||||
null
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@@ -0,0 +1,25 @@
|
||||
package com.insanusmokrassar.postssystem.core.publishing.repos
|
||||
|
||||
import com.insanusmokrassar.postssystem.core.post.PostId
|
||||
import com.insanusmokrassar.postssystem.core.publishing.TriggerControlKey
|
||||
|
||||
interface ReadPublishingKeysRepo {
|
||||
suspend fun getPostIdByTriggerControlKey(
|
||||
key: TriggerControlKey
|
||||
): PostId?
|
||||
suspend fun getTriggerControlKeyByPostId(
|
||||
postId: PostId
|
||||
): TriggerControlKey?
|
||||
}
|
||||
|
||||
interface WritePublishingKeysRepo {
|
||||
suspend fun setPostTriggerControlKey(
|
||||
postId: PostId,
|
||||
key: TriggerControlKey
|
||||
): Boolean
|
||||
suspend fun unsetPostTriggerControlKey(
|
||||
postId: PostId
|
||||
): Boolean
|
||||
}
|
||||
|
||||
interface PublishingKeysRepo : ReadPublishingKeysRepo, WritePublishingKeysRepo
|
Reference in New Issue
Block a user