Skip to content

Commit

Permalink
Group members service
Browse files Browse the repository at this point in the history
  • Loading branch information
madhead committed Oct 18, 2023
1 parent da33904 commit 1ebce01
Show file tree
Hide file tree
Showing 7 changed files with 80 additions and 16 deletions.
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package me.madhead.tyzenhaus.core.currencies
package me.madhead.tyzenhaus.core.service

import me.madhead.tyzenhaus.repository.BalanceRepository

/**
* Lists currencies used in transactions of the group.
*/
class ChatCurrenciesService(
class GroupCurrenciesService(
private val balanceRepository: BalanceRepository,
) {
/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package me.madhead.tyzenhaus.core.service

import dev.inmo.tgbotapi.bot.RequestsExecutor
import dev.inmo.tgbotapi.types.ChatId
import dev.inmo.tgbotapi.types.UserId
import me.madhead.tyzenhaus.core.telegram.updates.expense.getChatMemberSafe
import me.madhead.tyzenhaus.entity.group.members.GroupMember
import me.madhead.tyzenhaus.entity.group.members.GroupMembers
import me.madhead.tyzenhaus.repository.GroupConfigRepository

/**
* Lists group members.
*/
class GroupMembersService(
private val groupConfigRepository: GroupConfigRepository,
private val requestsExecutor: RequestsExecutor,
) {
/**
* Lists members of the [group].
*/
suspend fun groupMembers(group: Long): GroupMembers? {
val groupConfig = groupConfigRepository.get(group) ?: return null
val chatMembers = groupConfig.members.map { requestsExecutor.getChatMemberSafe(ChatId(groupConfig.id), UserId(it)) }

return GroupMembers(
id = groupConfig.id,
members = chatMembers.map {
GroupMember(
id = it.user.id.chatId,
firstName = it.user.firstName,
lastName = it.user.lastName,
)
}
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import dev.inmo.tgbotapi.types.message.content.TextContent
import dev.inmo.tgbotapi.types.update.MessageUpdate
import dev.inmo.tgbotapi.types.update.abstracts.Update
import java.math.BigDecimal
import me.madhead.tyzenhaus.core.currencies.ChatCurrenciesService
import me.madhead.tyzenhaus.core.service.GroupCurrenciesService
import me.madhead.tyzenhaus.core.telegram.updates.UpdateProcessor
import me.madhead.tyzenhaus.core.telegram.updates.UpdateReaction
import me.madhead.tyzenhaus.core.telegram.updates.groupId
Expand All @@ -29,7 +29,7 @@ import org.apache.logging.log4j.LogManager
class AmountReplyUpdateProcessor(
private val requestsExecutor: RequestsExecutor,
private val dialogStateRepository: DialogStateRepository,
private val chatCurrenciesService: ChatCurrenciesService,
private val groupCurrenciesService: GroupCurrenciesService,
) : UpdateProcessor {
companion object {
private val logger = LogManager.getLogger(AmountReplyUpdateProcessor::class.java)!!
Expand Down Expand Up @@ -110,7 +110,7 @@ class AmountReplyUpdateProcessor(
parseMode = MarkdownV2,
replyToMessageId = message.messageId,
replyMarkup = dev.inmo.tgbotapi.types.buttons.ReplyKeyboardMarkup(
keyboard = chatCurrenciesService
keyboard = groupCurrenciesService
.groupCurrencies(update.groupId)
.map { listOf(SimpleKeyboardButton(it)) },
resizeKeyboard = true,
Expand Down
4 changes: 2 additions & 2 deletions detekt.yml
Original file line number Diff line number Diff line change
Expand Up @@ -40,15 +40,15 @@ comments:
matchDeclarationsOrder: true
allowParamOnConstructorProperties: false
UndocumentedPublicClass:
active: false
active: true
excludes:
- '**/test/**'
searchInNestedClass: true
searchInInnerClass: true
searchInInnerObject: true
searchInInnerInterface: true
UndocumentedPublicFunction:
active: false
active: true
excludes:
- '**/test/**'
UndocumentedPublicProperty:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package me.madhead.tyzenhaus.entity.group.members

import kotlinx.serialization.Serializable

@Serializable
data class GroupMember(
val id: Long,
val firstName: String,
val lastName: String,
)

@Serializable
data class GroupMembers(
val id: Long,
val members: Collection<GroupMember> = emptySet(),
)
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
package me.madhead.tyzenhaus.launcher.fly.koin

import dev.inmo.tgbotapi.types.ChatId
import dev.inmo.tgbotapi.types.chat.ExtendedBot
import io.ktor.server.config.ApplicationConfig
import me.madhead.tyzenhaus.core.currencies.ChatCurrenciesService
import me.madhead.tyzenhaus.core.debts.DebtsCalculator
import me.madhead.tyzenhaus.core.telegram.updates.UpdateProcessingPipeline
import me.madhead.tyzenhaus.core.telegram.updates.expense.AmountReplyUpdateProcessor
Expand All @@ -27,11 +24,6 @@ import me.madhead.tyzenhaus.core.telegram.updates.support.IDCommandUpdateProcess
import org.koin.dsl.module

val pipelineModule = module {
single {
ChatCurrenciesService(
balanceRepository = get(),
)
}
single {
DebtsCalculator()
}
Expand Down Expand Up @@ -75,7 +67,7 @@ val pipelineModule = module {
AmountReplyUpdateProcessor(
requestsExecutor = get(),
dialogStateRepository = get(),
chatCurrenciesService = get(),
groupCurrenciesService = get(),
)
}
single {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package me.madhead.tyzenhaus.launcher.fly.koin

import me.madhead.tyzenhaus.core.service.GroupCurrenciesService
import me.madhead.tyzenhaus.core.service.GroupMembersService
import org.koin.dsl.module

val serviceModule = module {
single {
GroupCurrenciesService(
balanceRepository = get(),
)
}

single {
GroupMembersService(
requestsExecutor = get(),
groupConfigRepository = get(),
)
}
}

0 comments on commit 1ebce01

Please sign in to comment.